GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
intNDArray.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2004-2018 John W. Eaton
4 Copyright (C) 2009 VZLU Prague, a.s.
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
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License 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, see
20 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 // This file should not include config.h. It is only included in other
25 // C++ source files that should have included config.h before including
26 // this file.
27 
28 #include "Array-util.h"
29 #include "mx-base.h"
30 #include "lo-ieee.h"
31 #include "mx-inlines.cc"
32 
33 // unary operations
34 
35 template <typename T>
38 {
39  boolNDArray b (this->dims ());
40 
41  for (octave_idx_type i = 0; i < this->numel (); i++)
42  b.elem (i) = ! this->elem (i);
43 
44  return b;
45 }
46 
47 template <typename T>
48 bool
50 {
51  octave_idx_type nel = this->numel ();
52 
53  for (octave_idx_type i = 0; i < nel; i++)
54  {
55  T val = this->elem (i);
56 
57  if (val != 0.0 && val != 1.0)
58  return true;
59  }
60 
61  return false;
62 }
63 
64 template <typename T>
67 {
68  return MArray<T>::diag (k);
69 }
70 
71 template <typename T>
74 {
75  return MArray<T>::diag (m, n);
76 }
77 
78 // FIXME: this is not quite the right thing.
79 
80 template <typename T>
82 intNDArray<T>::all (int dim) const
83 {
84  return do_mx_red_op<bool, T > (*this, dim, mx_inline_all);
85 }
86 
87 template <typename T>
89 intNDArray<T>::any (int dim) const
90 {
91  return do_mx_red_op<bool, T > (*this, dim, mx_inline_any);
92 }
93 
94 template <typename T>
95 void
97  const dim_vector& dimensions,
98  int start_dimension)
99 {
100  ::increment_index (ra_idx, dimensions, start_dimension);
101 }
102 
103 template <typename T>
106  const dim_vector& dimensions)
107 {
108  return ::compute_index (ra_idx, dimensions);
109 }
110 
111 template <typename T>
115 {
116  if (rb.numel () > 0)
117  insert (rb, ra_idx);
118  return *this;
119 }
120 
121 template <typename T>
125 {
126  Array<T>::insert (a, r, c);
127  return *this;
128 }
129 
130 template <typename T>
134 {
136  return *this;
137 }
138 
139 // This contains no information on the array structure !!!
140 
141 template <typename T>
142 std::ostream&
143 operator << (std::ostream& os, const intNDArray<T>& a)
144 {
145  octave_idx_type nel = a.numel ();
146 
147  for (octave_idx_type i = 0; i < nel; i++)
148  os << ' ' << a.elem (i) << "\n";
149 
150  return os;
151 }
152 
153 template <typename T>
154 std::istream&
155 operator >> (std::istream& is, intNDArray<T>& a)
156 {
157  octave_idx_type nel = a.numel ();
158 
159  if (nel > 0)
160  {
161  T tmp;
162 
163  for (octave_idx_type i = 0; i < nel; i++)
164  {
165  is >> tmp;
166 
167  if (is)
168  a.elem (i) = tmp;
169  else
170  return is;
171  }
172  }
173 
174  return is;
175 }
176 
177 // FIXME: should abs and signum just be mapper functions?
178 
179 template <typename T>
181 intNDArray<T>::abs (void) const
182 {
183  octave_idx_type nel = this->numel ();
184  intNDArray<T> ret (this->dims ());
185 
186  for (octave_idx_type i = 0; i < nel; i++)
187  {
188  T val = this->elem (i);
189  ret.xelem (i) = val.abs ();
190  }
191 
192  return ret;
193 }
194 
195 template <typename T>
198 {
199  octave_idx_type nel = this->numel ();
200  intNDArray<T> ret (this->dims ());
201 
202  for (octave_idx_type i = 0; i < nel; i++)
203  {
204  T val = this->elem (i);
205  ret.xelem (i) = val.signum ();
206  }
207 
208  return ret;
209 }
210 
211 template <typename T>
213 intNDArray<T>::prod (int dim) const
214 {
215  return do_mx_red_op<T, T> (*this, dim, mx_inline_prod);
216 }
217 
218 template <typename T>
220 intNDArray<T>::sum (int dim) const
221 {
222  return do_mx_red_op<T, T> (*this, dim, mx_inline_sum);
223 }
224 
225 template <typename T>
226 NDArray
227 intNDArray<T>::dsum (int dim) const
228 {
229  return do_mx_red_op<double, T> (*this, dim, mx_inline_dsum);
230 }
231 
232 template <typename T>
234 intNDArray<T>::cumsum (int dim) const
235 {
236  return do_mx_cum_op<T, T> (*this, dim, mx_inline_cumsum);
237 }
238 
239 template <typename T>
241 intNDArray<T>::max (int dim) const
242 {
243  return do_mx_minmax_op<T> (*this, dim, mx_inline_max);
244 }
245 
246 template <typename T>
249 {
250  return do_mx_minmax_op<T> (*this, idx_arg, dim, mx_inline_max);
251 }
252 
253 template <typename T>
255 intNDArray<T>::min (int dim) const
256 {
257  return do_mx_minmax_op<T> (*this, dim, mx_inline_min);
258 }
259 
260 template <typename T>
263 {
264  return do_mx_minmax_op<T> (*this, idx_arg, dim, mx_inline_min);
265 }
266 
267 template <typename T>
269 intNDArray<T>::cummax (int dim) const
270 {
271  return do_mx_cumminmax_op<T> (*this, dim, mx_inline_cummax);
272 }
273 
274 template <typename T>
277 {
278  return do_mx_cumminmax_op<T> (*this, idx_arg, dim, mx_inline_cummax);
279 }
280 
281 template <typename T>
283 intNDArray<T>::cummin (int dim) const
284 {
285  return do_mx_cumminmax_op<T> (*this, dim, mx_inline_cummin);
286 }
287 
288 template <typename T>
291 {
292  return do_mx_cumminmax_op<T> (*this, idx_arg, dim, mx_inline_cummin);
293 }
294 
295 template <typename T>
297 intNDArray<T>::diff (octave_idx_type order, int dim) const
298 {
299  return do_mx_diff_op<T> (*this, dim, order, mx_inline_diff);
300 }
octave_idx_type compute_index(octave_idx_type n, const dim_vector &dims)
Definition: Array-util.cc:175
intNDArray cummin(int dim=-1) const
Definition: intNDArray.cc:283
boolNDArray operator!(void) const
Definition: intNDArray.cc:37
intNDArray cumsum(int dim) const
Definition: intNDArray.cc:234
static void increment_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions, int start_dimension=0)
Definition: intNDArray.cc:96
void mx_inline_cummax(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:1184
intNDArray abs(void) const
Definition: intNDArray.cc:181
intNDArray min(int dim=-1) const
Definition: intNDArray.cc:255
const octave_base_value const Array< octave_idx_type > & ra_idx
intNDArray diff(octave_idx_type order=1, int dim=-1) const
Definition: intNDArray.cc:297
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
intNDArray diag(octave_idx_type k=0) const
Definition: intNDArray.cc:66
for large enough k
Definition: lu.cc:617
boolNDArray any(int dim=-1) const
Definition: intNDArray.cc:89
intNDArray sum(int dim) const
Definition: intNDArray.cc:220
T & elem(octave_idx_type n)
Definition: Array.h:488
void mx_inline_max(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:962
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
Definition: file-io.cc:587
bool mx_inline_any(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:753
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:1583
std::istream & operator>>(std::istream &is, intNDArray< T > &a)
Definition: intNDArray.cc:155
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:400
intNDArray signum(void) const
Definition: intNDArray.cc:197
bool any_element_not_one_or_zero(void) const
Definition: intNDArray.cc:49
void increment_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions, int start_dimension)
Definition: Array-util.cc:58
static octave_idx_type compute_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions)
Definition: intNDArray.cc:105
intNDArray & insert(const intNDArray< T > &a, octave_idx_type r, octave_idx_type c)
Definition: intNDArray.cc:123
static int elem
Definition: __contourc__.cc:47
double tmp
Definition: data.cc:6252
intNDArray concat(const intNDArray< T > &rb, const Array< octave_idx_type > &ra_idx)
Definition: intNDArray.cc:113
void mx_inline_cummin(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:1183
intNDArray cummax(int dim=-1) const
Definition: intNDArray.cc:269
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
subst_template_param< std::complex, T, double >::type mx_inline_dsum(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:751
T & xelem(octave_idx_type n)
Definition: Array.h:458
T mx_inline_sum(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:751
NDArray dsum(int dim) const
Definition: intNDArray.cc:227
T::size_type numel(const T &str)
Definition: oct-string.cc:61
void mx_inline_cumsum(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:857
Array< T > diag(octave_idx_type k=0) const
Get the kth super or subdiagonal.
Definition: Array.cc:2530
b
Definition: cellfun.cc:400
intNDArray max(int dim=-1) const
Definition: intNDArray.cc:241
for i
Definition: data.cc:5264
bool mx_inline_all(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:753
intNDArray prod(int dim) const
Definition: intNDArray.cc:213
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:366
boolNDArray all(int dim=-1) const
Definition: intNDArray.cc:82
write the output to stdout if nargout is
Definition: load-save.cc:1612
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:752
octave::stream os
Definition: file-io.cc:627
void mx_inline_diff(const T *v, T *r, octave_idx_type n, octave_idx_type order)
Definition: mx-inlines.cc:1380
void mx_inline_min(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:961