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