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 { |
|
44 error ("matrix singular to machine precision, rcond = %g", rcond); |
|
45 return 0; |
|
46 } |
|
47 else |
|
48 return 1; |
|
49 } |
|
50 |
|
51 static inline int |
|
52 mx_leftdiv_conform (int a_nr, int a_nc, int b_nr, int warn = 1) |
|
53 { |
|
54 if (a_nr != b_nr) |
|
55 { |
|
56 error ("number of rows must be the same for left division"); |
|
57 return 0; |
|
58 } |
|
59 |
|
60 return 1; |
|
61 } |
|
62 |
|
63 static inline int |
|
64 mx_div_conform (int b_nr, int b_nc, int a_nc, int warn = 1) |
|
65 { |
|
66 if (a_nc != b_nc) |
|
67 { |
|
68 error ("number of columns must be the same for right division"); |
|
69 return 0; |
|
70 } |
|
71 |
|
72 return 1; |
|
73 } |
|
74 |
|
75 /* |
|
76 * Right division functions. |
|
77 * |
|
78 * op2 / op1: m cm |
|
79 * +-- +---+----+ |
|
80 * matrix | 1 | 3 | |
|
81 * +---+----+ |
|
82 * complex_matrix | 2 | 4 | |
|
83 * +---+----+ |
|
84 */ |
|
85 |
|
86 /* 1 */ |
|
87 tree_constant |
164
|
88 xdiv (const Matrix& a, const Matrix& b) |
1
|
89 { |
|
90 if (! mx_div_conform (b.rows (), b.columns (), a.columns ())) |
|
91 return tree_constant (); |
|
92 |
|
93 Matrix atmp = a.transpose (); |
|
94 Matrix btmp = b.transpose (); |
|
95 |
|
96 int info; |
|
97 if (btmp.rows () == btmp.columns ()) |
|
98 { |
|
99 double rcond = 0.0; |
|
100 Matrix result = btmp.solve (atmp, info, rcond); |
|
101 if (result_ok (info, rcond)) |
|
102 return tree_constant (result.transpose ()); |
|
103 } |
|
104 |
|
105 int rank; |
|
106 Matrix result = btmp.lssolve (atmp, info, rank); |
|
107 |
|
108 return tree_constant (result.transpose ()); |
|
109 } |
|
110 |
|
111 /* 2 */ |
|
112 tree_constant |
164
|
113 xdiv (const Matrix& a, const ComplexMatrix& b) |
1
|
114 { |
|
115 if (! mx_div_conform (b.rows (), b.columns (), a.columns ())) |
|
116 return tree_constant (); |
|
117 |
|
118 Matrix atmp = a.transpose (); |
|
119 ComplexMatrix btmp = b.hermitian (); |
|
120 |
|
121 int info; |
|
122 if (btmp.rows () == btmp.columns ()) |
|
123 { |
|
124 double rcond = 0.0; |
|
125 ComplexMatrix result = btmp.solve (atmp, info, rcond); |
|
126 if (result_ok (info, rcond)) |
|
127 return tree_constant (result.hermitian ()); |
|
128 } |
|
129 |
|
130 int rank; |
|
131 ComplexMatrix result = btmp.lssolve (atmp, info, rank); |
|
132 |
|
133 return tree_constant (result.hermitian ()); |
|
134 } |
|
135 |
|
136 /* 3 */ |
|
137 tree_constant |
164
|
138 xdiv (const ComplexMatrix& a, const Matrix& b) |
1
|
139 { |
|
140 if (! mx_div_conform (b.rows (), b.columns (), a.columns ())) |
|
141 return tree_constant (); |
|
142 |
|
143 ComplexMatrix atmp = a.hermitian (); |
|
144 Matrix btmp = b.transpose (); |
|
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)) |
|
152 return tree_constant (result.hermitian ()); |
|
153 } |
|
154 |
|
155 int rank; |
|
156 ComplexMatrix result = btmp.lssolve (atmp, info, rank); |
|
157 |
|
158 return tree_constant (result.hermitian ()); |
|
159 } |
|
160 |
|
161 /* 4 */ |
|
162 tree_constant |
164
|
163 xdiv (const ComplexMatrix& a, const ComplexMatrix& b) |
1
|
164 { |
|
165 if (! mx_div_conform (b.rows (), b.columns (), a.columns ())) |
|
166 return tree_constant (); |
|
167 |
|
168 ComplexMatrix atmp = a.hermitian (); |
|
169 ComplexMatrix btmp = b.hermitian (); |
|
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)) |
|
177 return tree_constant (result.hermitian ()); |
|
178 } |
|
179 |
|
180 int rank; |
|
181 ComplexMatrix result = btmp.lssolve (atmp, info, rank); |
|
182 |
|
183 return tree_constant (result.hermitian ()); |
|
184 } |
|
185 |
|
186 /* |
|
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 * +---+----+ |
|
195 */ |
|
196 |
|
197 tree_constant |
164
|
198 x_el_div (double a, const Matrix& b) |
1
|
199 { |
|
200 int nr = b.rows (); |
|
201 int nc = b.columns (); |
|
202 |
|
203 Matrix result (nr, nc); |
|
204 |
|
205 for (int j = 0; j < nc; j++) |
|
206 for (int i = 0; i < nr; i++) |
|
207 result.elem (i, j) = a / b.elem (i, j); |
|
208 |
|
209 return tree_constant (result); |
|
210 } |
|
211 |
|
212 tree_constant |
164
|
213 x_el_div (double a, const ComplexMatrix& b) |
1
|
214 { |
|
215 int nr = b.rows (); |
|
216 int nc = b.columns (); |
|
217 |
|
218 ComplexMatrix result (nr, nc); |
|
219 |
|
220 for (int j = 0; j < nc; j++) |
|
221 for (int i = 0; i < nr; i++) |
|
222 result.elem (i, j) = a / b.elem (i, j); |
|
223 |
|
224 return tree_constant (result); |
|
225 } |
|
226 |
|
227 tree_constant |
164
|
228 x_el_div (const Complex a, const Matrix& b) |
1
|
229 { |
|
230 int nr = b.rows (); |
|
231 int nc = b.columns (); |
|
232 |
|
233 ComplexMatrix result (nr, nc); |
|
234 |
|
235 for (int j = 0; j < nc; j++) |
|
236 for (int i = 0; i < nr; i++) |
|
237 result.elem (i, j) = a / b.elem (i, j); |
|
238 |
|
239 return tree_constant (result); |
|
240 } |
|
241 |
|
242 tree_constant |
164
|
243 x_el_div (const Complex a, const ComplexMatrix& b) |
1
|
244 { |
|
245 int nr = b.rows (); |
|
246 int nc = b.columns (); |
|
247 |
|
248 ComplexMatrix result (nr, nc); |
|
249 |
|
250 for (int j = 0; j < nc; j++) |
|
251 for (int i = 0; i < nr; i++) |
|
252 result.elem (i, j) = a / b.elem (i, j); |
|
253 |
|
254 return tree_constant (result); |
|
255 } |
|
256 |
|
257 /* |
|
258 * Left division functions. |
|
259 * |
|
260 * op2 \ op1: m cm |
|
261 * +-- +---+----+ |
|
262 * matrix | 1 | 3 | |
|
263 * +---+----+ |
|
264 * complex_matrix | 2 | 4 | |
|
265 * +---+----+ |
|
266 */ |
|
267 |
|
268 /* 1 */ |
|
269 tree_constant |
164
|
270 xleftdiv (const Matrix& a, const Matrix& b) |
1
|
271 { |
|
272 if (! mx_leftdiv_conform (a.rows (), a.columns (), b.rows ())) |
|
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 |
|
290 /* 2 */ |
|
291 tree_constant |
164
|
292 xleftdiv (const Matrix& a, const ComplexMatrix& b) |
1
|
293 { |
|
294 if (! mx_leftdiv_conform (a.rows (), a.columns (), b.rows ())) |
|
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 |
|
312 /* 3 */ |
|
313 tree_constant |
164
|
314 xleftdiv (const ComplexMatrix& a, const Matrix& b) |
1
|
315 { |
|
316 if (! mx_leftdiv_conform (a.rows (), a.columns (), b.rows ())) |
|
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 |
|
334 /* 4 */ |
|
335 tree_constant |
164
|
336 xleftdiv (const ComplexMatrix& a, const ComplexMatrix& b) |
1
|
337 { |
|
338 if (! mx_leftdiv_conform (a.rows (), a.columns (), b.rows ())) |
|
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 */ |