GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
chNDArray.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2003-2023 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <string>
31 
32 #include "Array-util.h"
33 #include "chNDArray.h"
34 #include "mx-base.h"
35 #include "lo-ieee.h"
36 #include "lo-mappers.h"
37 #include "mx-op-defs.h"
38 #include "str-vec.h"
39 
40 #include "bsxfun-defs.cc"
41 
43  : Array<char> ()
44 {
45  octave_idx_type n = 1;
46 
47  resize1 (n);
48 
49  elem (0) = c;
50 }
51 
52 charNDArray::charNDArray (const char *s)
53  : Array<char> ()
54 {
55  octave_idx_type n = (s ? strlen (s) : 0);
56 
57  resize1 (n);
58 
59  for (octave_idx_type i = 0; i < n; i++)
60  elem (i) = s[i];
61 }
62 
63 charNDArray::charNDArray (const std::string& s)
64  : Array<char> ()
65 {
66  octave_idx_type n = s.length ();
67 
68  resize1 (n);
69 
70  for (octave_idx_type i = 0; i < n; i++)
71  elem (i) = s[i];
72 }
73 
74 charNDArray::charNDArray (const string_vector& s, char fill_value)
75  : Array<char> (dim_vector (s.numel (), s.max_length ()), fill_value)
76 {
77  octave_idx_type nr = rows ();
78 
79  for (octave_idx_type i = 0; i < nr; i++)
80  {
81  const std::string si = s(i);
82  octave_idx_type nc = si.length ();
83  for (octave_idx_type j = 0; j < nc; j++)
84  elem (i, j) = si[j];
85  }
86 }
87 
88 // FIXME: this is not quite the right thing.
89 
91 charNDArray::all (int dim) const
92 {
93  return do_mx_red_op<bool, char> (*this, dim, mx_inline_all);
94 }
95 
97 charNDArray::any (int dim) const
98 {
99  return do_mx_red_op<bool, char> (*this, dim, mx_inline_any);
100 }
101 
105 {
106  if (rb.numel () > 0)
107  insert (rb, ra_idx);
108  return *this;
109 }
110 
113 {
114  charNDArray tmp (rb.dims ());
115  octave_idx_type nel = rb.numel ();
116 
117  if (rb.isempty ())
118  return *this;
119 
120  for (octave_idx_type i = 0; i < nel; i++)
121  {
122  double d = rb.elem (i);
123 
124  if (octave::math::isnan (d))
125  (*current_liboctave_error_handler)
126  ("invalid conversion from NaN to character");
127 
129 
130  if (ival < 0 || ival > std::numeric_limits<unsigned char>::max ())
131  // FIXME: is there something better to do? Should we warn the user?
132  ival = 0;
133 
134  tmp.elem (i) = static_cast<char> (ival);
135  }
136 
137  insert (tmp, ra_idx);
138  return *this;
139 }
140 
142 charNDArray::max (int dim) const
143 {
144  return do_mx_minmax_op<char> (*this, dim, mx_inline_max);
145 }
146 
148 charNDArray::max (Array<octave_idx_type>& idx_arg, int dim) const
149 {
150  return do_mx_minmax_op<char> (*this, idx_arg, dim, mx_inline_max);
151 }
152 
154 charNDArray::min (int dim) const
155 {
156  return do_mx_minmax_op<char> (*this, dim, mx_inline_min);
157 }
158 
160 charNDArray::min (Array<octave_idx_type>& idx_arg, int dim) const
161 {
162  return do_mx_minmax_op<char> (*this, idx_arg, dim, mx_inline_min);
163 }
164 
167 {
168  Array<char>::insert (a, r, c);
169  return *this;
170 }
171 
174 {
176  return *this;
177 }
178 
179 void
181  const dim_vector& dimensions,
182  int start_dimension)
183 {
184  ::increment_index (ra_idx, dimensions, start_dimension);
185 }
186 
189  const dim_vector& dimensions)
190 {
191  return ::compute_index (ra_idx, dimensions);
192 }
193 
196 {
197  return Array<char>::diag (k);
198 }
199 
202 {
203  return Array<char>::diag (m, n);
204 }
205 
207 min (char d, const charNDArray& m)
208 {
211 }
212 
214 min (const charNDArray& m, char d)
215 {
217  char> (m, d, mx_inline_xmin);
218 }
219 
221 min (const charNDArray& a, const charNDArray& b)
222 {
226  mx_inline_xmin, "min");
227 }
228 
230 max (char d, const charNDArray& m)
231 {
234 }
235 
237 max (const charNDArray& m, char d)
238 {
240  char> (m, d, mx_inline_xmax);
241 }
242 
244 max (const charNDArray& a, const charNDArray& b)
245 {
249  mx_inline_xmax, "max");
250 }
251 
254 
257 
260 
octave_idx_type compute_index(octave_idx_type n, const dim_vector &dims)
Definition: Array-util.cc:177
#define BSXFUN_STDREL_DEFS_MXLOOP(ARRAY)
Definition: bsxfun-defs.cc:244
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:230
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:207
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:130
OCTARRAY_OVERRIDABLE_FUNC_API bool isempty(void) const
Size of the specified dimension.
Definition: Array.h:651
OCTARRAY_API void resize1(octave_idx_type n, const char &rfv)
Definition: Array-base.cc:919
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:414
OCTARRAY_OVERRIDABLE_FUNC_API const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
Definition: Array.h:503
OCTARRAY_API Array< T, Alloc > & insert(const Array< T, Alloc > &a, const Array< octave_idx_type > &idx)
Insert an array into another at a specified position.
Definition: Array-base.cc:1610
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type rows(void) const
Definition: Array.h:459
char element_type
Definition: Array.h:230
OCTARRAY_OVERRIDABLE_FUNC_API char & elem(octave_idx_type n)
Definition: Array.h:562
OCTARRAY_API Array< T, Alloc > diag(octave_idx_type k=0) const
Get the kth super or subdiagonal.
Definition: Array-base.cc:2543
OCTAVE_API charNDArray concat(const charNDArray &rb, const Array< octave_idx_type > &ra_idx)
Definition: chNDArray.cc:103
OCTAVE_API charNDArray & insert(const charNDArray &a, octave_idx_type r, octave_idx_type c)
Definition: chNDArray.cc:166
OCTAVE_API charNDArray max(int dim=-1) const
Definition: chNDArray.cc:142
charNDArray(void)
Definition: chNDArray.h:45
OCTAVE_API charNDArray min(int dim=-1) const
Definition: chNDArray.cc:154
OCTAVE_API boolNDArray any(int dim=-1) const
Definition: chNDArray.cc:97
static OCTAVE_API void increment_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions, int start_dimension=0)
Definition: chNDArray.cc:180
static OCTAVE_API octave_idx_type compute_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions)
Definition: chNDArray.cc:188
OCTAVE_API charNDArray diag(octave_idx_type k=0) const
Definition: chNDArray.cc:195
OCTAVE_API boolNDArray all(int dim=-1) const
Definition: chNDArray.cc:91
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_idx_type nint_big(double x)
Definition: lo-mappers.cc:184
bool isnan(bool)
Definition: lo-mappers.h:178
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
void mx_inline_xmin(std::size_t n, T *r, const T *x, const T *y)
Definition: mx-inlines.cc:336
void mx_inline_any(const T *v, bool *r, octave_idx_type l, octave_idx_type n, octave_idx_type u)
Definition: mx-inlines.cc:851
Array< R > do_ms_binary_op(const Array< X > &x, const Y &y, void(*op)(std::size_t, R *, const X *, Y))
Definition: mx-inlines.cc:522
void mx_inline_max(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:972
void mx_inline_xmax(std::size_t n, T *r, const T *x, const T *y)
Definition: mx-inlines.cc:360
void mx_inline_all(const T *v, bool *r, octave_idx_type m, octave_idx_type n)
Definition: mx-inlines.cc:817
Array< R > do_mm_binary_op(const Array< X > &x, const Array< Y > &y, void(*op)(std::size_t, R *, const X *, const Y *), void(*op1)(std::size_t, R *, X, const Y *), void(*op2)(std::size_t, R *, const X *, Y), const char *opname)
Definition: mx-inlines.cc:498
void mx_inline_min(const T *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:971
T octave_idx_type m
Definition: mx-inlines.cc:773
Array< R > do_sm_binary_op(const X &x, const Array< Y > &y, void(*op)(std::size_t, R *, X, const Y *))
Definition: mx-inlines.cc:532
octave_idx_type n
Definition: mx-inlines.cc:753
T * r
Definition: mx-inlines.cc:773
#define NDND_BOOL_OPS(ND1, ND2)
Definition: mx-op-defs.h:350
#define NDS_BOOL_OPS(ND, S)
Definition: mx-op-defs.h:256
#define NDND_CMP_OPS(ND1, ND2)
Definition: mx-op-defs.h:333
#define SND_BOOL_OPS(S, ND)
Definition: mx-op-defs.h:303
#define NDS_CMP_OPS(ND, S)
Definition: mx-op-defs.h:239
#define SND_CMP_OPS(S, ND)
Definition: mx-op-defs.h:286
T::size_type numel(const T &str)
Definition: oct-string.cc:71
T::size_type strlen(const typename T::value_type *str)
Definition: oct-string.cc:85
const octave_base_value const Array< octave_idx_type > & ra_idx