1993
|
1 // DiagMatrix manipulations. |
458
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 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 |
4192
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
25 #pragma implementation |
|
26 #endif |
|
27 |
458
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
458
|
30 #endif |
|
31 |
3503
|
32 #include <iostream> |
458
|
33 |
4669
|
34 #include "Array-util.h" |
1368
|
35 #include "lo-error.h" |
458
|
36 #include "mx-base.h" |
|
37 #include "mx-inlines.cc" |
1650
|
38 #include "oct-cmplx.h" |
458
|
39 |
1360
|
40 // Diagonal Matrix class. |
458
|
41 |
2385
|
42 bool |
458
|
43 DiagMatrix::operator == (const DiagMatrix& a) const |
|
44 { |
|
45 if (rows () != a.rows () || cols () != a.cols ()) |
|
46 return 0; |
|
47 |
3769
|
48 return mx_inline_equal (data (), a.data (), length ()); |
458
|
49 } |
|
50 |
2385
|
51 bool |
458
|
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 |
1696
|
74 for (int i = beg; i <= end; i++) |
458
|
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 { |
3769
|
147 return DiagMatrix (mx_inline_dup (data (), length ()), cols (), rows ()); |
458
|
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) |
3769
|
156 retval = DiagMatrix (mx_inline_real_dup (a.data (), a_len), a.rows (), |
1205
|
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) |
3769
|
167 retval = DiagMatrix (mx_inline_imag_dup (a.data (), a_len), a.rows (), |
1205
|
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 { |
3504
|
195 int r = rows (); |
|
196 int c = cols (); |
|
197 if (i < 0 || i >= r) |
458
|
198 { |
|
199 (*current_liboctave_error_handler) ("invalid row selection"); |
|
200 return RowVector (); |
|
201 } |
|
202 |
3504
|
203 RowVector retval (c, 0.0); |
|
204 if (r <= c || (r > c && i < c)) |
458
|
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 { |
3504
|
234 int r = rows (); |
|
235 int c = cols (); |
|
236 if (i < 0 || i >= c) |
458
|
237 { |
|
238 (*current_liboctave_error_handler) ("invalid column selection"); |
|
239 return ColumnVector (); |
|
240 } |
|
241 |
3504
|
242 ColumnVector retval (r, 0.0); |
|
243 if (r >= c || (r < c && i < r)) |
458
|
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 { |
3504
|
280 int r = rows (); |
|
281 int c = cols (); |
458
|
282 int len = length (); |
3504
|
283 if (r != c) |
458
|
284 { |
|
285 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
286 return DiagMatrix (); |
|
287 } |
|
288 |
3504
|
289 DiagMatrix retval (r, c); |
1627
|
290 |
458
|
291 info = 0; |
|
292 for (int i = 0; i < len; i++) |
|
293 { |
|
294 if (elem (i, i) == 0.0) |
|
295 { |
|
296 info = -1; |
1627
|
297 return *this; |
458
|
298 } |
|
299 else |
1627
|
300 retval.elem (i, i) = 1.0 / elem (i, i); |
458
|
301 } |
|
302 |
1627
|
303 return retval; |
458
|
304 } |
|
305 |
|
306 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
307 |
|
308 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
309 |
|
310 DiagMatrix |
|
311 operator * (const DiagMatrix& a, const DiagMatrix& b) |
|
312 { |
3504
|
313 int a_nr = a.rows (); |
|
314 int a_nc = a.cols (); |
2385
|
315 |
3504
|
316 int b_nr = b.rows (); |
|
317 int b_nc = b.cols (); |
2385
|
318 |
3504
|
319 if (a_nc != b_nr) |
458
|
320 { |
3504
|
321 gripe_nonconformant ("operaotr *", a_nr, a_nc, b_nr, b_nc); |
458
|
322 return DiagMatrix (); |
|
323 } |
|
324 |
3504
|
325 if (a_nr == 0 || a_nc == 0 || b_nc == 0) |
|
326 return DiagMatrix (a_nr, a_nc, 0.0); |
458
|
327 |
3504
|
328 DiagMatrix c (a_nr, b_nc); |
458
|
329 |
3504
|
330 int len = a_nr < b_nc ? a_nr : b_nc; |
458
|
331 |
|
332 for (int i = 0; i < len; i++) |
|
333 { |
|
334 double a_element = a.elem (i, i); |
|
335 double b_element = b.elem (i, i); |
|
336 |
|
337 if (a_element == 0.0 || b_element == 0.0) |
|
338 c.elem (i, i) = 0.0; |
|
339 else if (a_element == 1.0) |
|
340 c.elem (i, i) = b_element; |
|
341 else if (b_element == 1.0) |
|
342 c.elem (i, i) = a_element; |
|
343 else |
|
344 c.elem (i, i) = a_element * b_element; |
|
345 } |
|
346 |
|
347 return c; |
|
348 } |
|
349 |
|
350 // other operations |
|
351 |
|
352 ColumnVector |
|
353 DiagMatrix::diag (void) const |
|
354 { |
|
355 return diag (0); |
|
356 } |
|
357 |
|
358 // Could be optimized... |
|
359 |
|
360 ColumnVector |
|
361 DiagMatrix::diag (int k) const |
|
362 { |
|
363 int nnr = rows (); |
|
364 int nnc = cols (); |
|
365 if (k > 0) |
|
366 nnc -= k; |
|
367 else if (k < 0) |
|
368 nnr += k; |
|
369 |
|
370 ColumnVector d; |
|
371 |
|
372 if (nnr > 0 && nnc > 0) |
|
373 { |
|
374 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
375 |
|
376 d.resize (ndiag); |
|
377 |
|
378 if (k > 0) |
|
379 { |
|
380 for (int i = 0; i < ndiag; i++) |
|
381 d.elem (i) = elem (i, i+k); |
|
382 } |
|
383 else if ( k < 0) |
|
384 { |
|
385 for (int i = 0; i < ndiag; i++) |
|
386 d.elem (i) = elem (i-k, i); |
|
387 } |
|
388 else |
|
389 { |
|
390 for (int i = 0; i < ndiag; i++) |
|
391 d.elem (i) = elem (i, i); |
|
392 } |
|
393 } |
|
394 else |
4513
|
395 (*current_liboctave_error_handler) |
|
396 ("diag: requested diagonal out of range"); |
458
|
397 |
|
398 return d; |
|
399 } |
|
400 |
3504
|
401 std::ostream& |
|
402 operator << (std::ostream& os, const DiagMatrix& a) |
458
|
403 { |
|
404 // int field_width = os.precision () + 7; |
1360
|
405 |
458
|
406 for (int i = 0; i < a.rows (); i++) |
|
407 { |
|
408 for (int j = 0; j < a.cols (); j++) |
|
409 { |
|
410 if (i == j) |
|
411 os << " " /* setw (field_width) */ << a.elem (i, i); |
|
412 else |
|
413 os << " " /* setw (field_width) */ << 0.0; |
|
414 } |
|
415 os << "\n"; |
|
416 } |
|
417 return os; |
|
418 } |
|
419 |
|
420 /* |
|
421 ;;; Local Variables: *** |
|
422 ;;; mode: C++ *** |
|
423 ;;; End: *** |
|
424 */ |