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 |
|
43 int octave_matrix::t_id = -1; |
|
44 |
|
45 const string octave_matrix::t_name ("matrix"); |
|
46 |
|
47 octave_matrix::octave_matrix (const RowVector& v, int pcv) |
|
48 : octave_base_value (), |
|
49 matrix ((pcv < 0 && Vprefer_column_vectors) || pcv |
|
50 ? Matrix (v.transpose ()) : Matrix (v)) { } |
|
51 |
|
52 octave_matrix::octave_matrix (const ColumnVector& v, int pcv) |
|
53 : octave_base_value (), |
|
54 matrix ((pcv < 0 && Vprefer_column_vectors) || pcv |
|
55 ? Matrix (v) : Matrix (v.transpose ())) { } |
|
56 |
|
57 #include <iostream.h> |
|
58 |
2410
|
59 octave_value * |
|
60 octave_matrix::try_narrowing_conversion (void) |
|
61 { |
|
62 octave_value *retval = 0; |
|
63 |
|
64 int nr = matrix.rows (); |
|
65 int nc = matrix.cols (); |
|
66 |
|
67 if (nr == 1 && nc == 1) |
|
68 retval = new octave_scalar (matrix (0, 0)); |
|
69 |
|
70 return retval; |
|
71 } |
|
72 |
2376
|
73 octave_value |
|
74 octave_matrix::index (const octave_value_list& idx) const |
|
75 { |
|
76 octave_value retval; |
|
77 |
|
78 int len = idx.length (); |
|
79 |
|
80 switch (len) |
|
81 { |
|
82 case 2: |
|
83 { |
|
84 idx_vector i = idx (0).index_vector (); |
|
85 idx_vector j = idx (1).index_vector (); |
2407
|
86 |
2376
|
87 retval = Matrix (matrix.index (i, j)); |
|
88 } |
|
89 break; |
|
90 |
|
91 case 1: |
|
92 { |
|
93 idx_vector i = idx (0).index_vector (); |
2407
|
94 |
2376
|
95 retval = Matrix (matrix.index (i)); |
|
96 } |
|
97 break; |
|
98 |
|
99 default: |
|
100 error ("invalid number of indices (%d) for matrix value", len); |
|
101 break; |
|
102 } |
|
103 |
|
104 return retval; |
|
105 } |
|
106 |
|
107 extern void assign (Array2<double>&, const Array2<double>&); |
|
108 |
|
109 void |
|
110 octave_matrix::assign (const octave_value_list& idx, const Matrix& rhs) |
|
111 { |
|
112 int len = idx.length (); |
|
113 |
|
114 switch (len) |
|
115 { |
|
116 case 2: |
|
117 { |
|
118 idx_vector i = idx (0).index_vector (); |
|
119 idx_vector j = idx (1).index_vector (); |
|
120 |
|
121 matrix.set_index (i); |
|
122 matrix.set_index (j); |
|
123 |
|
124 ::assign (matrix, rhs); |
|
125 } |
|
126 break; |
|
127 |
|
128 case 1: |
|
129 { |
|
130 idx_vector i = idx (0).index_vector (); |
|
131 |
|
132 matrix.set_index (i); |
|
133 |
|
134 ::assign (matrix, rhs); |
|
135 } |
|
136 break; |
|
137 |
|
138 default: |
|
139 error ("invalid number of indices (%d) for indexed matrix assignment", |
|
140 len); |
|
141 break; |
|
142 } |
|
143 } |
|
144 |
|
145 bool |
|
146 octave_matrix::valid_as_scalar_index (void) const |
|
147 { |
|
148 // XXX FIXME XXX |
|
149 return false; |
|
150 } |
|
151 |
|
152 bool |
|
153 octave_matrix::is_true (void) const |
|
154 { |
|
155 bool retval = false; |
|
156 |
|
157 if (rows () == 0 || columns () == 0) |
|
158 { |
|
159 int flag = Vpropagate_empty_matrices; |
|
160 |
|
161 if (flag < 0) |
|
162 warning ("empty matrix used in conditional expression"); |
|
163 else if (flag == 0) |
|
164 error ("empty matrix used in conditional expression"); |
|
165 } |
|
166 else |
|
167 { |
|
168 Matrix m = (matrix.all ()) . all (); |
|
169 |
|
170 retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0) != 0.0); |
|
171 } |
|
172 |
|
173 return retval; |
|
174 } |
|
175 |
|
176 double |
|
177 octave_matrix::double_value (bool) const |
|
178 { |
|
179 double retval = octave_NaN; |
|
180 |
|
181 // XXX FIXME XXX -- maybe this should be a function, valid_as_scalar() |
|
182 if ((rows () == 1 && columns () == 1) |
|
183 || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) |
|
184 retval = matrix (0, 0); |
|
185 else |
|
186 gripe_invalid_conversion ("real matrix", "real scalar"); |
|
187 |
|
188 return retval; |
|
189 } |
|
190 |
|
191 Complex |
|
192 octave_matrix::complex_value (bool) const |
|
193 { |
|
194 Complex retval (octave_NaN, octave_NaN); |
|
195 |
|
196 if ((rows () == 1 && columns () == 1) |
|
197 || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) |
|
198 retval = matrix (0, 0); |
|
199 else |
|
200 gripe_invalid_conversion ("real matrix", "complex scalar"); |
|
201 |
|
202 return retval; |
|
203 } |
|
204 |
|
205 octave_value |
|
206 octave_matrix::convert_to_str (void) const |
|
207 { |
|
208 octave_value retval; |
|
209 |
|
210 int nr = matrix.rows (); |
|
211 int nc = matrix.columns (); |
|
212 |
|
213 if (nr == 0 && nc == 0) |
|
214 { |
|
215 char s = '\0'; |
|
216 retval = octave_value (&s); |
|
217 } |
|
218 else |
|
219 { |
|
220 if (nr == 0 || nc == 0) |
|
221 { |
|
222 char s = '\0'; |
|
223 retval = octave_value (&s); |
|
224 } |
|
225 else |
|
226 { |
|
227 charMatrix chm (nr, nc); |
|
228 |
|
229 for (int j = 0; j < nc; j++) |
|
230 { |
|
231 for (int i = 0; i < nr; i++) |
|
232 { |
|
233 double d = matrix (i, j); |
|
234 |
|
235 if (xisnan (d)) |
|
236 { |
|
237 ::error ("invalid conversion from NaN to character"); |
|
238 return retval; |
|
239 } |
|
240 else |
|
241 { |
|
242 // XXX FIXME XXX -- warn about out of range |
|
243 // conversions? |
|
244 |
|
245 int ival = NINT (d); |
|
246 chm (i, j) = (char) ival; |
|
247 } |
|
248 } |
|
249 } |
|
250 |
|
251 retval = octave_value (chm, 1); |
|
252 } |
|
253 } |
|
254 |
|
255 return retval; |
|
256 } |
|
257 |
|
258 void |
2466
|
259 octave_matrix::print (ostream& os, bool pr_as_read_syntax) |
2376
|
260 { |
2466
|
261 octave_print_internal (os, matrix, pr_as_read_syntax, struct_indent); |
2376
|
262 } |
|
263 |
|
264 /* |
|
265 ;;; Local Variables: *** |
|
266 ;;; mode: C++ *** |
|
267 ;;; End: *** |
|
268 */ |