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