458
|
1 // DiagMatrix manipulations. -*- C++ -*- |
|
2 /* |
|
3 |
1011
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
458
|
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. |
458
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
458
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
458
|
30 #endif |
|
31 |
|
32 #include <iostream.h> |
|
33 |
|
34 #include <Complex.h> |
|
35 |
|
36 #include "mx-base.h" |
|
37 #include "mx-inlines.cc" |
|
38 #include "lo-error.h" |
|
39 |
|
40 /* |
|
41 * Diagonal Matrix class. |
|
42 */ |
|
43 |
|
44 int |
|
45 DiagMatrix::operator == (const DiagMatrix& a) const |
|
46 { |
|
47 if (rows () != a.rows () || cols () != a.cols ()) |
|
48 return 0; |
|
49 |
|
50 return equal (data (), a.data (), length ()); |
|
51 } |
|
52 |
|
53 int |
|
54 DiagMatrix::operator != (const DiagMatrix& a) const |
|
55 { |
|
56 return !(*this == a); |
|
57 } |
|
58 |
|
59 DiagMatrix& |
|
60 DiagMatrix::fill (double val) |
|
61 { |
|
62 for (int i = 0; i < length (); i++) |
|
63 elem (i, i) = val; |
|
64 return *this; |
|
65 } |
|
66 |
|
67 DiagMatrix& |
|
68 DiagMatrix::fill (double val, int beg, int end) |
|
69 { |
|
70 if (beg < 0 || end >= length () || end < beg) |
|
71 { |
|
72 (*current_liboctave_error_handler) ("range error for fill"); |
|
73 return *this; |
|
74 } |
|
75 |
|
76 for (int i = beg; i < end; i++) |
|
77 elem (i, i) = val; |
|
78 |
|
79 return *this; |
|
80 } |
|
81 |
|
82 DiagMatrix& |
|
83 DiagMatrix::fill (const ColumnVector& a) |
|
84 { |
|
85 int len = length (); |
|
86 if (a.length () != len) |
|
87 { |
|
88 (*current_liboctave_error_handler) ("range error for fill"); |
|
89 return *this; |
|
90 } |
|
91 |
|
92 for (int i = 0; i < len; i++) |
|
93 elem (i, i) = a.elem (i); |
|
94 |
|
95 return *this; |
|
96 } |
|
97 |
|
98 DiagMatrix& |
|
99 DiagMatrix::fill (const RowVector& a) |
|
100 { |
|
101 int len = length (); |
|
102 if (a.length () != len) |
|
103 { |
|
104 (*current_liboctave_error_handler) ("range error for fill"); |
|
105 return *this; |
|
106 } |
|
107 |
|
108 for (int i = 0; i < len; i++) |
|
109 elem (i, i) = a.elem (i); |
|
110 |
|
111 return *this; |
|
112 } |
|
113 |
|
114 DiagMatrix& |
|
115 DiagMatrix::fill (const ColumnVector& a, int beg) |
|
116 { |
|
117 int a_len = a.length (); |
|
118 if (beg < 0 || beg + a_len >= length ()) |
|
119 { |
|
120 (*current_liboctave_error_handler) ("range error for fill"); |
|
121 return *this; |
|
122 } |
|
123 |
|
124 for (int i = 0; i < a_len; i++) |
|
125 elem (i+beg, i+beg) = a.elem (i); |
|
126 |
|
127 return *this; |
|
128 } |
|
129 |
|
130 DiagMatrix& |
|
131 DiagMatrix::fill (const RowVector& a, int beg) |
|
132 { |
|
133 int a_len = a.length (); |
|
134 if (beg < 0 || beg + a_len >= length ()) |
|
135 { |
|
136 (*current_liboctave_error_handler) ("range error for fill"); |
|
137 return *this; |
|
138 } |
|
139 |
|
140 for (int i = 0; i < a_len; i++) |
|
141 elem (i+beg, i+beg) = a.elem (i); |
|
142 |
|
143 return *this; |
|
144 } |
|
145 |
|
146 DiagMatrix |
|
147 DiagMatrix::transpose (void) const |
|
148 { |
|
149 return DiagMatrix (dup (data (), length ()), cols (), rows ()); |
|
150 } |
|
151 |
1205
|
152 DiagMatrix |
|
153 real (const ComplexDiagMatrix& a) |
|
154 { |
|
155 DiagMatrix retval; |
|
156 int a_len = a.length (); |
|
157 if (a_len > 0) |
|
158 retval = DiagMatrix (real_dup (a.data (), a_len), a.rows (), |
|
159 a.cols ()); |
|
160 return retval; |
|
161 } |
|
162 |
|
163 DiagMatrix |
|
164 imag (const ComplexDiagMatrix& a) |
|
165 { |
|
166 DiagMatrix retval; |
|
167 int a_len = a.length (); |
|
168 if (a_len > 0) |
|
169 retval = DiagMatrix (imag_dup (a.data (), a_len), a.rows (), |
|
170 a.cols ()); |
|
171 return retval; |
|
172 } |
|
173 |
458
|
174 Matrix |
|
175 DiagMatrix::extract (int r1, int c1, int r2, int c2) const |
|
176 { |
|
177 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
178 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
179 |
|
180 int new_r = r2 - r1 + 1; |
|
181 int new_c = c2 - c1 + 1; |
|
182 |
|
183 Matrix result (new_r, new_c); |
|
184 |
|
185 for (int j = 0; j < new_c; j++) |
|
186 for (int i = 0; i < new_r; i++) |
|
187 result.elem (i, j) = elem (r1+i, c1+j); |
|
188 |
|
189 return result; |
|
190 } |
|
191 |
|
192 // extract row or column i. |
|
193 |
|
194 RowVector |
|
195 DiagMatrix::row (int i) const |
|
196 { |
|
197 int nr = rows (); |
|
198 int nc = cols (); |
|
199 if (i < 0 || i >= nr) |
|
200 { |
|
201 (*current_liboctave_error_handler) ("invalid row selection"); |
|
202 return RowVector (); |
|
203 } |
|
204 |
|
205 RowVector retval (nc, 0.0); |
|
206 if (nr <= nc || (nr > nc && i < nc)) |
|
207 retval.elem (i) = elem (i, i); |
|
208 |
|
209 return retval; |
|
210 } |
|
211 |
|
212 RowVector |
|
213 DiagMatrix::row (char *s) const |
|
214 { |
533
|
215 if (! s) |
458
|
216 { |
|
217 (*current_liboctave_error_handler) ("invalid row selection"); |
|
218 return RowVector (); |
|
219 } |
|
220 |
|
221 char c = *s; |
|
222 if (c == 'f' || c == 'F') |
|
223 return row (0); |
|
224 else if (c == 'l' || c == 'L') |
|
225 return row (rows () - 1); |
|
226 else |
|
227 { |
|
228 (*current_liboctave_error_handler) ("invalid row selection"); |
|
229 return RowVector (); |
|
230 } |
|
231 } |
|
232 |
|
233 ColumnVector |
|
234 DiagMatrix::column (int i) const |
|
235 { |
|
236 int nr = rows (); |
|
237 int nc = cols (); |
|
238 if (i < 0 || i >= nc) |
|
239 { |
|
240 (*current_liboctave_error_handler) ("invalid column selection"); |
|
241 return ColumnVector (); |
|
242 } |
|
243 |
|
244 ColumnVector retval (nr, 0.0); |
|
245 if (nr >= nc || (nr < nc && i < nr)) |
|
246 retval.elem (i) = elem (i, i); |
|
247 |
|
248 return retval; |
|
249 } |
|
250 |
|
251 ColumnVector |
|
252 DiagMatrix::column (char *s) const |
|
253 { |
533
|
254 if (! s) |
458
|
255 { |
|
256 (*current_liboctave_error_handler) ("invalid column selection"); |
|
257 return ColumnVector (); |
|
258 } |
|
259 |
|
260 char c = *s; |
|
261 if (c == 'f' || c == 'F') |
|
262 return column (0); |
|
263 else if (c == 'l' || c == 'L') |
|
264 return column (cols () - 1); |
|
265 else |
|
266 { |
|
267 (*current_liboctave_error_handler) ("invalid column selection"); |
|
268 return ColumnVector (); |
|
269 } |
|
270 } |
|
271 |
|
272 DiagMatrix |
|
273 DiagMatrix::inverse (void) const |
|
274 { |
|
275 int info; |
|
276 return inverse (info); |
|
277 } |
|
278 |
|
279 DiagMatrix |
|
280 DiagMatrix::inverse (int &info) const |
|
281 { |
|
282 int nr = rows (); |
|
283 int nc = cols (); |
|
284 int len = length (); |
|
285 if (nr != nc) |
|
286 { |
|
287 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
288 return DiagMatrix (); |
|
289 } |
|
290 |
|
291 info = 0; |
|
292 double *tmp_data = dup (data (), len); |
|
293 for (int i = 0; i < len; i++) |
|
294 { |
|
295 if (elem (i, i) == 0.0) |
|
296 { |
|
297 info = -1; |
|
298 copy (tmp_data, data (), len); // Restore contents. |
|
299 break; |
|
300 } |
|
301 else |
|
302 { |
|
303 tmp_data[i] = 1.0 / elem (i, i); |
|
304 } |
|
305 } |
|
306 |
|
307 return DiagMatrix (tmp_data, nr, nc); |
|
308 } |
|
309 |
|
310 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
311 |
|
312 DiagMatrix& |
|
313 DiagMatrix::operator += (const DiagMatrix& a) |
|
314 { |
|
315 int nr = rows (); |
|
316 int nc = cols (); |
|
317 if (nr != a.rows () || nc != a.cols ()) |
|
318 { |
|
319 (*current_liboctave_error_handler) |
|
320 ("nonconformant matrix += operation attempted"); |
|
321 return *this; |
|
322 } |
|
323 |
|
324 if (nc == 0 || nr == 0) |
|
325 return *this; |
|
326 |
|
327 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
328 |
|
329 add2 (d, a.data (), length ()); |
|
330 return *this; |
|
331 } |
|
332 |
|
333 DiagMatrix& |
|
334 DiagMatrix::operator -= (const DiagMatrix& a) |
|
335 { |
|
336 int nr = rows (); |
|
337 int nc = cols (); |
|
338 if (nr != a.rows () || nc != a.cols ()) |
|
339 { |
|
340 (*current_liboctave_error_handler) |
|
341 ("nonconformant matrix -= operation attempted"); |
|
342 return *this; |
|
343 } |
|
344 |
|
345 if (nr == 0 || nc == 0) |
|
346 return *this; |
|
347 |
|
348 double *d = fortran_vec (); // Ensures only one reference to my privates! |
|
349 |
|
350 subtract2 (d, a.data (), length ()); |
|
351 return *this; |
|
352 } |
|
353 |
|
354 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
355 |
|
356 DiagMatrix |
|
357 operator * (const DiagMatrix& a, const DiagMatrix& b) |
|
358 { |
|
359 int nr_a = a.rows (); |
|
360 int nc_a = a.cols (); |
|
361 int nr_b = b.rows (); |
|
362 int nc_b = b.cols (); |
|
363 if (nc_a != nr_b) |
|
364 { |
|
365 (*current_liboctave_error_handler) |
|
366 ("nonconformant matrix multiplication attempted"); |
|
367 return DiagMatrix (); |
|
368 } |
|
369 |
|
370 if (nr_a == 0 || nc_a == 0 || nc_b == 0) |
|
371 return DiagMatrix (nr_a, nc_a, 0.0); |
|
372 |
|
373 DiagMatrix c (nr_a, nc_b); |
|
374 |
|
375 int len = nr_a < nc_b ? nr_a : nc_b; |
|
376 |
|
377 for (int i = 0; i < len; i++) |
|
378 { |
|
379 double a_element = a.elem (i, i); |
|
380 double b_element = b.elem (i, i); |
|
381 |
|
382 if (a_element == 0.0 || b_element == 0.0) |
|
383 c.elem (i, i) = 0.0; |
|
384 else if (a_element == 1.0) |
|
385 c.elem (i, i) = b_element; |
|
386 else if (b_element == 1.0) |
|
387 c.elem (i, i) = a_element; |
|
388 else |
|
389 c.elem (i, i) = a_element * b_element; |
|
390 } |
|
391 |
|
392 return c; |
|
393 } |
|
394 |
|
395 // other operations |
|
396 |
|
397 ColumnVector |
|
398 DiagMatrix::diag (void) const |
|
399 { |
|
400 return diag (0); |
|
401 } |
|
402 |
|
403 // Could be optimized... |
|
404 |
|
405 ColumnVector |
|
406 DiagMatrix::diag (int k) const |
|
407 { |
|
408 int nnr = rows (); |
|
409 int nnc = cols (); |
|
410 if (k > 0) |
|
411 nnc -= k; |
|
412 else if (k < 0) |
|
413 nnr += k; |
|
414 |
|
415 ColumnVector d; |
|
416 |
|
417 if (nnr > 0 && nnc > 0) |
|
418 { |
|
419 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
420 |
|
421 d.resize (ndiag); |
|
422 |
|
423 if (k > 0) |
|
424 { |
|
425 for (int i = 0; i < ndiag; i++) |
|
426 d.elem (i) = elem (i, i+k); |
|
427 } |
|
428 else if ( k < 0) |
|
429 { |
|
430 for (int i = 0; i < ndiag; i++) |
|
431 d.elem (i) = elem (i-k, i); |
|
432 } |
|
433 else |
|
434 { |
|
435 for (int i = 0; i < ndiag; i++) |
|
436 d.elem (i) = elem (i, i); |
|
437 } |
|
438 } |
|
439 else |
|
440 cerr << "diag: requested diagonal out of range\n"; |
|
441 |
|
442 return d; |
|
443 } |
|
444 |
|
445 ostream& |
|
446 operator << (ostream& os, const DiagMatrix& a) |
|
447 { |
|
448 // int field_width = os.precision () + 7; |
|
449 for (int i = 0; i < a.rows (); i++) |
|
450 { |
|
451 for (int j = 0; j < a.cols (); j++) |
|
452 { |
|
453 if (i == j) |
|
454 os << " " /* setw (field_width) */ << a.elem (i, i); |
|
455 else |
|
456 os << " " /* setw (field_width) */ << 0.0; |
|
457 } |
|
458 os << "\n"; |
|
459 } |
|
460 return os; |
|
461 } |
|
462 |
|
463 /* |
|
464 ;;; Local Variables: *** |
|
465 ;;; mode: C++ *** |
|
466 ;;; page-delimiter: "^/\\*" *** |
|
467 ;;; End: *** |
|
468 */ |