3
|
1 // NLEqn.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 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 |
238
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
3
|
26 #endif |
|
27 |
|
28 #include <float.h> |
238
|
29 |
3
|
30 #include "NLEqn.h" |
|
31 #include "f77-uscore.h" |
227
|
32 #include "lo-error.h" |
3
|
33 |
|
34 extern "C" |
|
35 { |
|
36 int F77_FCN (hybrd1) (int (*)(), const int*, double*, double*, |
|
37 const double*, int*, double*, const int*); |
|
38 |
|
39 int F77_FCN (hybrj1) (int (*)(), const int*, double*, double*, |
|
40 double*, const int*, const double*, int*, |
|
41 double*, const int*); |
|
42 } |
|
43 |
|
44 static nonlinear_fcn user_fun; |
|
45 static jacobian_fcn user_jac; |
|
46 |
|
47 // error handling |
|
48 |
|
49 void |
|
50 NLEqn::error (const char* msg) |
|
51 { |
227
|
52 (*current_liboctave_error_handler) ("fatal NLEqn error: %s", msg); |
3
|
53 } |
|
54 |
|
55 // Constructors |
|
56 |
|
57 NLEqn::NLEqn (void) : NLFunc (), x (), n (0) {} |
|
58 |
|
59 NLEqn::NLEqn (const Vector& xvec, const NLFunc f) |
190
|
60 : NLFunc (f), x (xvec), n (xvec.capacity ()) {} |
3
|
61 |
|
62 NLEqn::NLEqn (const NLEqn& a) : NLFunc (a.fun, a.jac), x (a.x), n (a.n) {} |
|
63 |
|
64 void |
|
65 NLEqn::resize (int nn) |
|
66 { |
|
67 if (n != nn) |
|
68 { |
|
69 n = nn; |
|
70 x.resize (n); |
|
71 } |
|
72 } |
|
73 |
|
74 int |
|
75 NLEqn::size (void) const |
|
76 { |
|
77 return n; |
|
78 } |
|
79 |
|
80 // Assignment |
|
81 |
|
82 NLEqn& |
|
83 NLEqn::operator = (const NLEqn& a) |
|
84 { |
|
85 fun = a.fun; |
|
86 jac = a.jac; |
|
87 x = a.n; |
|
88 |
|
89 return *this; |
|
90 } |
|
91 |
|
92 Vector |
|
93 NLEqn::states (void) const |
|
94 { |
|
95 return x; |
|
96 } |
|
97 |
|
98 void |
|
99 NLEqn::set_states (const Vector& xvec) |
|
100 { |
|
101 if (xvec.capacity () != n) |
227
|
102 { |
|
103 error ("dimension error"); |
|
104 return; |
|
105 } |
3
|
106 |
|
107 x = xvec; |
|
108 } |
|
109 |
|
110 // Other operations |
|
111 |
|
112 Vector |
|
113 NLEqn::solve (const Vector& xvec) |
|
114 { |
|
115 set_states (xvec); |
|
116 int info; |
|
117 return solve (info); |
|
118 } |
|
119 |
|
120 Vector |
|
121 NLEqn::solve (const Vector& xvec, int& info) |
|
122 { |
|
123 set_states (xvec); |
|
124 return solve (info); |
|
125 } |
|
126 |
|
127 Vector |
|
128 NLEqn::solve (void) |
|
129 { |
|
130 int info; |
|
131 return solve (info); |
|
132 } |
|
133 |
|
134 int |
|
135 hybrd1_fcn (int *n, double *x, double *fvec, int *iflag) |
|
136 { |
|
137 int nn = *n; |
|
138 Vector tmp_f (nn); |
|
139 Vector tmp_x (nn); |
|
140 |
|
141 for (int i = 0; i < nn; i++) |
|
142 tmp_x.elem (i) = x[i]; |
|
143 |
|
144 tmp_f = (*user_fun) (tmp_x); |
|
145 |
|
146 for (i = 0; i < nn; i++) |
|
147 fvec[i] = tmp_f.elem (i); |
|
148 |
|
149 return 0; |
|
150 } |
|
151 |
|
152 int |
|
153 hybrj1_fcn (int *n, double *x, double *fvec, double *fjac, |
|
154 int *ldfjac, int *iflag) |
|
155 { |
|
156 int nn = *n; |
|
157 Vector tmp_x (nn); |
|
158 |
|
159 for (int i = 0; i < nn; i++) |
|
160 tmp_x.elem (i) = x[i]; |
|
161 |
|
162 int flag = *iflag; |
|
163 if (flag == 1) |
|
164 { |
|
165 Vector tmp_f (nn); |
|
166 |
|
167 tmp_f = (*user_fun) (tmp_x); |
|
168 |
|
169 for (i = 0; i < nn; i++) |
|
170 fvec[i] = tmp_f.elem (i); |
|
171 } |
|
172 else |
|
173 { |
|
174 Matrix tmp_fj (nn, nn); |
|
175 |
|
176 tmp_fj = (*user_jac) (tmp_x); |
|
177 |
|
178 int ld = *ldfjac; |
|
179 for (int j = 0; j < nn; j++) |
|
180 for (i = 0; i < nn; i++) |
|
181 fjac[j*ld+i] = tmp_fj.elem (i, j); |
|
182 } |
|
183 |
|
184 return 0; |
|
185 } |
|
186 |
|
187 Vector |
|
188 NLEqn::solve (int& info) |
|
189 { |
|
190 int tmp_info = 0; |
|
191 |
|
192 if (n == 0) |
227
|
193 { |
|
194 error ("equation set not initialized"); |
|
195 return Vector (); |
|
196 } |
3
|
197 |
|
198 double tol = sqrt (DBL_EPSILON); |
|
199 |
|
200 double *fvec = new double [n]; |
|
201 double *px = new double [n]; |
|
202 for (int i = 0; i < n; i++) |
|
203 px[i] = x.elem (i); |
|
204 |
|
205 user_fun = fun; |
|
206 user_jac = jac; |
|
207 |
|
208 if (jac == NULL) |
|
209 { |
|
210 int lwa = (n*(3*n+13))/2; |
|
211 double *wa = new double [lwa]; |
|
212 |
|
213 F77_FCN (hybrd1) (hybrd1_fcn, &n, px, fvec, &tol, &tmp_info, wa, &lwa); |
|
214 |
|
215 delete [] wa; |
|
216 } |
|
217 else |
|
218 { |
|
219 int lwa = (n*(n+13))/2; |
|
220 double *wa = new double [lwa]; |
|
221 double *fjac = new double [n*n]; |
|
222 |
|
223 F77_FCN (hybrj1) (hybrj1_fcn, &n, px, fvec, fjac, &n, &tol, |
|
224 &tmp_info, wa, &lwa); |
|
225 |
|
226 delete [] wa; |
|
227 delete [] fjac; |
|
228 } |
|
229 |
|
230 info = tmp_info; |
|
231 |
|
232 Vector retval (n); |
|
233 |
|
234 for (i = 0; i < n; i++) |
|
235 retval.elem (i) = px[i]; |
|
236 |
|
237 return retval; |
|
238 } |
|
239 |
|
240 /* |
|
241 ;;; Local Variables: *** |
|
242 ;;; mode: C++ *** |
|
243 ;;; page-delimiter: "^/\\*" *** |
|
244 ;;; End: *** |
|
245 */ |