1
|
1 // xdiv.cc -*- C++ -*- |
|
2 /* |
|
3 |
453
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
|
28 #include <assert.h> |
164
|
29 #include <Complex.h> |
|
30 |
|
31 #include "xdiv.h" |
453
|
32 #include "dMatrix.h" |
|
33 #include "CMatrix.h" |
164
|
34 #include "tree-const.h" |
1
|
35 #include "error.h" |
|
36 |
|
37 static inline int |
|
38 result_ok (int info, double rcond, int warn = 1) |
|
39 { |
|
40 assert (info != -1); |
|
41 |
|
42 if (info == -2) |
|
43 { |
931
|
44 if (warn) |
|
45 { |
|
46 warning ("matrix singular to machine precision, rcond = %g", rcond); |
|
47 return 1; |
|
48 } |
|
49 else |
|
50 { |
|
51 error ("matrix singular to machine precision, rcond = %g", rcond); |
|
52 return 0; |
|
53 } |
1
|
54 } |
|
55 else |
|
56 return 1; |
|
57 } |
|
58 |
|
59 static inline int |
|
60 mx_leftdiv_conform (int a_nr, int a_nc, int b_nr, int warn = 1) |
|
61 { |
|
62 if (a_nr != b_nr) |
|
63 { |
|
64 error ("number of rows must be the same for left division"); |
|
65 return 0; |
|
66 } |
|
67 |
|
68 return 1; |
|
69 } |
|
70 |
|
71 static inline int |
|
72 mx_div_conform (int b_nr, int b_nc, int a_nc, int warn = 1) |
|
73 { |
|
74 if (a_nc != b_nc) |
|
75 { |
|
76 error ("number of columns must be the same for right division"); |
|
77 return 0; |
|
78 } |
|
79 |
|
80 return 1; |
|
81 } |
|
82 |
767
|
83 // Right division functions. |
|
84 // |
|
85 // op2 / op1: m cm |
|
86 // +-- +---+----+ |
|
87 // matrix | 1 | 3 | |
|
88 // +---+----+ |
|
89 // complex_matrix | 2 | 4 | |
|
90 // +---+----+ |
1
|
91 |
767
|
92 // -*- 1 -*- |
1
|
93 tree_constant |
164
|
94 xdiv (const Matrix& a, const Matrix& b) |
1
|
95 { |
|
96 if (! mx_div_conform (b.rows (), b.columns (), a.columns ())) |
|
97 return tree_constant (); |
|
98 |
|
99 Matrix atmp = a.transpose (); |
|
100 Matrix btmp = b.transpose (); |
|
101 |
|
102 int info; |
|
103 if (btmp.rows () == btmp.columns ()) |
|
104 { |
|
105 double rcond = 0.0; |
|
106 Matrix result = btmp.solve (atmp, info, rcond); |
|
107 if (result_ok (info, rcond)) |
|
108 return tree_constant (result.transpose ()); |
|
109 } |
|
110 |
|
111 int rank; |
|
112 Matrix result = btmp.lssolve (atmp, info, rank); |
|
113 |
|
114 return tree_constant (result.transpose ()); |
|
115 } |
|
116 |
767
|
117 // -*- 2 -*- |
1
|
118 tree_constant |
164
|
119 xdiv (const Matrix& a, const ComplexMatrix& b) |
1
|
120 { |
|
121 if (! mx_div_conform (b.rows (), b.columns (), a.columns ())) |
|
122 return tree_constant (); |
|
123 |
|
124 Matrix atmp = a.transpose (); |
|
125 ComplexMatrix btmp = b.hermitian (); |
|
126 |
|
127 int info; |
|
128 if (btmp.rows () == btmp.columns ()) |
|
129 { |
|
130 double rcond = 0.0; |
|
131 ComplexMatrix result = btmp.solve (atmp, info, rcond); |
|
132 if (result_ok (info, rcond)) |
|
133 return tree_constant (result.hermitian ()); |
|
134 } |
|
135 |
|
136 int rank; |
|
137 ComplexMatrix result = btmp.lssolve (atmp, info, rank); |
|
138 |
|
139 return tree_constant (result.hermitian ()); |
|
140 } |
|
141 |
767
|
142 // -*- 3 -*- |
1
|
143 tree_constant |
164
|
144 xdiv (const ComplexMatrix& a, const Matrix& b) |
1
|
145 { |
|
146 if (! mx_div_conform (b.rows (), b.columns (), a.columns ())) |
|
147 return tree_constant (); |
|
148 |
|
149 ComplexMatrix atmp = a.hermitian (); |
|
150 Matrix btmp = b.transpose (); |
|
151 |
|
152 int info; |
|
153 if (btmp.rows () == btmp.columns ()) |
|
154 { |
|
155 double rcond = 0.0; |
|
156 ComplexMatrix result = btmp.solve (atmp, info, rcond); |
|
157 if (result_ok (info, rcond)) |
|
158 return tree_constant (result.hermitian ()); |
|
159 } |
|
160 |
|
161 int rank; |
|
162 ComplexMatrix result = btmp.lssolve (atmp, info, rank); |
|
163 |
|
164 return tree_constant (result.hermitian ()); |
|
165 } |
|
166 |
767
|
167 // -*- 4 -*- |
1
|
168 tree_constant |
164
|
169 xdiv (const ComplexMatrix& a, const ComplexMatrix& b) |
1
|
170 { |
|
171 if (! mx_div_conform (b.rows (), b.columns (), a.columns ())) |
|
172 return tree_constant (); |
|
173 |
|
174 ComplexMatrix atmp = a.hermitian (); |
|
175 ComplexMatrix btmp = b.hermitian (); |
|
176 |
|
177 int info; |
|
178 if (btmp.rows () == btmp.columns ()) |
|
179 { |
|
180 double rcond = 0.0; |
|
181 ComplexMatrix result = btmp.solve (atmp, info, rcond); |
|
182 if (result_ok (info, rcond)) |
|
183 return tree_constant (result.hermitian ()); |
|
184 } |
|
185 |
|
186 int rank; |
|
187 ComplexMatrix result = btmp.lssolve (atmp, info, rank); |
|
188 |
|
189 return tree_constant (result.hermitian ()); |
|
190 } |
|
191 |
767
|
192 // Funny element by element division operations. |
|
193 // |
|
194 // op2 \ op1: s cs |
|
195 // +-- +---+----+ |
|
196 // matrix | 1 | 3 | |
|
197 // +---+----+ |
|
198 // complex_matrix | 2 | 4 | |
|
199 // +---+----+ |
1
|
200 |
|
201 tree_constant |
164
|
202 x_el_div (double a, const Matrix& b) |
1
|
203 { |
|
204 int nr = b.rows (); |
|
205 int nc = b.columns (); |
|
206 |
|
207 Matrix result (nr, nc); |
|
208 |
|
209 for (int j = 0; j < nc; j++) |
|
210 for (int i = 0; i < nr; i++) |
|
211 result.elem (i, j) = a / b.elem (i, j); |
|
212 |
|
213 return tree_constant (result); |
|
214 } |
|
215 |
|
216 tree_constant |
164
|
217 x_el_div (double a, const ComplexMatrix& b) |
1
|
218 { |
|
219 int nr = b.rows (); |
|
220 int nc = b.columns (); |
|
221 |
|
222 ComplexMatrix result (nr, nc); |
|
223 |
|
224 for (int j = 0; j < nc; j++) |
|
225 for (int i = 0; i < nr; i++) |
|
226 result.elem (i, j) = a / b.elem (i, j); |
|
227 |
|
228 return tree_constant (result); |
|
229 } |
|
230 |
|
231 tree_constant |
164
|
232 x_el_div (const Complex a, const Matrix& b) |
1
|
233 { |
|
234 int nr = b.rows (); |
|
235 int nc = b.columns (); |
|
236 |
|
237 ComplexMatrix result (nr, nc); |
|
238 |
|
239 for (int j = 0; j < nc; j++) |
|
240 for (int i = 0; i < nr; i++) |
|
241 result.elem (i, j) = a / b.elem (i, j); |
|
242 |
|
243 return tree_constant (result); |
|
244 } |
|
245 |
|
246 tree_constant |
164
|
247 x_el_div (const Complex a, const ComplexMatrix& b) |
1
|
248 { |
|
249 int nr = b.rows (); |
|
250 int nc = b.columns (); |
|
251 |
|
252 ComplexMatrix result (nr, nc); |
|
253 |
|
254 for (int j = 0; j < nc; j++) |
|
255 for (int i = 0; i < nr; i++) |
|
256 result.elem (i, j) = a / b.elem (i, j); |
|
257 |
|
258 return tree_constant (result); |
|
259 } |
|
260 |
767
|
261 // Left division functions. |
|
262 // |
|
263 // op2 \ op1: m cm |
|
264 // +-- +---+----+ |
|
265 // matrix | 1 | 3 | |
|
266 // +---+----+ |
|
267 // complex_matrix | 2 | 4 | |
|
268 // +---+----+ |
1
|
269 |
767
|
270 // -*- 1 -*- |
1
|
271 tree_constant |
164
|
272 xleftdiv (const Matrix& a, const Matrix& b) |
1
|
273 { |
|
274 if (! mx_leftdiv_conform (a.rows (), a.columns (), b.rows ())) |
|
275 return tree_constant (); |
|
276 |
|
277 int info; |
|
278 if (a.rows () == a.columns ()) |
|
279 { |
|
280 double rcond = 0.0; |
|
281 Matrix result = a.solve (b, info, rcond); |
|
282 if (result_ok (info, rcond)) |
|
283 return tree_constant (result); |
|
284 } |
|
285 |
|
286 int rank; |
|
287 Matrix result = a.lssolve (b, info, rank); |
|
288 |
|
289 return tree_constant (result); |
|
290 } |
|
291 |
767
|
292 // -*- 2 -*- |
1
|
293 tree_constant |
164
|
294 xleftdiv (const Matrix& a, const ComplexMatrix& b) |
1
|
295 { |
|
296 if (! mx_leftdiv_conform (a.rows (), a.columns (), b.rows ())) |
|
297 return tree_constant (); |
|
298 |
|
299 int info; |
|
300 if (a.rows () == a.columns ()) |
|
301 { |
|
302 double rcond = 0.0; |
|
303 ComplexMatrix result = a.solve (b, info, rcond); |
|
304 if (result_ok (info, rcond)) |
|
305 return tree_constant (result); |
|
306 } |
|
307 |
|
308 int rank; |
|
309 ComplexMatrix result = a.lssolve (b, info, rank); |
|
310 |
|
311 return tree_constant (result); |
|
312 } |
|
313 |
767
|
314 // -*- 3 -*- |
1
|
315 tree_constant |
164
|
316 xleftdiv (const ComplexMatrix& a, const Matrix& b) |
1
|
317 { |
|
318 if (! mx_leftdiv_conform (a.rows (), a.columns (), b.rows ())) |
|
319 return tree_constant (); |
|
320 |
|
321 int info; |
|
322 if (a.rows () == a.columns ()) |
|
323 { |
|
324 double rcond = 0.0; |
|
325 ComplexMatrix result = a.solve (b, info, rcond); |
|
326 if (result_ok (info, rcond)) |
|
327 return tree_constant (result); |
|
328 } |
|
329 |
|
330 int rank; |
|
331 ComplexMatrix result = a.lssolve (b, info, rank); |
|
332 |
|
333 return tree_constant (result); |
|
334 } |
|
335 |
767
|
336 // -*- 4 -*- |
1
|
337 tree_constant |
164
|
338 xleftdiv (const ComplexMatrix& a, const ComplexMatrix& b) |
1
|
339 { |
|
340 if (! mx_leftdiv_conform (a.rows (), a.columns (), b.rows ())) |
|
341 return tree_constant (); |
|
342 |
|
343 int info; |
|
344 if (a.rows () == a.columns ()) |
|
345 { |
|
346 double rcond = 0.0; |
|
347 ComplexMatrix result = a.solve (b, info, rcond); |
|
348 if (result_ok (info, rcond)) |
|
349 return tree_constant (result); |
|
350 } |
|
351 |
|
352 int rank; |
|
353 ComplexMatrix result = a.lssolve (b, info, rank); |
|
354 |
|
355 return tree_constant (result); |
|
356 } |
|
357 |
|
358 /* |
|
359 ;;; Local Variables: *** |
|
360 ;;; mode: C++ *** |
|
361 ;;; page-delimiter: "^/\\*" *** |
|
362 ;;; End: *** |
|
363 */ |