1993
|
1 // Matrix manipulations. |
1573
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
1573
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
3503
|
32 #include <iostream> |
1728
|
33 #include <string> |
|
34 |
1573
|
35 #include "lo-error.h" |
2349
|
36 #include "str-vec.h" |
1573
|
37 #include "mx-base.h" |
|
38 #include "mx-inlines.cc" |
|
39 |
|
40 // charMatrix class. |
|
41 |
3189
|
42 charMatrix::charMatrix (char c) |
|
43 : MArray2<char> () |
|
44 { |
|
45 int nc = 1; |
|
46 int nr = 1; |
|
47 |
|
48 resize (nr, nc); |
|
49 |
|
50 elem (0, 0) = c; |
|
51 } |
|
52 |
1573
|
53 charMatrix::charMatrix (const char *s) |
2613
|
54 : MArray2<char> () |
1573
|
55 { |
2613
|
56 int nc = s ? strlen (s) : 0; |
|
57 int nr = s && nc > 0 ? 1 : 0; |
|
58 |
|
59 resize (nr, nc); |
|
60 |
1573
|
61 for (int i = 0; i < nc; i++) |
|
62 elem (0, i) = s[i]; |
|
63 } |
|
64 |
3504
|
65 charMatrix::charMatrix (const std::string& s) |
2613
|
66 : MArray2<char> () |
1733
|
67 { |
2613
|
68 int nc = s.length (); |
|
69 int nr = nc > 0 ? 1 : 0; |
|
70 |
|
71 resize (nr, nc); |
|
72 |
1733
|
73 for (int i = 0; i < nc; i++) |
|
74 elem (0, i) = s[i]; |
|
75 } |
|
76 |
2349
|
77 charMatrix::charMatrix (const string_vector& s) |
2350
|
78 : MArray2<char> (s.length (), s.max_length (), 0) |
2349
|
79 { |
2350
|
80 int nr = rows (); |
2613
|
81 |
2349
|
82 for (int i = 0; i < nr; i++) |
|
83 { |
|
84 int nc = s[i].length (); |
|
85 for (int j = 0; j < nc; j++) |
|
86 elem (i, j) = s[i][j]; |
|
87 } |
|
88 } |
|
89 |
2384
|
90 bool |
1573
|
91 charMatrix::operator == (const charMatrix& a) const |
|
92 { |
|
93 if (rows () != a.rows () || cols () != a.cols ()) |
|
94 return 0; |
|
95 |
3769
|
96 return mx_inline_equal (data (), a.data (), length ()); |
1573
|
97 } |
|
98 |
2384
|
99 bool |
1573
|
100 charMatrix::operator != (const charMatrix& a) const |
|
101 { |
|
102 return !(*this == a); |
|
103 } |
|
104 |
|
105 charMatrix& |
|
106 charMatrix::insert (const char *s, int r, int c) |
|
107 { |
|
108 if (s) |
|
109 { |
|
110 int s_len = strlen (s); |
|
111 |
|
112 if (r < 0 || r >= rows () || c < 0 || c + s_len - 1 > cols ()) |
|
113 { |
|
114 (*current_liboctave_error_handler) ("range error for insert"); |
|
115 return *this; |
|
116 } |
|
117 |
|
118 for (int i = 0; i < s_len; i++) |
|
119 elem (r, c+i) = s[i]; |
|
120 } |
|
121 return *this; |
|
122 } |
|
123 |
|
124 charMatrix& |
|
125 charMatrix::insert (const charMatrix& a, int r, int c) |
|
126 { |
|
127 Array2<char>::insert (a, r, c); |
|
128 return *this; |
|
129 } |
|
130 |
3504
|
131 std::string |
|
132 charMatrix::row_as_string (int r, bool strip_ws) const |
1573
|
133 { |
3504
|
134 std::string retval; |
2613
|
135 |
|
136 int nr = rows (); |
|
137 int nc = cols (); |
|
138 |
|
139 if (r == 0 && nr == 0 && nc == 0) |
|
140 return retval; |
|
141 |
|
142 if (r < 0 || r >= nr) |
1573
|
143 { |
|
144 (*current_liboctave_error_handler) ("range error for row_as_string"); |
2613
|
145 return retval; |
1573
|
146 } |
|
147 |
2613
|
148 retval.resize (nc, '\0'); |
1573
|
149 |
|
150 for (int i = 0; i < nc; i++) |
|
151 retval[i] = elem (r, i); |
|
152 |
2493
|
153 if (strip_ws) |
|
154 { |
|
155 while (--nc >= 0) |
|
156 { |
|
157 char c = retval[nc]; |
|
158 if (c && c != ' ') |
|
159 break; |
|
160 } |
|
161 } |
|
162 else |
|
163 { |
|
164 while (--nc >= 0) |
|
165 if (retval[nc]) |
|
166 break; |
|
167 } |
1780
|
168 |
|
169 retval.resize (nc+1); |
|
170 |
1573
|
171 return retval; |
|
172 } |
|
173 |
2255
|
174 charMatrix |
3189
|
175 charMatrix::extract (int r1, int c1, int r2, int c2) const |
|
176 { |
|
177 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
178 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
179 |
|
180 int new_r = r2 - r1 + 1; |
|
181 int new_c = c2 - c1 + 1; |
|
182 |
|
183 charMatrix result (new_r, new_c); |
|
184 |
|
185 for (int j = 0; j < new_c; j++) |
|
186 for (int i = 0; i < new_r; i++) |
|
187 result.elem (i, j) = elem (r1+i, c1+j); |
|
188 |
|
189 return result; |
|
190 } |
|
191 |
3136
|
192 // XXX FIXME XXX -- these should probably return a boolMatrix type |
|
193 // instead, but that will have to wait for a future version... |
|
194 |
|
195 Matrix |
|
196 charMatrix::all (void) const |
|
197 { |
|
198 int nr = rows (); |
|
199 int nc = cols (); |
|
200 Matrix retval; |
|
201 if (nr > 0 && nc > 0) |
|
202 { |
|
203 if (nr == 1) |
|
204 { |
|
205 retval.resize (1, 1); |
|
206 retval.elem (0, 0) = 1.0; |
|
207 for (int j = 0; j < nc; j++) |
|
208 { |
|
209 if (elem (0, j) == 0) |
|
210 { |
|
211 retval.elem (0, 0) = 0.0; |
|
212 break; |
|
213 } |
|
214 } |
|
215 } |
|
216 else if (nc == 1) |
|
217 { |
|
218 retval.resize (1, 1); |
|
219 retval.elem (0, 0) = 1.0; |
|
220 for (int i = 0; i < nr; i++) |
|
221 { |
|
222 if (elem (i, 0) == 0) |
|
223 { |
|
224 retval.elem (0, 0) = 0.0; |
|
225 break; |
|
226 } |
|
227 } |
|
228 } |
|
229 else |
|
230 { |
|
231 retval.resize (1, nc); |
|
232 for (int j = 0; j < nc; j++) |
|
233 { |
|
234 retval.elem (0, j) = 1.0; |
|
235 for (int i = 0; i < nr; i++) |
|
236 { |
|
237 if (elem (i, j) == 0) |
|
238 { |
|
239 retval.elem (0, j) = 0.0; |
|
240 break; |
|
241 } |
|
242 } |
|
243 } |
|
244 } |
|
245 } |
|
246 return retval; |
|
247 } |
|
248 |
|
249 Matrix |
|
250 charMatrix::any (void) const |
|
251 { |
|
252 int nr = rows (); |
|
253 int nc = cols (); |
|
254 Matrix retval; |
|
255 if (nr > 0 && nc > 0) |
|
256 { |
|
257 if (nr == 1) |
|
258 { |
|
259 retval.resize (1, 1); |
|
260 retval.elem (0, 0) = 0.0; |
|
261 for (int j = 0; j < nc; j++) |
|
262 { |
|
263 if (elem (0, j) != 0) |
|
264 { |
|
265 retval.elem (0, 0) = 1.0; |
|
266 break; |
|
267 } |
|
268 } |
|
269 } |
|
270 else if (nc == 1) |
|
271 { |
|
272 retval.resize (1, 1); |
|
273 retval.elem (0, 0) = 0.0; |
|
274 for (int i = 0; i < nr; i++) |
|
275 { |
|
276 if (elem (i, 0) != 0) |
|
277 { |
|
278 retval.elem (0, 0) = 1.0; |
|
279 break; |
|
280 } |
|
281 } |
|
282 } |
|
283 else |
|
284 { |
|
285 retval.resize (1, nc); |
|
286 for (int j = 0; j < nc; j++) |
|
287 { |
|
288 retval.elem (0, j) = 0.0; |
|
289 for (int i = 0; i < nr; i++) |
|
290 { |
|
291 if (elem (i, j) != 0) |
|
292 { |
|
293 retval.elem (0, j) = 1.0; |
|
294 break; |
|
295 } |
|
296 } |
|
297 } |
|
298 } |
|
299 } |
|
300 return retval; |
|
301 } |
|
302 |
1573
|
303 /* |
|
304 ;;; Local Variables: *** |
|
305 ;;; mode: C++ *** |
|
306 ;;; End: *** |
|
307 */ |