7
|
1 // f-fsolve.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
277
|
4 Copyright (C) 1993, 1994 John W. Eaton |
1
|
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 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
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 * |
277
|
42 builtin_fsolve_2 (const tree_constant *args, int nargin, int nargout) |
1
|
43 { |
|
44 return fsolve (args, nargin, nargout); |
|
45 } |
272
|
46 |
|
47 tree_constant * |
|
48 builtin_fsolve_options (const tree_constant *args, int nargin, int nargout) |
|
49 { |
|
50 return fsolve_options (args, nargin, nargout); |
|
51 } |
1
|
52 #endif |
|
53 |
|
54 int |
|
55 hybrd_info_to_fsolve_info (int info) |
|
56 { |
|
57 switch (info) |
|
58 { |
252
|
59 case -1: |
|
60 info = -2; |
|
61 break; |
1
|
62 case 0: |
|
63 info = -1; |
|
64 break; |
|
65 case 1: |
|
66 break; |
|
67 case 2: |
|
68 info = 4; |
|
69 break; |
|
70 case 3: |
|
71 case 4: |
|
72 case 5: |
|
73 info = 3; |
|
74 break; |
|
75 default: |
|
76 panic_impossible (); |
|
77 break; |
|
78 } |
|
79 return info; |
|
80 } |
|
81 |
|
82 ColumnVector |
162
|
83 fsolve_user_function (const ColumnVector& x) |
1
|
84 { |
|
85 ColumnVector retval; |
|
86 |
|
87 int n = x.capacity (); |
|
88 |
|
89 // tree_constant name = tree_constant (fsolve_fcn->name ()); |
|
90 tree_constant *args = new tree_constant [2]; |
|
91 // args[0] = name; |
|
92 |
|
93 if (n > 1) |
|
94 { |
|
95 Matrix m (n, 1); |
|
96 for (int i = 0; i < n; i++) |
|
97 m (i, 0) = x.elem (i); |
|
98 tree_constant vars (m); |
|
99 args[1] = vars; |
|
100 } |
|
101 else |
|
102 { |
|
103 double d = x.elem (0); |
|
104 tree_constant vars (d); |
|
105 args[1] = vars; |
|
106 } |
|
107 |
|
108 if (fsolve_fcn != NULL_TREE) |
|
109 { |
|
110 tree_constant *tmp = fsolve_fcn->eval (args, 2, 1, 0); |
|
111 delete [] args; |
|
112 if (tmp != NULL_TREE_CONST && tmp[0].is_defined ()) |
|
113 { |
|
114 retval = tmp[0].to_vector (); |
252
|
115 |
1
|
116 delete [] tmp; |
252
|
117 |
|
118 if (retval.length () <= 0) |
|
119 gripe_user_supplied_eval ("fsolve"); |
1
|
120 } |
|
121 else |
|
122 { |
|
123 delete [] tmp; |
|
124 gripe_user_supplied_eval ("fsolve"); |
|
125 } |
|
126 } |
|
127 |
|
128 return retval; |
|
129 } |
|
130 |
|
131 tree_constant * |
162
|
132 fsolve (const tree_constant *args, int nargin, int nargout) |
1
|
133 { |
|
134 // Assumes that we have been given the correct number of arguments. |
|
135 |
|
136 tree_constant *retval = NULL_TREE_CONST; |
|
137 |
|
138 fsolve_fcn = is_valid_function (args[1], "fsolve", 1); |
|
139 if (fsolve_fcn == NULL_TREE |
|
140 || takes_correct_nargs (fsolve_fcn, 2, "fsolve", 1) != 1) |
|
141 return retval; |
|
142 |
|
143 ColumnVector x = args[2].to_vector (); |
|
144 |
|
145 if (nargin > 3) |
216
|
146 warning ("fsolve: ignoring optional arguments"); |
1
|
147 |
|
148 if (nargout > 2) |
216
|
149 warning ("fsolve: can't compute path output yet"); |
1
|
150 |
|
151 NLFunc foo_fcn (fsolve_user_function); |
|
152 NLEqn foo (x, foo_fcn); |
|
153 |
|
154 int info; |
|
155 ColumnVector soln = foo.solve (info); |
|
156 |
|
157 info = hybrd_info_to_fsolve_info (info); |
|
158 |
|
159 retval = new tree_constant [nargout+1]; |
|
160 retval[0] = tree_constant (soln, 1); |
|
161 |
|
162 if (nargout > 1) |
|
163 retval[1] = tree_constant ((double) info); |
|
164 |
|
165 if (nargout > 2) |
|
166 retval[2] = tree_constant (); |
|
167 |
|
168 return retval; |
|
169 } |
|
170 |
272
|
171 tree_constant * |
|
172 fsolve_options (const tree_constant *args, int nargin, int nargout) |
|
173 { |
|
174 // Assumes that we have been given the correct number of arguments. |
|
175 |
|
176 tree_constant *retval = NULL_TREE_CONST; |
|
177 error ("fsolve_options: not implemented yet"); |
|
178 return retval; |
|
179 } |
|
180 |
1
|
181 /* |
|
182 ;;; Local Variables: *** |
|
183 ;;; mode: C++ *** |
|
184 ;;; page-delimiter: "^/\\*" *** |
|
185 ;;; End: *** |
|
186 */ |