GNU Octave  4.0.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-2015 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>
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>
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>::prod (int dim) const
217 {
218  return do_mx_red_op<T, T> (*this, dim, mx_inline_prod);
219 }
220 
221 template <class T>
223 intNDArray<T>::sum (int dim) const
224 {
225  return do_mx_red_op<T, T> (*this, dim, mx_inline_sum);
226 }
227 
228 template <class T>
229 NDArray
230 intNDArray<T>::dsum (int dim) const
231 {
232  return do_mx_red_op<double, T> (*this, dim, mx_inline_dsum);
233 }
234 
235 template <class T>
237 intNDArray<T>::cumsum (int dim) const
238 {
239  return do_mx_cum_op<T, T> (*this, dim, mx_inline_cumsum);
240 }
241 
242 template <class T>
244 intNDArray<T>::max (int dim) const
245 {
246  return do_mx_minmax_op<T> (*this, dim, mx_inline_max);
247 }
248 
249 template <class T>
252 {
253  return do_mx_minmax_op<T> (*this, idx_arg, dim, mx_inline_max);
254 }
255 
256 template <class T>
258 intNDArray<T>::min (int dim) const
259 {
260  return do_mx_minmax_op<T> (*this, dim, mx_inline_min);
261 }
262 
263 template <class T>
266 {
267  return do_mx_minmax_op<T> (*this, idx_arg, dim, mx_inline_min);
268 }
269 
270 template <class T>
272 intNDArray<T>::cummax (int dim) const
273 {
274  return do_mx_cumminmax_op<T> (*this, dim, mx_inline_cummax);
275 }
276 
277 template <class T>
280 {
281  return do_mx_cumminmax_op<T> (*this, idx_arg, dim, mx_inline_cummax);
282 }
283 
284 template <class T>
286 intNDArray<T>::cummin (int dim) const
287 {
288  return do_mx_cumminmax_op<T> (*this, dim, mx_inline_cummin);
289 }
290 
291 template <class T>
294 {
295  return do_mx_cumminmax_op<T> (*this, idx_arg, dim, mx_inline_cummin);
296 }
297 
298 template <class T>
300 intNDArray<T>::diff (octave_idx_type order, int dim) const
301 {
302  return do_mx_diff_op<T> (*this, dim, order, mx_inline_diff);
303 }
octave_idx_type compute_index(octave_idx_type n, const dim_vector &dims)
Definition: Array-util.cc:178
NDArray dsum(int dim) const
Definition: intNDArray.cc:230
static void increment_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions, int start_dimension=0)
Definition: intNDArray.cc:97
void mx_inline_cummax(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:894
intNDArray abs(void) const
Definition: intNDArray.cc:184
const octave_base_value const Array< octave_idx_type > & ra_idx
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:275
intNDArray sum(int dim) const
Definition: intNDArray.cc:223
intNDArray cummin(int dim=-1) const
Definition: intNDArray.cc:286
intNDArray max(int dim=-1) const
Definition: intNDArray.cc:244
Array< T > diag(octave_idx_type k=0) const
Get the kth super or subdiagonal.
Definition: Array.cc:2526
bool any_element_not_one_or_zero(void) const
Definition: intNDArray.cc:50
T & elem(octave_idx_type n)
Definition: Array.h:380
boolNDArray operator!(void) const
Definition: intNDArray.cc:38
void mx_inline_max(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:719
bool mx_inline_any(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:524
intNDArray prod(int dim) const
Definition: intNDArray.cc:216
intNDArray min(int dim=-1) const
Definition: intNDArray.cc:258
Array< T > & insert(const Array< T > &a, const Array< octave_idx_type > &idx)
Insert an array into another at a specified position.
Definition: Array.cc:1591
std::istream & operator>>(std::istream &is, intNDArray< T > &a)
Definition: intNDArray.cc:156
octave_idx_type nelem(void) const
Number of elements in the array.
Definition: Array.h:271
void increment_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions, int start_dimension)
Definition: Array-util.cc:59
static octave_idx_type compute_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions)
Definition: intNDArray.cc:106
intNDArray & insert(const intNDArray< T > &a, octave_idx_type r, octave_idx_type c)
Definition: intNDArray.cc:124
static int elem
Definition: __contourc__.cc:49
intNDArray concat(const intNDArray< T > &rb, const Array< octave_idx_type > &ra_idx)
Definition: intNDArray.cc:114
boolNDArray all(int dim=-1) const
Definition: intNDArray.cc:83
void mx_inline_cummin(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:893
subst_template_param< std::complex, T, double >::type mx_inline_dsum(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:522
T & xelem(octave_idx_type n)
Definition: Array.h:353
intNDArray cummax(int dim=-1) const
Definition: intNDArray.cc:272
T mx_inline_sum(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:522
void mx_inline_cumsum(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:627
intNDArray diag(octave_idx_type k=0) const
Definition: intNDArray.cc:67
bool mx_inline_all(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:524
intNDArray signum(void) const
Definition: intNDArray.cc:200
boolNDArray any(int dim=-1) const
Definition: intNDArray.cc:90
intNDArray cumsum(int dim) const
Definition: intNDArray.cc:237
T mx_inline_prod(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:523
void mx_inline_diff(const T *v, T *r, octave_idx_type n, octave_idx_type order)
Definition: mx-inlines.cc:1036
intNDArray diff(octave_idx_type order=1, int dim=-1) const
Definition: intNDArray.cc:300
void mx_inline_min(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:718