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