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 |
3219
|
56 template class octave_base_matrix<Matrix>; |
2376
|
57 |
3219
|
58 DEFINE_OCTAVE_ALLOCATOR (octave_matrix); |
2477
|
59 |
3219
|
60 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_matrix, "matrix"); |
2376
|
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 bool |
|
77 octave_matrix::valid_as_scalar_index (void) const |
|
78 { |
|
79 // XXX FIXME XXX |
|
80 return false; |
|
81 } |
|
82 |
|
83 double |
|
84 octave_matrix::double_value (bool) const |
|
85 { |
4102
|
86 double retval = lo_ieee_nan_value (); |
2376
|
87 |
|
88 // XXX FIXME XXX -- maybe this should be a function, valid_as_scalar() |
|
89 if ((rows () == 1 && columns () == 1) |
|
90 || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) |
|
91 retval = matrix (0, 0); |
|
92 else |
|
93 gripe_invalid_conversion ("real matrix", "real scalar"); |
|
94 |
|
95 return retval; |
|
96 } |
|
97 |
|
98 Complex |
|
99 octave_matrix::complex_value (bool) const |
|
100 { |
4102
|
101 double tmp = lo_ieee_nan_value (); |
|
102 |
|
103 Complex retval (tmp, tmp); |
2376
|
104 |
|
105 if ((rows () == 1 && columns () == 1) |
|
106 || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) |
|
107 retval = matrix (0, 0); |
|
108 else |
|
109 gripe_invalid_conversion ("real matrix", "complex scalar"); |
|
110 |
|
111 return retval; |
|
112 } |
|
113 |
|
114 octave_value |
|
115 octave_matrix::convert_to_str (void) const |
|
116 { |
|
117 octave_value retval; |
|
118 |
|
119 int nr = matrix.rows (); |
|
120 int nc = matrix.columns (); |
|
121 |
|
122 if (nr == 0 && nc == 0) |
|
123 { |
|
124 char s = '\0'; |
|
125 retval = octave_value (&s); |
|
126 } |
|
127 else |
|
128 { |
|
129 if (nr == 0 || nc == 0) |
|
130 { |
|
131 char s = '\0'; |
|
132 retval = octave_value (&s); |
|
133 } |
|
134 else |
|
135 { |
|
136 charMatrix chm (nr, nc); |
4100
|
137 |
|
138 bool warned = false; |
2376
|
139 |
|
140 for (int j = 0; j < nc; j++) |
|
141 { |
|
142 for (int i = 0; i < nr; i++) |
|
143 { |
4153
|
144 OCTAVE_QUIT; |
|
145 |
2376
|
146 double d = matrix (i, j); |
|
147 |
|
148 if (xisnan (d)) |
|
149 { |
|
150 ::error ("invalid conversion from NaN to character"); |
|
151 return retval; |
|
152 } |
|
153 else |
|
154 { |
4100
|
155 int ival = NINT (d); |
|
156 |
|
157 if (ival < 0 || ival > UCHAR_MAX) |
|
158 { |
|
159 // XXX FIXME XXX -- is there something |
|
160 // better we could do? |
|
161 |
|
162 ival = 0; |
2376
|
163 |
4100
|
164 if (! warned) |
|
165 { |
|
166 ::warning ("range error for conversion to character value"); |
|
167 warned = true; |
|
168 } |
|
169 } |
|
170 |
|
171 chm (i, j) = static_cast<char> (ival); |
2376
|
172 } |
|
173 } |
|
174 } |
|
175 |
|
176 retval = octave_value (chm, 1); |
|
177 } |
|
178 } |
|
179 |
|
180 return retval; |
|
181 } |
|
182 |
|
183 /* |
|
184 ;;; Local Variables: *** |
|
185 ;;; mode: C++ *** |
|
186 ;;; End: *** |
|
187 */ |