2376
|
1 /* |
|
2 |
|
3 Copyright (C) 1996 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
|
31 #include "lo-ieee.h" |
|
32 #include "lo-utils.h" |
|
33 #include "mx-base.h" |
|
34 |
|
35 #include "gripes.h" |
|
36 #include "mappers.h" |
|
37 #include "oct-obj.h" |
2410
|
38 #include "ops.h" |
|
39 #include "ov-scalar.h" |
2376
|
40 #include "ov-re-mat.h" |
|
41 #include "pr-output.h" |
|
42 |
2477
|
43 octave_allocator |
|
44 octave_matrix::allocator (sizeof (octave_matrix)); |
2376
|
45 |
2477
|
46 int |
|
47 octave_matrix::t_id (-1); |
|
48 |
|
49 const string |
|
50 octave_matrix::t_name ("matrix"); |
2376
|
51 |
|
52 octave_matrix::octave_matrix (const RowVector& v, int pcv) |
|
53 : octave_base_value (), |
|
54 matrix ((pcv < 0 && Vprefer_column_vectors) || pcv |
|
55 ? Matrix (v.transpose ()) : Matrix (v)) { } |
|
56 |
|
57 octave_matrix::octave_matrix (const ColumnVector& v, int pcv) |
|
58 : octave_base_value (), |
|
59 matrix ((pcv < 0 && Vprefer_column_vectors) || pcv |
|
60 ? Matrix (v) : Matrix (v.transpose ())) { } |
|
61 |
2410
|
62 octave_value * |
|
63 octave_matrix::try_narrowing_conversion (void) |
|
64 { |
|
65 octave_value *retval = 0; |
|
66 |
|
67 int nr = matrix.rows (); |
|
68 int nc = matrix.cols (); |
|
69 |
|
70 if (nr == 1 && nc == 1) |
|
71 retval = new octave_scalar (matrix (0, 0)); |
|
72 |
|
73 return retval; |
|
74 } |
|
75 |
2376
|
76 octave_value |
|
77 octave_matrix::index (const octave_value_list& idx) const |
|
78 { |
|
79 octave_value retval; |
|
80 |
|
81 int len = idx.length (); |
|
82 |
|
83 switch (len) |
|
84 { |
|
85 case 2: |
|
86 { |
|
87 idx_vector i = idx (0).index_vector (); |
|
88 idx_vector j = idx (1).index_vector (); |
2407
|
89 |
2376
|
90 retval = Matrix (matrix.index (i, j)); |
|
91 } |
|
92 break; |
|
93 |
|
94 case 1: |
|
95 { |
|
96 idx_vector i = idx (0).index_vector (); |
2407
|
97 |
2376
|
98 retval = Matrix (matrix.index (i)); |
|
99 } |
|
100 break; |
|
101 |
|
102 default: |
|
103 error ("invalid number of indices (%d) for matrix value", len); |
|
104 break; |
|
105 } |
|
106 |
|
107 return retval; |
|
108 } |
|
109 |
|
110 extern void assign (Array2<double>&, const Array2<double>&); |
|
111 |
|
112 void |
|
113 octave_matrix::assign (const octave_value_list& idx, const Matrix& rhs) |
|
114 { |
|
115 int len = idx.length (); |
|
116 |
|
117 switch (len) |
|
118 { |
|
119 case 2: |
|
120 { |
|
121 idx_vector i = idx (0).index_vector (); |
|
122 idx_vector j = idx (1).index_vector (); |
|
123 |
|
124 matrix.set_index (i); |
|
125 matrix.set_index (j); |
|
126 |
|
127 ::assign (matrix, rhs); |
|
128 } |
|
129 break; |
|
130 |
|
131 case 1: |
|
132 { |
|
133 idx_vector i = idx (0).index_vector (); |
|
134 |
|
135 matrix.set_index (i); |
|
136 |
|
137 ::assign (matrix, rhs); |
|
138 } |
|
139 break; |
|
140 |
|
141 default: |
|
142 error ("invalid number of indices (%d) for indexed matrix assignment", |
|
143 len); |
|
144 break; |
|
145 } |
|
146 } |
|
147 |
|
148 bool |
|
149 octave_matrix::valid_as_scalar_index (void) const |
|
150 { |
|
151 // XXX FIXME XXX |
|
152 return false; |
|
153 } |
|
154 |
|
155 bool |
|
156 octave_matrix::is_true (void) const |
|
157 { |
|
158 bool retval = false; |
|
159 |
|
160 if (rows () == 0 || columns () == 0) |
|
161 { |
|
162 int flag = Vpropagate_empty_matrices; |
|
163 |
|
164 if (flag < 0) |
|
165 warning ("empty matrix used in conditional expression"); |
|
166 else if (flag == 0) |
|
167 error ("empty matrix used in conditional expression"); |
|
168 } |
|
169 else |
|
170 { |
|
171 Matrix m = (matrix.all ()) . all (); |
|
172 |
|
173 retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0) != 0.0); |
|
174 } |
|
175 |
|
176 return retval; |
|
177 } |
|
178 |
|
179 double |
|
180 octave_matrix::double_value (bool) const |
|
181 { |
|
182 double retval = octave_NaN; |
|
183 |
|
184 // XXX FIXME XXX -- maybe this should be a function, valid_as_scalar() |
|
185 if ((rows () == 1 && columns () == 1) |
|
186 || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) |
|
187 retval = matrix (0, 0); |
|
188 else |
|
189 gripe_invalid_conversion ("real matrix", "real scalar"); |
|
190 |
|
191 return retval; |
|
192 } |
|
193 |
|
194 Complex |
|
195 octave_matrix::complex_value (bool) const |
|
196 { |
|
197 Complex retval (octave_NaN, octave_NaN); |
|
198 |
|
199 if ((rows () == 1 && columns () == 1) |
|
200 || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) |
|
201 retval = matrix (0, 0); |
|
202 else |
|
203 gripe_invalid_conversion ("real matrix", "complex scalar"); |
|
204 |
|
205 return retval; |
|
206 } |
|
207 |
|
208 octave_value |
|
209 octave_matrix::convert_to_str (void) const |
|
210 { |
|
211 octave_value retval; |
|
212 |
|
213 int nr = matrix.rows (); |
|
214 int nc = matrix.columns (); |
|
215 |
|
216 if (nr == 0 && nc == 0) |
|
217 { |
|
218 char s = '\0'; |
|
219 retval = octave_value (&s); |
|
220 } |
|
221 else |
|
222 { |
|
223 if (nr == 0 || nc == 0) |
|
224 { |
|
225 char s = '\0'; |
|
226 retval = octave_value (&s); |
|
227 } |
|
228 else |
|
229 { |
|
230 charMatrix chm (nr, nc); |
|
231 |
|
232 for (int j = 0; j < nc; j++) |
|
233 { |
|
234 for (int i = 0; i < nr; i++) |
|
235 { |
|
236 double d = matrix (i, j); |
|
237 |
|
238 if (xisnan (d)) |
|
239 { |
|
240 ::error ("invalid conversion from NaN to character"); |
|
241 return retval; |
|
242 } |
|
243 else |
|
244 { |
|
245 // XXX FIXME XXX -- warn about out of range |
|
246 // conversions? |
|
247 |
|
248 int ival = NINT (d); |
|
249 chm (i, j) = (char) ival; |
|
250 } |
|
251 } |
|
252 } |
|
253 |
|
254 retval = octave_value (chm, 1); |
|
255 } |
|
256 } |
|
257 |
|
258 return retval; |
|
259 } |
|
260 |
|
261 void |
2466
|
262 octave_matrix::print (ostream& os, bool pr_as_read_syntax) |
2376
|
263 { |
2466
|
264 octave_print_internal (os, matrix, pr_as_read_syntax, struct_indent); |
2376
|
265 } |
|
266 |
|
267 /* |
|
268 ;;; Local Variables: *** |
|
269 ;;; mode: C++ *** |
|
270 ;;; End: *** |
|
271 */ |