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