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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
1
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
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 |
5275
|
41 result_ok (octave_idx_type 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 { |
5275
|
59 octave_idx_type a_nr = a.rows (); |
|
60 octave_idx_type b_nr = b.rows (); |
2364
|
61 |
1
|
62 if (a_nr != b_nr) |
|
63 { |
5275
|
64 octave_idx_type a_nc = a.cols (); |
|
65 octave_idx_type b_nc = b.cols (); |
2364
|
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 { |
5275
|
86 octave_idx_type a_nc = a.cols (); |
|
87 octave_idx_type b_nc = b.cols (); |
2364
|
88 |
1
|
89 if (a_nc != b_nc) |
|
90 { |
5275
|
91 octave_idx_type a_nr = a.rows (); |
|
92 octave_idx_type b_nr = b.rows (); |
2364
|
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 |
5785
|
120 xdiv (const Matrix& a, const Matrix& b, MatrixType &typ) |
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 (); |
5785
|
127 MatrixType btyp = typ.transpose (); |
1
|
128 |
5275
|
129 octave_idx_type info; |
5785
|
130 double rcond = 0.0; |
3480
|
131 |
5785
|
132 Matrix result |
|
133 = btmp.solve (btyp, atmp, info, rcond, solve_singularity_warning); |
1
|
134 |
5785
|
135 typ = btyp.transpose (); |
1800
|
136 return result.transpose (); |
1
|
137 } |
|
138 |
767
|
139 // -*- 2 -*- |
1800
|
140 ComplexMatrix |
5785
|
141 xdiv (const Matrix& a, const ComplexMatrix& b, MatrixType &typ) |
1
|
142 { |
2364
|
143 if (! mx_div_conform (a, b)) |
1800
|
144 return ComplexMatrix (); |
1
|
145 |
|
146 Matrix atmp = a.transpose (); |
|
147 ComplexMatrix btmp = b.hermitian (); |
5785
|
148 MatrixType btyp = typ.transpose (); |
1
|
149 |
5275
|
150 octave_idx_type info; |
5785
|
151 double rcond = 0.0; |
3480
|
152 |
5785
|
153 ComplexMatrix result |
|
154 = btmp.solve (btyp, atmp, info, rcond, solve_singularity_warning); |
1
|
155 |
5785
|
156 typ = btyp.transpose (); |
1800
|
157 return result.hermitian (); |
1
|
158 } |
|
159 |
767
|
160 // -*- 3 -*- |
1800
|
161 ComplexMatrix |
5785
|
162 xdiv (const ComplexMatrix& a, const Matrix& b, MatrixType &typ) |
1
|
163 { |
2364
|
164 if (! mx_div_conform (a, b)) |
1800
|
165 return ComplexMatrix (); |
1
|
166 |
|
167 ComplexMatrix atmp = a.hermitian (); |
|
168 Matrix btmp = b.transpose (); |
5785
|
169 MatrixType btyp = typ.transpose (); |
1
|
170 |
5275
|
171 octave_idx_type info; |
5785
|
172 double rcond = 0.0; |
3480
|
173 |
5785
|
174 ComplexMatrix result |
|
175 = btmp.solve (btyp, atmp, info, rcond, solve_singularity_warning); |
1
|
176 |
5785
|
177 typ = btyp.transpose (); |
1800
|
178 return result.hermitian (); |
1
|
179 } |
|
180 |
767
|
181 // -*- 4 -*- |
1800
|
182 ComplexMatrix |
5785
|
183 xdiv (const ComplexMatrix& a, const ComplexMatrix& b, MatrixType &typ) |
1
|
184 { |
2364
|
185 if (! mx_div_conform (a, b)) |
1800
|
186 return ComplexMatrix (); |
1
|
187 |
|
188 ComplexMatrix atmp = a.hermitian (); |
|
189 ComplexMatrix btmp = b.hermitian (); |
5785
|
190 MatrixType btyp = typ.transpose (); |
1
|
191 |
5275
|
192 octave_idx_type info; |
5785
|
193 double rcond = 0.0; |
3480
|
194 |
5785
|
195 ComplexMatrix result |
|
196 = btmp.solve (btyp, atmp, info, rcond, solve_singularity_warning); |
1
|
197 |
5785
|
198 typ = btyp.transpose (); |
1800
|
199 return result.hermitian (); |
1
|
200 } |
|
201 |
767
|
202 // Funny element by element division operations. |
|
203 // |
|
204 // op2 \ op1: s cs |
|
205 // +-- +---+----+ |
|
206 // matrix | 1 | 3 | |
|
207 // +---+----+ |
|
208 // complex_matrix | 2 | 4 | |
|
209 // +---+----+ |
1
|
210 |
1800
|
211 Matrix |
164
|
212 x_el_div (double a, const Matrix& b) |
1
|
213 { |
5275
|
214 octave_idx_type nr = b.rows (); |
|
215 octave_idx_type nc = b.columns (); |
1
|
216 |
|
217 Matrix result (nr, nc); |
|
218 |
5275
|
219 for (octave_idx_type j = 0; j < nc; j++) |
|
220 for (octave_idx_type i = 0; i < nr; i++) |
4153
|
221 { |
|
222 OCTAVE_QUIT; |
|
223 result (i, j) = a / b (i, j); |
|
224 } |
1
|
225 |
1800
|
226 return result; |
1
|
227 } |
|
228 |
1800
|
229 ComplexMatrix |
164
|
230 x_el_div (double a, const ComplexMatrix& b) |
1
|
231 { |
5275
|
232 octave_idx_type nr = b.rows (); |
|
233 octave_idx_type nc = b.columns (); |
1
|
234 |
|
235 ComplexMatrix result (nr, nc); |
|
236 |
5275
|
237 for (octave_idx_type j = 0; j < nc; j++) |
|
238 for (octave_idx_type i = 0; i < nr; i++) |
4153
|
239 { |
|
240 OCTAVE_QUIT; |
|
241 result (i, j) = a / b (i, j); |
|
242 } |
1
|
243 |
1800
|
244 return result; |
1
|
245 } |
|
246 |
1800
|
247 ComplexMatrix |
164
|
248 x_el_div (const Complex a, const Matrix& b) |
1
|
249 { |
5275
|
250 octave_idx_type nr = b.rows (); |
|
251 octave_idx_type nc = b.columns (); |
1
|
252 |
|
253 ComplexMatrix result (nr, nc); |
|
254 |
5275
|
255 for (octave_idx_type j = 0; j < nc; j++) |
|
256 for (octave_idx_type i = 0; i < nr; i++) |
4153
|
257 { |
|
258 OCTAVE_QUIT; |
|
259 result (i, j) = a / b (i, j); |
|
260 } |
1
|
261 |
1800
|
262 return result; |
1
|
263 } |
|
264 |
1800
|
265 ComplexMatrix |
164
|
266 x_el_div (const Complex a, const ComplexMatrix& b) |
1
|
267 { |
5275
|
268 octave_idx_type nr = b.rows (); |
|
269 octave_idx_type nc = b.columns (); |
1
|
270 |
|
271 ComplexMatrix result (nr, nc); |
|
272 |
5275
|
273 for (octave_idx_type j = 0; j < nc; j++) |
|
274 for (octave_idx_type i = 0; i < nr; i++) |
4153
|
275 { |
|
276 OCTAVE_QUIT; |
|
277 result (i, j) = a / b (i, j); |
|
278 } |
1
|
279 |
1800
|
280 return result; |
1
|
281 } |
|
282 |
4543
|
283 // Funny element by element division operations. |
|
284 // |
|
285 // op2 \ op1: s cs |
|
286 // +-- +---+----+ |
|
287 // N-d array | 1 | 3 | |
|
288 // +---+----+ |
|
289 // complex N-d array | 2 | 4 | |
|
290 // +---+----+ |
|
291 |
|
292 NDArray |
|
293 x_el_div (double a, const NDArray& b) |
|
294 { |
|
295 NDArray result (b.dims ()); |
|
296 |
5275
|
297 for (octave_idx_type i = 0; i < b.length (); i++) |
4543
|
298 { |
|
299 OCTAVE_QUIT; |
|
300 result (i) = a / b (i); |
|
301 } |
|
302 |
|
303 return result; |
|
304 } |
|
305 |
|
306 ComplexNDArray |
|
307 x_el_div (double a, const ComplexNDArray& b) |
|
308 { |
|
309 ComplexNDArray result (b.dims ()); |
|
310 |
5275
|
311 for (octave_idx_type i = 0; i < b.length (); i++) |
4543
|
312 { |
|
313 OCTAVE_QUIT; |
|
314 result (i) = a / b (i); |
|
315 } |
|
316 |
|
317 return result; |
|
318 } |
|
319 |
|
320 ComplexNDArray |
|
321 x_el_div (const Complex a, const NDArray& b) |
|
322 { |
|
323 ComplexNDArray result (b.dims ()); |
|
324 |
5275
|
325 for (octave_idx_type i = 0; i < b.length (); i++) |
4543
|
326 { |
|
327 OCTAVE_QUIT; |
|
328 result (i) = a / b (i); |
|
329 } |
|
330 |
|
331 return result; |
|
332 } |
|
333 |
|
334 ComplexNDArray |
|
335 x_el_div (const Complex a, const ComplexNDArray& b) |
|
336 { |
|
337 ComplexNDArray result (b.dims ()); |
|
338 |
5275
|
339 for (octave_idx_type i = 0; i < b.length (); i++) |
4543
|
340 { |
|
341 OCTAVE_QUIT; |
|
342 result (i) = a / b (i); |
|
343 } |
|
344 |
|
345 return result; |
|
346 } |
|
347 |
767
|
348 // Left division functions. |
|
349 // |
|
350 // op2 \ op1: m cm |
|
351 // +-- +---+----+ |
|
352 // matrix | 1 | 3 | |
|
353 // +---+----+ |
|
354 // complex_matrix | 2 | 4 | |
|
355 // +---+----+ |
1
|
356 |
767
|
357 // -*- 1 -*- |
1800
|
358 Matrix |
5785
|
359 xleftdiv (const Matrix& a, const Matrix& b, MatrixType &typ) |
1
|
360 { |
2364
|
361 if (! mx_leftdiv_conform (a, b)) |
1800
|
362 return Matrix (); |
1
|
363 |
5275
|
364 octave_idx_type info; |
5785
|
365 double rcond = 0.0; |
|
366 return a.solve (typ, b, info, rcond, solve_singularity_warning); |
1
|
367 } |
|
368 |
767
|
369 // -*- 2 -*- |
1800
|
370 ComplexMatrix |
5785
|
371 xleftdiv (const Matrix& a, const ComplexMatrix& b, MatrixType &typ) |
1
|
372 { |
2364
|
373 if (! mx_leftdiv_conform (a, b)) |
1800
|
374 return ComplexMatrix (); |
1
|
375 |
5275
|
376 octave_idx_type info; |
5785
|
377 double rcond = 0.0; |
3480
|
378 |
5785
|
379 return a.solve (typ, b, info, rcond, solve_singularity_warning); |
1
|
380 } |
|
381 |
767
|
382 // -*- 3 -*- |
1800
|
383 ComplexMatrix |
5785
|
384 xleftdiv (const ComplexMatrix& a, const Matrix& b, MatrixType &typ) |
1
|
385 { |
2364
|
386 if (! mx_leftdiv_conform (a, b)) |
1800
|
387 return ComplexMatrix (); |
1
|
388 |
5275
|
389 octave_idx_type info; |
5785
|
390 double rcond = 0.0; |
|
391 return a.solve (typ, b, info, rcond, solve_singularity_warning); |
1
|
392 } |
|
393 |
767
|
394 // -*- 4 -*- |
1800
|
395 ComplexMatrix |
5785
|
396 xleftdiv (const ComplexMatrix& a, const ComplexMatrix& b, MatrixType &typ) |
1
|
397 { |
2364
|
398 if (! mx_leftdiv_conform (a, b)) |
1800
|
399 return ComplexMatrix (); |
1
|
400 |
5275
|
401 octave_idx_type info; |
5785
|
402 double rcond = 0.0; |
|
403 return a.solve (typ, b, info, rcond, solve_singularity_warning); |
1
|
404 } |
|
405 |
|
406 /* |
|
407 ;;; Local Variables: *** |
|
408 ;;; mode: C++ *** |
|
409 ;;; End: *** |
|
410 */ |