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 |
|
36 static inline int |
|
37 result_ok (int info, double rcond, int warn = 1) |
|
38 { |
|
39 assert (info != -1); |
|
40 |
|
41 if (info == -2) |
|
42 { |
931
|
43 if (warn) |
932
|
44 warning ("matrix singular to machine precision, rcond = %g", rcond); |
931
|
45 else |
932
|
46 error ("matrix singular to machine precision, rcond = %g", rcond); |
|
47 |
|
48 return 0; |
1
|
49 } |
|
50 else |
|
51 return 1; |
|
52 } |
|
53 |
2364
|
54 template <class T1, class T2> |
|
55 bool |
|
56 mx_leftdiv_conform (T1 a, T2 b) |
1
|
57 { |
2364
|
58 int a_nr = a.rows (); |
|
59 int b_nr = b.rows (); |
|
60 |
1
|
61 if (a_nr != b_nr) |
|
62 { |
2364
|
63 int a_nc = a.cols (); |
|
64 int b_nc = b.cols (); |
|
65 |
|
66 gripe_nonconformant ("operator \\", a_nr, a_nc, b_nr, b_nc); |
|
67 return false; |
1
|
68 } |
|
69 |
2364
|
70 return true; |
1
|
71 } |
|
72 |
2364
|
73 template bool mx_leftdiv_conform (const Matrix&, const Matrix&); |
|
74 template bool mx_leftdiv_conform (const Matrix&, const ComplexMatrix&); |
|
75 template bool mx_leftdiv_conform (const ComplexMatrix&, const ComplexMatrix&); |
|
76 template bool mx_leftdiv_conform (const ComplexMatrix&, const Matrix&); |
|
77 |
|
78 template <class T1, class T2> |
|
79 bool |
|
80 mx_div_conform (T1 a, 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 |
2364
|
97 template bool mx_div_conform (const Matrix&, const Matrix&); |
|
98 template bool mx_div_conform (const Matrix&, const ComplexMatrix&); |
|
99 template bool mx_div_conform (const ComplexMatrix&, const ComplexMatrix&); |
|
100 template bool mx_div_conform (const ComplexMatrix&, const Matrix&); |
|
101 |
767
|
102 // Right division functions. |
|
103 // |
|
104 // op2 / op1: m cm |
|
105 // +-- +---+----+ |
|
106 // matrix | 1 | 3 | |
|
107 // +---+----+ |
|
108 // complex_matrix | 2 | 4 | |
|
109 // +---+----+ |
1
|
110 |
767
|
111 // -*- 1 -*- |
1800
|
112 Matrix |
164
|
113 xdiv (const Matrix& a, const Matrix& b) |
1
|
114 { |
2364
|
115 if (! mx_div_conform (a, b)) |
1800
|
116 return Matrix (); |
1
|
117 |
|
118 Matrix atmp = a.transpose (); |
|
119 Matrix btmp = b.transpose (); |
|
120 |
|
121 int info; |
|
122 if (btmp.rows () == btmp.columns ()) |
|
123 { |
|
124 double rcond = 0.0; |
|
125 Matrix result = btmp.solve (atmp, info, rcond); |
|
126 if (result_ok (info, rcond)) |
1800
|
127 return Matrix (result.transpose ()); |
1
|
128 } |
|
129 |
|
130 int rank; |
|
131 Matrix result = btmp.lssolve (atmp, info, rank); |
|
132 |
1800
|
133 return result.transpose (); |
1
|
134 } |
|
135 |
767
|
136 // -*- 2 -*- |
1800
|
137 ComplexMatrix |
164
|
138 xdiv (const Matrix& a, const ComplexMatrix& b) |
1
|
139 { |
2364
|
140 if (! mx_div_conform (a, b)) |
1800
|
141 return ComplexMatrix (); |
1
|
142 |
|
143 Matrix atmp = a.transpose (); |
|
144 ComplexMatrix btmp = b.hermitian (); |
|
145 |
|
146 int info; |
|
147 if (btmp.rows () == btmp.columns ()) |
|
148 { |
|
149 double rcond = 0.0; |
|
150 ComplexMatrix result = btmp.solve (atmp, info, rcond); |
|
151 if (result_ok (info, rcond)) |
1800
|
152 return result.hermitian (); |
1
|
153 } |
|
154 |
|
155 int rank; |
|
156 ComplexMatrix result = btmp.lssolve (atmp, info, rank); |
|
157 |
1800
|
158 return result.hermitian (); |
1
|
159 } |
|
160 |
767
|
161 // -*- 3 -*- |
1800
|
162 ComplexMatrix |
164
|
163 xdiv (const ComplexMatrix& a, const Matrix& b) |
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 (); |
|
170 |
|
171 int info; |
|
172 if (btmp.rows () == btmp.columns ()) |
|
173 { |
|
174 double rcond = 0.0; |
|
175 ComplexMatrix result = btmp.solve (atmp, info, rcond); |
|
176 if (result_ok (info, rcond)) |
1800
|
177 return result.hermitian (); |
1
|
178 } |
|
179 |
|
180 int rank; |
|
181 ComplexMatrix result = btmp.lssolve (atmp, info, rank); |
|
182 |
1800
|
183 return result.hermitian (); |
1
|
184 } |
|
185 |
767
|
186 // -*- 4 -*- |
1800
|
187 ComplexMatrix |
164
|
188 xdiv (const ComplexMatrix& a, const ComplexMatrix& b) |
1
|
189 { |
2364
|
190 if (! mx_div_conform (a, b)) |
1800
|
191 return ComplexMatrix (); |
1
|
192 |
|
193 ComplexMatrix atmp = a.hermitian (); |
|
194 ComplexMatrix btmp = b.hermitian (); |
|
195 |
|
196 int info; |
|
197 if (btmp.rows () == btmp.columns ()) |
|
198 { |
|
199 double rcond = 0.0; |
|
200 ComplexMatrix result = btmp.solve (atmp, info, rcond); |
|
201 if (result_ok (info, rcond)) |
1800
|
202 return result.hermitian (); |
1
|
203 } |
|
204 |
|
205 int rank; |
|
206 ComplexMatrix result = btmp.lssolve (atmp, info, rank); |
|
207 |
1800
|
208 return result.hermitian (); |
1
|
209 } |
|
210 |
767
|
211 // Funny element by element division operations. |
|
212 // |
|
213 // op2 \ op1: s cs |
|
214 // +-- +---+----+ |
|
215 // matrix | 1 | 3 | |
|
216 // +---+----+ |
|
217 // complex_matrix | 2 | 4 | |
|
218 // +---+----+ |
1
|
219 |
1800
|
220 Matrix |
164
|
221 x_el_div (double a, const Matrix& b) |
1
|
222 { |
|
223 int nr = b.rows (); |
|
224 int nc = b.columns (); |
|
225 |
|
226 Matrix result (nr, nc); |
|
227 |
|
228 for (int j = 0; j < nc; j++) |
|
229 for (int i = 0; i < nr; i++) |
2305
|
230 result (i, j) = a / b (i, j); |
1
|
231 |
1800
|
232 return result; |
1
|
233 } |
|
234 |
1800
|
235 ComplexMatrix |
164
|
236 x_el_div (double a, const ComplexMatrix& b) |
1
|
237 { |
|
238 int nr = b.rows (); |
|
239 int nc = b.columns (); |
|
240 |
|
241 ComplexMatrix 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 (const Complex a, const Matrix& 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 ComplexMatrix& 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 |
767
|
280 // Left division functions. |
|
281 // |
|
282 // op2 \ op1: m cm |
|
283 // +-- +---+----+ |
|
284 // matrix | 1 | 3 | |
|
285 // +---+----+ |
|
286 // complex_matrix | 2 | 4 | |
|
287 // +---+----+ |
1
|
288 |
767
|
289 // -*- 1 -*- |
1800
|
290 Matrix |
164
|
291 xleftdiv (const Matrix& a, const Matrix& b) |
1
|
292 { |
2364
|
293 if (! mx_leftdiv_conform (a, b)) |
1800
|
294 return Matrix (); |
1
|
295 |
|
296 int info; |
|
297 if (a.rows () == a.columns ()) |
|
298 { |
|
299 double rcond = 0.0; |
|
300 Matrix result = a.solve (b, info, rcond); |
|
301 if (result_ok (info, rcond)) |
1800
|
302 return result; |
1
|
303 } |
|
304 |
|
305 int rank; |
1800
|
306 return a.lssolve (b, info, rank); |
1
|
307 } |
|
308 |
767
|
309 // -*- 2 -*- |
1800
|
310 ComplexMatrix |
164
|
311 xleftdiv (const Matrix& a, const ComplexMatrix& b) |
1
|
312 { |
2364
|
313 if (! mx_leftdiv_conform (a, b)) |
1800
|
314 return ComplexMatrix (); |
1
|
315 |
|
316 int info; |
|
317 if (a.rows () == a.columns ()) |
|
318 { |
|
319 double rcond = 0.0; |
|
320 ComplexMatrix result = a.solve (b, info, rcond); |
|
321 if (result_ok (info, rcond)) |
1800
|
322 return result; |
1
|
323 } |
|
324 |
|
325 int rank; |
1800
|
326 return a.lssolve (b, info, rank); |
1
|
327 } |
|
328 |
767
|
329 // -*- 3 -*- |
1800
|
330 ComplexMatrix |
164
|
331 xleftdiv (const ComplexMatrix& a, const Matrix& b) |
1
|
332 { |
2364
|
333 if (! mx_leftdiv_conform (a, b)) |
1800
|
334 return ComplexMatrix (); |
1
|
335 |
|
336 int info; |
|
337 if (a.rows () == a.columns ()) |
|
338 { |
|
339 double rcond = 0.0; |
|
340 ComplexMatrix result = a.solve (b, info, rcond); |
|
341 if (result_ok (info, rcond)) |
1800
|
342 return result; |
1
|
343 } |
|
344 |
|
345 int rank; |
1800
|
346 return a.lssolve (b, info, rank); |
1
|
347 } |
|
348 |
767
|
349 // -*- 4 -*- |
1800
|
350 ComplexMatrix |
164
|
351 xleftdiv (const ComplexMatrix& a, const ComplexMatrix& 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; |
|
360 ComplexMatrix result = a.solve (b, info, rcond); |
|
361 if (result_ok (info, rcond)) |
1800
|
362 return result; |
1
|
363 } |
|
364 |
|
365 int rank; |
1800
|
366 return a.lssolve (b, info, rank); |
1
|
367 } |
|
368 |
|
369 /* |
|
370 ;;; Local Variables: *** |
|
371 ;;; mode: C++ *** |
|
372 ;;; End: *** |
|
373 */ |