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
chNDArray.cc
Go to the documentation of this file.
1 // N-D Array manipulations.
2 /*
3 
4 Copyright (C) 2003-2017 John W. Eaton
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
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
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
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27 
28 #include <string>
29 
30 #include "Array-util.h"
31 #include "chNDArray.h"
32 #include "mx-base.h"
33 #include "lo-ieee.h"
34 #include "lo-mappers.h"
35 #include "mx-op-defs.h"
36 #include "str-vec.h"
37 
38 #include "bsxfun-defs.cc"
39 
41  : Array<char> ()
42 {
43  octave_idx_type n = 1;
44 
45  resize1 (n);
46 
47  elem (0) = c;
48 }
49 
51  : Array<char> ()
52 {
53  octave_idx_type n = s ? strlen (s) : 0;
54 
55  resize1 (n);
56 
57  for (octave_idx_type i = 0; i < n; i++)
58  elem (i) = s[i];
59 }
60 
62  : Array<char> ()
63 {
64  octave_idx_type n = s.length ();
65 
66  resize1 (n);
67 
68  for (octave_idx_type i = 0; i < n; i++)
69  elem (i) = s[i];
70 }
71 
72 charNDArray::charNDArray (const string_vector& s, char fill_value)
73  : Array<char> (dim_vector (s.numel (), s.max_length ()), fill_value)
74 {
75  octave_idx_type nr = rows ();
76 
77  for (octave_idx_type i = 0; i < nr; i++)
78  {
79  const std::string si = s(i);
80  octave_idx_type nc = si.length ();
81  for (octave_idx_type j = 0; j < nc; j++)
82  elem (i, j) = si[j];
83  }
84 }
85 
86 // FIXME: this is not quite the right thing.
87 
89 charNDArray::all (int dim) const
90 {
91  return do_mx_red_op<bool, char> (*this, dim, mx_inline_all);
92 }
93 
95 charNDArray::any (int dim) const
96 {
97  return do_mx_red_op<bool, char> (*this, dim, mx_inline_any);
98 }
99 
103 {
104  if (rb.numel () > 0)
105  insert (rb, ra_idx);
106  return *this;
107 }
108 
111 {
112  charNDArray tmp (rb.dims ());
113  octave_idx_type nel = rb.numel ();
114 
115  if (rb.is_empty ())
116  return *this;
117 
118  for (octave_idx_type i = 0; i < nel; i++)
119  {
120  double d = rb.elem (i);
121 
122  if (octave::math::isnan (d))
123  (*current_liboctave_error_handler)
124  ("invalid conversion from NaN to character");
125 
127 
128  if (ival < 0 || ival > std::numeric_limits<unsigned char>::max ())
129  // FIXME: is there something better to do? Should we warn the user?
130  ival = 0;
131 
132  tmp.elem (i) = static_cast<char>(ival);
133  }
134 
135  insert (tmp, ra_idx);
136  return *this;
137 }
138 
140 charNDArray::max (int dim) const
141 {
142  return do_mx_minmax_op<char> (*this, dim, mx_inline_max);
143 }
144 
146 charNDArray::max (Array<octave_idx_type>& idx_arg, int dim) const
147 {
148  return do_mx_minmax_op<char> (*this, idx_arg, dim, mx_inline_max);
149 }
150 
152 charNDArray::min (int dim) const
153 {
154  return do_mx_minmax_op<char> (*this, dim, mx_inline_min);
155 }
156 
158 charNDArray::min (Array<octave_idx_type>& idx_arg, int dim) const
159 {
160  return do_mx_minmax_op<char> (*this, idx_arg, dim, mx_inline_min);
161 }
162 
165 {
166  Array<char>::insert (a, r, c);
167  return *this;
168 }
169 
172 {
173  Array<char>::insert (a, ra_idx);
174  return *this;
175 }
176 
177 void
179  const dim_vector& dimensions,
180  int start_dimension)
181 {
182  ::increment_index (ra_idx, dimensions, start_dimension);
183 }
184 
187  const dim_vector& dimensions)
188 {
189  return ::compute_index (ra_idx, dimensions);
190 }
191 
194 {
195  return Array<char>::diag (k);
196 }
197 
200 {
201  return Array<char>::diag (m, n);
202 }
203 
205 min (char d, const charNDArray& m)
206 {
208  charNDArray::element_type> (d, m, mx_inline_xmin);
209 }
210 
212 min (const charNDArray& m, char d)
213 {
215  char> (m, d, mx_inline_xmin);
216 }
217 
219 min (const charNDArray& a, const charNDArray& b)
220 {
222  charNDArray::element_type> (a, b, mx_inline_xmin,
224  mx_inline_xmin, "min");
225 }
226 
228 max (char d, const charNDArray& m)
229 {
231  charNDArray::element_type> (d, m, mx_inline_xmax);
232 }
233 
235 max (const charNDArray& m, char d)
236 {
238  char> (m, d, mx_inline_xmax);
239 }
240 
242 max (const charNDArray& a, const charNDArray& b)
243 {
245  charNDArray::element_type> (a, b, mx_inline_xmax,
247  mx_inline_xmax, "max");
248 }
249 
252 
253 SND_CMP_OPS (char, charNDArray)
254 SND_BOOL_OPS (char, charNDArray)
255 
256 NDND_CMP_OPS (charNDArray, charNDArray)
257 NDND_BOOL_OPS (charNDArray, charNDArray)
258 
dim_vector dimensions
Definition: Array.h:214
octave_idx_type compute_index(octave_idx_type n, const dim_vector &dims)
Definition: Array-util.cc:176
charNDArray max(int dim=-1) const
Definition: chNDArray.cc:140
#define NDS_BOOL_OPS(ND, S)
Definition: mx-op-defs.h:255
#define NDS_CMP_OPS(ND, S)
Definition: mx-op-defs.h:238
charNDArray diag(octave_idx_type k=0) const
Definition: chNDArray.cc:193
bool is_empty(void) const
Definition: Array.h:575
const octave_base_value const Array< octave_idx_type > & ra_idx
#define SND_CMP_OPS(S, ND)
Definition: mx-op-defs.h:285
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
bool isnan(double x)
Definition: lo-mappers.cc:347
for large enough k
Definition: lu.cc:606
charNDArray(void)
Definition: chNDArray.h:45
Array< T > diag(octave_idx_type k=0) const
Get the kth super or subdiagonal.
Definition: Array.cc:2529
charNDArray concat(const charNDArray &rb, const Array< octave_idx_type > &ra_idx)
Definition: chNDArray.cc:101
static void increment_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions, int start_dimension=0)
Definition: chNDArray.cc:178
char & elem(octave_idx_type n)
Definition: Array.h:482
void mx_inline_max(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:925
s
Definition: file-io.cc:2682
bool mx_inline_any(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:716
octave_idx_type rows(void) const
Definition: Array.h:401
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
Array< R > do_mm_binary_op(const Array< X > &x, const Array< Y > &y, void(*op)(size_t, R *, const X *, const Y *) throw(), void(*op1)(size_t, R *, X, const Y *) throw(), void(*op2)(size_t, R *, const X *, Y) throw(), const char *opname)
Definition: mx-inlines.cc:460
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
void mx_inline_xmin(size_t n, T *r, const T *x, const T *y)
Definition: mx-inlines.cc:349
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
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
Definition: Array.h:439
#define NDND_CMP_OPS(ND1, ND2)
Definition: mx-op-defs.h:332
#define NDND_BOOL_OPS(ND1, ND2)
Definition: mx-op-defs.h:349
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
void resize1(octave_idx_type n, const char &rfv)
double tmp
Definition: data.cc:6300
#define SND_BOOL_OPS(S, ND)
Definition: mx-op-defs.h:302
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
T::size_type strlen(const typename T::value_type *str)
Definition: oct-string.cc:75
boolNDArray all(int dim=-1) const
Definition: chNDArray.cc:89
Array< R > do_sm_binary_op(const X &x, const Array< Y > &y, void(*op)(size_t, R *, X, const Y *) throw())
Definition: mx-inlines.cc:494
#define BSXFUN_STDREL_DEFS_MXLOOP(ARRAY)
Definition: bsxfun-defs.cc:245
charNDArray & insert(const charNDArray &a, octave_idx_type r, octave_idx_type c)
Definition: chNDArray.cc:164
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:126
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:228
T::size_type numel(const T &str)
Definition: oct-string.cc:61
char element_type
Definition: Array.h:199
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
octave_idx_type nint_big(double x)
Definition: lo-mappers.cc:409
charNDArray min(int dim=-1) const
Definition: chNDArray.cc:152
boolNDArray any(int dim=-1) const
Definition: chNDArray.cc:95
b
Definition: cellfun.cc:398
Array< R > do_ms_binary_op(const Array< X > &x, const Y &y, void(*op)(size_t, R *, const X *, Y) throw())
Definition: mx-inlines.cc:484
bool mx_inline_all(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:716
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
static octave_idx_type compute_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions)
Definition: chNDArray.cc:186
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:205
void mx_inline_xmax(size_t n, T *r, const T *x, const T *y)
Definition: mx-inlines.cc:350
void mx_inline_min(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:924