3
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
3
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
3
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
3
|
20 |
|
21 */ |
|
22 |
238
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
3
|
25 #endif |
|
26 |
|
27 #include "NLEqn.h" |
465
|
28 #include "dMatrix.h" |
1847
|
29 #include "f77-fcn.h" |
227
|
30 #include "lo-error.h" |
4180
|
31 #include "quit.h" |
3
|
32 |
5275
|
33 typedef octave_idx_type (*hybrd1_fcn_ptr) (octave_idx_type*, double*, double*, octave_idx_type*); |
3507
|
34 |
5275
|
35 typedef octave_idx_type (*hybrj1_fcn_ptr) (octave_idx_type*, double*, double*, double*, octave_idx_type*, octave_idx_type*); |
3507
|
36 |
|
37 extern "C" |
4552
|
38 { |
|
39 F77_RET_T |
5275
|
40 F77_FUNC (hybrd1, HYBRD1) (hybrd1_fcn_ptr, const octave_idx_type&, double*, |
|
41 double*, const double&, octave_idx_type&, double*, |
|
42 const octave_idx_type&); |
4552
|
43 |
|
44 |
|
45 F77_RET_T |
5275
|
46 F77_FUNC (hybrj1, HYBRJ1) (hybrj1_fcn_ptr, const octave_idx_type&, double*, |
|
47 double*, double*, const octave_idx_type&, const |
|
48 double&, octave_idx_type&, double*, const octave_idx_type&); |
4552
|
49 } |
3
|
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 // Other operations |
|
63 |
5275
|
64 octave_idx_type |
|
65 hybrd1_fcn (octave_idx_type *n, double *x, double *fvec, octave_idx_type *iflag) |
3
|
66 { |
4180
|
67 BEGIN_INTERRUPT_WITH_EXCEPTIONS; |
|
68 |
5275
|
69 octave_idx_type nn = *n; |
1536
|
70 ColumnVector tmp_f (nn); |
|
71 ColumnVector tmp_x (nn); |
3
|
72 |
5275
|
73 for (octave_idx_type i = 0; i < nn; i++) |
3
|
74 tmp_x.elem (i) = x[i]; |
|
75 |
|
76 tmp_f = (*user_fun) (tmp_x); |
|
77 |
253
|
78 if (tmp_f.length () == 0) |
|
79 *iflag = -1; |
|
80 else |
|
81 { |
5275
|
82 for (octave_idx_type i = 0; i < nn; i++) |
253
|
83 fvec[i] = tmp_f.elem (i); |
|
84 } |
3
|
85 |
4180
|
86 END_INTERRUPT_WITH_EXCEPTIONS; |
|
87 |
3
|
88 return 0; |
|
89 } |
|
90 |
5275
|
91 octave_idx_type |
|
92 hybrj1_fcn (octave_idx_type *n, double *x, double *fvec, double *fjac, |
|
93 octave_idx_type *ldfjac, octave_idx_type *iflag) |
3
|
94 { |
4180
|
95 BEGIN_INTERRUPT_WITH_EXCEPTIONS; |
|
96 |
5275
|
97 octave_idx_type nn = *n; |
1536
|
98 ColumnVector tmp_x (nn); |
3
|
99 |
5275
|
100 for (octave_idx_type i = 0; i < nn; i++) |
3
|
101 tmp_x.elem (i) = x[i]; |
|
102 |
5275
|
103 octave_idx_type flag = *iflag; |
3
|
104 if (flag == 1) |
|
105 { |
1536
|
106 ColumnVector tmp_f (nn); |
3
|
107 |
|
108 tmp_f = (*user_fun) (tmp_x); |
|
109 |
253
|
110 if (tmp_f.length () == 0) |
|
111 *iflag = -1; |
|
112 else |
|
113 { |
5275
|
114 for (octave_idx_type i = 0; i < nn; i++) |
253
|
115 fvec[i] = tmp_f.elem (i); |
|
116 } |
3
|
117 } |
|
118 else |
|
119 { |
|
120 Matrix tmp_fj (nn, nn); |
|
121 |
|
122 tmp_fj = (*user_jac) (tmp_x); |
|
123 |
253
|
124 if (tmp_fj.rows () == 0 || tmp_fj.columns () == 0) |
|
125 *iflag = -1; |
|
126 else |
|
127 { |
5275
|
128 octave_idx_type ld = *ldfjac; |
|
129 for (octave_idx_type j = 0; j < nn; j++) |
|
130 for (octave_idx_type i = 0; i < nn; i++) |
253
|
131 fjac[j*ld+i] = tmp_fj.elem (i, j); |
|
132 } |
3
|
133 } |
|
134 |
4180
|
135 END_INTERRUPT_WITH_EXCEPTIONS; |
|
136 |
3
|
137 return 0; |
|
138 } |
|
139 |
1536
|
140 ColumnVector |
5275
|
141 NLEqn::solve (octave_idx_type& info) |
3
|
142 { |
1536
|
143 ColumnVector retval; |
|
144 |
5275
|
145 octave_idx_type n = x.capacity (); |
1878
|
146 |
3
|
147 if (n == 0) |
227
|
148 { |
|
149 error ("equation set not initialized"); |
1536
|
150 return retval; |
227
|
151 } |
3
|
152 |
289
|
153 double tol = tolerance (); |
3
|
154 |
1937
|
155 retval = x; |
|
156 double *px = retval.fortran_vec (); |
3
|
157 |
|
158 user_fun = fun; |
|
159 user_jac = jac; |
|
160 |
465
|
161 if (jac) |
3
|
162 { |
1937
|
163 Array<double> fvec (n); |
|
164 double *pfvec = fvec.fortran_vec (); |
|
165 |
5275
|
166 octave_idx_type lwa = (n*(n+13))/2; |
1937
|
167 Array<double> wa (lwa); |
|
168 double *pwa = wa.fortran_vec (); |
3
|
169 |
1937
|
170 Array<double> fjac (n*n); |
|
171 double *pfjac = fjac.fortran_vec (); |
3
|
172 |
1937
|
173 F77_XFCN (hybrj1, HYBRJ1, (hybrj1_fcn, n, px, pfvec, pfjac, n, |
|
174 tol, info, pwa, lwa)); |
|
175 |
3971
|
176 solution_status = info; |
|
177 |
1937
|
178 if (f77_exception_encountered) |
|
179 (*current_liboctave_error_handler) ("unrecoverable error in hybrj1"); |
3
|
180 } |
465
|
181 else |
|
182 { |
1937
|
183 Array<double> fvec (n); |
|
184 double *pfvec = fvec.fortran_vec (); |
465
|
185 |
5275
|
186 octave_idx_type lwa = (n*(3*n+13))/2; |
1937
|
187 Array<double> wa (lwa); |
|
188 double *pwa = wa.fortran_vec (); |
465
|
189 |
1937
|
190 F77_XFCN (hybrd1, HYBRD1, (hybrd1_fcn, n, px, pfvec, tol, info, |
|
191 pwa, lwa)); |
|
192 |
3971
|
193 solution_status = info; |
|
194 |
1937
|
195 if (f77_exception_encountered) |
|
196 (*current_liboctave_error_handler) ("unrecoverable error in hybrd1"); |
465
|
197 } |
3
|
198 |
|
199 return retval; |
|
200 } |
|
201 |
3971
|
202 std::string |
|
203 NLEqn::error_message (void) const |
|
204 { |
|
205 std::string retval; |
|
206 |
|
207 std::string prefix; |
|
208 |
5275
|
209 octave_idx_type info = solution_status; |
3971
|
210 if (info < 0) |
|
211 info = -info; |
|
212 |
|
213 switch (info) |
|
214 { |
|
215 case 0: |
|
216 retval = "improper input parameters"; |
|
217 break; |
|
218 |
|
219 case 1: |
|
220 retval = "solution converged within specified tolerance"; |
|
221 break; |
|
222 |
|
223 case 2: |
|
224 retval = "number of function calls exceeded limit"; |
|
225 break; |
|
226 |
|
227 case 3: |
|
228 retval = "no further improvement possible (tolerance may be too small)"; |
|
229 break; |
|
230 |
|
231 case 4: |
|
232 retval = "iteration is not making good progress"; |
|
233 break; |
|
234 |
|
235 default: |
|
236 retval = "unknown error state"; |
|
237 break; |
|
238 } |
|
239 |
|
240 if (solution_status < 0) |
|
241 retval = std::string ("user requested termination: ") + retval; |
|
242 |
|
243 return retval; |
|
244 } |
|
245 |
3
|
246 /* |
|
247 ;;; Local Variables: *** |
|
248 ;;; mode: C++ *** |
|
249 ;;; End: *** |
|
250 */ |