comparison src/data.cc @ 9658:3429c956de6f

extend linspace & fix up liboctave rewrite
author Jaroslav Hajek <highegg@gmail.com>
date Sat, 19 Sep 2009 22:17:12 +0200
parents b8db3595f706
children e793865ede63
comparison
equal deleted inserted replaced
9657:3c1c3a38ec7b 9658:3429c956de6f
43 43
44 #include "lo-ieee.h" 44 #include "lo-ieee.h"
45 #include "lo-math.h" 45 #include "lo-math.h"
46 #include "str-vec.h" 46 #include "str-vec.h"
47 #include "quit.h" 47 #include "quit.h"
48 #include "mx-base.h"
48 49
49 #include "Cell.h" 50 #include "Cell.h"
50 #include "defun.h" 51 #include "defun.h"
51 #include "error.h" 52 #include "error.h"
52 #include "gripes.h" 53 #include "gripes.h"
4380 4381
4381 %!error <Invalid call to eye.*> eye (1, 2, 3); 4382 %!error <Invalid call to eye.*> eye (1, 2, 3);
4382 4383
4383 */ 4384 */
4384 4385
4386 template <class MT>
4387 static octave_value
4388 do_linspace (const octave_value& base, const octave_value& limit,
4389 octave_idx_type n)
4390 {
4391 typedef typename MT::column_vector_type CVT;
4392 typedef typename MT::element_type T;
4393
4394 octave_value retval;
4395
4396 if (base.is_scalar_type ())
4397 {
4398 T bs = octave_value_extract<T> (base);
4399 if (limit.is_scalar_type ())
4400 {
4401 T ls = octave_value_extract<T> (limit);
4402 retval = linspace (bs, ls, n);
4403 }
4404 else
4405 {
4406 CVT lv = octave_value_extract<CVT> (limit);
4407 CVT bv (lv.length (), bs);
4408 retval = linspace (bv, lv, n);
4409 }
4410 }
4411 else
4412 {
4413 CVT bv = octave_value_extract<CVT> (base);
4414 if (limit.is_scalar_type ())
4415 {
4416 T ls = octave_value_extract<T> (limit);
4417 CVT lv (bv.length (), ls);
4418 retval = linspace (bv, lv, n);
4419 }
4420 else
4421 {
4422 CVT lv = octave_value_extract<CVT> (limit);
4423 retval = linspace (bv, lv, n);
4424 }
4425 }
4426
4427 return retval;
4428 }
4429
4385 DEFUN (linspace, args, , 4430 DEFUN (linspace, args, ,
4386 "-*- texinfo -*-\n\ 4431 "-*- texinfo -*-\n\
4387 @deftypefn {Built-in Function} {} linspace (@var{base}, @var{limit}, @var{n})\n\ 4432 @deftypefn {Built-in Function} {} linspace (@var{base}, @var{limit}, @var{n})\n\
4388 Return a row vector with @var{n} linearly spaced elements between\n\ 4433 Return a row vector with @var{n} linearly spaced elements between\n\
4389 @var{base} and @var{limit}. If the number of elements is greater than one,\n\ 4434 @var{base} and @var{limit}. If the number of elements is greater than one,\n\
4390 then the @var{base} and @var{limit} are always included in\n\ 4435 then the @var{base} and @var{limit} are always included in\n\
4391 the range. If @var{base} is greater than @var{limit}, the elements are\n\ 4436 the range. If @var{base} is greater than @var{limit}, the elements are\n\
4392 stored in decreasing order. If the number of points is not specified, a\n\ 4437 stored in decreasing order. If the number of points is not specified, a\n\
4393 value of 100 is used.\n\ 4438 value of 100 is used.\n\
4394 \n\ 4439 \n\
4395 The @code{linspace} function always returns a row vector.\n\ 4440 The @code{linspace} function always returns a row vector if both\n\
4441 @var{base} and @var{limit} are scalars. If one of them or both are column\n\
4442 vectors, @code{linspace} returns a matrix.\n\
4396 \n\ 4443 \n\
4397 For compatibility with @sc{matlab}, return the second argument if\n\ 4444 For compatibility with @sc{matlab}, return the second argument if\n\
4398 fewer than two values are requested.\n\ 4445 fewer than two values are requested.\n\
4399 @end deftypefn") 4446 @end deftypefn")
4400 { 4447 {
4419 octave_value arg_2 = args(1); 4466 octave_value arg_2 = args(1);
4420 4467
4421 if (arg_1.is_single_type () || arg_2.is_single_type ()) 4468 if (arg_1.is_single_type () || arg_2.is_single_type ())
4422 { 4469 {
4423 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) 4470 if (arg_1.is_complex_type () || arg_2.is_complex_type ())
4424 { 4471 retval = do_linspace<FloatComplexMatrix> (arg_1, arg_2, npoints);
4425 FloatComplex x1 = arg_1.float_complex_value ();
4426 FloatComplex x2 = arg_2.float_complex_value ();
4427
4428 if (! error_state)
4429 {
4430 FloatComplexRowVector rv = linspace (x1, x2, npoints);
4431
4432 if (! error_state)
4433 retval = rv;
4434 }
4435 }
4436 else 4472 else
4437 { 4473 retval = do_linspace<FloatMatrix> (arg_1, arg_2, npoints);
4438 float x1 = arg_1.float_value (); 4474
4439 float x2 = arg_2.float_value ();
4440
4441 if (! error_state)
4442 {
4443 FloatRowVector rv = linspace (x1, x2, npoints);
4444
4445 if (! error_state)
4446 retval = rv;
4447 }
4448 }
4449 } 4475 }
4450 else 4476 else
4451 { 4477 {
4452 if (arg_1.is_complex_type () || arg_2.is_complex_type ()) 4478 if (arg_1.is_complex_type () || arg_2.is_complex_type ())
4453 { 4479 retval = do_linspace<ComplexMatrix> (arg_1, arg_2, npoints);
4454 Complex x1 = arg_1.complex_value ();
4455 Complex x2 = arg_2.complex_value ();
4456
4457 if (! error_state)
4458 {
4459 ComplexRowVector rv = linspace (x1, x2, npoints);
4460
4461 if (! error_state)
4462 retval = rv;
4463 }
4464 }
4465 else 4480 else
4466 { 4481 retval = do_linspace<Matrix> (arg_1, arg_2, npoints);
4467 double x1 = arg_1.double_value ();
4468 double x2 = arg_2.double_value ();
4469
4470 if (! error_state)
4471 {
4472 RowVector rv = linspace (x1, x2, npoints);
4473
4474 if (! error_state)
4475 retval = rv;
4476 }
4477 }
4478 } 4482 }
4479 } 4483 }
4480 else 4484 else
4481 error ("linspace: expecting third argument to be an integer"); 4485 error ("linspace: expecting third argument to be an integer");
4482 4486