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 |
|
32 #include "xdiv.h" |
453
|
33 #include "dMatrix.h" |
|
34 #include "CMatrix.h" |
164
|
35 #include "tree-const.h" |
1
|
36 #include "error.h" |
|
37 |
|
38 static inline int |
|
39 result_ok (int info, double rcond, int warn = 1) |
|
40 { |
|
41 assert (info != -1); |
|
42 |
|
43 if (info == -2) |
|
44 { |
931
|
45 if (warn) |
932
|
46 warning ("matrix singular to machine precision, rcond = %g", rcond); |
931
|
47 else |
932
|
48 error ("matrix singular to machine precision, rcond = %g", rcond); |
|
49 |
|
50 return 0; |
1
|
51 } |
|
52 else |
|
53 return 1; |
|
54 } |
|
55 |
|
56 static inline int |
|
57 mx_leftdiv_conform (int a_nr, int a_nc, int b_nr, int warn = 1) |
|
58 { |
|
59 if (a_nr != b_nr) |
|
60 { |
|
61 error ("number of rows must be the same for left division"); |
|
62 return 0; |
|
63 } |
|
64 |
|
65 return 1; |
|
66 } |
|
67 |
|
68 static inline int |
|
69 mx_div_conform (int b_nr, int b_nc, int a_nc, int warn = 1) |
|
70 { |
|
71 if (a_nc != b_nc) |
|
72 { |
|
73 error ("number of columns must be the same for right division"); |
|
74 return 0; |
|
75 } |
|
76 |
|
77 return 1; |
|
78 } |
|
79 |
767
|
80 // Right division functions. |
|
81 // |
|
82 // op2 / op1: m cm |
|
83 // +-- +---+----+ |
|
84 // matrix | 1 | 3 | |
|
85 // +---+----+ |
|
86 // complex_matrix | 2 | 4 | |
|
87 // +---+----+ |
1
|
88 |
767
|
89 // -*- 1 -*- |
1
|
90 tree_constant |
164
|
91 xdiv (const Matrix& a, const Matrix& b) |
1
|
92 { |
|
93 if (! mx_div_conform (b.rows (), b.columns (), a.columns ())) |
|
94 return tree_constant (); |
|
95 |
|
96 Matrix atmp = a.transpose (); |
|
97 Matrix btmp = b.transpose (); |
|
98 |
|
99 int info; |
|
100 if (btmp.rows () == btmp.columns ()) |
|
101 { |
|
102 double rcond = 0.0; |
|
103 Matrix result = btmp.solve (atmp, info, rcond); |
|
104 if (result_ok (info, rcond)) |
|
105 return tree_constant (result.transpose ()); |
|
106 } |
|
107 |
|
108 int rank; |
|
109 Matrix result = btmp.lssolve (atmp, info, rank); |
|
110 |
|
111 return tree_constant (result.transpose ()); |
|
112 } |
|
113 |
767
|
114 // -*- 2 -*- |
1
|
115 tree_constant |
164
|
116 xdiv (const Matrix& a, const ComplexMatrix& b) |
1
|
117 { |
|
118 if (! mx_div_conform (b.rows (), b.columns (), a.columns ())) |
|
119 return tree_constant (); |
|
120 |
|
121 Matrix atmp = a.transpose (); |
|
122 ComplexMatrix btmp = b.hermitian (); |
|
123 |
|
124 int info; |
|
125 if (btmp.rows () == btmp.columns ()) |
|
126 { |
|
127 double rcond = 0.0; |
|
128 ComplexMatrix result = btmp.solve (atmp, info, rcond); |
|
129 if (result_ok (info, rcond)) |
|
130 return tree_constant (result.hermitian ()); |
|
131 } |
|
132 |
|
133 int rank; |
|
134 ComplexMatrix result = btmp.lssolve (atmp, info, rank); |
|
135 |
|
136 return tree_constant (result.hermitian ()); |
|
137 } |
|
138 |
767
|
139 // -*- 3 -*- |
1
|
140 tree_constant |
164
|
141 xdiv (const ComplexMatrix& a, const Matrix& b) |
1
|
142 { |
|
143 if (! mx_div_conform (b.rows (), b.columns (), a.columns ())) |
|
144 return tree_constant (); |
|
145 |
|
146 ComplexMatrix atmp = a.hermitian (); |
|
147 Matrix btmp = b.transpose (); |
|
148 |
|
149 int info; |
|
150 if (btmp.rows () == btmp.columns ()) |
|
151 { |
|
152 double rcond = 0.0; |
|
153 ComplexMatrix result = btmp.solve (atmp, info, rcond); |
|
154 if (result_ok (info, rcond)) |
|
155 return tree_constant (result.hermitian ()); |
|
156 } |
|
157 |
|
158 int rank; |
|
159 ComplexMatrix result = btmp.lssolve (atmp, info, rank); |
|
160 |
|
161 return tree_constant (result.hermitian ()); |
|
162 } |
|
163 |
767
|
164 // -*- 4 -*- |
1
|
165 tree_constant |
164
|
166 xdiv (const ComplexMatrix& a, const ComplexMatrix& b) |
1
|
167 { |
|
168 if (! mx_div_conform (b.rows (), b.columns (), a.columns ())) |
|
169 return tree_constant (); |
|
170 |
|
171 ComplexMatrix atmp = a.hermitian (); |
|
172 ComplexMatrix btmp = b.hermitian (); |
|
173 |
|
174 int info; |
|
175 if (btmp.rows () == btmp.columns ()) |
|
176 { |
|
177 double rcond = 0.0; |
|
178 ComplexMatrix result = btmp.solve (atmp, info, rcond); |
|
179 if (result_ok (info, rcond)) |
|
180 return tree_constant (result.hermitian ()); |
|
181 } |
|
182 |
|
183 int rank; |
|
184 ComplexMatrix result = btmp.lssolve (atmp, info, rank); |
|
185 |
|
186 return tree_constant (result.hermitian ()); |
|
187 } |
|
188 |
767
|
189 // Funny element by element division operations. |
|
190 // |
|
191 // op2 \ op1: s cs |
|
192 // +-- +---+----+ |
|
193 // matrix | 1 | 3 | |
|
194 // +---+----+ |
|
195 // complex_matrix | 2 | 4 | |
|
196 // +---+----+ |
1
|
197 |
|
198 tree_constant |
164
|
199 x_el_div (double a, const Matrix& b) |
1
|
200 { |
|
201 int nr = b.rows (); |
|
202 int nc = b.columns (); |
|
203 |
|
204 Matrix result (nr, nc); |
|
205 |
|
206 for (int j = 0; j < nc; j++) |
|
207 for (int i = 0; i < nr; i++) |
|
208 result.elem (i, j) = a / b.elem (i, j); |
|
209 |
|
210 return tree_constant (result); |
|
211 } |
|
212 |
|
213 tree_constant |
164
|
214 x_el_div (double a, const ComplexMatrix& b) |
1
|
215 { |
|
216 int nr = b.rows (); |
|
217 int nc = b.columns (); |
|
218 |
|
219 ComplexMatrix result (nr, nc); |
|
220 |
|
221 for (int j = 0; j < nc; j++) |
|
222 for (int i = 0; i < nr; i++) |
|
223 result.elem (i, j) = a / b.elem (i, j); |
|
224 |
|
225 return tree_constant (result); |
|
226 } |
|
227 |
|
228 tree_constant |
164
|
229 x_el_div (const Complex a, const Matrix& b) |
1
|
230 { |
|
231 int nr = b.rows (); |
|
232 int nc = b.columns (); |
|
233 |
|
234 ComplexMatrix result (nr, nc); |
|
235 |
|
236 for (int j = 0; j < nc; j++) |
|
237 for (int i = 0; i < nr; i++) |
|
238 result.elem (i, j) = a / b.elem (i, j); |
|
239 |
|
240 return tree_constant (result); |
|
241 } |
|
242 |
|
243 tree_constant |
164
|
244 x_el_div (const Complex a, const ComplexMatrix& b) |
1
|
245 { |
|
246 int nr = b.rows (); |
|
247 int nc = b.columns (); |
|
248 |
|
249 ComplexMatrix result (nr, nc); |
|
250 |
|
251 for (int j = 0; j < nc; j++) |
|
252 for (int i = 0; i < nr; i++) |
|
253 result.elem (i, j) = a / b.elem (i, j); |
|
254 |
|
255 return tree_constant (result); |
|
256 } |
|
257 |
767
|
258 // Left division functions. |
|
259 // |
|
260 // op2 \ op1: m cm |
|
261 // +-- +---+----+ |
|
262 // matrix | 1 | 3 | |
|
263 // +---+----+ |
|
264 // complex_matrix | 2 | 4 | |
|
265 // +---+----+ |
1
|
266 |
767
|
267 // -*- 1 -*- |
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 |
767
|
289 // -*- 2 -*- |
1
|
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 |
767
|
311 // -*- 3 -*- |
1
|
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 |
767
|
333 // -*- 4 -*- |
1
|
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 */ |