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