GNU Octave  4.2.1
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-2017 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 // This file should not include config.h. It is only included in other
26 // C++ source files that should have included config.h before including
27 // this file.
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 <typename T>
39 {
40  boolNDArray b (this->dims ());
41 
42  for (octave_idx_type i = 0; i < this->numel (); i++)
43  b.elem (i) = ! this->elem (i);
44 
45  return b;
46 }
47 
48 template <typename T>
49 bool
51 {
52  octave_idx_type nel = this->numel ();
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 <typename T>
68 {
69  return MArray<T>::diag (k);
70 }
71 
72 template <typename T>
75 {
76  return MArray<T>::diag (m, n);
77 }
78 
79 // FIXME: this is not quite the right thing.
80 
81 template <typename 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 <typename 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 <typename 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 <typename T>
107  const dim_vector& dimensions)
108 {
109  return ::compute_index (ra_idx, dimensions);
110 }
111 
112 template <typename T>
116 {
117  if (rb.numel () > 0)
118  insert (rb, ra_idx);
119  return *this;
120 }
121 
122 template <typename T>
126 {
127  Array<T>::insert (a, r, c);
128  return *this;
129 }
130 
131 template <typename 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 <typename T>
143 std::ostream&
144 operator << (std::ostream& os, const intNDArray<T>& a)
145 {
146  octave_idx_type nel = a.numel ();
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 <typename T>
155 std::istream&
156 operator >> (std::istream& is, intNDArray<T>& a)
157 {
158  octave_idx_type nel = a.numel ();
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  return is;
172  }
173  }
174 
175  return is;
176 }
177 
178 // FIXME: should abs and signum just be mapper functions?
179 
180 template <typename T>
182 intNDArray<T>::abs (void) const
183 {
184  octave_idx_type nel = this->numel ();
185  intNDArray<T> ret (this->dims ());
186 
187  for (octave_idx_type i = 0; i < nel; i++)
188  {
189  T val = this->elem (i);
190  ret.xelem (i) = val.abs ();
191  }
192 
193  return ret;
194 }
195 
196 template <typename T>
199 {
200  octave_idx_type nel = this->numel ();
201  intNDArray<T> ret (this->dims ());
202 
203  for (octave_idx_type i = 0; i < nel; i++)
204  {
205  T val = this->elem (i);
206  ret.xelem (i) = val.signum ();
207  }
208 
209  return ret;
210 }
211 
212 template <typename T>
214 intNDArray<T>::prod (int dim) const
215 {
216  return do_mx_red_op<T, T> (*this, dim, mx_inline_prod);
217 }
218 
219 template <typename T>
221 intNDArray<T>::sum (int dim) const
222 {
223  return do_mx_red_op<T, T> (*this, dim, mx_inline_sum);
224 }
225 
226 template <typename T>
227 NDArray
228 intNDArray<T>::dsum (int dim) const
229 {
230  return do_mx_red_op<double, T> (*this, dim, mx_inline_dsum);
231 }
232 
233 template <typename T>
235 intNDArray<T>::cumsum (int dim) const
236 {
237  return do_mx_cum_op<T, T> (*this, dim, mx_inline_cumsum);
238 }
239 
240 template <typename T>
242 intNDArray<T>::max (int dim) const
243 {
244  return do_mx_minmax_op<T> (*this, dim, mx_inline_max);
245 }
246 
247 template <typename T>
250 {
251  return do_mx_minmax_op<T> (*this, idx_arg, dim, mx_inline_max);
252 }
253 
254 template <typename T>
256 intNDArray<T>::min (int dim) const
257 {
258  return do_mx_minmax_op<T> (*this, dim, mx_inline_min);
259 }
260 
261 template <typename T>
264 {
265  return do_mx_minmax_op<T> (*this, idx_arg, dim, mx_inline_min);
266 }
267 
268 template <typename T>
270 intNDArray<T>::cummax (int dim) const
271 {
272  return do_mx_cumminmax_op<T> (*this, dim, mx_inline_cummax);
273 }
274 
275 template <typename T>
278 {
279  return do_mx_cumminmax_op<T> (*this, idx_arg, dim, mx_inline_cummax);
280 }
281 
282 template <typename T>
284 intNDArray<T>::cummin (int dim) const
285 {
286  return do_mx_cumminmax_op<T> (*this, dim, mx_inline_cummin);
287 }
288 
289 template <typename T>
292 {
293  return do_mx_cumminmax_op<T> (*this, idx_arg, dim, mx_inline_cummin);
294 }
295 
296 template <typename T>
298 intNDArray<T>::diff (octave_idx_type order, int dim) const
299 {
300  return do_mx_diff_op<T> (*this, dim, order, mx_inline_diff);
301 }
octave_idx_type compute_index(octave_idx_type n, const dim_vector &dims)
Definition: Array-util.cc:176
bool any_element_not_one_or_zero(void) const
Definition: intNDArray.cc:50
void mx_inline_cummax(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:1147
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:363
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:5068
for large enough k
Definition: lu.cc:606
intNDArray max(int dim=-1) const
Definition: intNDArray.cc:242
intNDArray & insert(const intNDArray< T > &a, octave_idx_type r, octave_idx_type c)
Definition: intNDArray.cc:124
Array< T > diag(octave_idx_type k=0) const
Get the kth super or subdiagonal.
Definition: Array.cc:2529
intNDArray diag(octave_idx_type k=0) const
Definition: intNDArray.cc:67
static void increment_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions, int start_dimension=0)
Definition: intNDArray.cc:97
intNDArray cumsum(int dim) const
Definition: intNDArray.cc:235
T & elem(octave_idx_type n)
Definition: Array.h:482
static octave_idx_type compute_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions)
Definition: intNDArray.cc:106
void mx_inline_max(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:925
boolNDArray any(int dim=-1) const
Definition: intNDArray.cc:90
bool mx_inline_any(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:716
intNDArray sum(int dim) const
Definition: intNDArray.cc:221
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:1601
std::istream & operator>>(std::istream &is, intNDArray< T > &a)
Definition: intNDArray.cc:156
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:398
intNDArray cummin(int dim=-1) const
Definition: intNDArray.cc:284
intNDArray diff(octave_idx_type order=1, int dim=-1) const
Definition: intNDArray.cc:298
void increment_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions, int start_dimension)
Definition: Array-util.cc:59
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
NDArray dsum(int dim) const
Definition: intNDArray.cc:228
static int elem
Definition: __contourc__.cc:50
double tmp
Definition: data.cc:6300
void mx_inline_cummin(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:1146
the sparsity preserving column transformation such that that defines the pivoting threshold can be given in which case it defines the c
Definition: lu.cc:138
the exceeded dimensions are set to if fewer subscripts than dimensions are the exceeding dimensions are merged into the final requested dimension For consider the following dims
Definition: sub2ind.cc:255
intNDArray abs(void) const
Definition: intNDArray.cc:182
intNDArray concat(const intNDArray< T > &rb, const Array< octave_idx_type > &ra_idx)
Definition: intNDArray.cc:114
subst_template_param< std::complex, T, double >::type mx_inline_dsum(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:714
intNDArray cummax(int dim=-1) const
Definition: intNDArray.cc:270
T & xelem(octave_idx_type n)
Definition: Array.h:455
T mx_inline_sum(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:714
T::size_type numel(const T &str)
Definition: oct-string.cc:61
boolNDArray operator!(void) const
Definition: intNDArray.cc:38
void mx_inline_cumsum(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:820
intNDArray min(int dim=-1) const
Definition: intNDArray.cc:256
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
boolNDArray all(int dim=-1) const
Definition: intNDArray.cc:83
b
Definition: cellfun.cc:398
intNDArray signum(void) const
Definition: intNDArray.cc:198
bool mx_inline_all(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:716
intNDArray prod(int dim) const
Definition: intNDArray.cc:214
write the output to stdout if nargout is
Definition: load-save.cc:1576
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
T mx_inline_prod(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:715
void mx_inline_diff(const T *v, T *r, octave_idx_type n, octave_idx_type order)
Definition: mx-inlines.cc:1343
void mx_inline_min(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:924