1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1343
|
27 #include <cassert> |
|
28 |
1352
|
29 #include "CMatrix.h" |
453
|
30 #include "dMatrix.h" |
4543
|
31 #include "CNDArray.h" |
|
32 #include "dNDArray.h" |
1651
|
33 #include "oct-cmplx.h" |
4153
|
34 #include "quit.h" |
1352
|
35 |
|
36 #include "error.h" |
|
37 #include "xdiv.h" |
1
|
38 |
3480
|
39 static inline bool |
|
40 result_ok (int info) |
1
|
41 { |
|
42 assert (info != -1); |
|
43 |
3480
|
44 return (info != -2); |
|
45 } |
932
|
46 |
3480
|
47 static void |
|
48 solve_singularity_warning (double rcond) |
|
49 { |
|
50 warning ("matrix singular to machine precision, rcond = %g", rcond); |
3719
|
51 warning ("attempting to find minimum norm solution"); |
1
|
52 } |
|
53 |
2364
|
54 template <class T1, class T2> |
|
55 bool |
3195
|
56 mx_leftdiv_conform (const T1& a, const T2& b) |
1
|
57 { |
2364
|
58 int a_nr = a.rows (); |
|
59 int b_nr = b.rows (); |
|
60 |
1
|
61 if (a_nr != b_nr) |
|
62 { |
2364
|
63 int a_nc = a.cols (); |
|
64 int b_nc = b.cols (); |
|
65 |
|
66 gripe_nonconformant ("operator \\", a_nr, a_nc, b_nr, b_nc); |
|
67 return false; |
1
|
68 } |
|
69 |
2364
|
70 return true; |
1
|
71 } |
|
72 |
3195
|
73 #define INSTANTIATE_MX_LEFTDIV_CONFORM(T1, T2) \ |
|
74 template bool mx_leftdiv_conform (const T1&, const T2&) |
|
75 |
|
76 INSTANTIATE_MX_LEFTDIV_CONFORM (Matrix, Matrix); |
|
77 INSTANTIATE_MX_LEFTDIV_CONFORM (Matrix, ComplexMatrix); |
|
78 INSTANTIATE_MX_LEFTDIV_CONFORM (ComplexMatrix, Matrix); |
|
79 INSTANTIATE_MX_LEFTDIV_CONFORM (ComplexMatrix, ComplexMatrix); |
2364
|
80 |
|
81 template <class T1, class T2> |
|
82 bool |
3195
|
83 mx_div_conform (const T1& a, const T2& b) |
1
|
84 { |
2364
|
85 int a_nc = a.cols (); |
|
86 int b_nc = b.cols (); |
|
87 |
1
|
88 if (a_nc != b_nc) |
|
89 { |
2364
|
90 int a_nr = a.rows (); |
|
91 int b_nr = b.rows (); |
|
92 |
|
93 gripe_nonconformant ("operator /", a_nr, a_nc, b_nr, b_nc); |
|
94 return false; |
1
|
95 } |
|
96 |
2364
|
97 return true; |
1
|
98 } |
|
99 |
3195
|
100 #define INSTANTIATE_MX_DIV_CONFORM(T1, T2) \ |
|
101 template bool mx_div_conform (const T1&, const T2&) |
|
102 |
|
103 INSTANTIATE_MX_DIV_CONFORM (Matrix, Matrix); |
|
104 INSTANTIATE_MX_DIV_CONFORM (Matrix, ComplexMatrix); |
|
105 INSTANTIATE_MX_DIV_CONFORM (ComplexMatrix, Matrix); |
|
106 INSTANTIATE_MX_DIV_CONFORM (ComplexMatrix, ComplexMatrix); |
2364
|
107 |
767
|
108 // Right division functions. |
|
109 // |
|
110 // op2 / op1: m cm |
|
111 // +-- +---+----+ |
|
112 // matrix | 1 | 3 | |
|
113 // +---+----+ |
|
114 // complex_matrix | 2 | 4 | |
|
115 // +---+----+ |
1
|
116 |
767
|
117 // -*- 1 -*- |
1800
|
118 Matrix |
164
|
119 xdiv (const Matrix& a, const Matrix& b) |
1
|
120 { |
2364
|
121 if (! mx_div_conform (a, b)) |
1800
|
122 return Matrix (); |
1
|
123 |
|
124 Matrix atmp = a.transpose (); |
|
125 Matrix btmp = b.transpose (); |
|
126 |
|
127 int info; |
|
128 if (btmp.rows () == btmp.columns ()) |
|
129 { |
|
130 double rcond = 0.0; |
3480
|
131 |
|
132 Matrix result |
|
133 = btmp.solve (atmp, info, rcond, solve_singularity_warning); |
|
134 |
|
135 if (result_ok (info)) |
1800
|
136 return Matrix (result.transpose ()); |
1
|
137 } |
|
138 |
|
139 int rank; |
|
140 Matrix result = btmp.lssolve (atmp, info, rank); |
|
141 |
1800
|
142 return result.transpose (); |
1
|
143 } |
|
144 |
767
|
145 // -*- 2 -*- |
1800
|
146 ComplexMatrix |
164
|
147 xdiv (const Matrix& a, const ComplexMatrix& b) |
1
|
148 { |
2364
|
149 if (! mx_div_conform (a, b)) |
1800
|
150 return ComplexMatrix (); |
1
|
151 |
|
152 Matrix atmp = a.transpose (); |
|
153 ComplexMatrix btmp = b.hermitian (); |
|
154 |
|
155 int info; |
|
156 if (btmp.rows () == btmp.columns ()) |
|
157 { |
|
158 double rcond = 0.0; |
3480
|
159 |
|
160 ComplexMatrix result |
|
161 = btmp.solve (atmp, info, rcond, solve_singularity_warning); |
|
162 |
|
163 if (result_ok (info)) |
1800
|
164 return result.hermitian (); |
1
|
165 } |
|
166 |
|
167 int rank; |
|
168 ComplexMatrix result = btmp.lssolve (atmp, info, rank); |
|
169 |
1800
|
170 return result.hermitian (); |
1
|
171 } |
|
172 |
767
|
173 // -*- 3 -*- |
1800
|
174 ComplexMatrix |
164
|
175 xdiv (const ComplexMatrix& a, const Matrix& b) |
1
|
176 { |
2364
|
177 if (! mx_div_conform (a, b)) |
1800
|
178 return ComplexMatrix (); |
1
|
179 |
|
180 ComplexMatrix atmp = a.hermitian (); |
|
181 Matrix btmp = b.transpose (); |
|
182 |
|
183 int info; |
|
184 if (btmp.rows () == btmp.columns ()) |
|
185 { |
|
186 double rcond = 0.0; |
3480
|
187 |
|
188 ComplexMatrix result |
|
189 = btmp.solve (atmp, info, rcond, solve_singularity_warning); |
|
190 |
|
191 if (result_ok (info)) |
1800
|
192 return result.hermitian (); |
1
|
193 } |
|
194 |
|
195 int rank; |
|
196 ComplexMatrix result = btmp.lssolve (atmp, info, rank); |
|
197 |
1800
|
198 return result.hermitian (); |
1
|
199 } |
|
200 |
767
|
201 // -*- 4 -*- |
1800
|
202 ComplexMatrix |
164
|
203 xdiv (const ComplexMatrix& a, const ComplexMatrix& b) |
1
|
204 { |
2364
|
205 if (! mx_div_conform (a, b)) |
1800
|
206 return ComplexMatrix (); |
1
|
207 |
|
208 ComplexMatrix atmp = a.hermitian (); |
|
209 ComplexMatrix btmp = b.hermitian (); |
|
210 |
|
211 int info; |
|
212 if (btmp.rows () == btmp.columns ()) |
|
213 { |
|
214 double rcond = 0.0; |
3480
|
215 |
|
216 ComplexMatrix result |
|
217 = btmp.solve (atmp, info, rcond, solve_singularity_warning); |
|
218 |
|
219 if (result_ok (info)) |
1800
|
220 return result.hermitian (); |
1
|
221 } |
|
222 |
|
223 int rank; |
|
224 ComplexMatrix result = btmp.lssolve (atmp, info, rank); |
|
225 |
1800
|
226 return result.hermitian (); |
1
|
227 } |
|
228 |
767
|
229 // Funny element by element division operations. |
|
230 // |
|
231 // op2 \ op1: s cs |
|
232 // +-- +---+----+ |
|
233 // matrix | 1 | 3 | |
|
234 // +---+----+ |
|
235 // complex_matrix | 2 | 4 | |
|
236 // +---+----+ |
1
|
237 |
1800
|
238 Matrix |
164
|
239 x_el_div (double a, const Matrix& b) |
1
|
240 { |
|
241 int nr = b.rows (); |
|
242 int nc = b.columns (); |
|
243 |
|
244 Matrix result (nr, nc); |
|
245 |
|
246 for (int j = 0; j < nc; j++) |
|
247 for (int i = 0; i < nr; i++) |
4153
|
248 { |
|
249 OCTAVE_QUIT; |
|
250 result (i, j) = a / b (i, j); |
|
251 } |
1
|
252 |
1800
|
253 return result; |
1
|
254 } |
|
255 |
1800
|
256 ComplexMatrix |
164
|
257 x_el_div (double a, const ComplexMatrix& b) |
1
|
258 { |
|
259 int nr = b.rows (); |
|
260 int nc = b.columns (); |
|
261 |
|
262 ComplexMatrix result (nr, nc); |
|
263 |
|
264 for (int j = 0; j < nc; j++) |
|
265 for (int i = 0; i < nr; i++) |
4153
|
266 { |
|
267 OCTAVE_QUIT; |
|
268 result (i, j) = a / b (i, j); |
|
269 } |
1
|
270 |
1800
|
271 return result; |
1
|
272 } |
|
273 |
1800
|
274 ComplexMatrix |
164
|
275 x_el_div (const Complex a, const Matrix& b) |
1
|
276 { |
|
277 int nr = b.rows (); |
|
278 int nc = b.columns (); |
|
279 |
|
280 ComplexMatrix result (nr, nc); |
|
281 |
|
282 for (int j = 0; j < nc; j++) |
|
283 for (int i = 0; i < nr; i++) |
4153
|
284 { |
|
285 OCTAVE_QUIT; |
|
286 result (i, j) = a / b (i, j); |
|
287 } |
1
|
288 |
1800
|
289 return result; |
1
|
290 } |
|
291 |
1800
|
292 ComplexMatrix |
164
|
293 x_el_div (const Complex a, const ComplexMatrix& b) |
1
|
294 { |
|
295 int nr = b.rows (); |
|
296 int nc = b.columns (); |
|
297 |
|
298 ComplexMatrix result (nr, nc); |
|
299 |
|
300 for (int j = 0; j < nc; j++) |
|
301 for (int i = 0; i < nr; i++) |
4153
|
302 { |
|
303 OCTAVE_QUIT; |
|
304 result (i, j) = a / b (i, j); |
|
305 } |
1
|
306 |
1800
|
307 return result; |
1
|
308 } |
|
309 |
4543
|
310 // Funny element by element division operations. |
|
311 // |
|
312 // op2 \ op1: s cs |
|
313 // +-- +---+----+ |
|
314 // N-d array | 1 | 3 | |
|
315 // +---+----+ |
|
316 // complex N-d array | 2 | 4 | |
|
317 // +---+----+ |
|
318 |
|
319 NDArray |
|
320 x_el_div (double a, const NDArray& b) |
|
321 { |
|
322 NDArray result (b.dims ()); |
|
323 |
|
324 for (int i = 0; i < b.length (); i++) |
|
325 { |
|
326 OCTAVE_QUIT; |
|
327 result (i) = a / b (i); |
|
328 } |
|
329 |
|
330 return result; |
|
331 } |
|
332 |
|
333 ComplexNDArray |
|
334 x_el_div (double a, const ComplexNDArray& b) |
|
335 { |
|
336 ComplexNDArray result (b.dims ()); |
|
337 |
|
338 for (int i = 0; i < b.length (); i++) |
|
339 { |
|
340 OCTAVE_QUIT; |
|
341 result (i) = a / b (i); |
|
342 } |
|
343 |
|
344 return result; |
|
345 } |
|
346 |
|
347 ComplexNDArray |
|
348 x_el_div (const Complex a, const NDArray& b) |
|
349 { |
|
350 ComplexNDArray result (b.dims ()); |
|
351 |
|
352 for (int i = 0; i < b.length (); i++) |
|
353 { |
|
354 OCTAVE_QUIT; |
|
355 result (i) = a / b (i); |
|
356 } |
|
357 |
|
358 return result; |
|
359 } |
|
360 |
|
361 ComplexNDArray |
|
362 x_el_div (const Complex a, const ComplexNDArray& b) |
|
363 { |
|
364 ComplexNDArray result (b.dims ()); |
|
365 |
|
366 for (int i = 0; i < b.length (); i++) |
|
367 { |
|
368 OCTAVE_QUIT; |
|
369 result (i) = a / b (i); |
|
370 } |
|
371 |
|
372 return result; |
|
373 } |
|
374 |
767
|
375 // Left division functions. |
|
376 // |
|
377 // op2 \ op1: m cm |
|
378 // +-- +---+----+ |
|
379 // matrix | 1 | 3 | |
|
380 // +---+----+ |
|
381 // complex_matrix | 2 | 4 | |
|
382 // +---+----+ |
1
|
383 |
767
|
384 // -*- 1 -*- |
1800
|
385 Matrix |
164
|
386 xleftdiv (const Matrix& a, const Matrix& b) |
1
|
387 { |
2364
|
388 if (! mx_leftdiv_conform (a, b)) |
1800
|
389 return Matrix (); |
1
|
390 |
|
391 int info; |
|
392 if (a.rows () == a.columns ()) |
|
393 { |
|
394 double rcond = 0.0; |
3480
|
395 |
|
396 Matrix result |
|
397 = a.solve (b, info, rcond, solve_singularity_warning); |
|
398 |
|
399 if (result_ok (info)) |
1800
|
400 return result; |
1
|
401 } |
|
402 |
|
403 int rank; |
1800
|
404 return a.lssolve (b, info, rank); |
1
|
405 } |
|
406 |
767
|
407 // -*- 2 -*- |
1800
|
408 ComplexMatrix |
164
|
409 xleftdiv (const Matrix& a, const ComplexMatrix& b) |
1
|
410 { |
2364
|
411 if (! mx_leftdiv_conform (a, b)) |
1800
|
412 return ComplexMatrix (); |
1
|
413 |
|
414 int info; |
|
415 if (a.rows () == a.columns ()) |
|
416 { |
|
417 double rcond = 0.0; |
3480
|
418 |
|
419 ComplexMatrix result |
|
420 = a.solve (b, info, rcond, solve_singularity_warning); |
|
421 |
|
422 if (result_ok (info)) |
1800
|
423 return result; |
1
|
424 } |
|
425 |
|
426 int rank; |
1800
|
427 return a.lssolve (b, info, rank); |
1
|
428 } |
|
429 |
767
|
430 // -*- 3 -*- |
1800
|
431 ComplexMatrix |
164
|
432 xleftdiv (const ComplexMatrix& a, const Matrix& b) |
1
|
433 { |
2364
|
434 if (! mx_leftdiv_conform (a, b)) |
1800
|
435 return ComplexMatrix (); |
1
|
436 |
|
437 int info; |
|
438 if (a.rows () == a.columns ()) |
|
439 { |
|
440 double rcond = 0.0; |
3480
|
441 |
|
442 ComplexMatrix result |
|
443 = a.solve (b, info, rcond, solve_singularity_warning); |
|
444 |
|
445 if (result_ok (info)) |
1800
|
446 return result; |
1
|
447 } |
|
448 |
|
449 int rank; |
1800
|
450 return a.lssolve (b, info, rank); |
1
|
451 } |
|
452 |
767
|
453 // -*- 4 -*- |
1800
|
454 ComplexMatrix |
164
|
455 xleftdiv (const ComplexMatrix& a, const ComplexMatrix& b) |
1
|
456 { |
2364
|
457 if (! mx_leftdiv_conform (a, b)) |
1800
|
458 return ComplexMatrix (); |
1
|
459 |
|
460 int info; |
|
461 if (a.rows () == a.columns ()) |
|
462 { |
|
463 double rcond = 0.0; |
3480
|
464 |
|
465 ComplexMatrix result |
|
466 = a.solve (b, info, rcond, solve_singularity_warning); |
|
467 |
|
468 if (result_ok (info)) |
1800
|
469 return result; |
1
|
470 } |
|
471 |
|
472 int rank; |
1800
|
473 return a.lssolve (b, info, rank); |
1
|
474 } |
|
475 |
|
476 /* |
|
477 ;;; Local Variables: *** |
|
478 ;;; mode: C++ *** |
|
479 ;;; End: *** |
|
480 */ |