GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
chNDArray.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2003-2025 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
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
63charNDArray::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
74charNDArray::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
91charNDArray::all (int dim) const
92{
93 return do_mx_red_op<bool, char> (*this, dim, mx_inline_all);
94}
95
97charNDArray::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
128 octave_idx_type ival = octave::math::nint_big (d);
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
142charNDArray::max (int dim) const
143{
144 return do_mx_minmax_op<char> (*this, dim, mx_inline_max);
145}
146
149{
150 return do_mx_minmax_op<char> (*this, idx_arg, dim, mx_inline_max);
151}
152
154charNDArray::min (int dim) const
155{
156 return do_mx_minmax_op<char> (*this, dim, mx_inline_min);
157}
158
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
179void
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
212
219
228
235
242
251
254
257
260
#define BSXFUN_STDREL_DEFS_MXLOOP(ARRAY)
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
const dim_vector & dims() const
Return a const-reference so that dims ()(i) works efficiently.
Definition Array.h:507
void resize1(octave_idx_type n, const char &rfv)
char & elem(octave_idx_type n)
Definition Array.h:563
octave_idx_type rows() const
Definition Array.h:463
Array< T, Alloc > & insert(const Array< T, Alloc > &a, const Array< octave_idx_type > &idx)
Insert an array into another at a specified position.
bool isempty() const
Size of the specified dimension.
Definition Array.h:652
char element_type
Definition Array.h:234
Array< T, Alloc > diag(octave_idx_type k=0) const
Get the kth super or subdiagonal.
octave_idx_type numel() const
Number of elements in the array.
Definition Array.h:418
charNDArray concat(const charNDArray &rb, const Array< octave_idx_type > &ra_idx)
Definition chNDArray.cc:103
charNDArray & insert(const charNDArray &a, octave_idx_type r, octave_idx_type c)
Definition chNDArray.cc:166
charNDArray max(int dim=-1) const
Definition chNDArray.cc:142
charNDArray min(int dim=-1) const
Definition chNDArray.cc:154
boolNDArray any(int dim=-1) const
Definition chNDArray.cc:97
static void increment_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions, int start_dimension=0)
Definition chNDArray.cc:180
static octave_idx_type compute_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions)
Definition chNDArray.cc:188
charNDArray diag(octave_idx_type k=0) const
Definition chNDArray.cc:195
boolNDArray all(int dim=-1) const
Definition chNDArray.cc:91
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
bool mx_inline_any(const T *v, octave_idx_type n)
void mx_inline_xmin(std::size_t n, T *r, const T *x, const T *y)
Array< R > do_ms_binary_op(const Array< X > &x, const Y &y, void(*op)(std::size_t, R *, const X *, Y))
void mx_inline_max(const T *v, T *r, octave_idx_type n)
void mx_inline_xmax(std::size_t n, T *r, const T *x, const T *y)
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)
Array< R > do_sm_binary_op(const X &x, const Array< Y > &y, void(*op)(std::size_t, R *, X, const Y *))
bool mx_inline_all(const T *v, octave_idx_type n)
void mx_inline_min(const T *v, T *r, octave_idx_type n)
#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:74
T::size_type strlen(const typename T::value_type *str)
Definition oct-string.cc:88
const octave_base_value const Array< octave_idx_type > & ra_idx