comparison libinterp/corefcn/data.cc @ 20608:32a0bf9906c1

linspace: remove use of error_state. * libinterp/corefcn/data.cc: current use of error_state is only useful for a nicer error message about N not being an integer. However, octave::idx_type_value() does not error for that, and even in that case, it must be cast to integer silently for Matlab compatibility.
author Carnë Draug <carandraug@octave.org>
date Thu, 08 Oct 2015 19:56:51 +0100
parents a05a0432dff4
children 780431fc4137
comparison
equal deleted inserted replaced
20605:734d446560a8 20608:32a0bf9906c1
5231 if (arg_3.is_numeric_type () && arg_3.is_empty ()) 5231 if (arg_3.is_numeric_type () && arg_3.is_empty ())
5232 npoints = 1; 5232 npoints = 1;
5233 else if (! arg_3.is_scalar_type ()) 5233 else if (! arg_3.is_scalar_type ())
5234 error ("linspace: N must be a scalar"); 5234 error ("linspace: N must be a scalar");
5235 else 5235 else
5236 // Even if third arg is not an integer, it must be cast to int
5236 npoints = arg_3.idx_type_value (); 5237 npoints = arg_3.idx_type_value ();
5237 } 5238 }
5238 5239
5239 if (! error_state) 5240 octave_value arg_1 = args(0);
5240 { 5241 octave_value arg_2 = args(1);
5241 octave_value arg_1 = args(0); 5242
5242 octave_value arg_2 = args(1); 5243 dim_vector sz1 = arg_1.dims ();
5243 5244 bool isvector1 = sz1.length () == 2 && (sz1(0) == 1 || sz1(1) == 1);
5244 dim_vector sz1 = arg_1.dims (); 5245 dim_vector sz2 = arg_2.dims ();
5245 bool isvector1 = sz1.length () == 2 && (sz1(0) == 1 || sz1(1) == 1); 5246 bool isvector2 = sz2.length () == 2 && (sz2(0) == 1 || sz2(1) == 1);
5246 dim_vector sz2 = arg_2.dims (); 5247
5247 bool isvector2 = sz2.length () == 2 && (sz2(0) == 1 || sz2(1) == 1); 5248 if (! isvector1 || ! isvector2)
5248 5249 error ("linspace: A, B must be scalars or vectors");
5249 if (! isvector1 || ! isvector2) 5250 else if (arg_1.is_single_type () || arg_2.is_single_type ())
5250 error ("linspace: A, B must be scalars or vectors"); 5251 {
5251 else if (arg_1.is_single_type () || arg_2.is_single_type ()) 5252 if (arg_1.is_complex_type () || arg_2.is_complex_type ())
5252 { 5253 retval = do_linspace<FloatComplexMatrix> (arg_1, arg_2, npoints);
5253 if (arg_1.is_complex_type () || arg_2.is_complex_type ())
5254 retval = do_linspace<FloatComplexMatrix> (arg_1, arg_2, npoints);
5255 else
5256 retval = do_linspace<FloatMatrix> (arg_1, arg_2, npoints);
5257
5258 }
5259 else 5254 else
5260 { 5255 retval = do_linspace<FloatMatrix> (arg_1, arg_2, npoints);
5261 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) 5256
5262 retval = do_linspace<ComplexMatrix> (arg_1, arg_2, npoints);
5263 else
5264 retval = do_linspace<Matrix> (arg_1, arg_2, npoints);
5265 }
5266 } 5257 }
5267 else 5258 else
5268 error ("linspace: N must be an integer"); 5259 {
5260 if (arg_1.is_complex_type () || arg_2.is_complex_type ())
5261 retval = do_linspace<ComplexMatrix> (arg_1, arg_2, npoints);
5262 else
5263 retval = do_linspace<Matrix> (arg_1, arg_2, npoints);
5264 }
5269 5265
5270 return retval; 5266 return retval;
5271 } 5267 }
5272 5268
5273 5269
5297 %!assert (linspace (10, 20, 1), [20]) 5293 %!assert (linspace (10, 20, 1), [20])
5298 %!assert (linspace (10, 20, 0), zeros (1, 0)) 5294 %!assert (linspace (10, 20, 0), zeros (1, 0))
5299 %!assert (linspace (10, 20, -1), zeros (1, 0)) 5295 %!assert (linspace (10, 20, -1), zeros (1, 0))
5300 %!assert (numel (linspace (0, 1, 2+eps)), 2) 5296 %!assert (numel (linspace (0, 1, 2+eps)), 2)
5301 %!assert (numel (linspace (0, 1, 2-eps)), 1) 5297 %!assert (numel (linspace (0, 1, 2-eps)), 1)
5298 %!assert (linspace (10, 20, 2.1), [10 20])
5299 %!assert (linspace (10, 20, 2.9), [10 20])
5302 5300
5303 %!error linspace () 5301 %!error linspace ()
5304 %!error linspace (1, 2, 3, 4) 5302 %!error linspace (1, 2, 3, 4)
5305 %!error <N must be a scalar> linspace (1, 2, [3, 4]) 5303 %!error <N must be a scalar> linspace (1, 2, [3, 4])
5306 %!error <must be scalars or vectors> linspace (ones (2,2), 2, 3) 5304 %!error <must be scalars or vectors> linspace (ones (2,2), 2, 3)