3
|
1 // NLEqn.cc -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
3
|
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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
3
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
238
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
3
|
30 #endif |
|
31 |
|
32 #include "NLEqn.h" |
465
|
33 #include "dMatrix.h" |
3
|
34 #include "f77-uscore.h" |
227
|
35 #include "lo-error.h" |
3
|
36 |
|
37 extern "C" |
|
38 { |
1253
|
39 int F77_FCN (hybrd1, HYBRD1) (int (*)(int*, double*, double*, int*), |
|
40 const int&, double*, double*, |
|
41 const double&, int&, double*, |
|
42 const int&); |
3
|
43 |
1254
|
44 int F77_FCN (hybrj1, HYBRJ1) (int (*)(int*, double*, double*, |
|
45 double*, int*, int*), |
|
46 const int&, double*, double*, double*, |
|
47 const int&, const double&, int&, |
|
48 double*, const int&); |
3
|
49 } |
|
50 |
1536
|
51 static NLFunc::nonlinear_fcn user_fun; |
|
52 static NLFunc::jacobian_fcn user_jac; |
3
|
53 |
|
54 // error handling |
|
55 |
|
56 void |
|
57 NLEqn::error (const char* msg) |
|
58 { |
227
|
59 (*current_liboctave_error_handler) ("fatal NLEqn error: %s", msg); |
3
|
60 } |
|
61 |
|
62 void |
1536
|
63 NLEqn::set_states (const ColumnVector& xvec) |
3
|
64 { |
|
65 if (xvec.capacity () != n) |
227
|
66 { |
|
67 error ("dimension error"); |
|
68 return; |
|
69 } |
3
|
70 |
|
71 x = xvec; |
|
72 } |
|
73 |
|
74 // Other operations |
|
75 |
|
76 int |
|
77 hybrd1_fcn (int *n, double *x, double *fvec, int *iflag) |
|
78 { |
|
79 int nn = *n; |
1536
|
80 ColumnVector tmp_f (nn); |
|
81 ColumnVector tmp_x (nn); |
3
|
82 |
|
83 for (int i = 0; i < nn; i++) |
|
84 tmp_x.elem (i) = x[i]; |
|
85 |
|
86 tmp_f = (*user_fun) (tmp_x); |
|
87 |
253
|
88 if (tmp_f.length () == 0) |
|
89 *iflag = -1; |
|
90 else |
|
91 { |
1321
|
92 for (int i = 0; i < nn; i++) |
253
|
93 fvec[i] = tmp_f.elem (i); |
|
94 } |
3
|
95 |
|
96 return 0; |
|
97 } |
|
98 |
|
99 int |
|
100 hybrj1_fcn (int *n, double *x, double *fvec, double *fjac, |
|
101 int *ldfjac, int *iflag) |
|
102 { |
|
103 int nn = *n; |
1536
|
104 ColumnVector tmp_x (nn); |
3
|
105 |
|
106 for (int i = 0; i < nn; i++) |
|
107 tmp_x.elem (i) = x[i]; |
|
108 |
|
109 int flag = *iflag; |
|
110 if (flag == 1) |
|
111 { |
1536
|
112 ColumnVector tmp_f (nn); |
3
|
113 |
|
114 tmp_f = (*user_fun) (tmp_x); |
|
115 |
253
|
116 if (tmp_f.length () == 0) |
|
117 *iflag = -1; |
|
118 else |
|
119 { |
1321
|
120 for (int i = 0; i < nn; i++) |
253
|
121 fvec[i] = tmp_f.elem (i); |
|
122 } |
3
|
123 } |
|
124 else |
|
125 { |
|
126 Matrix tmp_fj (nn, nn); |
|
127 |
|
128 tmp_fj = (*user_jac) (tmp_x); |
|
129 |
253
|
130 if (tmp_fj.rows () == 0 || tmp_fj.columns () == 0) |
|
131 *iflag = -1; |
|
132 else |
|
133 { |
|
134 int ld = *ldfjac; |
|
135 for (int j = 0; j < nn; j++) |
1321
|
136 for (int i = 0; i < nn; i++) |
253
|
137 fjac[j*ld+i] = tmp_fj.elem (i, j); |
|
138 } |
3
|
139 } |
|
140 |
|
141 return 0; |
|
142 } |
|
143 |
1536
|
144 ColumnVector |
3
|
145 NLEqn::solve (int& info) |
|
146 { |
1536
|
147 ColumnVector retval; |
|
148 |
3
|
149 if (n == 0) |
227
|
150 { |
|
151 error ("equation set not initialized"); |
1536
|
152 return retval; |
227
|
153 } |
3
|
154 |
289
|
155 double tol = tolerance (); |
3
|
156 |
|
157 double *fvec = new double [n]; |
|
158 double *px = new double [n]; |
|
159 for (int i = 0; i < n; i++) |
|
160 px[i] = x.elem (i); |
|
161 |
|
162 user_fun = fun; |
|
163 user_jac = jac; |
|
164 |
465
|
165 if (jac) |
3
|
166 { |
|
167 int lwa = (n*(n+13))/2; |
|
168 double *wa = new double [lwa]; |
|
169 double *fjac = new double [n*n]; |
|
170 |
1253
|
171 F77_FCN (hybrj1, HYBRJ1) (hybrj1_fcn, n, px, fvec, fjac, n, tol, |
|
172 info, wa, lwa); |
3
|
173 |
|
174 delete [] wa; |
|
175 delete [] fjac; |
|
176 } |
465
|
177 else |
|
178 { |
|
179 int lwa = (n*(3*n+13))/2; |
|
180 double *wa = new double [lwa]; |
|
181 |
1253
|
182 F77_FCN (hybrd1, HYBRD1) (hybrd1_fcn, n, px, fvec, tol, info, |
|
183 wa, lwa); |
465
|
184 |
|
185 delete [] wa; |
|
186 } |
3
|
187 |
253
|
188 if (info >= 0) |
|
189 { |
|
190 retval.resize (n); |
3
|
191 |
1321
|
192 for (int i = 0; i < n; i++) |
253
|
193 retval.elem (i) = px[i]; |
|
194 } |
3
|
195 |
657
|
196 delete [] fvec; |
|
197 delete [] px; |
|
198 |
3
|
199 return retval; |
|
200 } |
|
201 |
|
202 /* |
|
203 ;;; Local Variables: *** |
|
204 ;;; mode: C++ *** |
|
205 ;;; page-delimiter: "^/\\*" *** |
|
206 ;;; End: *** |
|
207 */ |