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