7
|
1 // f-fsolve.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
|
4 Copyright (C) 1993 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef __GNUG__ |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #include "NLEqn.h" |
|
29 |
|
30 #include "tree-const.h" |
|
31 #include "variables.h" |
|
32 #include "gripes.h" |
|
33 #include "error.h" |
|
34 #include "utils.h" |
7
|
35 #include "f-fsolve.h" |
1
|
36 |
|
37 // Global pointer for user defined function required by hybrd1. |
|
38 static tree *fsolve_fcn; |
|
39 |
|
40 #ifdef WITH_DLD |
|
41 tree_constant * |
|
42 builtin_fsolve (tree_constant *args, int nargin, int nargout) |
|
43 { |
|
44 return fsolve (args, nargin, nargout); |
|
45 } |
|
46 #endif |
|
47 |
|
48 int |
|
49 hybrd_info_to_fsolve_info (int info) |
|
50 { |
|
51 switch (info) |
|
52 { |
|
53 case 0: |
|
54 info = -1; |
|
55 break; |
|
56 case 1: |
|
57 break; |
|
58 case 2: |
|
59 info = 4; |
|
60 break; |
|
61 case 3: |
|
62 case 4: |
|
63 case 5: |
|
64 info = 3; |
|
65 break; |
|
66 default: |
|
67 panic_impossible (); |
|
68 break; |
|
69 } |
|
70 return info; |
|
71 } |
|
72 |
|
73 ColumnVector |
|
74 fsolve_user_function (ColumnVector& x) |
|
75 { |
|
76 ColumnVector retval; |
|
77 |
|
78 int n = x.capacity (); |
|
79 |
|
80 // tree_constant name = tree_constant (fsolve_fcn->name ()); |
|
81 tree_constant *args = new tree_constant [2]; |
|
82 // args[0] = name; |
|
83 |
|
84 if (n > 1) |
|
85 { |
|
86 Matrix m (n, 1); |
|
87 for (int i = 0; i < n; i++) |
|
88 m (i, 0) = x.elem (i); |
|
89 tree_constant vars (m); |
|
90 args[1] = vars; |
|
91 } |
|
92 else |
|
93 { |
|
94 double d = x.elem (0); |
|
95 tree_constant vars (d); |
|
96 args[1] = vars; |
|
97 } |
|
98 |
|
99 if (fsolve_fcn != NULL_TREE) |
|
100 { |
|
101 tree_constant *tmp = fsolve_fcn->eval (args, 2, 1, 0); |
|
102 delete [] args; |
|
103 if (tmp != NULL_TREE_CONST && tmp[0].is_defined ()) |
|
104 { |
|
105 retval = tmp[0].to_vector (); |
|
106 delete [] tmp; |
|
107 } |
|
108 else |
|
109 { |
|
110 delete [] tmp; |
|
111 gripe_user_supplied_eval ("fsolve"); |
|
112 jump_to_top_level (); |
|
113 } |
|
114 } |
|
115 |
|
116 return retval; |
|
117 } |
|
118 |
|
119 tree_constant * |
|
120 fsolve (tree_constant *args, int nargin, int nargout) |
|
121 { |
|
122 // Assumes that we have been given the correct number of arguments. |
|
123 |
|
124 tree_constant *retval = NULL_TREE_CONST; |
|
125 |
|
126 fsolve_fcn = is_valid_function (args[1], "fsolve", 1); |
|
127 if (fsolve_fcn == NULL_TREE |
|
128 || takes_correct_nargs (fsolve_fcn, 2, "fsolve", 1) != 1) |
|
129 return retval; |
|
130 |
|
131 ColumnVector x = args[2].to_vector (); |
|
132 |
|
133 if (nargin > 3) |
|
134 message ("fsolve", "ignoring optional arguments..."); |
|
135 |
|
136 if (nargout > 2) |
|
137 message ("fsolve", "can't compute path output yet..."); |
|
138 |
|
139 NLFunc foo_fcn (fsolve_user_function); |
|
140 NLEqn foo (x, foo_fcn); |
|
141 |
|
142 int info; |
|
143 ColumnVector soln = foo.solve (info); |
|
144 |
|
145 info = hybrd_info_to_fsolve_info (info); |
|
146 |
|
147 retval = new tree_constant [nargout+1]; |
|
148 retval[0] = tree_constant (soln, 1); |
|
149 |
|
150 if (nargout > 1) |
|
151 retval[1] = tree_constant ((double) info); |
|
152 |
|
153 if (nargout > 2) |
|
154 retval[2] = tree_constant (); |
|
155 |
|
156 return retval; |
|
157 } |
|
158 |
|
159 /* |
|
160 ;;; Local Variables: *** |
|
161 ;;; mode: C++ *** |
|
162 ;;; page-delimiter: "^/\\*" *** |
|
163 ;;; End: *** |
|
164 */ |
|
165 |