GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-ch-mat.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 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
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License 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 <https://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 
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 
252 {
254 
255  switch (umap)
256  {
257 #define STRING_MAPPER(UMAP,FCN,TYPE) \
258  case umap_ ## UMAP: \
259  return octave_value (matrix.map<TYPE, int (&) (int)> (FCN))
260 
261  STRING_MAPPER (xisalnum, std::isalnum, bool);
262  STRING_MAPPER (xisalpha, std::isalpha, bool);
264  STRING_MAPPER (xiscntrl, std::iscntrl, bool);
265  STRING_MAPPER (xisdigit, std::isdigit, bool);
266  STRING_MAPPER (xisgraph, std::isgraph, bool);
267  STRING_MAPPER (xislower, std::islower, bool);
268  STRING_MAPPER (xisprint, std::isprint, bool);
269  STRING_MAPPER (xispunct, std::ispunct, bool);
270  STRING_MAPPER (xisspace, std::isspace, bool);
271  STRING_MAPPER (xisupper, std::isupper, bool);
272  STRING_MAPPER (xisxdigit, std::isxdigit, bool);
273  STRING_MAPPER (xtolower, std::tolower, char);
274  STRING_MAPPER (xtoupper, std::toupper, char);
275 
276  // For Matlab compatibility, these should work on ASCII values
277  // without error or warning.
278  case umap_abs:
279  case umap_ceil:
280  case umap_fix:
281  case umap_floor:
282  case umap_imag:
283  case umap_isinf:
284  case umap_isnan:
285  case umap_real:
286  case umap_round:
287  {
288  octave_matrix m (array_value (true));
289  return m.map (umap);
290  }
291 
292  default:
293  error ("%s: argument must be numeric", get_umap_name (umap));
294  break;
295  }
296 
297  return retval;
298 }
octave_int64 int64_scalar_value() const
Definition: ov-ch-mat.cc:90
octave_int< uint64_t > octave_uint64
octave_value as_double(void) const
Definition: ov-ch-mat.cc:152
octave_value as_uint16(void) const
Definition: ov-ch-mat.cc:194
intNDArray< octave_int64 > int64NDArray
Definition: int64NDArray.h:33
OCTAVE_IDX_TYPE mwSize
Definition: mxarray.in.h:93
const T * data(void) const
Definition: Array.h:582
idx_vector index_vector(bool require_integers=false) const
Definition: ov-ch-mat.cc:56
octave_value as_uint8(void) const
Definition: ov-ch-mat.cc:188
int current_print_indent_level(void) const
Definition: ov-base.h:849
void print_raw(std::ostream &os, bool pr_as_read_syntax=false) const
Definition: ov-ch-mat.cc:212
octave_value as_uint64(void) const
Definition: ov-ch-mat.cc:206
intNDArray< octave_uint32 > uint32NDArray
Definition: uint32NDArray.h:33
intNDArray< octave_uint8 > uint8NDArray
Definition: uint8NDArray.h:33
intNDArray< octave_uint16 > uint16NDArray
Definition: uint16NDArray.h:33
#define STRING_MAPPER(UMAP, FCN, TYPE)
void error(const char *fmt,...)
Definition: error.cc:578
octave_value as_uint32(void) const
Definition: ov-ch-mat.cc:200
intNDArray< octave_int16 > int16NDArray
Definition: int16NDArray.h:33
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
Definition: file-io.cc:587
static const char * get_umap_name(unary_mapper_t)
Definition: ov-base.cc:1024
mxArray * as_mxArray(void) const
Definition: ov-ch-mat.cc:220
octave_value as_int16(void) const
Definition: ov-ch-mat.cc:170
void octave_print_internal(std::ostream &os, const float_display_format &fmt, bool d, bool pr_as_read_syntax)
Definition: pr-output.cc:1780
octave_uint64 uint64_scalar_value() const
Definition: ov-ch-mat.cc:106
FloatComplex float_complex_value(bool=false) const
Definition: ov-ch-mat.cc:134
intNDArray< octave_int8 > int8NDArray
Definition: int8NDArray.h:33
octave_idx_type rows(void) const
Definition: ov-base.h:316
octave_value as_int8(void) const
Definition: ov-ch-mat.cc:164
double tmp
Definition: data.cc:6252
Complex complex_value(bool=false) const
Definition: ov-ch-mat.cc:122
octave_value retval
Definition: data.cc:6246
octave_value map(unary_mapper_t umap) const
Definition: ov-ch-mat.cc:251
double double_value(bool=false) const
Definition: ov-ch-mat.cc:66
octave_value map(unary_mapper_t umap) const
Definition: ov-re-mat.cc:900
octave_value as_int32(void) const
Definition: ov-ch-mat.cc:176
float lo_ieee_float_nan_value(void)
Definition: lo-ieee.cc:123
void mxArray
Definition: mex.h:55
octave_value as_single(void) const
Definition: ov-ch-mat.cc:158
intNDArray< octave_int32 > int32NDArray
Definition: int32NDArray.h:33
void err_invalid_conversion(const std::string &from, const std::string &to)
Definition: errwarn.cc:68
NDArray array_value(bool=false) const
Definition: ov-ch-mat.h:118
char mxChar
Definition: mxarray.in.h:83
p
Definition: lu.cc:138
octave_value as_int64(void) const
Definition: ov-ch-mat.cc:182
dim_vector dims(void) const
Definition: ov-base-mat.h:105
void warn_implicit_conversion(const char *id, const char *from, const char *to)
Definition: errwarn.cc:338
octave_int< int64_t > octave_int64
octave_idx_type numel(void) const
Definition: ov-base-mat.h:107
for i
Definition: data.cc:5264
octave_idx_type columns(void) const
Definition: ov-base.h:323
std::complex< float > FloatComplex
Definition: oct-cmplx.h:32
std::complex< double > Complex
Definition: oct-cmplx.h:31
OCTAVE_IDX_TYPE mwIndex
Definition: mxarray.in.h:94
float float_value(bool=false) const
Definition: ov-ch-mat.cc:78
octave::stream os
Definition: file-io.cc:627
static int xisascii(int c)
Definition: ov-ch-mat.cc:241
intNDArray< octave_uint64 > uint64NDArray
Definition: uint64NDArray.h:33