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
lo-array-errwarn.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2016-2017 Rik Wehbring
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <sstream>
28 
29 #include "lo-array-errwarn.h"
30 #include "lo-error.h"
31 
32 // Text constants used to shorten code below.
33 static const char *error_id_nonconformant_args = "Octave:nonconformant-args";
34 
35 static const char *error_id_index_out_of_bounds = "Octave:index-out-of-bounds";
36 
37 static const char *error_id_invalid_index = "Octave:invalid-index";
38 
40  "Octave:nearly-singular-matrix";
41 
42 static const char *warning_id_singular_matrix = "Octave:singular-matrix";
43 
44 namespace octave
45 {
46  void
48  {
49  (*current_liboctave_error_handler)
50  ("invalid conversion from NaN to logical");
51  }
52 
53  void
55  {
56  (*current_liboctave_error_handler)
57  ("invalid conversion from NaN to character");
58  }
59 
60  void
61  err_nonconformant (const char *op,
62  octave_idx_type op1_len, octave_idx_type op2_len)
63  {
64  const char *err_id = error_id_nonconformant_args;
65 
66  (*current_liboctave_error_with_id_handler)
67  (err_id, "%s: nonconformant arguments (op1 len: %d, op2 len: %d)",
68  op, op1_len, op2_len);
69  }
70 
71  void
72  err_nonconformant (const char *op,
73  octave_idx_type op1_nr, octave_idx_type op1_nc,
74  octave_idx_type op2_nr, octave_idx_type op2_nc)
75  {
76  const char *err_id = error_id_nonconformant_args;
77 
78  (*current_liboctave_error_with_id_handler)
79  (err_id, "%s: nonconformant arguments (op1 is %dx%d, op2 is %dx%d)",
80  op, op1_nr, op1_nc, op2_nr, op2_nc);
81  }
82 
83  void
84  err_nonconformant (const char *op,
85  const dim_vector& op1_dims, const dim_vector& op2_dims)
86  {
87  const char *err_id = error_id_nonconformant_args;
88 
89  std::string op1_dims_str = op1_dims.str ();
90  std::string op2_dims_str = op2_dims.str ();
91 
92  (*current_liboctave_error_with_id_handler)
93  (err_id, "%s: nonconformant arguments (op1 is %s, op2 is %s)",
94  op, op1_dims_str.c_str (), op2_dims_str.c_str ());
95  }
96 
97  void
99  octave_idx_type ext)
100  {
101  const char *err_id = error_id_index_out_of_bounds;
102 
103  (*current_liboctave_error_with_id_handler)
104  (err_id, "A(%s) = []: index out of bounds: value %d out of bound %d",
105  is1d ? "I" : "..,I,..", idx, ext);
106  }
107 
108  // Common procedures of base class index_exception, thrown whenever an
109  // object is indexed incorrectly, such as by an index that is out of
110  // range, negative, fractional, complex, or of a non-numeric type.
111 
114  {
115  std::string msg = expression () + ": " + details ();
116  return msg.c_str ();
117  }
118 
119  // Show the expression that caused the error, e.g., "A(-1,_)",
120  // "A(0+1i)", "A(_,3)". Show how many indices come before/after the
121  // offending one, e.g., (<error>), (<error>,_), or (_,<error>,...[x5]...)
122 
125  {
126  std::ostringstream buf;
127 
128  if (var.empty () || var == "<unknown>")
129  buf << "index ";
130  else
131  buf << var;
132 
133  bool show_parens = dim > 0;
134 
135  if (show_parens)
136  {
137  if (dim < 5)
138  {
139  buf << "(";
140 
141  for (octave_idx_type i = 1; i < dim; i++)
142  buf << "_,";
143  }
144  else
145  buf << "(...[x" << dim - 1 << "]...";
146  }
147 
148  buf << idx ();
149 
150  if (show_parens)
151  {
152  if (nd - dim < 5)
153  {
154  for (octave_idx_type i = 0; i < nd - dim; i++)
155  buf << ",_";
156 
157  if (nd >= dim)
158  buf << ")";
159  }
160  else
161  buf << "...[x" << nd - dim << "]...)";
162  }
163 
164  return buf.str ();
165  }
166 
168  {
169  public:
170 
172  octave_idx_type dimen)
173  : index_exception (value, ndim, dimen)
174  { }
175 
176  std::string details (void) const
177  {
178 #if defined (OCTAVE_ENABLE_64)
179  return "subscripts must be either integers 1 to (2^63)-1 or logicals";
180 #else
181  return "subscripts must be either integers 1 to (2^31)-1 or logicals";
182 #endif
183  }
184 
185  // ID of error to throw
186  const char *err_id (void) const
187  {
188  return error_id_invalid_index;
189  }
190  };
191 
192  // Complain if an index is negative, fractional, or too big.
193 
194  void
196  octave_idx_type dim, const std::string&)
197  {
198  invalid_index e (idx, nd, dim);
199 
200  throw e;
201  }
202 
203  void
205  octave_idx_type dim, const std::string& var)
206  {
207  std::ostringstream buf;
208  buf << n + 1;
209  err_invalid_index (buf.str (), nd, dim, var);
210  }
211 
212  void
214  const std::string& var)
215  {
216  std::ostringstream buf;
217  buf << n + 1;
218  err_invalid_index (buf.str (), nd, dim, var);
219  }
220 
221  // Complain for read access beyond the bounds of an array.
222 
224  {
225  public:
226 
228  octave_idx_type dim_in)
229  : index_exception (value, nd_in, dim_in), extent (0)
230  { }
231 
232  std::string details (void) const
233  {
234  std::string expl;
235 
236  if (nd >= size.ndims ()) // if not an index slice
237  {
238  if (var != "")
239  expl = "but " + var + " has size ";
240  else
241  expl = "but object has size ";
242 
243  expl = expl + size.str ('x');
244  }
245  else
246  {
247  std::ostringstream buf;
248  buf << extent;
249  expl = "out of bound " + buf.str ();
250  }
251 
252  return expl;
253  }
254 
255  // ID of error to throw.
256  const char *err_id (void) const
257  {
259  }
260 
261  void set_size (const dim_vector& size_in) { size = size_in; }
262 
263  void set_extent (octave_idx_type ext) { extent = ext; }
264 
265  private:
266 
267  // Dimension of object being accessed.
269 
270  // Length of dimension being accessed.
272  };
273 
274  // Complain of an index that is out of range, but we don't know matrix size
275  void
277  octave_idx_type ext)
278  {
279  std::ostringstream buf;
280  buf << idx;
281  out_of_range e (buf.str (), nd, dim);
282 
283  e.set_extent (ext);
284  // ??? Make details method give extent not size.
285  e.set_size (dim_vector (1, 1, 1, 1, 1, 1,1));
286 
287  throw e;
288  }
289 
290  // Complain of an index that is out of range
291  void
293  octave_idx_type ext, const dim_vector& d)
294  {
295  std::ostringstream buf;
296  buf << idx;
297  out_of_range e (buf.str (), nd, dim);
298 
299  e.set_extent (ext);
300  e.set_size (d);
301 
302  throw e;
303  }
304 
305  void
307  {
308  (*current_liboctave_error_with_id_handler)
309  ("Octave:invalid-resize",
310  "Invalid resizing operation or ambiguous assignment to an out-of-bounds array element");
311  }
312 
313  void
314  warn_singular_matrix (double rcond)
315  {
316  if (rcond == 0.0)
317  {
318  (*current_liboctave_warning_with_id_handler)
320  "matrix singular to machine precision");
321  }
322  else
323  {
324  (*current_liboctave_warning_with_id_handler)
326  "matrix singular to machine precision, rcond = %g", rcond);
327  }
328  }
329 }
330 
331 /* Tests in test/index.tst */
Octave interface to the compression and uncompression libraries.
Definition: aepbalance.cc:47
out_of_range(const std::string &value, octave_idx_type nd_in, octave_idx_type dim_in)
std::string str(char sep= 'x') const
Definition: dim-vector.cc:73
static const char * warning_id_nearly_singular_matrix
static const char * error_id_nonconformant_args
in that an updated permutation matrix is returned Note that if var
Definition: lu.cc:606
void set_size(const dim_vector &size_in)
void err_index_out_of_range(int nd, int dim, octave_idx_type idx, octave_idx_type ext)
void err_invalid_index(const std::string &idx, octave_idx_type nd, octave_idx_type dim, const std::string &)
std::string idx(void) const
void err_nan_to_logical_conversion(void)
i e
Definition: data.cc:2724
static const char * error_id_invalid_index
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
virtual std::string message(void) const
void err_del_index_out_of_range(bool is1d, octave_idx_type idx, octave_idx_type ext)
octave_idx_type extent
void set_extent(octave_idx_type ext)
static const char * warning_id_singular_matrix
void err_nonconformant(const char *op, octave_idx_type op1_len, octave_idx_type op2_len)
invalid_index(const std::string &value, octave_idx_type ndim, octave_idx_type dimen)
const char * err_id(void) const
void err_invalid_resize(void)
std::string details(void) const
const char * err_id(void) const
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
OCTAVE_EXPORT octave_value_list or N dimensional array whose elements are all equal to the IEEE symbol zero divided by nd tex zero divided by nd ifnottex and any operation involving another NaN value(5+NaN).Note that NaN always compares not equal to NaN(NaN!
std::string details(void) const
octave_idx_type ndims(void) const
Number of dimensions.
Definition: dim-vector.h:301
static const char * error_id_index_out_of_bounds
virtual std::string details(void) const =0
void err_nan_to_character_conversion(void)
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
void warn_singular_matrix(double rcond)
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
std::string expression(void) const