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 |
1728
|
32 #include <string> |
|
33 |
1573
|
34 #include <iostream.h> |
|
35 |
|
36 #include "lo-error.h" |
2349
|
37 #include "str-vec.h" |
1573
|
38 #include "mx-base.h" |
|
39 #include "mx-inlines.cc" |
|
40 |
|
41 // charMatrix class. |
|
42 |
3189
|
43 charMatrix::charMatrix (char c) |
|
44 : MArray2<char> () |
|
45 { |
|
46 int nc = 1; |
|
47 int nr = 1; |
|
48 |
|
49 resize (nr, nc); |
|
50 |
|
51 elem (0, 0) = c; |
|
52 } |
|
53 |
1573
|
54 charMatrix::charMatrix (const char *s) |
2613
|
55 : MArray2<char> () |
1573
|
56 { |
2613
|
57 int nc = s ? strlen (s) : 0; |
|
58 int nr = s && nc > 0 ? 1 : 0; |
|
59 |
|
60 resize (nr, nc); |
|
61 |
1573
|
62 for (int i = 0; i < nc; i++) |
|
63 elem (0, i) = s[i]; |
|
64 } |
|
65 |
1733
|
66 charMatrix::charMatrix (const string& s) |
2613
|
67 : MArray2<char> () |
1733
|
68 { |
2613
|
69 int nc = s.length (); |
|
70 int nr = nc > 0 ? 1 : 0; |
|
71 |
|
72 resize (nr, nc); |
|
73 |
1733
|
74 for (int i = 0; i < nc; i++) |
|
75 elem (0, i) = s[i]; |
|
76 } |
|
77 |
2349
|
78 charMatrix::charMatrix (const string_vector& s) |
2350
|
79 : MArray2<char> (s.length (), s.max_length (), 0) |
2349
|
80 { |
2350
|
81 int nr = rows (); |
2613
|
82 |
2349
|
83 for (int i = 0; i < nr; i++) |
|
84 { |
|
85 int nc = s[i].length (); |
|
86 for (int j = 0; j < nc; j++) |
|
87 elem (i, j) = s[i][j]; |
|
88 } |
|
89 } |
|
90 |
2384
|
91 bool |
1573
|
92 charMatrix::operator == (const charMatrix& a) const |
|
93 { |
|
94 if (rows () != a.rows () || cols () != a.cols ()) |
|
95 return 0; |
|
96 |
|
97 return equal (data (), a.data (), length ()); |
|
98 } |
|
99 |
2384
|
100 bool |
1573
|
101 charMatrix::operator != (const charMatrix& a) const |
|
102 { |
|
103 return !(*this == a); |
|
104 } |
|
105 |
|
106 charMatrix& |
|
107 charMatrix::insert (const char *s, int r, int c) |
|
108 { |
|
109 if (s) |
|
110 { |
|
111 int s_len = strlen (s); |
|
112 |
|
113 if (r < 0 || r >= rows () || c < 0 || c + s_len - 1 > cols ()) |
|
114 { |
|
115 (*current_liboctave_error_handler) ("range error for insert"); |
|
116 return *this; |
|
117 } |
|
118 |
|
119 for (int i = 0; i < s_len; i++) |
|
120 elem (r, c+i) = s[i]; |
|
121 } |
|
122 return *this; |
|
123 } |
|
124 |
|
125 charMatrix& |
|
126 charMatrix::insert (const charMatrix& a, int r, int c) |
|
127 { |
|
128 Array2<char>::insert (a, r, c); |
|
129 return *this; |
|
130 } |
|
131 |
1728
|
132 string |
2493
|
133 charMatrix::row_as_string (int r, bool strip_ws = false) const |
1573
|
134 { |
2613
|
135 string retval; |
|
136 |
|
137 int nr = rows (); |
|
138 int nc = cols (); |
|
139 |
|
140 if (r == 0 && nr == 0 && nc == 0) |
|
141 return retval; |
|
142 |
|
143 if (r < 0 || r >= nr) |
1573
|
144 { |
|
145 (*current_liboctave_error_handler) ("range error for row_as_string"); |
2613
|
146 return retval; |
1573
|
147 } |
|
148 |
2613
|
149 retval.resize (nc, '\0'); |
1573
|
150 |
|
151 for (int i = 0; i < nc; i++) |
|
152 retval[i] = elem (r, i); |
|
153 |
2493
|
154 if (strip_ws) |
|
155 { |
|
156 while (--nc >= 0) |
|
157 { |
|
158 char c = retval[nc]; |
|
159 if (c && c != ' ') |
|
160 break; |
|
161 } |
|
162 } |
|
163 else |
|
164 { |
|
165 while (--nc >= 0) |
|
166 if (retval[nc]) |
|
167 break; |
|
168 } |
1780
|
169 |
|
170 retval.resize (nc+1); |
|
171 |
1573
|
172 return retval; |
|
173 } |
|
174 |
2255
|
175 charMatrix |
3189
|
176 charMatrix::extract (int r1, int c1, int r2, int c2) const |
|
177 { |
|
178 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
179 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
180 |
|
181 int new_r = r2 - r1 + 1; |
|
182 int new_c = c2 - c1 + 1; |
|
183 |
|
184 charMatrix result (new_r, new_c); |
|
185 |
|
186 for (int j = 0; j < new_c; j++) |
|
187 for (int i = 0; i < new_r; i++) |
|
188 result.elem (i, j) = elem (r1+i, c1+j); |
|
189 |
|
190 return result; |
|
191 } |
|
192 |
3136
|
193 // XXX FIXME XXX -- these should probably return a boolMatrix type |
|
194 // instead, but that will have to wait for a future version... |
|
195 |
|
196 Matrix |
|
197 charMatrix::all (void) const |
|
198 { |
|
199 int nr = rows (); |
|
200 int nc = cols (); |
|
201 Matrix retval; |
|
202 if (nr > 0 && nc > 0) |
|
203 { |
|
204 if (nr == 1) |
|
205 { |
|
206 retval.resize (1, 1); |
|
207 retval.elem (0, 0) = 1.0; |
|
208 for (int j = 0; j < nc; j++) |
|
209 { |
|
210 if (elem (0, j) == 0) |
|
211 { |
|
212 retval.elem (0, 0) = 0.0; |
|
213 break; |
|
214 } |
|
215 } |
|
216 } |
|
217 else if (nc == 1) |
|
218 { |
|
219 retval.resize (1, 1); |
|
220 retval.elem (0, 0) = 1.0; |
|
221 for (int i = 0; i < nr; i++) |
|
222 { |
|
223 if (elem (i, 0) == 0) |
|
224 { |
|
225 retval.elem (0, 0) = 0.0; |
|
226 break; |
|
227 } |
|
228 } |
|
229 } |
|
230 else |
|
231 { |
|
232 retval.resize (1, nc); |
|
233 for (int j = 0; j < nc; j++) |
|
234 { |
|
235 retval.elem (0, j) = 1.0; |
|
236 for (int i = 0; i < nr; i++) |
|
237 { |
|
238 if (elem (i, j) == 0) |
|
239 { |
|
240 retval.elem (0, j) = 0.0; |
|
241 break; |
|
242 } |
|
243 } |
|
244 } |
|
245 } |
|
246 } |
|
247 return retval; |
|
248 } |
|
249 |
|
250 Matrix |
|
251 charMatrix::any (void) const |
|
252 { |
|
253 int nr = rows (); |
|
254 int nc = cols (); |
|
255 Matrix retval; |
|
256 if (nr > 0 && nc > 0) |
|
257 { |
|
258 if (nr == 1) |
|
259 { |
|
260 retval.resize (1, 1); |
|
261 retval.elem (0, 0) = 0.0; |
|
262 for (int j = 0; j < nc; j++) |
|
263 { |
|
264 if (elem (0, j) != 0) |
|
265 { |
|
266 retval.elem (0, 0) = 1.0; |
|
267 break; |
|
268 } |
|
269 } |
|
270 } |
|
271 else if (nc == 1) |
|
272 { |
|
273 retval.resize (1, 1); |
|
274 retval.elem (0, 0) = 0.0; |
|
275 for (int i = 0; i < nr; i++) |
|
276 { |
|
277 if (elem (i, 0) != 0) |
|
278 { |
|
279 retval.elem (0, 0) = 1.0; |
|
280 break; |
|
281 } |
|
282 } |
|
283 } |
|
284 else |
|
285 { |
|
286 retval.resize (1, nc); |
|
287 for (int j = 0; j < nc; j++) |
|
288 { |
|
289 retval.elem (0, j) = 0.0; |
|
290 for (int i = 0; i < nr; i++) |
|
291 { |
|
292 if (elem (i, j) != 0) |
|
293 { |
|
294 retval.elem (0, j) = 1.0; |
|
295 break; |
|
296 } |
|
297 } |
|
298 } |
|
299 } |
|
300 } |
|
301 return retval; |
|
302 } |
|
303 |
1573
|
304 /* |
|
305 ;;; Local Variables: *** |
|
306 ;;; mode: C++ *** |
|
307 ;;; End: *** |
|
308 */ |