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
ov-ch-mat.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2017 John W. Eaton
4 Copyright (C) 2009-2010 VZLU Prague
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 <cctype>
29 #include <iostream>
30 
31 #include "dNDArray.h"
32 #include "fNDArray.h"
33 #include "int8NDArray.h"
34 #include "int16NDArray.h"
35 #include "int32NDArray.h"
36 #include "int64NDArray.h"
37 #include "uint8NDArray.h"
38 #include "uint16NDArray.h"
39 #include "uint32NDArray.h"
40 #include "uint64NDArray.h"
41 
42 #include "lo-ieee.h"
43 #include "mx-base.h"
44 
45 #include "mxarray.h"
46 #include "ov-base.h"
47 #include "ov-base-mat.h"
48 #include "ov-base-mat.cc"
49 #include "ov-ch-mat.h"
50 #include "errwarn.h"
51 #include "pr-output.h"
52 
53 template class octave_base_matrix<charNDArray>;
54 
56 octave_char_matrix::index_vector (bool /* require_integers */) const
57 {
58  const char *p = matrix.data ();
59  if (numel () == 1 && *p == ':')
60  return idx_vector (':');
61  else
62  return idx_vector (array_value (true));
63 }
64 
65 double
67 {
68  if (rows () == 0 || columns () == 0)
69  err_invalid_conversion ("character matrix", "real scalar");
70 
71  warn_implicit_conversion ("Octave:array-to-scalar",
72  "character matrix", "real scalar");
73 
74  return static_cast<unsigned char> (matrix(0, 0));
75 }
76 
77 float
79 {
80  if (rows () == 0 && columns () == 0)
81  err_invalid_conversion ("character matrix", "real scalar");
82 
83  warn_implicit_conversion ("Octave:array-to-scalar",
84  "character matrix", "real scalar");
85 
86  return static_cast<unsigned char> (matrix(0, 0));
87 }
88 
91 {
92  octave_int64 retval = 0;
93 
94  if (rows () == 0 || columns () == 0)
95  err_invalid_conversion ("character matrix", "int64 scalar");
96 
97  warn_implicit_conversion ("Octave:array-to-scalar",
98  "character matrix", "int64 scalar");
99 
100  retval = octave_int64 (matrix(0, 0));
101 
102  return retval;
103 }
104 
107 {
108  octave_uint64 retval = 0;
109 
110  if (rows () == 0 || columns () == 0)
111  err_invalid_conversion ("character matrix", "uint64 scalar");
112 
113  warn_implicit_conversion ("Octave:array-to-scalar",
114  "character matrix", "uint64 scalar");
115 
116  retval = octave_uint64 (matrix(0, 0));
117 
118  return retval;
119 }
120 
121 Complex
123 {
124  if (rows () == 0 && columns () == 0)
125  err_invalid_conversion ("character matrix", "complex scalar");
126 
127  warn_implicit_conversion ("Octave:array-to-scalar",
128  "character matrix", "complex scalar");
129 
130  return Complex (static_cast<unsigned char> (matrix(0, 0)), 0);
131 }
132 
135 {
136  float tmp = lo_ieee_float_nan_value ();
137 
138  FloatComplex retval (tmp, tmp);
139 
140  if (rows () == 0 || columns () == 0)
141  err_invalid_conversion ("character matrix", "complex scalar");
142 
143  warn_implicit_conversion ("Octave:array-to-scalar",
144  "character matrix", "complex scalar");
145 
146  retval = static_cast<unsigned char> (matrix(0, 0));
147 
148  return retval;
149 }
150 
153 {
154  return NDArray (matrix);
155 }
156 
159 {
160  return FloatNDArray (matrix);
161 }
162 
165 {
166  return int8NDArray (matrix);
167 }
168 
171 {
172  return int16NDArray (matrix);
173 }
174 
177 {
178  return int32NDArray (matrix);
179 }
180 
183 {
184  return int64NDArray (matrix);
185 }
186 
189 {
190  return uint8NDArray (matrix);
191 }
192 
195 {
196  return uint16NDArray (matrix);
197 }
198 
201 {
202  return uint32NDArray (matrix);
203 }
204 
207 {
208  return uint64NDArray (matrix);
209 }
210 
211 void
213  bool pr_as_read_syntax) const
214 {
215  octave_print_internal (os, matrix, pr_as_read_syntax,
217 }
218 
219 mxArray *
221 {
223 
224  mxChar *pr = static_cast<mxChar *> (retval->get_data ());
225 
226  mwSize nel = numel ();
227 
228  const char *p = matrix.data ();
229 
230  for (mwIndex i = 0; i < nel; i++)
231  pr[i] = p[i];
232 
233  return retval;
234 }
235 
236 // The C++ standard guarantees cctype defines functions, not macros (and
237 // hence macros *CAN'T* be defined if only cctype is included) so
238 // there's no need to fuck around. The exceptions are isascii and
239 // toascii, which are not C++. Oddly enough, all those character
240 // functions are int (*) (int), even in C++. Wicked!
241 static inline int xisascii (int c)
242 {
243 #if defined (HAVE_ISASCII)
244  return isascii (c);
245 #else
246  return (c >= 0x00 && c <= 0x7f);
247 #endif
248 }
249 
250 static inline int xtoascii (int c)
251 {
252 #if defined (HAVE_TOASCII)
253  return toascii (c);
254 #else
255  return (c & 0x7F);
256 #endif
257 }
258 
261 {
263 
264  switch (umap)
265  {
266 #define STRING_MAPPER(UMAP,FCN,TYPE) \
267  case umap_ ## UMAP: \
268  return octave_value (matrix.map<TYPE, int (&) (int)> (FCN))
269 
270  STRING_MAPPER (xisalnum, std::isalnum, bool);
271  STRING_MAPPER (xisalpha, std::isalpha, bool);
273  STRING_MAPPER (xiscntrl, std::iscntrl, bool);
274  STRING_MAPPER (xisdigit, std::isdigit, bool);
275  STRING_MAPPER (xisgraph, std::isgraph, bool);
276  STRING_MAPPER (xislower, std::islower, bool);
277  STRING_MAPPER (xisprint, std::isprint, bool);
278  STRING_MAPPER (xispunct, std::ispunct, bool);
279  STRING_MAPPER (xisspace, std::isspace, bool);
280  STRING_MAPPER (xisupper, std::isupper, bool);
281  STRING_MAPPER (xisxdigit, std::isxdigit, bool);
282  STRING_MAPPER (xtoascii, xtoascii, double);
283  STRING_MAPPER (xtolower, std::tolower, char);
284  STRING_MAPPER (xtoupper, std::toupper, char);
285 
286  // For Matlab compatibility, these should work on ASCII values
287  // without error or warning.
288  case umap_abs:
289  case umap_ceil:
290  case umap_fix:
291  case umap_floor:
292  case umap_imag:
293  case umap_isinf:
294  case umap_isnan:
295  case umap_real:
296  case umap_round:
297  {
298  octave_matrix m (array_value (true));
299  return m.map (umap);
300  }
301 
302  default:
303  error ("%s: argument must be numeric", get_umap_name (umap));
304  break;
305  }
306 
307  return retval;
308 }
octave_int< uint64_t > octave_uint64
intNDArray< octave_int64 > int64NDArray
Definition: int64NDArray.h:33
octave_uint64 uint64_scalar_value() const
Definition: ov-ch-mat.cc:106
octave_value as_uint32(void) const
Definition: ov-ch-mat.cc:200
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
Definition: ov-ch-mat.cc:212
octave_idx_type columns(void) const
Definition: ov-base.h:319
static int xtoascii(int c)
Definition: ov-ch-mat.cc:250
intNDArray< octave_uint32 > uint32NDArray
Definition: uint32NDArray.h:33
intNDArray< octave_uint8 > uint8NDArray
Definition: uint8NDArray.h:33
octave_value as_int8(void) const
Definition: ov-ch-mat.cc:164
intNDArray< octave_uint16 > uint16NDArray
Definition: uint16NDArray.h:33
FloatComplex float_complex_value(bool=false) const
Definition: ov-ch-mat.cc:134
octave_value as_single(void) const
Definition: ov-ch-mat.cc:158
octave_idx_type numel(void) const
Definition: ov-base-mat.h:112
#define STRING_MAPPER(UMAP, FCN, TYPE)
void error(const char *fmt,...)
Definition: error.cc:570
void * get_data(void) const
Definition: mxarray.h:449
octave_value as_int16(void) const
Definition: ov-ch-mat.cc:170
intNDArray< octave_int16 > int16NDArray
Definition: int16NDArray.h:33
static const char * get_umap_name(unary_mapper_t)
Definition: ov-base.cc:1077
octave_value map(unary_mapper_t umap) const
Definition: ov-re-mat.cc:899
intNDArray< octave_int8 > int8NDArray
Definition: int8NDArray.h:33
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
mxArray * as_mxArray(void) const
Definition: ov-ch-mat.cc:220
const T * data(void) const
Definition: Array.h:582
octave_value as_double(void) const
Definition: ov-ch-mat.cc:152
octave_value as_uint64(void) const
Definition: ov-ch-mat.cc:206
double tmp
Definition: data.cc:6300
char mxChar
Definition: mxarray.h:83
octave_value retval
Definition: data.cc:6294
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
float lo_ieee_float_nan_value(void)
Definition: lo-ieee.cc:202
void mxArray
Definition: mex.h:55
octave_int64 int64_scalar_value() const
Definition: ov-ch-mat.cc:90
float float_value(bool=false) const
Definition: ov-ch-mat.cc:78
dim_vector dims(void) const
Definition: ov-base-mat.h:110
intNDArray< octave_int32 > int32NDArray
Definition: int32NDArray.h:33
void err_invalid_conversion(const std::string &from, const std::string &to)
Definition: errwarn.cc:68
octave_value as_uint16(void) const
Definition: ov-ch-mat.cc:194
octave_value map(unary_mapper_t umap) const
Definition: ov-ch-mat.cc:260
octave_value as_uint8(void) const
Definition: ov-ch-mat.cc:188
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
octave_value as_int64(void) const
Definition: ov-ch-mat.cc:182
int current_print_indent_level(void) const
Definition: ov-base.h:830
p
Definition: lu.cc:138
void octave_print_internal(std::ostream &, char, bool)
Definition: pr-output.cc:1722
void warn_implicit_conversion(const char *id, const char *from, const char *to)
Definition: errwarn.cc:332
octave_int< int64_t > octave_int64
NDArray array_value(bool=false) const
Definition: ov-ch-mat.h:120
std::complex< float > FloatComplex
Definition: oct-cmplx.h:32
Complex complex_value(bool=false) const
Definition: ov-ch-mat.cc:122
double double_value(bool=false) const
Definition: ov-ch-mat.cc:66
Definition: mxarray.h:76
std::complex< double > Complex
Definition: oct-cmplx.h:31
idx_vector index_vector(bool require_integers=false) const
Definition: ov-ch-mat.cc:56
octave_value as_int32(void) const
Definition: ov-ch-mat.cc:176
octave_idx_type rows(void) const
Definition: ov-base.h:312
static int xisascii(int c)
Definition: ov-ch-mat.cc:241
intNDArray< octave_uint64 > uint64NDArray
Definition: uint64NDArray.h:33