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