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 |
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 |
2385
|
41 bool |
458
|
42 DiagMatrix::operator == (const DiagMatrix& a) const |
|
43 { |
|
44 if (rows () != a.rows () || cols () != a.cols ()) |
|
45 return 0; |
|
46 |
3769
|
47 return mx_inline_equal (data (), a.data (), length ()); |
458
|
48 } |
|
49 |
2385
|
50 bool |
458
|
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 { |
3769
|
146 return DiagMatrix (mx_inline_dup (data (), length ()), cols (), rows ()); |
458
|
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) |
3769
|
155 retval = DiagMatrix (mx_inline_real_dup (a.data (), a_len), a.rows (), |
1205
|
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) |
3769
|
166 retval = DiagMatrix (mx_inline_imag_dup (a.data (), a_len), a.rows (), |
1205
|
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 { |
3504
|
194 int r = rows (); |
|
195 int c = cols (); |
|
196 if (i < 0 || i >= r) |
458
|
197 { |
|
198 (*current_liboctave_error_handler) ("invalid row selection"); |
|
199 return RowVector (); |
|
200 } |
|
201 |
3504
|
202 RowVector retval (c, 0.0); |
|
203 if (r <= c || (r > c && i < c)) |
458
|
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 { |
3504
|
233 int r = rows (); |
|
234 int c = cols (); |
|
235 if (i < 0 || i >= c) |
458
|
236 { |
|
237 (*current_liboctave_error_handler) ("invalid column selection"); |
|
238 return ColumnVector (); |
|
239 } |
|
240 |
3504
|
241 ColumnVector retval (r, 0.0); |
|
242 if (r >= c || (r < c && i < r)) |
458
|
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 { |
3504
|
279 int r = rows (); |
|
280 int c = cols (); |
458
|
281 int len = length (); |
3504
|
282 if (r != c) |
458
|
283 { |
|
284 (*current_liboctave_error_handler) ("inverse requires square matrix"); |
|
285 return DiagMatrix (); |
|
286 } |
|
287 |
3504
|
288 DiagMatrix retval (r, c); |
1627
|
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 // diagonal matrix by diagonal matrix -> diagonal matrix operations |
|
308 |
|
309 DiagMatrix |
|
310 operator * (const DiagMatrix& a, const DiagMatrix& b) |
|
311 { |
3504
|
312 int a_nr = a.rows (); |
|
313 int a_nc = a.cols (); |
2385
|
314 |
3504
|
315 int b_nr = b.rows (); |
|
316 int b_nc = b.cols (); |
2385
|
317 |
3504
|
318 if (a_nc != b_nr) |
458
|
319 { |
3504
|
320 gripe_nonconformant ("operaotr *", a_nr, a_nc, b_nr, b_nc); |
458
|
321 return DiagMatrix (); |
|
322 } |
|
323 |
3504
|
324 if (a_nr == 0 || a_nc == 0 || b_nc == 0) |
|
325 return DiagMatrix (a_nr, a_nc, 0.0); |
458
|
326 |
3504
|
327 DiagMatrix c (a_nr, b_nc); |
458
|
328 |
3504
|
329 int len = a_nr < b_nc ? a_nr : b_nc; |
458
|
330 |
|
331 for (int i = 0; i < len; i++) |
|
332 { |
|
333 double a_element = a.elem (i, i); |
|
334 double b_element = b.elem (i, i); |
|
335 |
|
336 if (a_element == 0.0 || b_element == 0.0) |
|
337 c.elem (i, i) = 0.0; |
|
338 else if (a_element == 1.0) |
|
339 c.elem (i, i) = b_element; |
|
340 else if (b_element == 1.0) |
|
341 c.elem (i, i) = a_element; |
|
342 else |
|
343 c.elem (i, i) = a_element * b_element; |
|
344 } |
|
345 |
|
346 return c; |
|
347 } |
|
348 |
|
349 // other operations |
|
350 |
|
351 ColumnVector |
|
352 DiagMatrix::diag (void) const |
|
353 { |
|
354 return diag (0); |
|
355 } |
|
356 |
|
357 // Could be optimized... |
|
358 |
|
359 ColumnVector |
|
360 DiagMatrix::diag (int k) const |
|
361 { |
|
362 int nnr = rows (); |
|
363 int nnc = cols (); |
|
364 if (k > 0) |
|
365 nnc -= k; |
|
366 else if (k < 0) |
|
367 nnr += k; |
|
368 |
|
369 ColumnVector d; |
|
370 |
|
371 if (nnr > 0 && nnc > 0) |
|
372 { |
|
373 int ndiag = (nnr < nnc) ? nnr : nnc; |
|
374 |
|
375 d.resize (ndiag); |
|
376 |
|
377 if (k > 0) |
|
378 { |
|
379 for (int i = 0; i < ndiag; i++) |
|
380 d.elem (i) = elem (i, i+k); |
|
381 } |
|
382 else if ( k < 0) |
|
383 { |
|
384 for (int i = 0; i < ndiag; i++) |
|
385 d.elem (i) = elem (i-k, i); |
|
386 } |
|
387 else |
|
388 { |
|
389 for (int i = 0; i < ndiag; i++) |
|
390 d.elem (i) = elem (i, i); |
|
391 } |
|
392 } |
|
393 else |
3504
|
394 std::cerr << "diag: requested diagonal out of range\n"; |
458
|
395 |
|
396 return d; |
|
397 } |
|
398 |
3504
|
399 std::ostream& |
|
400 operator << (std::ostream& os, const DiagMatrix& a) |
458
|
401 { |
|
402 // int field_width = os.precision () + 7; |
1360
|
403 |
458
|
404 for (int i = 0; i < a.rows (); i++) |
|
405 { |
|
406 for (int j = 0; j < a.cols (); j++) |
|
407 { |
|
408 if (i == j) |
|
409 os << " " /* setw (field_width) */ << a.elem (i, i); |
|
410 else |
|
411 os << " " /* setw (field_width) */ << 0.0; |
|
412 } |
|
413 os << "\n"; |
|
414 } |
|
415 return os; |
|
416 } |
|
417 |
|
418 /* |
|
419 ;;; Local Variables: *** |
|
420 ;;; mode: C++ *** |
|
421 ;;; End: *** |
|
422 */ |