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