2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
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 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
2376
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
4100
|
31 #include <climits> |
|
32 |
3503
|
33 #include <iostream> |
2901
|
34 |
2376
|
35 #include "lo-ieee.h" |
|
36 #include "lo-utils.h" |
|
37 #include "mx-base.h" |
4153
|
38 #include "quit.h" |
2376
|
39 |
|
40 #include "gripes.h" |
|
41 #include "oct-obj.h" |
2979
|
42 #include "oct-lvalue.h" |
2410
|
43 #include "ops.h" |
3219
|
44 #include "ov-base.h" |
|
45 #include "ov-base-mat.h" |
|
46 #include "ov-base-mat.cc" |
2410
|
47 #include "ov-scalar.h" |
2376
|
48 #include "ov-re-mat.h" |
|
49 #include "pr-output.h" |
2948
|
50 #include "variables.h" |
2376
|
51 |
4100
|
52 #if ! defined (UCHAR_MAX) |
|
53 #define UCHAR_MAX 255 |
|
54 #endif |
|
55 |
4513
|
56 template class octave_base_matrix<NDArray>; |
2376
|
57 |
3219
|
58 DEFINE_OCTAVE_ALLOCATOR (octave_matrix); |
2477
|
59 |
4612
|
60 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_matrix, "matrix", "double"); |
2376
|
61 |
2410
|
62 octave_value * |
|
63 octave_matrix::try_narrowing_conversion (void) |
|
64 { |
|
65 octave_value *retval = 0; |
|
66 |
4513
|
67 if (matrix.nelem () == 1) |
|
68 retval = new octave_scalar (matrix (0)); |
2410
|
69 |
|
70 return retval; |
|
71 } |
|
72 |
2376
|
73 bool |
|
74 octave_matrix::valid_as_scalar_index (void) const |
|
75 { |
|
76 // XXX FIXME XXX |
|
77 return false; |
|
78 } |
|
79 |
|
80 double |
|
81 octave_matrix::double_value (bool) const |
|
82 { |
4102
|
83 double retval = lo_ieee_nan_value (); |
2376
|
84 |
4645
|
85 if (numel () > 0) |
4455
|
86 { |
|
87 // XXX FIXME XXX -- is warn_fortran_indexing the right variable here? |
|
88 if (Vwarn_fortran_indexing) |
|
89 gripe_implicit_conversion ("real matrix", "real scalar"); |
|
90 |
|
91 retval = matrix (0, 0); |
|
92 } |
2376
|
93 else |
|
94 gripe_invalid_conversion ("real matrix", "real scalar"); |
|
95 |
|
96 return retval; |
|
97 } |
|
98 |
4513
|
99 // XXX FIXME XXX |
|
100 |
|
101 Matrix |
|
102 octave_matrix::matrix_value (bool) const |
|
103 { |
|
104 return matrix.matrix_value (); |
|
105 } |
|
106 |
2376
|
107 Complex |
|
108 octave_matrix::complex_value (bool) const |
|
109 { |
4102
|
110 double tmp = lo_ieee_nan_value (); |
|
111 |
|
112 Complex retval (tmp, tmp); |
2376
|
113 |
4455
|
114 // XXX FIXME XXX -- maybe this should be a function, valid_as_scalar() |
|
115 if (rows () > 0 && columns () > 0) |
|
116 { |
|
117 // XXX FIXME XXX -- is warn_fortran_indexing the right variable here? |
|
118 if (Vwarn_fortran_indexing) |
|
119 gripe_implicit_conversion ("real matrix", "complex scalar"); |
|
120 |
|
121 retval = matrix (0, 0); |
|
122 } |
2376
|
123 else |
|
124 gripe_invalid_conversion ("real matrix", "complex scalar"); |
|
125 |
|
126 return retval; |
|
127 } |
|
128 |
4513
|
129 // XXX FIXME XXX |
|
130 |
|
131 ComplexMatrix |
|
132 octave_matrix::complex_matrix_value (bool) const |
|
133 { |
|
134 return ComplexMatrix (matrix.matrix_value ()); |
|
135 } |
|
136 |
4645
|
137 streamoff_array |
|
138 octave_matrix::streamoff_array_value (void) const |
|
139 { |
|
140 streamoff_array retval (dims ()); |
|
141 |
|
142 int nel = numel (); |
|
143 |
|
144 for (int i = 0; i < nel; i++) |
|
145 { |
|
146 double d = matrix(i); |
|
147 |
|
148 if (D_NINT (d) == d) |
|
149 retval(i) = std::streamoff (static_cast<long> (d)); |
|
150 else |
|
151 { |
|
152 error ("conversion to streamoff_array value failed"); |
|
153 break; |
|
154 } |
|
155 } |
|
156 |
|
157 return retval; |
|
158 } |
|
159 |
2376
|
160 octave_value |
4457
|
161 octave_matrix::convert_to_str_internal (bool, bool) const |
2376
|
162 { |
|
163 octave_value retval; |
|
164 |
|
165 int nr = matrix.rows (); |
|
166 int nc = matrix.columns (); |
|
167 |
|
168 if (nr == 0 && nc == 0) |
|
169 { |
|
170 char s = '\0'; |
|
171 retval = octave_value (&s); |
|
172 } |
|
173 else |
|
174 { |
|
175 if (nr == 0 || nc == 0) |
|
176 { |
|
177 char s = '\0'; |
|
178 retval = octave_value (&s); |
|
179 } |
|
180 else |
|
181 { |
|
182 charMatrix chm (nr, nc); |
4100
|
183 |
|
184 bool warned = false; |
2376
|
185 |
|
186 for (int j = 0; j < nc; j++) |
|
187 { |
|
188 for (int i = 0; i < nr; i++) |
|
189 { |
4153
|
190 OCTAVE_QUIT; |
|
191 |
2376
|
192 double d = matrix (i, j); |
|
193 |
|
194 if (xisnan (d)) |
|
195 { |
|
196 ::error ("invalid conversion from NaN to character"); |
|
197 return retval; |
|
198 } |
|
199 else |
|
200 { |
4100
|
201 int ival = NINT (d); |
|
202 |
|
203 if (ival < 0 || ival > UCHAR_MAX) |
|
204 { |
|
205 // XXX FIXME XXX -- is there something |
|
206 // better we could do? |
|
207 |
|
208 ival = 0; |
2376
|
209 |
4100
|
210 if (! warned) |
|
211 { |
|
212 ::warning ("range error for conversion to character value"); |
|
213 warned = true; |
|
214 } |
|
215 } |
|
216 |
|
217 chm (i, j) = static_cast<char> (ival); |
2376
|
218 } |
|
219 } |
|
220 } |
|
221 |
|
222 retval = octave_value (chm, 1); |
|
223 } |
|
224 } |
|
225 |
|
226 return retval; |
|
227 } |
|
228 |
4643
|
229 void |
|
230 octave_matrix::print_raw (std::ostream& os, |
|
231 bool pr_as_read_syntax) const |
|
232 { |
|
233 octave_print_internal (os, matrix, pr_as_read_syntax, |
|
234 current_print_indent_level ()); |
|
235 } |
|
236 |
2376
|
237 /* |
|
238 ;;; Local Variables: *** |
|
239 ;;; mode: C++ *** |
|
240 ;;; End: *** |
|
241 */ |