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