GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
intNDArray.cc
Go to the documentation of this file.
1 // N-D Array manipulations.
2 /*
3 
4 Copyright (C) 2004-2013 John W. Eaton
5 Copyright (C) 2009 VZLU Prague, a.s.
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
11 Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
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
20 along with Octave; see the file COPYING. If not, see
21 <http://www.gnu.org/licenses/>.
22 
23 */
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include "Array-util.h"
30 #include "mx-base.h"
31 #include "lo-ieee.h"
32 #include "mx-inlines.cc"
33 
34 // unary operations
35 
36 template <class T>
39 {
40  boolNDArray b (this->dims ());
41 
42  for (octave_idx_type i = 0; i < this->length (); i++)
43  b.elem (i) = ! this->elem (i);
44 
45  return b;
46 }
47 
48 template <class T>
49 bool
51 {
52  octave_idx_type nel = this->nelem ();
53 
54  for (octave_idx_type i = 0; i < nel; i++)
55  {
56  T val = this->elem (i);
57 
58  if (val != 0.0 && val != 1.0)
59  return true;
60  }
61 
62  return false;
63 }
64 
65 template <class T>
68 {
69  return MArray<T>::diag (k);
70 }
71 
72 template <class T>
75 {
76  return MArray<T>::diag (m, n);
77 }
78 
79 // FIXME: this is not quite the right thing.
80 
81 template <class T>
83 intNDArray<T>::all (int dim) const
84 {
85  return do_mx_red_op<bool, T > (*this, dim, mx_inline_all);
86 }
87 
88 template <class T>
90 intNDArray<T>::any (int dim) const
91 {
92  return do_mx_red_op<bool, T > (*this, dim, mx_inline_any);
93 }
94 
95 template <class T>
96 void
98  const dim_vector& dimensions,
99  int start_dimension)
100 {
101  ::increment_index (ra_idx, dimensions, start_dimension);
102 }
103 
104 template <class T>
107  const dim_vector& dimensions)
108 {
109  return ::compute_index (ra_idx, dimensions);
110 }
111 
112 template <class T>
115  const Array<octave_idx_type>& ra_idx)
116 {
117  if (rb.numel () > 0)
118  insert (rb, ra_idx);
119  return *this;
120 }
121 
122 template <class T>
125  octave_idx_type c)
126 {
127  Array<T>::insert (a, r, c);
128  return *this;
129 }
130 
131 template <class T>
134  const Array<octave_idx_type>& ra_idx)
135 {
136  Array<T>::insert (a, ra_idx);
137  return *this;
138 }
139 
140 // This contains no information on the array structure !!!
141 
142 template <class T>
143 std::ostream&
144 operator << (std::ostream& os, const intNDArray<T>& a)
145 {
146  octave_idx_type nel = a.nelem ();
147 
148  for (octave_idx_type i = 0; i < nel; i++)
149  os << " " << a.elem (i) << "\n";
150 
151  return os;
152 }
153 
154 template <class T>
155 std::istream&
156 operator >> (std::istream& is, intNDArray<T>& a)
157 {
158  octave_idx_type nel = a.nelem ();
159 
160  if (nel > 0)
161  {
162  T tmp;
163 
164  for (octave_idx_type i = 0; i < nel; i++)
165  {
166  is >> tmp;
167 
168  if (is)
169  a.elem (i) = tmp;
170  else
171  goto done;
172  }
173  }
174 
175 done:
176 
177  return is;
178 }
179 
180 // FIXME: should abs and signum just be mapper functions?
181 
182 template <class T>
184 intNDArray<T>::abs (void) const
185 {
186  octave_idx_type nel = this->nelem ();
187  intNDArray<T> ret (this->dims ());
188 
189  for (octave_idx_type i = 0; i < nel; i++)
190  {
191  T val = this->elem (i);
192  ret.xelem (i) = val.abs ();
193  }
194 
195  return ret;
196 }
197 
198 template <class T>
201 {
202  octave_idx_type nel = this->nelem ();
203  intNDArray<T> ret (this->dims ());
204 
205  for (octave_idx_type i = 0; i < nel; i++)
206  {
207  T val = this->elem (i);
208  ret.xelem (i) = val.signum ();
209  }
210 
211  return ret;
212 }
213 
214 template <class T>
216 intNDArray<T>::sum (int dim) const
217 {
218  return do_mx_red_op<T, T> (*this, dim, mx_inline_sum);
219 }
220 
221 template <class T>
222 NDArray
223 intNDArray<T>::dsum (int dim) const
224 {
225  return do_mx_red_op<double, T> (*this, dim, mx_inline_dsum);
226 }
227 
228 template <class T>
230 intNDArray<T>::cumsum (int dim) const
231 {
232  return do_mx_cum_op<T, T> (*this, dim, mx_inline_cumsum);
233 }
234 
235 template <class T>
237 intNDArray<T>::max (int dim) const
238 {
239  return do_mx_minmax_op<T> (*this, dim, mx_inline_max);
240 }
241 
242 template <class T>
245 {
246  return do_mx_minmax_op<T> (*this, idx_arg, dim, mx_inline_max);
247 }
248 
249 template <class T>
251 intNDArray<T>::min (int dim) const
252 {
253  return do_mx_minmax_op<T> (*this, dim, mx_inline_min);
254 }
255 
256 template <class T>
259 {
260  return do_mx_minmax_op<T> (*this, idx_arg, dim, mx_inline_min);
261 }
262 
263 template <class T>
265 intNDArray<T>::cummax (int dim) const
266 {
267  return do_mx_cumminmax_op<T> (*this, dim, mx_inline_cummax);
268 }
269 
270 template <class T>
273 {
274  return do_mx_cumminmax_op<T> (*this, idx_arg, dim, mx_inline_cummax);
275 }
276 
277 template <class T>
279 intNDArray<T>::cummin (int dim) const
280 {
281  return do_mx_cumminmax_op<T> (*this, dim, mx_inline_cummin);
282 }
283 
284 template <class T>
287 {
288  return do_mx_cumminmax_op<T> (*this, idx_arg, dim, mx_inline_cummin);
289 }
290 
291 template <class T>
293 intNDArray<T>::diff (octave_idx_type order, int dim) const
294 {
295  return do_mx_diff_op<T> (*this, dim, order, mx_inline_diff);
296 }