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
chNDArray.cc
Go to the documentation of this file.
1 // N-D Array manipulations.
2 /*
3 
4 Copyright (C) 2003-2015 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 #ifdef 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 
50 charNDArray::charNDArray (const char *s)
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 
61 charNDArray::charNDArray (const std::string& s)
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.length (), 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.numel () == 0)
116  return *this;
117 
118  for (octave_idx_type i = 0; i < nel; i++)
119  {
120  double d = rb.elem (i);
121 
122  if (xisnan (d))
123  {
124  (*current_liboctave_error_handler)
125  ("invalid conversion from NaN to character");
126  return *this;
127  }
128  else
129  {
130  octave_idx_type ival = NINTbig (d);
131 
132  if (ival < 0 || ival > std::numeric_limits<unsigned char>::max ())
133  // FIXME: is there something better to do? Should we warn the user?
134  ival = 0;
135 
136  tmp.elem (i) = static_cast<char>(ival);
137  }
138  }
139 
140  insert (tmp, ra_idx);
141  return *this;
142 }
143 
145 charNDArray::max (int dim) const
146 {
147  return do_mx_minmax_op<char> (*this, dim, mx_inline_max);
148 }
149 
151 charNDArray::max (Array<octave_idx_type>& idx_arg, int dim) const
152 {
153  return do_mx_minmax_op<char> (*this, idx_arg, dim, mx_inline_max);
154 }
155 
157 charNDArray::min (int dim) const
158 {
159  return do_mx_minmax_op<char> (*this, dim, mx_inline_min);
160 }
161 
163 charNDArray::min (Array<octave_idx_type>& idx_arg, int dim) const
164 {
165  return do_mx_minmax_op<char> (*this, idx_arg, dim, mx_inline_min);
166 }
167 
170 {
171  Array<char>::insert (a, r, c);
172  return *this;
173 }
174 
177 {
178  Array<char>::insert (a, ra_idx);
179  return *this;
180 }
181 
182 void
184  const dim_vector& dimensions,
185  int start_dimension)
186 {
187  ::increment_index (ra_idx, dimensions, start_dimension);
188 }
189 
192  const dim_vector& dimensions)
193 {
194  return ::compute_index (ra_idx, dimensions);
195 }
196 
199 {
200  return Array<char>::diag (k);
201 }
202 
205 {
206  return Array<char>::diag (m, n);
207 }
208 
210 min (char d, const charNDArray& m)
211 {
213  charNDArray::element_type> (d, m, mx_inline_xmin);
214 }
215 
217 min (const charNDArray& m, char d)
218 {
220  char> (m, d, mx_inline_xmin);
221 }
222 
224 min (const charNDArray& a, const charNDArray& b)
225 {
227  charNDArray::element_type> (a, b, mx_inline_xmin,
229  mx_inline_xmin, "min");
230 }
231 
233 max (char d, const charNDArray& m)
234 {
236  charNDArray::element_type> (d, m, mx_inline_xmax);
237 }
238 
240 max (const charNDArray& m, char d)
241 {
243  char> (m, d, mx_inline_xmax);
244 }
245 
247 max (const charNDArray& a, const charNDArray& b)
248 {
250  charNDArray::element_type> (a, b, mx_inline_xmax,
252  mx_inline_xmax, "max");
253 }
254 
257 
258 SND_CMP_OPS (char, charNDArray)
259 SND_BOOL_OPS (char, charNDArray)
260 
261 NDND_CMP_OPS (charNDArray, charNDArray)
262 NDND_BOOL_OPS (charNDArray, charNDArray)
263 
dim_vector dimensions
Definition: Array.h:127
octave_idx_type compute_index(octave_idx_type n, const dim_vector &dims)
Definition: Array-util.cc:178
charNDArray max(int dim=-1) const
Definition: chNDArray.cc:145
#define NDS_BOOL_OPS(ND, S)
Definition: mx-op-defs.h:253
#define NDS_CMP_OPS(ND, S)
Definition: mx-op-defs.h:236
charNDArray diag(octave_idx_type k=0) const
Definition: chNDArray.cc:198
const octave_base_value const Array< octave_idx_type > & ra_idx
#define SND_CMP_OPS(S, ND)
Definition: mx-op-defs.h:283
bool xisnan(double x)
Definition: lo-mappers.cc:144
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:275
charNDArray(void)
Definition: chNDArray.h:43
Array< T > diag(octave_idx_type k=0) const
Get the kth super or subdiagonal.
Definition: Array.cc:2526
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:183
char & elem(octave_idx_type n)
Definition: Array.h:380
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
octave_idx_type rows(void) const
Definition: Array.h:313
F77_RET_T const double const double double * 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:363
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
void mx_inline_xmin(size_t n, T *r, const T *x, const T *y)
Definition: mx-inlines.cc:269
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
Definition: Array.h:337
#define NDND_CMP_OPS(ND1, ND2)
Definition: mx-op-defs.h:330
#define NDND_BOOL_OPS(ND1, ND2)
Definition: mx-op-defs.h:347
void resize1(octave_idx_type n, const char &rfv)
#define SND_BOOL_OPS(S, ND)
Definition: mx-op-defs.h:300
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:400
#define BSXFUN_STDREL_DEFS_MXLOOP(ARRAY)
Definition: bsxfun-defs.cc:244
charNDArray & insert(const charNDArray &a, octave_idx_type r, octave_idx_type c)
Definition: chNDArray.cc:169
Handles the reference counting for all the derived classes.
Definition: Array.h:45
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:233
char element_type
Definition: Array.h:118
charNDArray min(int dim=-1) const
Definition: chNDArray.cc:157
boolNDArray any(int dim=-1) const
Definition: chNDArray.cc:95
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:390
bool mx_inline_all(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:524
octave_idx_type NINTbig(double x)
Definition: lo-mappers.cc:635
static octave_idx_type compute_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions)
Definition: chNDArray.cc:191
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:210
void mx_inline_xmax(size_t n, T *r, const T *x, const T *y)
Definition: mx-inlines.cc:270
void mx_inline_min(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:718