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