2828
|
1 // Matrix manipulations. |
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
2828
|
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 |
|
32 #include <iostream.h> |
|
33 |
|
34 #include "lo-error.h" |
|
35 #include "str-vec.h" |
|
36 #include "mx-base.h" |
|
37 #include "mx-inlines.cc" |
|
38 |
|
39 // boolMatrix class. |
|
40 |
|
41 bool |
|
42 boolMatrix::operator == (const boolMatrix& a) const |
|
43 { |
|
44 if (rows () != a.rows () || cols () != a.cols ()) |
|
45 return 0; |
|
46 |
|
47 return equal (data (), a.data (), length ()); |
|
48 } |
|
49 |
|
50 bool |
|
51 boolMatrix::operator != (const boolMatrix& a) const |
|
52 { |
|
53 return !(*this == a); |
|
54 } |
|
55 |
|
56 boolMatrix& |
|
57 boolMatrix::insert (const boolMatrix& a, int r, int c) |
|
58 { |
|
59 Array2<bool>::insert (a, r, c); |
|
60 return *this; |
|
61 } |
|
62 |
|
63 boolMatrix |
|
64 boolMatrix::transpose (void) const |
|
65 { |
|
66 int nr = rows (); |
|
67 int nc = cols (); |
|
68 boolMatrix result (nc, nr); |
|
69 if (length () > 0) |
|
70 { |
|
71 for (int j = 0; j < nc; j++) |
|
72 for (int i = 0; i < nr; i++) |
|
73 result.elem (j, i) = elem (i, j); |
|
74 } |
|
75 return result; |
|
76 } |
|
77 |
3203
|
78 // unary operations |
|
79 |
|
80 boolMatrix |
|
81 boolMatrix::operator ! (void) const |
|
82 { |
|
83 int nr = rows (); |
|
84 int nc = cols (); |
|
85 |
|
86 boolMatrix b (nr, nc); |
|
87 |
|
88 for (int j = 0; j < nc; j++) |
|
89 for (int i = 0; i < nr; i++) |
|
90 b.elem (i, j) = ! elem (i, j); |
|
91 |
|
92 return b; |
|
93 } |
|
94 |
|
95 // other operations |
|
96 |
2832
|
97 boolMatrix |
2833
|
98 boolMatrix::all (void) const |
2832
|
99 { |
|
100 int nr = rows (); |
|
101 int nc = cols (); |
|
102 boolMatrix retval; |
|
103 if (nr > 0 && nc > 0) |
|
104 { |
|
105 if (nr == 1) |
|
106 { |
|
107 retval.resize (1, 1); |
|
108 retval.elem (0, 0) = true; |
|
109 for (int j = 0; j < nc; j++) |
|
110 { |
|
111 if (! elem (0, j)) |
|
112 { |
|
113 retval.elem (0, 0) = false; |
|
114 break; |
|
115 } |
|
116 } |
|
117 } |
|
118 else if (nc == 1) |
|
119 { |
|
120 retval.resize (1, 1); |
|
121 retval.elem (0, 0) = true; |
|
122 for (int i = 0; i < nr; i++) |
|
123 { |
|
124 if (! elem (i, 0)) |
|
125 { |
|
126 retval.elem (0, 0) = false; |
|
127 break; |
|
128 } |
|
129 } |
|
130 } |
|
131 else |
|
132 { |
|
133 retval.resize (1, nc); |
|
134 for (int j = 0; j < nc; j++) |
|
135 { |
|
136 retval.elem (0, j) = true; |
|
137 for (int i = 0; i < nr; i++) |
|
138 { |
|
139 if (! elem (i, j)) |
|
140 { |
|
141 retval.elem (0, j) = false; |
|
142 break; |
|
143 } |
|
144 } |
|
145 } |
|
146 } |
|
147 } |
|
148 return retval; |
|
149 } |
|
150 |
|
151 boolMatrix |
2833
|
152 boolMatrix::any (void) const |
2832
|
153 { |
|
154 int nr = rows (); |
|
155 int nc = cols (); |
|
156 boolMatrix retval; |
|
157 if (nr > 0 && nc > 0) |
|
158 { |
|
159 if (nr == 1) |
|
160 { |
|
161 retval.resize (1, 1); |
|
162 retval.elem (0, 0) = false; |
|
163 for (int j = 0; j < nc; j++) |
|
164 { |
|
165 if (elem (0, j)) |
|
166 { |
|
167 retval.elem (0, 0) = true; |
|
168 break; |
|
169 } |
|
170 } |
|
171 } |
|
172 else if (nc == 1) |
|
173 { |
|
174 retval.resize (1, 1); |
|
175 retval.elem (0, 0) = false; |
|
176 for (int i = 0; i < nr; i++) |
|
177 { |
|
178 if (elem (i, 0)) |
|
179 { |
|
180 retval.elem (0, 0) = true; |
|
181 break; |
|
182 } |
|
183 } |
|
184 } |
|
185 else |
|
186 { |
|
187 retval.resize (1, nc); |
|
188 for (int j = 0; j < nc; j++) |
|
189 { |
|
190 retval.elem (0, j) = false; |
|
191 for (int i = 0; i < nr; i++) |
|
192 { |
|
193 if (elem (i, j)) |
|
194 { |
|
195 retval.elem (0, j) = true; |
|
196 break; |
|
197 } |
|
198 } |
|
199 } |
|
200 } |
|
201 } |
|
202 return retval; |
|
203 } |
|
204 |
2828
|
205 /* |
|
206 ;;; Local Variables: *** |
|
207 ;;; mode: C++ *** |
|
208 ;;; End: *** |
|
209 */ |