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