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