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 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
3503
|
31 #include <iostream> |
2901
|
32 |
2376
|
33 #include "lo-ieee.h" |
|
34 #include "lo-utils.h" |
|
35 #include "mx-base.h" |
|
36 |
|
37 #include "gripes.h" |
|
38 #include "oct-obj.h" |
2979
|
39 #include "oct-lvalue.h" |
2410
|
40 #include "ops.h" |
3219
|
41 #include "ov-base.h" |
|
42 #include "ov-base-mat.h" |
|
43 #include "ov-base-mat.cc" |
2410
|
44 #include "ov-scalar.h" |
2376
|
45 #include "ov-re-mat.h" |
|
46 #include "pr-output.h" |
2948
|
47 #include "variables.h" |
2376
|
48 |
3219
|
49 template class octave_base_matrix<Matrix>; |
2376
|
50 |
3219
|
51 DEFINE_OCTAVE_ALLOCATOR (octave_matrix); |
2477
|
52 |
3219
|
53 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_matrix, "matrix"); |
2376
|
54 |
2410
|
55 octave_value * |
|
56 octave_matrix::try_narrowing_conversion (void) |
|
57 { |
|
58 octave_value *retval = 0; |
|
59 |
|
60 int nr = matrix.rows (); |
|
61 int nc = matrix.cols (); |
|
62 |
|
63 if (nr == 1 && nc == 1) |
|
64 retval = new octave_scalar (matrix (0, 0)); |
|
65 |
|
66 return retval; |
|
67 } |
|
68 |
3109
|
69 #if !defined (CXX_NEW_FRIEND_TEMPLATE_DECL) |
2376
|
70 extern void assign (Array2<double>&, const Array2<double>&); |
3107
|
71 #endif |
2376
|
72 |
|
73 void |
|
74 octave_matrix::assign (const octave_value_list& idx, const Matrix& rhs) |
|
75 { |
|
76 int len = idx.length (); |
|
77 |
|
78 switch (len) |
|
79 { |
|
80 case 2: |
|
81 { |
|
82 idx_vector i = idx (0).index_vector (); |
|
83 idx_vector j = idx (1).index_vector (); |
|
84 |
|
85 matrix.set_index (i); |
|
86 matrix.set_index (j); |
|
87 |
|
88 ::assign (matrix, rhs); |
|
89 } |
|
90 break; |
|
91 |
|
92 case 1: |
|
93 { |
|
94 idx_vector i = idx (0).index_vector (); |
|
95 |
|
96 matrix.set_index (i); |
|
97 |
|
98 ::assign (matrix, rhs); |
|
99 } |
|
100 break; |
|
101 |
|
102 default: |
|
103 error ("invalid number of indices (%d) for indexed matrix assignment", |
|
104 len); |
|
105 break; |
|
106 } |
|
107 } |
|
108 |
|
109 bool |
|
110 octave_matrix::valid_as_scalar_index (void) const |
|
111 { |
|
112 // XXX FIXME XXX |
|
113 return false; |
|
114 } |
|
115 |
|
116 double |
|
117 octave_matrix::double_value (bool) const |
|
118 { |
|
119 double retval = octave_NaN; |
|
120 |
|
121 // XXX FIXME XXX -- maybe this should be a function, valid_as_scalar() |
|
122 if ((rows () == 1 && columns () == 1) |
|
123 || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) |
|
124 retval = matrix (0, 0); |
|
125 else |
|
126 gripe_invalid_conversion ("real matrix", "real scalar"); |
|
127 |
|
128 return retval; |
|
129 } |
|
130 |
|
131 Complex |
|
132 octave_matrix::complex_value (bool) const |
|
133 { |
|
134 Complex retval (octave_NaN, octave_NaN); |
|
135 |
|
136 if ((rows () == 1 && columns () == 1) |
|
137 || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) |
|
138 retval = matrix (0, 0); |
|
139 else |
|
140 gripe_invalid_conversion ("real matrix", "complex scalar"); |
|
141 |
|
142 return retval; |
|
143 } |
|
144 |
|
145 octave_value |
|
146 octave_matrix::convert_to_str (void) const |
|
147 { |
|
148 octave_value retval; |
|
149 |
|
150 int nr = matrix.rows (); |
|
151 int nc = matrix.columns (); |
|
152 |
|
153 if (nr == 0 && nc == 0) |
|
154 { |
|
155 char s = '\0'; |
|
156 retval = octave_value (&s); |
|
157 } |
|
158 else |
|
159 { |
|
160 if (nr == 0 || nc == 0) |
|
161 { |
|
162 char s = '\0'; |
|
163 retval = octave_value (&s); |
|
164 } |
|
165 else |
|
166 { |
|
167 charMatrix chm (nr, nc); |
|
168 |
|
169 for (int j = 0; j < nc; j++) |
|
170 { |
|
171 for (int i = 0; i < nr; i++) |
|
172 { |
|
173 double d = matrix (i, j); |
|
174 |
|
175 if (xisnan (d)) |
|
176 { |
|
177 ::error ("invalid conversion from NaN to character"); |
|
178 return retval; |
|
179 } |
|
180 else |
|
181 { |
|
182 // XXX FIXME XXX -- warn about out of range |
|
183 // conversions? |
|
184 |
|
185 int ival = NINT (d); |
|
186 chm (i, j) = (char) ival; |
|
187 } |
|
188 } |
|
189 } |
|
190 |
|
191 retval = octave_value (chm, 1); |
|
192 } |
|
193 } |
|
194 |
|
195 return retval; |
|
196 } |
|
197 |
|
198 /* |
|
199 ;;; Local Variables: *** |
|
200 ;;; mode: C++ *** |
|
201 ;;; End: *** |
|
202 */ |