4902
|
1 // N-D Array manipulations. |
|
2 /* |
|
3 |
7017
|
4 Copyright (C) 2004, 2005, 2006, 2007 John W. Eaton |
4902
|
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 |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
4902
|
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 |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
4902
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include "Array-util.h" |
|
29 #include "mx-base.h" |
|
30 #include "lo-ieee.h" |
|
31 |
|
32 // unary operations |
|
33 |
|
34 template <class T> |
|
35 boolNDArray |
|
36 intNDArray<T>::operator ! (void) const |
|
37 { |
4932
|
38 boolNDArray b (this->dims ()); |
4902
|
39 |
5275
|
40 for (octave_idx_type i = 0; i < this->length (); i++) |
4932
|
41 b.elem (i) = ! this->elem (i); |
4902
|
42 |
|
43 return b; |
|
44 } |
|
45 |
5943
|
46 template <class T> |
|
47 bool |
|
48 intNDArray<T>::any_element_not_one_or_zero (void) const |
|
49 { |
|
50 octave_idx_type nel = this->nelem (); |
|
51 |
|
52 for (octave_idx_type i = 0; i < nel; i++) |
|
53 { |
|
54 T val = this->elem (i); |
|
55 |
|
56 if (val != 0.0 && val != 1.0) |
|
57 return true; |
|
58 } |
|
59 |
|
60 return false; |
|
61 } |
|
62 |
6979
|
63 |
|
64 template <class T> |
|
65 intNDArray<T> |
|
66 intNDArray<T>::diag (void) const |
|
67 { |
|
68 return diag (0); |
|
69 } |
|
70 |
|
71 template <class T> |
|
72 intNDArray<T> |
|
73 intNDArray<T>::diag (octave_idx_type k) const |
|
74 { |
|
75 dim_vector dv = this->dims (); |
|
76 octave_idx_type nd = dv.length (); |
|
77 |
|
78 if (nd > 2) |
|
79 { |
|
80 (*current_liboctave_error_handler) ("Matrix must be 2-dimensional"); |
|
81 return intNDArray<T>(); |
|
82 } |
|
83 else |
|
84 { |
|
85 octave_idx_type nnr = dv (0); |
|
86 octave_idx_type nnc = dv (1); |
|
87 |
|
88 if (k > 0) |
|
89 nnc -= k; |
|
90 else if (k < 0) |
|
91 nnr += k; |
|
92 |
|
93 intNDArray<T> d; |
|
94 |
|
95 if (nnr > 0 && nnc > 0) |
|
96 { |
|
97 octave_idx_type ndiag = (nnr < nnc) ? nnr : nnc; |
|
98 |
|
99 d.resize (dim_vector (ndiag, 1)); |
|
100 |
|
101 if (k > 0) |
|
102 { |
|
103 for (octave_idx_type i = 0; i < ndiag; i++) |
|
104 d.xelem (i) = this->elem (i, i+k); |
|
105 } |
|
106 else if (k < 0) |
|
107 { |
|
108 for (octave_idx_type i = 0; i < ndiag; i++) |
|
109 d.xelem (i) = this->elem (i-k, i); |
|
110 } |
|
111 else |
|
112 { |
|
113 for (octave_idx_type i = 0; i < ndiag; i++) |
|
114 d.xelem (i) = this->elem (i, i); |
|
115 } |
|
116 } |
|
117 else |
|
118 (*current_liboctave_error_handler) |
|
119 ("diag: requested diagonal out of range"); |
|
120 |
|
121 return d; |
|
122 } |
|
123 } |
|
124 |
5775
|
125 // FIXME -- this is not quite the right thing. |
4902
|
126 |
|
127 template <class T> |
|
128 boolNDArray |
|
129 intNDArray<T>::all (int dim) const |
|
130 { |
4932
|
131 MX_ND_ANY_ALL_REDUCTION (MX_ND_ALL_EVAL (this->elem (iter_idx) == T (0)), true); |
4902
|
132 } |
|
133 |
|
134 template <class T> |
|
135 boolNDArray |
|
136 intNDArray<T>::any (int dim) const |
|
137 { |
4932
|
138 MX_ND_ANY_ALL_REDUCTION (MX_ND_ALL_EVAL (this->elem (iter_idx) == T (0)), false); |
4902
|
139 } |
|
140 |
|
141 template <class T> |
|
142 void |
5275
|
143 intNDArray<T>::increment_index (Array<octave_idx_type>& ra_idx, |
4902
|
144 const dim_vector& dimensions, |
|
145 int start_dimension) |
|
146 { |
|
147 ::increment_index (ra_idx, dimensions, start_dimension); |
|
148 } |
|
149 |
|
150 template <class T> |
5275
|
151 octave_idx_type |
|
152 intNDArray<T>::compute_index (Array<octave_idx_type>& ra_idx, |
4902
|
153 const dim_vector& dimensions) |
|
154 { |
|
155 return ::compute_index (ra_idx, dimensions); |
|
156 } |
|
157 |
4915
|
158 template <class T> |
5073
|
159 intNDArray<T> |
5275
|
160 intNDArray<T>::concat (const intNDArray<T>& rb, const Array<octave_idx_type>& ra_idx) |
5073
|
161 { |
6482
|
162 if (rb.numel () > 0) |
5073
|
163 insert (rb, ra_idx); |
|
164 return *this; |
|
165 } |
|
166 |
|
167 template <class T> |
4915
|
168 intNDArray<T>& |
5275
|
169 intNDArray<T>::insert (const intNDArray<T>& a, octave_idx_type r, octave_idx_type c) |
4915
|
170 { |
|
171 Array<T>::insert (a, r, c); |
|
172 return *this; |
|
173 } |
|
174 |
|
175 template <class T> |
|
176 intNDArray<T>& |
5275
|
177 intNDArray<T>::insert (const intNDArray<T>& a, const Array<octave_idx_type>& ra_idx) |
4915
|
178 { |
|
179 Array<T>::insert (a, ra_idx); |
|
180 return *this; |
|
181 } |
|
182 |
4902
|
183 // This contains no information on the array structure !!! |
|
184 |
|
185 template <class T> |
|
186 std::ostream& |
|
187 operator << (std::ostream& os, const intNDArray<T>& a) |
|
188 { |
5275
|
189 octave_idx_type nel = a.nelem (); |
4902
|
190 |
5275
|
191 for (octave_idx_type i = 0; i < nel; i++) |
4902
|
192 os << " " << a.elem (i) << "\n"; |
|
193 |
|
194 return os; |
|
195 } |
|
196 |
|
197 template <class T> |
|
198 std::istream& |
|
199 operator >> (std::istream& is, intNDArray<T>& a) |
|
200 { |
5275
|
201 octave_idx_type nel = a.nelem (); |
4902
|
202 |
|
203 if (nel < 1 ) |
|
204 is.clear (std::ios::badbit); |
|
205 else |
|
206 { |
|
207 T tmp; |
|
208 |
5275
|
209 for (octave_idx_type i = 0; i < nel; i++) |
4902
|
210 { |
|
211 is >> tmp; |
|
212 |
|
213 if (is) |
|
214 a.elem (i) = tmp; |
|
215 else |
|
216 goto done; |
|
217 } |
|
218 } |
|
219 |
|
220 done: |
|
221 |
|
222 return is; |
|
223 } |
|
224 |
7113
|
225 template <class T> |
|
226 intNDArray<T> |
|
227 intNDArray<T>::sum (int dim) const |
|
228 { |
|
229 MX_ND_REDUCTION (retval(result_idx) += intNDArray<T>::elem (iter_idx), 0, intNDArray<T>); |
|
230 } |
|
231 |
4902
|
232 /* |
|
233 ;;; Local Variables: *** |
|
234 ;;; mode: C++ *** |
|
235 ;;; End: *** |
|
236 */ |