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