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