GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
lo-array-errwarn.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2016-2024 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 <cinttypes>
31 #include <cmath>
32 
33 #include <limits>
34 #include <sstream>
35 
36 #include "lo-array-errwarn.h"
37 #include "lo-error.h"
38 
40 
41 // Text constants used to shorten code below.
42 
43 static const char *error_id_nonconformant_args
44  = "Octave:nonconformant-args";
45 
46 static const char *error_id_index_out_of_bounds
47  = "Octave:index-out-of-bounds";
48 
49 static const char *error_id_invalid_index = "Octave:invalid-index";
50 
51 static const char *warning_id_nearly_singular_matrix
52  = "Octave:nearly-singular-matrix";
53 
54 static const char *warning_id_singular_matrix = "Octave:singular-matrix";
55 
56 void
58 {
59  (*current_liboctave_error_handler)
60  ("invalid conversion from NaN to logical");
61 }
62 
63 void
65 {
66  (*current_liboctave_error_handler)
67  ("invalid conversion from NaN to character");
68 }
69 
70 void
71 err_nonconformant (const char *op,
72  octave_idx_type op1_len, octave_idx_type op2_len)
73 {
74  const char *err_id = error_id_nonconformant_args;
75 
76  (*current_liboctave_error_with_id_handler)
77  (err_id, "%s: nonconformant arguments (op1 len: %" OCTAVE_IDX_TYPE_FORMAT
78  ", op2 len: % " OCTAVE_IDX_TYPE_FORMAT ")",
79  op, op1_len, op2_len);
80 }
81 
82 void
83 err_nonconformant (const char *op,
84  octave_idx_type op1_nr, octave_idx_type op1_nc,
85  octave_idx_type op2_nr, octave_idx_type op2_nc)
86 {
87  const char *err_id = error_id_nonconformant_args;
88 
89  (*current_liboctave_error_with_id_handler)
90  (err_id, "%s: nonconformant arguments "
91  "(op1 is %" OCTAVE_IDX_TYPE_FORMAT "x%" OCTAVE_IDX_TYPE_FORMAT ", "
92  "op2 is %" OCTAVE_IDX_TYPE_FORMAT"x%" OCTAVE_IDX_TYPE_FORMAT ")",
93  op, op1_nr, op1_nc, op2_nr, op2_nc);
94 }
95 
96 void
97 err_nonconformant (const char *op,
98  const dim_vector& op1_dims, const dim_vector& op2_dims)
99 {
100  const char *err_id = error_id_nonconformant_args;
101 
102  std::string op1_dims_str = op1_dims.str ();
103  std::string op2_dims_str = op2_dims.str ();
104 
105  (*current_liboctave_error_with_id_handler)
106  (err_id, "%s: nonconformant arguments (op1 is %s, op2 is %s)",
107  op, op1_dims_str.c_str (), op2_dims_str.c_str ());
108 }
109 
110 void
112  octave_idx_type ext)
113 {
114  const char *err_id = error_id_index_out_of_bounds;
115 
116  (*current_liboctave_error_with_id_handler)
117  (err_id, "A(%s) = []: index out of bounds: value %" OCTAVE_IDX_TYPE_FORMAT
118  " out of bound %" OCTAVE_IDX_TYPE_FORMAT,
119  is1d ? "I" : "..,I,..", idx, ext);
120 }
121 
122 // Show the expression that caused the error, e.g., "A(-1,_)",
123 // "A(0+1i)", "A(_,3)". Show how many indices come before/after the
124 // offending one, e.g., (<error>), (<error>,_), or (_,<error>,...[x5]...)
125 
126 std::string
128 {
129  std::ostringstream buf;
130 
131  if (m_var.empty () || m_var == "<unknown>")
132  buf << "index ";
133  else
134  buf << m_var;
135 
136  bool show_parens = m_dim > 0;
137 
138  if (show_parens)
139  {
140  if (m_dim < 5)
141  {
142  buf << '(';
143 
144  for (octave_idx_type i = 1; i < m_dim; i++)
145  buf << "_,";
146  }
147  else
148  buf << "(...[x" << m_dim - 1 << "]...";
149  }
150 
151  buf << m_index;
152 
153  if (show_parens)
154  {
155  if (m_nd - m_dim < 5)
156  {
157  for (octave_idx_type i = 0; i < m_nd - m_dim; i++)
158  buf << ",_";
159 
160  if (m_nd >= m_dim)
161  buf << ')';
162  }
163  else
164  buf << "...[x" << m_nd - m_dim << "]...)";
165  }
166 
167  return buf.str ();
168 }
169 
170 class invalid_index : public index_exception
171 {
172 public:
173 
174  invalid_index (const std::string& value, octave_idx_type ndim,
175  octave_idx_type dimen)
176  : index_exception (value, ndim, dimen)
177  {
178  // Virtual, but the one we want to call is defined in this class.
179  update_message ();
180  }
181 
182  OCTAVE_DEFAULT_COPY_MOVE (invalid_index)
183 
184  void update_message ()
185  {
186  static std::string exp
187  = std::to_string (std::numeric_limits<octave_idx_type>::digits);
188 
189  set_message (expression ()
190  + ": subscripts must be either integers 1 to (2^" + exp
191  + ")-1 or logicals");
192  }
193 
194  // ID of error to throw
195  const char * err_id () const
196  {
197  return error_id_invalid_index;
198  }
199 
200  index_exception * dup ()
201  {
202  invalid_index *retval = new invalid_index {*this};
203  retval->set_identifier (retval->err_id ());
204  return retval;
205  }
206 };
207 
208 // Complain if an index is negative, fractional, or too big.
209 
210 void
211 err_invalid_index (const std::string& idx, octave_idx_type nd,
212  octave_idx_type dim, const std::string&)
213 {
214  invalid_index e (idx, nd, dim);
215 
216  throw e;
217 }
218 
219 void
221  octave_idx_type dim, const std::string& var)
222 {
223  err_invalid_index (std::to_string (n + 1), nd, dim, var);
224 }
225 
226 void
228  const std::string& var)
229 {
230  std::ostringstream buf;
231  buf << n + 1;
232 
233  if (! std::isnan (n))
234  {
235  // if n not an integer, but would be printed as one, show diff
236  double nearest = std::floor (n + 1.5);
237  if (n + 1 != nearest && (buf.str ().find ('.') == std::string::npos))
238  buf << std::showpos << (n + 1 - nearest);
239  }
240 
241  err_invalid_index (buf.str (), nd, dim, var);
242 }
243 
244 // Complain for read access beyond the bounds of an array.
245 
246 class out_of_range : public index_exception
247 {
248 public:
249 
250  out_of_range (const std::string& value, octave_idx_type nd,
252  const dim_vector& size)
253  : index_exception (value, nd, dim), m_size (size), m_extent (ext)
254  {
255  // Virtual, but the one we want to call is defined in this class.
256  update_message ();
257  }
258 
259  OCTAVE_DEFAULT_COPY_MOVE (out_of_range)
260 
261  void update_message ()
262  {
263  set_message (expression () + ": out of bound "
264  + std::to_string (m_extent)
265  + " (dimensions are " + m_size.str ('x') + ")");
266  }
267 
268  // ID of error to throw.
269  const char * err_id () const
270  {
271  return error_id_index_out_of_bounds;
272  }
273 
274  index_exception * dup ()
275  {
276  out_of_range *retval = new out_of_range {*this};
277  retval->set_identifier (retval->err_id ());
278  return retval;
279  }
280 
281 private:
282 
283  // Dimension of object being accessed.
284  dim_vector m_size;
285 
286  // Length of dimension being accessed.
287  octave_idx_type m_extent;
288 };
289 
290 // Complain of an index that is out of range
291 void
293  octave_idx_type ext, const dim_vector& dv)
294 {
295  throw out_of_range (std::to_string (idx), nd, dim, ext, dv);
296 }
297 
298 void
300 {
301  (*current_liboctave_error_with_id_handler)
302  ("Octave:invalid-resize",
303  "Invalid resizing operation or ambiguous assignment to an out-of-bounds array element");
304 }
305 
306 void
307 warn_singular_matrix (double rcond)
308 {
309  if (rcond == 0.0)
310  {
311  (*current_liboctave_warning_with_id_handler)
312  (warning_id_singular_matrix,
313  "matrix singular to machine precision");
314  }
315  else
316  {
317  (*current_liboctave_warning_with_id_handler)
318  (warning_id_nearly_singular_matrix,
319  "matrix singular to machine precision, rcond = %g", rcond);
320  }
321 }
322 
323 OCTAVE_END_NAMESPACE(octave)
324 
325 /* Tests in test/index.tst */
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
std::string str(char sep='x') const
Definition: dim-vector.cc:68
std::string expression() const
octave_idx_type m_nd
virtual const char * err_id() const =0
octave_idx_type m_dim
virtual index_exception * dup()=0
virtual void update_message()
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void err_invalid_resize()
void err_nonconformant(const char *op, octave_idx_type op1_len, octave_idx_type op2_len)
void err_nan_to_logical_conversion()
void err_invalid_index(const std::string &idx, octave_idx_type nd, octave_idx_type dim, const std::string &)
void err_index_out_of_range(int nd, int dim, octave_idx_type idx, octave_idx_type ext, const dim_vector &dv)
void err_nan_to_character_conversion()
void err_del_index_out_of_range(bool is1d, octave_idx_type idx, octave_idx_type ext)
void warn_singular_matrix(double rcond)
bool isnan(bool)
Definition: lo-mappers.h:178
std::complex< T > floor(const std::complex< T > &x)
Definition: lo-mappers.h:130
octave_idx_type n
Definition: mx-inlines.cc:761