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 |
3503
|
29 #include <iostream> |
1728
|
30 #include <string> |
|
31 |
1573
|
32 #include "lo-error.h" |
2349
|
33 #include "str-vec.h" |
1573
|
34 #include "mx-base.h" |
|
35 #include "mx-inlines.cc" |
|
36 |
|
37 // charMatrix class. |
|
38 |
3189
|
39 charMatrix::charMatrix (char c) |
|
40 : MArray2<char> () |
|
41 { |
5275
|
42 octave_idx_type nc = 1; |
|
43 octave_idx_type nr = 1; |
3189
|
44 |
|
45 resize (nr, nc); |
|
46 |
|
47 elem (0, 0) = c; |
|
48 } |
|
49 |
1573
|
50 charMatrix::charMatrix (const char *s) |
2613
|
51 : MArray2<char> () |
1573
|
52 { |
5275
|
53 octave_idx_type nc = s ? strlen (s) : 0; |
|
54 octave_idx_type nr = s && nc > 0 ? 1 : 0; |
2613
|
55 |
|
56 resize (nr, nc); |
|
57 |
5275
|
58 for (octave_idx_type i = 0; i < nc; i++) |
1573
|
59 elem (0, i) = s[i]; |
|
60 } |
|
61 |
3504
|
62 charMatrix::charMatrix (const std::string& s) |
2613
|
63 : MArray2<char> () |
1733
|
64 { |
5275
|
65 octave_idx_type nc = s.length (); |
|
66 octave_idx_type nr = nc > 0 ? 1 : 0; |
2613
|
67 |
|
68 resize (nr, nc); |
|
69 |
5275
|
70 for (octave_idx_type i = 0; i < nc; i++) |
1733
|
71 elem (0, i) = s[i]; |
|
72 } |
|
73 |
2349
|
74 charMatrix::charMatrix (const string_vector& s) |
2350
|
75 : MArray2<char> (s.length (), s.max_length (), 0) |
2349
|
76 { |
5275
|
77 octave_idx_type nr = rows (); |
2613
|
78 |
5275
|
79 for (octave_idx_type i = 0; i < nr; i++) |
2349
|
80 { |
5275
|
81 octave_idx_type nc = s[i].length (); |
|
82 for (octave_idx_type j = 0; j < nc; j++) |
2349
|
83 elem (i, j) = s[i][j]; |
|
84 } |
|
85 } |
|
86 |
2384
|
87 bool |
1573
|
88 charMatrix::operator == (const charMatrix& a) const |
|
89 { |
|
90 if (rows () != a.rows () || cols () != a.cols ()) |
|
91 return 0; |
|
92 |
3769
|
93 return mx_inline_equal (data (), a.data (), length ()); |
1573
|
94 } |
|
95 |
2384
|
96 bool |
1573
|
97 charMatrix::operator != (const charMatrix& a) const |
|
98 { |
|
99 return !(*this == a); |
|
100 } |
|
101 |
|
102 charMatrix& |
5275
|
103 charMatrix::insert (const char *s, octave_idx_type r, octave_idx_type c) |
1573
|
104 { |
|
105 if (s) |
|
106 { |
5275
|
107 octave_idx_type s_len = strlen (s); |
1573
|
108 |
|
109 if (r < 0 || r >= rows () || c < 0 || c + s_len - 1 > cols ()) |
|
110 { |
|
111 (*current_liboctave_error_handler) ("range error for insert"); |
|
112 return *this; |
|
113 } |
|
114 |
5275
|
115 for (octave_idx_type i = 0; i < s_len; i++) |
1573
|
116 elem (r, c+i) = s[i]; |
|
117 } |
|
118 return *this; |
|
119 } |
|
120 |
|
121 charMatrix& |
5275
|
122 charMatrix::insert (const charMatrix& a, octave_idx_type r, octave_idx_type c) |
1573
|
123 { |
|
124 Array2<char>::insert (a, r, c); |
|
125 return *this; |
|
126 } |
|
127 |
3504
|
128 std::string |
5275
|
129 charMatrix::row_as_string (octave_idx_type r, bool strip_ws, bool raw) const |
1573
|
130 { |
3504
|
131 std::string retval; |
2613
|
132 |
5275
|
133 octave_idx_type nr = rows (); |
|
134 octave_idx_type nc = cols (); |
2613
|
135 |
|
136 if (r == 0 && nr == 0 && nc == 0) |
|
137 return retval; |
|
138 |
|
139 if (r < 0 || r >= nr) |
1573
|
140 { |
|
141 (*current_liboctave_error_handler) ("range error for row_as_string"); |
2613
|
142 return retval; |
1573
|
143 } |
|
144 |
2613
|
145 retval.resize (nc, '\0'); |
1573
|
146 |
5275
|
147 for (octave_idx_type i = 0; i < nc; i++) |
1573
|
148 retval[i] = elem (r, i); |
|
149 |
3836
|
150 if (! raw) |
2493
|
151 { |
3836
|
152 if (strip_ws) |
2493
|
153 { |
3836
|
154 while (--nc >= 0) |
|
155 { |
|
156 char c = retval[nc]; |
|
157 if (c && c != ' ') |
|
158 break; |
|
159 } |
2493
|
160 } |
3836
|
161 else |
|
162 { |
|
163 while (--nc >= 0) |
|
164 if (retval[nc]) |
|
165 break; |
|
166 } |
|
167 |
|
168 retval.resize (nc+1); |
2493
|
169 } |
1780
|
170 |
1573
|
171 return retval; |
|
172 } |
|
173 |
2255
|
174 charMatrix |
5275
|
175 charMatrix::extract (octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const |
3189
|
176 { |
5275
|
177 if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; } |
|
178 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
3189
|
179 |
5275
|
180 octave_idx_type new_r = r2 - r1 + 1; |
|
181 octave_idx_type new_c = c2 - c1 + 1; |
3189
|
182 |
|
183 charMatrix result (new_r, new_c); |
|
184 |
5275
|
185 for (octave_idx_type j = 0; j < new_c; j++) |
|
186 for (octave_idx_type i = 0; i < new_r; i++) |
3189
|
187 result.elem (i, j) = elem (r1+i, c1+j); |
|
188 |
|
189 return result; |
|
190 } |
|
191 |
6979
|
192 charMatrix |
|
193 charMatrix::diag (void) const |
|
194 { |
|
195 return diag (0); |
|
196 } |
|
197 |
|
198 charMatrix |
|
199 charMatrix::diag (octave_idx_type k) const |
|
200 { |
|
201 octave_idx_type nnr = rows (); |
|
202 octave_idx_type nnc = cols (); |
|
203 if (k > 0) |
|
204 nnc -= k; |
|
205 else if (k < 0) |
|
206 nnr += k; |
|
207 |
|
208 charMatrix d; |
|
209 |
|
210 if (nnr > 0 && nnc > 0) |
|
211 { |
|
212 octave_idx_type ndiag = (nnr < nnc) ? nnr : nnc; |
|
213 |
|
214 d.resize (ndiag, 1); |
|
215 |
|
216 if (k > 0) |
|
217 { |
|
218 for (octave_idx_type i = 0; i < ndiag; i++) |
|
219 d.xelem (i) = elem (i, i+k); |
|
220 } |
|
221 else if (k < 0) |
|
222 { |
|
223 for (octave_idx_type i = 0; i < ndiag; i++) |
|
224 d.xelem (i) = elem (i-k, i); |
|
225 } |
|
226 else |
|
227 { |
|
228 for (octave_idx_type i = 0; i < ndiag; i++) |
|
229 d.xelem (i) = elem (i, i); |
|
230 } |
|
231 } |
|
232 else |
|
233 (*current_liboctave_error_handler) |
|
234 ("diag: requested diagonal out of range"); |
|
235 |
|
236 return d; |
|
237 } |
|
238 |
5775
|
239 // FIXME Do these really belong here? Maybe they should be |
4015
|
240 // in a base class? |
3136
|
241 |
4015
|
242 boolMatrix |
|
243 charMatrix::all (int dim) const |
3136
|
244 { |
4015
|
245 MX_ALL_OP (dim); |
3136
|
246 } |
|
247 |
4015
|
248 boolMatrix |
|
249 charMatrix::any (int dim) const |
3136
|
250 { |
4015
|
251 MX_ANY_OP (dim); |
3136
|
252 } |
|
253 |
6456
|
254 MS_CMP_OPS(charMatrix, , char, ) |
|
255 MS_BOOL_OPS(charMatrix, char, 0) |
|
256 |
|
257 SM_CMP_OPS(char, , charMatrix, ) |
|
258 SM_BOOL_OPS(char, charMatrix, 0) |
|
259 |
|
260 MM_CMP_OPS(charMatrix, , charMatrix, ) |
|
261 MM_BOOL_OPS(charMatrix, charMatrix, 0) |
|
262 |
1573
|
263 /* |
|
264 ;;; Local Variables: *** |
|
265 ;;; mode: C++ *** |
|
266 ;;; End: *** |
|
267 */ |