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