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