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