GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
lo-array-gripes.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2003-2018 John W. Eaton
4 Copyright (C) 2009 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 // FIXME: All gripe_XXX functions deprecated in 4.2. Remove file in
25 // version 5.
26 
27 #if defined (HAVE_CONFIG_H)
28 # include "config.h"
29 #endif
30 
31 #include <sstream>
32 
33 #include "lo-array-gripes.h"
34 #include "lo-error.h"
35 
36 // Text constants used to shorten code below.
37 static const char *error_id_nonconformant_args = "Octave:nonconformant-args";
38 
39 static const char *error_id_index_out_of_bounds = "Octave:index-out-of-bounds";
40 
41 static const char *error_id_invalid_index = "Octave:invalid-index";
42 
44  "Octave:nearly-singular-matrix";
45 
46 static const char *warning_id_singular_matrix = "Octave:singular-matrix";
47 
48 void
50 {
51  (*current_liboctave_error_handler)
52  ("invalid conversion from NaN to logical");
53 }
54 
55 void
57 {
58  (*current_liboctave_error_handler)
59  ("invalid conversion from NaN to character");
60 }
61 
62 void
63 gripe_nonconformant (const char *op, octave_idx_type op1_len,
64  octave_idx_type op2_len)
65 {
66  const char *err_id = error_id_nonconformant_args;
67 
68  (*current_liboctave_error_with_id_handler)
69  (err_id, "%s: nonconformant arguments (op1 len: %d, op2 len: %d)",
70  op, op1_len, op2_len);
71 }
72 
73 void
74 gripe_nonconformant (const char *op,
75  octave_idx_type op1_nr, octave_idx_type op1_nc,
76  octave_idx_type op2_nr, octave_idx_type op2_nc)
77 {
78  const char *err_id = error_id_nonconformant_args;
79 
80  (*current_liboctave_error_with_id_handler)
81  (err_id, "%s: nonconformant arguments (op1 is %dx%d, op2 is %dx%d)",
82  op, op1_nr, op1_nc, op2_nr, op2_nc);
83 }
84 
85 void
86 gripe_nonconformant (const char *op, const dim_vector& op1_dims,
87  const dim_vector& op2_dims)
88 {
89  const char *err_id = error_id_nonconformant_args;
90 
91  std::string op1_dims_str = op1_dims.str ();
92  std::string op2_dims_str = op2_dims.str ();
93 
94  (*current_liboctave_error_with_id_handler)
95  (err_id, "%s: nonconformant arguments (op1 is %s, op2 is %s)",
96  op, op1_dims_str.c_str (), op2_dims_str.c_str ());
97 }
98 
99 void
101  octave_idx_type ext)
102 {
103  const char *err_id = error_id_index_out_of_bounds;
104 
105  (*current_liboctave_error_with_id_handler)
106  (err_id, "A(%s) = []: index out of bounds: value %d out of bound %d",
107  is1d ? "I" : "..,I,..", idx, ext);
108 }
109 
110 namespace octave
111 {
112  class invalid_index : public index_exception
113  {
114  public:
115 
117  octave_idx_type dimen)
118  : index_exception (value, ndim, dimen)
119  { }
120 
121  std::string details (void) const
122  {
123 #if defined (OCTAVE_ENABLE_64)
124  return "subscripts must be either integers 1 to (2^63)-1 or logicals";
125 #else
126  return "subscripts must be either integers 1 to (2^31)-1 or logicals";
127 #endif
128  }
129 
130  // ID of error to throw
131  const char * err_id (void) const
132  {
133  return error_id_invalid_index;
134  }
135  };
136 }
137 
138 // Complain if an index is negative, fractional, or too big.
139 
140 void
142  octave_idx_type dim, const std::string&)
143 {
144  octave::invalid_index e (idx, nd, dim);
145 
146  throw e;
147 }
148 
149 void
151  octave_idx_type dim, const std::string& var)
152 {
153  std::ostringstream buf;
154  buf << n + 1;
155 
156 #if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC)
157 # pragma GCC diagnostic push
158 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
159 #endif
160 
161  gripe_invalid_index (buf.str (), nd, dim, var);
162 
163 #if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC)
164 # pragma GCC diagnostic pop
165 #endif
166 }
167 
168 void
170  const std::string& var)
171 {
172  std::ostringstream buf;
173  buf << n + 1;
174 
175 #if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC)
176 # pragma GCC diagnostic push
177 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
178 #endif
179 
180  gripe_invalid_index (buf.str (), nd, dim, var);
181 
182 #if defined (HAVE_PRAGMA_GCC_DIAGNOSTIC)
183 # pragma GCC diagnostic pop
184 #endif
185 }
186 
187 namespace octave
188 {
189  // Gripe and exception for read access beyond the bounds of an array.
190 
191  class out_of_range : public index_exception
192  {
193  public:
194 
196  octave_idx_type dim_in)
197  : index_exception (value, nd_in, dim_in), extent (0)
198  { }
199 
200  std::string details (void) const
201  {
202  std::string expl;
203 
204  if (nd >= size.ndims ()) // if not an index slice
205  {
206  if (var != "")
207  expl = "but " + var + " has size ";
208  else
209  expl = "but object has size ";
210 
211  expl = expl + size.str ('x');
212  }
213  else
214  {
215  std::ostringstream buf;
216  buf << extent;
217  expl = "out of bound " + buf.str ();
218  }
219 
220  return expl;
221  }
222 
223  // ID of error to throw.
224  const char * err_id (void) const
225  {
227  }
228 
229  void set_size (const dim_vector& size_in) { size = size_in; }
230 
231  void set_extent (octave_idx_type ext) { extent = ext; }
232 
233  private:
234 
235  // Dimension of object being accessed.
237 
238  // Length of dimension being accessed.
240  };
241 }
242 
243 // Complain of an index that is out of range, but we don't know matrix size
244 void
246  octave_idx_type ext)
247 {
248  std::ostringstream buf;
249  buf << idx;
250  octave::out_of_range e (buf.str (), nd, dim);
251 
252  e.set_extent (ext);
253  // ??? Make details method give extent not size.
254  e.set_size (dim_vector (1, 1, 1, 1, 1, 1,1));
255 
256  throw e;
257 }
258 
259 // Complain of an index that is out of range
260 void
262  octave_idx_type ext, const dim_vector& d)
263 {
264  std::ostringstream buf;
265  buf << idx;
266  octave::out_of_range e (buf.str (), nd, dim);
267 
268  e.set_extent (ext);
269  e.set_size (d);
270 
271  throw e;
272 }
273 
274 void
276 {
277  (*current_liboctave_error_with_id_handler)
278  ("Octave:invalid-resize",
279  "Invalid resizing operation or ambiguous assignment to an out-of-bounds array element");
280 }
281 
282 void
283 gripe_singular_matrix (double rcond)
284 {
285  if (rcond == 0.0)
286  {
287  (*current_liboctave_warning_with_id_handler)
289  "matrix singular to machine precision");
290  }
291  else
292  {
293  (*current_liboctave_warning_with_id_handler)
295  "matrix singular to machine precision, rcond = %g", rcond);
296  }
297 }
298 
299 /* Tests in test/index.tst */
std::string str(char sep='x') const
Definition: dim-vector.cc:73
void gripe_nonconformant(const char *op, octave_idx_type op1_len, octave_idx_type op2_len)
out_of_range(const std::string &value, octave_idx_type nd_in, octave_idx_type dim_in)
void set_size(const dim_vector &size_in)
static const char * warning_id_nearly_singular_matrix
i e
Definition: data.cc:2591
var
Definition: givens.cc:88
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 const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
static const char * error_id_nonconformant_args
void gripe_nan_to_logical_conversion(void)
octave_idx_type extent
void set_extent(octave_idx_type ext)
void gripe_invalid_resize(void)
void gripe_invalid_index(const std::string &idx, octave_idx_type nd, octave_idx_type dim, const std::string &)
invalid_index(const std::string &value, octave_idx_type ndim, octave_idx_type dimen)
void gripe_nan_to_character_conversion(void)
void gripe_singular_matrix(double rcond)
const char * err_id(void) const
static const char * error_id_index_out_of_bounds
std::string details(void) const
std::string details(void) const
const char * err_id(void) const
static const char * error_id_invalid_index
void gripe_index_out_of_range(int nd, int dim, octave_idx_type idx, octave_idx_type ext)
octave_idx_type ndims(void) const
Number of dimensions.
Definition: dim-vector.h:295
void gripe_del_index_out_of_range(bool is1d, octave_idx_type idx, octave_idx_type ext)
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
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:888
nd group nd example For each display the value
Definition: sysdep.cc:866
static const char * warning_id_singular_matrix