comparison libinterp/corefcn/data.cc @ 20501:16b9ec39ff46

Return empty matrix when linspace called with 0 points (bug #45820) * NEWS: Announce change. * data.cc (Flinspace): Verify input N is scalar. Verify inputs BASE, LIMIT are scalar/vectors. Add BIST tests for input validation, complex values, class of output, Matlab compatibility. Clarify documentation about vector inputs. * CMatrix.cc, CRowVector.cc, dMatrix.cc, dRowVector.cc, fCMatrix.cc, fCRowVector.cc, fMatrix.cc, fRowVector.cc: Return empty matrix when N < 1.
author Rik <rik@octave.org>
date Thu, 27 Aug 2015 13:12:21 -0700
parents e3f84a8c6788
children 4bb41929286b
comparison
equal deleted inserted replaced
20500:44eb1102f8a8 20501:16b9ec39ff46
5340 If the number of elements is greater than one, then the endpoints @var{base}\n\ 5340 If the number of elements is greater than one, then the endpoints @var{base}\n\
5341 and @var{limit} are always included in the range. If @var{base} is greater\n\ 5341 and @var{limit} are always included in the range. If @var{base} is greater\n\
5342 than @var{limit}, the elements are stored in decreasing order. If the\n\ 5342 than @var{limit}, the elements are stored in decreasing order. If the\n\
5343 number of points is not specified, a value of 100 is used.\n\ 5343 number of points is not specified, a value of 100 is used.\n\
5344 \n\ 5344 \n\
5345 The @code{linspace} function always returns a row vector if both @var{base}\n\ 5345 The @code{linspace} function returns a row vector when both @var{base}\n\
5346 and @var{limit} are scalars. If one, or both, of them are column vectors,\n\ 5346 and @var{limit} are scalars. If one, or both, inputs are vectors, then\n\
5347 @code{linspace} returns a matrix.\n\ 5347 @code{linspace} transforms them to column vectors and returns a matrix where\n\
5348 each row is an independent sequence between\n\
5349 @w{@code{@var{base}(@var{row_n}), @var{limit}(@var{row_n})}}.\n\
5348 \n\ 5350 \n\
5349 For compatibility with @sc{matlab}, return the second argument (@var{limit})\n\ 5351 For compatibility with @sc{matlab}, return the second argument (@var{limit})\n\
5350 if fewer than two values are requested.\n\ 5352 if fewer than two values are requested.\n\
5351 @seealso{logspace}\n\ 5353 @seealso{logspace}\n\
5352 @end deftypefn") 5354 @end deftypefn")
5370 5372
5371 octave_value arg_3 = args(2); 5373 octave_value arg_3 = args(2);
5372 5374
5373 if (arg_3.is_numeric_type () && arg_3.is_empty ()) 5375 if (arg_3.is_numeric_type () && arg_3.is_empty ())
5374 npoints = 1; 5376 npoints = 1;
5377 else if (! arg_3.is_scalar_type ())
5378 error ("linspace: N must be a scalar");
5375 else 5379 else
5376 npoints = arg_3.idx_type_value (); 5380 npoints = arg_3.idx_type_value ();
5377 } 5381 }
5378 5382
5379 if (! error_state) 5383 if (! error_state)
5380 { 5384 {
5381 octave_value arg_1 = args(0); 5385 octave_value arg_1 = args(0);
5382 octave_value arg_2 = args(1); 5386 octave_value arg_2 = args(1);
5383 5387
5384 if (arg_1.is_single_type () || arg_2.is_single_type ()) 5388 dim_vector sz1 = arg_1.dims ();
5389 bool isvector1 = sz1.length () == 2 && (sz1(0) == 1 || sz1(1) == 1);
5390 dim_vector sz2 = arg_2.dims ();
5391 bool isvector2 = sz2.length () == 2 && (sz2(0) == 1 || sz2(1) == 1);
5392
5393 if (! isvector1 || ! isvector2)
5394 error ("linspace: A, B must be scalars or vectors");
5395 else if (arg_1.is_single_type () || arg_2.is_single_type ())
5385 { 5396 {
5386 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) 5397 if (arg_1.is_complex_type () || arg_2.is_complex_type ())
5387 retval = do_linspace<FloatComplexMatrix> (arg_1, arg_2, npoints); 5398 retval = do_linspace<FloatComplexMatrix> (arg_1, arg_2, npoints);
5388 else 5399 else
5389 retval = do_linspace<FloatMatrix> (arg_1, arg_2, npoints); 5400 retval = do_linspace<FloatMatrix> (arg_1, arg_2, npoints);
5411 %! x3 = linspace (1, -2, 10); 5422 %! x3 = linspace (1, -2, 10);
5412 %! assert (size (x1) == [1, 100] && x1(1) == 1 && x1(100) == 2); 5423 %! assert (size (x1) == [1, 100] && x1(1) == 1 && x1(100) == 2);
5413 %! assert (size (x2) == [1, 10] && x2(1) == 1 && x2(10) == 2); 5424 %! assert (size (x2) == [1, 10] && x2(1) == 1 && x2(10) == 2);
5414 %! assert (size (x3) == [1, 10] && x3(1) == 1 && x3(10) == -2); 5425 %! assert (size (x3) == [1, 10] && x3(1) == 1 && x3(10) == -2);
5415 5426
5416 %! ##assert (linspace ([1, 2; 3, 4], 5, 6), linspace (1, 5, 6)) 5427 ## Test complex values
5417 5428 %!test
5429 %! exp = [1+0i, 2-1.25i, 3-2.5i, 4-3.75i, 5-5i];
5430 %! obs = linspace (1, 5-5i, 5);
5431 %! assert (obs, exp);
5432
5433 ## Test class of output
5434 %!assert (class (linspace (1, 2)), "double")
5435 %!assert (class (linspace (single (1), 2)), "single")
5436 %!assert (class (linspace (1, single (2))), "single")
5437
5438 ## Test obscure Matlab compatibility options
5418 %!assert (linspace (0, 1, []), 1) 5439 %!assert (linspace (0, 1, []), 1)
5440 %!assert (linspace (10, 20, 2), [10 20])
5441 %!assert (linspace (10, 20, 1), [20])
5442 %!assert (linspace (10, 20, 0), zeros (1, 0))
5443 %!assert (linspace (10, 20, -1), zeros (1, 0))
5444 %!assert (numel (linspace (0, 1, 2+eps)), 2)
5445 %!assert (numel (linspace (0, 1, 2-eps)), 1)
5419 5446
5420 %!error linspace () 5447 %!error linspace ()
5421 %!error linspace (1, 2, 3, 4) 5448 %!error linspace (1, 2, 3, 4)
5449 %!error <N must be a scalar> linspace (1, 2, [3, 4])
5450 %!error <must be scalars or vectors> linspace (ones (2,2), 2, 3)
5451 %!error <must be scalars or vectors> linspace (2, ones (2,2), 3)
5452 %!error <must be scalars or vectors> linspace (1, [], 3)
5422 */ 5453 */
5423 5454
5424 // FIXME: should accept dimensions as separate args for N-d 5455 // FIXME: should accept dimensions as separate args for N-d
5425 // arrays as well as 1-d and 2-d arrays. 5456 // arrays as well as 1-d and 2-d arrays.
5426 5457