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