2871
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 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 |
2901
|
31 #include <iostream.h> |
|
32 |
2871
|
33 #include "lo-ieee.h" |
|
34 #include "mx-base.h" |
|
35 |
|
36 #include "gripes.h" |
|
37 #include "oct-obj.h" |
|
38 #include "ops.h" |
|
39 #include "ov-bool.h" |
|
40 #include "ov-bool-mat.h" |
|
41 #include "ov-re-mat.h" |
|
42 #include "pr-output.h" |
|
43 |
|
44 octave_allocator |
|
45 octave_bool_matrix::allocator (sizeof (octave_bool_matrix)); |
|
46 |
|
47 int |
|
48 octave_bool_matrix::t_id (-1); |
|
49 |
|
50 const string |
|
51 octave_bool_matrix::t_name ("bool matrix"); |
|
52 |
|
53 static octave_value * |
|
54 default_numeric_conversion_function (const octave_value& a) |
|
55 { |
|
56 CAST_CONV_ARG (const octave_bool_matrix&); |
|
57 |
|
58 return new octave_matrix (Matrix (v.bool_matrix_value ())); |
|
59 } |
|
60 |
|
61 type_conv_fcn |
|
62 octave_bool_matrix::numeric_conversion_function (void) const |
|
63 { |
|
64 return default_numeric_conversion_function; |
|
65 } |
|
66 |
|
67 octave_value * |
|
68 octave_bool_matrix::try_narrowing_conversion (void) |
|
69 { |
|
70 octave_value *retval = 0; |
|
71 |
|
72 int nr = matrix.rows (); |
|
73 int nc = matrix.cols (); |
|
74 |
|
75 if (nr == 1 && nc == 1) |
|
76 retval = new octave_bool (matrix (0, 0)); |
|
77 |
|
78 return retval; |
|
79 } |
|
80 |
|
81 octave_value |
|
82 octave_bool_matrix::index (const octave_value_list& idx) const |
|
83 { |
|
84 octave_value retval; |
|
85 |
|
86 int len = idx.length (); |
|
87 |
|
88 switch (len) |
|
89 { |
|
90 case 2: |
|
91 { |
|
92 idx_vector i = idx (0).index_vector (); |
|
93 idx_vector j = idx (1).index_vector (); |
|
94 |
|
95 retval = boolMatrix (matrix.index (i, j)); |
|
96 } |
|
97 break; |
|
98 |
|
99 case 1: |
|
100 { |
|
101 idx_vector i = idx (0).index_vector (); |
|
102 |
|
103 retval = boolMatrix (matrix.index (i)); |
|
104 } |
|
105 break; |
|
106 |
|
107 default: |
|
108 error ("invalid number of indices (%d) for matrix value", len); |
|
109 break; |
|
110 } |
|
111 |
|
112 return retval; |
|
113 } |
|
114 |
|
115 extern void assign (Array2<bool>&, const Array2<bool>&); |
|
116 |
|
117 void |
|
118 octave_bool_matrix::assign (const octave_value_list& idx, |
|
119 const boolMatrix& rhs) |
|
120 { |
|
121 int len = idx.length (); |
|
122 |
|
123 switch (len) |
|
124 { |
|
125 case 2: |
|
126 { |
|
127 idx_vector i = idx (0).index_vector (); |
|
128 idx_vector j = idx (1).index_vector (); |
|
129 |
|
130 matrix.set_index (i); |
|
131 matrix.set_index (j); |
|
132 |
|
133 ::assign (matrix, rhs); |
|
134 } |
|
135 break; |
|
136 |
|
137 case 1: |
|
138 { |
|
139 idx_vector i = idx (0).index_vector (); |
|
140 |
|
141 matrix.set_index (i); |
|
142 |
|
143 ::assign (matrix, rhs); |
|
144 } |
|
145 break; |
|
146 |
|
147 default: |
|
148 error ("invalid number of indices (%d) for indexed matrix assignment", |
|
149 len); |
|
150 break; |
|
151 } |
|
152 } |
|
153 |
|
154 bool |
|
155 octave_bool_matrix::valid_as_scalar_index (void) const |
|
156 { |
|
157 // XXX FIXME XXX |
|
158 return false; |
|
159 } |
|
160 |
|
161 bool |
|
162 octave_bool_matrix::is_true (void) const |
|
163 { |
|
164 bool retval = false; |
|
165 |
|
166 if (rows () == 0 || columns () == 0) |
|
167 { |
|
168 int flag = Vpropagate_empty_matrices; |
|
169 |
|
170 if (flag < 0) |
|
171 warning ("empty matrix used in conditional expression"); |
|
172 else if (flag == 0) |
|
173 error ("empty matrix used in conditional expression"); |
|
174 } |
|
175 else |
|
176 { |
|
177 boolMatrix m = (matrix.all ()) . all (); |
|
178 |
|
179 retval = (m.rows () == 1 && m.columns () == 1 && m (0, 0)); |
|
180 } |
|
181 |
|
182 return retval; |
|
183 } |
|
184 |
|
185 double |
|
186 octave_bool_matrix::double_value (bool) const |
|
187 { |
|
188 double retval = octave_NaN; |
|
189 |
|
190 if ((rows () == 1 && columns () == 1) |
|
191 || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) |
|
192 retval = matrix (0, 0); |
|
193 else |
|
194 gripe_invalid_conversion ("bool matrix", "real scalar"); |
|
195 |
|
196 return retval; |
|
197 } |
|
198 |
|
199 Complex |
|
200 octave_bool_matrix::complex_value (bool) const |
|
201 { |
|
202 Complex retval (octave_NaN, octave_NaN); |
|
203 |
|
204 if ((rows () == 1 && columns () == 1) |
|
205 || (Vdo_fortran_indexing && rows () > 0 && columns () > 0)) |
|
206 retval = matrix (0, 0); |
|
207 else |
|
208 gripe_invalid_conversion ("bool matrix", "complex scalar"); |
|
209 |
|
210 return retval; |
|
211 } |
|
212 |
|
213 void |
2901
|
214 octave_bool_matrix::print (ostream& os, bool pr_as_read_syntax) const |
|
215 { |
|
216 indent (os); |
|
217 print_raw (os, pr_as_read_syntax); |
|
218 newline (os); |
|
219 } |
|
220 |
|
221 void |
|
222 octave_bool_matrix::print_raw (ostream& os, bool pr_as_read_syntax) const |
2871
|
223 { |
|
224 Matrix tmp (matrix); |
2901
|
225 octave_print_internal (os, tmp, pr_as_read_syntax, |
|
226 current_print_indent_level ()); |
|
227 } |
2871
|
228 |
2901
|
229 bool |
|
230 octave_bool_matrix::print_name_tag (ostream& os, const string& name) const |
|
231 { |
|
232 bool retval = false; |
|
233 |
|
234 int nr = rows (); |
|
235 int nc = columns (); |
|
236 |
|
237 indent (os); |
|
238 |
|
239 if (nr == 1 && nc == 1 || (nr == 0 || nc == 0)) |
|
240 os << name << " = "; |
|
241 else |
|
242 { |
|
243 os << name << " ="; |
|
244 newline (os); |
|
245 newline (os); |
|
246 retval = true; |
|
247 } |
|
248 |
|
249 return retval; |
2871
|
250 } |
|
251 |
|
252 /* |
|
253 ;;; Local Variables: *** |
|
254 ;;; mode: C++ *** |
|
255 ;;; End: *** |
|
256 */ |