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