Mercurial > hg > octave-avbm
changeset 9658:3429c956de6f
extend linspace & fix up liboctave rewrite
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Sat, 19 Sep 2009 22:17:12 +0200 |
parents | 3c1c3a38ec7b |
children | 0bcfeadb6178 |
files | liboctave/CMatrix.cc liboctave/CRowVector.cc liboctave/dMatrix.cc liboctave/dRowVector.cc liboctave/fCMatrix.cc liboctave/fCRowVector.cc liboctave/fMatrix.cc liboctave/fRowVector.cc src/ChangeLog src/data.cc |
diffstat | 10 files changed, 71 insertions(+), 65 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/CMatrix.cc +++ b/liboctave/CMatrix.cc @@ -4085,13 +4085,13 @@ retval(i, 0) = x1(i); // The last column is not needed while using delta. - Complex *delta = &retval(0, 1); + Complex *delta = &retval(0, n-1); for (octave_idx_type i = 0; i < m; i++) delta[i] = (x2(i) - x1(i)) / (n - 1.0); for (octave_idx_type j = 1; j < n-1; j++) for (octave_idx_type i = 0; i < m; i++) - retval(i, j) = retval(i, j-1) + delta[i]; + retval(i, j) = x1(i) + static_cast<double> (j)*delta[i]; for (octave_idx_type i = 0; i < m; i++) retval(i, n-1) = x2(i);
--- a/liboctave/CRowVector.cc +++ b/liboctave/CRowVector.cc @@ -487,9 +487,9 @@ NoAlias<ComplexRowVector> retval (n); Complex delta = (x2 - x1) / (n - 1.0); - Complex y = retval(0) = x1; + retval(0) = x1; for (octave_idx_type i = 1; i < n-1; i++) - retval(i) = y += delta; + retval(i) = x1 + static_cast<double> (i)*delta; retval(n-1) = x2; return retval;
--- a/liboctave/dMatrix.cc +++ b/liboctave/dMatrix.cc @@ -3414,13 +3414,13 @@ retval(i, 0) = x1(i); // The last column is not needed while using delta. - double *delta = &retval(0, 1); + double *delta = &retval(0, n-1); for (octave_idx_type i = 0; i < m; i++) delta[i] = (x2(i) - x1(i)) / (n - 1); for (octave_idx_type j = 1; j < n-1; j++) for (octave_idx_type i = 0; i < m; i++) - retval(i, j) = retval(i, j-1) + delta[i]; + retval(i, j) = x1(i) + j*delta[i]; for (octave_idx_type i = 0; i < m; i++) retval(i, n-1) = x2(i);
--- a/liboctave/dRowVector.cc +++ b/liboctave/dRowVector.cc @@ -316,9 +316,9 @@ NoAlias<RowVector> retval (n); double delta = (x2 - x1) / (n - 1); - double y = retval(0) = x1; + retval(0) = x1; for (octave_idx_type i = 1; i < n-1; i++) - retval(i) = y += delta; + retval(i) = x1 + i*delta; retval(n-1) = x2; return retval;
--- a/liboctave/fCMatrix.cc +++ b/liboctave/fCMatrix.cc @@ -4078,13 +4078,13 @@ retval(i, 0) = x1(i); // The last column is not needed while using delta. - FloatComplex *delta = &retval(0, 1); + FloatComplex *delta = &retval(0, n-1); for (octave_idx_type i = 0; i < m; i++) delta[i] = (x2(i) - x1(i)) / (n - 1.0f); for (octave_idx_type j = 1; j < n-1; j++) for (octave_idx_type i = 0; i < m; i++) - retval(i, j) = retval(i, j-1) + delta[i]; + retval(i, j) = x1(i) + static_cast<float> (j)*delta[i]; for (octave_idx_type i = 0; i < m; i++) retval(i, n-1) = x2(i);
--- a/liboctave/fCRowVector.cc +++ b/liboctave/fCRowVector.cc @@ -487,9 +487,9 @@ NoAlias<FloatComplexRowVector> retval (n); FloatComplex delta = (x2 - x1) / (n - 1.0f); - FloatComplex y = retval(0) = x1; + retval(0) = x1; for (octave_idx_type i = 1; i < n-1; i++) - retval(i) = y += delta; + retval(i) = x1 + static_cast<float> (i)*delta; retval(n-1) = x2; return retval;
--- a/liboctave/fMatrix.cc +++ b/liboctave/fMatrix.cc @@ -3413,13 +3413,13 @@ retval(i, 0) = x1(i); // The last column is not needed while using delta. - float *delta = &retval(0, 1); + float *delta = &retval(0, n-1); for (octave_idx_type i = 0; i < m; i++) delta[i] = (x2(i) - x1(i)) / (n - 1); for (octave_idx_type j = 1; j < n-1; j++) for (octave_idx_type i = 0; i < m; i++) - retval(i, j) = retval(i, j-1) + delta[i]; + retval(i, j) = x1(i) + j*delta[i]; for (octave_idx_type i = 0; i < m; i++) retval(i, n-1) = x2(i);
--- a/liboctave/fRowVector.cc +++ b/liboctave/fRowVector.cc @@ -316,9 +316,9 @@ NoAlias<FloatRowVector> retval (n); float delta = (x2 - x1) / (n - 1); - float y = retval(0) = x1; + retval(0) = x1; for (octave_idx_type i = 1; i < n-1; i++) - retval(i) = y += delta; + retval(i) = x1 + i*delta; retval(n-1) = x2; return retval;
--- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,6 +1,8 @@ 2009-09-19 Jaroslav Hajek <highegg@gmail.com> * ov.h (octave_value_extract): New template function. + * data.cc (do_linspace): New static template function. + (Flinspace): Rewrite. 2009-09-18 John W. Eaton <jwe@octave.org>
--- a/src/data.cc +++ b/src/data.cc @@ -45,6 +45,7 @@ #include "lo-math.h" #include "str-vec.h" #include "quit.h" +#include "mx-base.h" #include "Cell.h" #include "defun.h" @@ -4382,6 +4383,50 @@ */ +template <class MT> +static octave_value +do_linspace (const octave_value& base, const octave_value& limit, + octave_idx_type n) +{ + typedef typename MT::column_vector_type CVT; + typedef typename MT::element_type T; + + octave_value retval; + + if (base.is_scalar_type ()) + { + T bs = octave_value_extract<T> (base); + if (limit.is_scalar_type ()) + { + T ls = octave_value_extract<T> (limit); + retval = linspace (bs, ls, n); + } + else + { + CVT lv = octave_value_extract<CVT> (limit); + CVT bv (lv.length (), bs); + retval = linspace (bv, lv, n); + } + } + else + { + CVT bv = octave_value_extract<CVT> (base); + if (limit.is_scalar_type ()) + { + T ls = octave_value_extract<T> (limit); + CVT lv (bv.length (), ls); + retval = linspace (bv, lv, n); + } + else + { + CVT lv = octave_value_extract<CVT> (limit); + retval = linspace (bv, lv, n); + } + } + + return retval; +} + DEFUN (linspace, args, , "-*- texinfo -*-\n\ @deftypefn {Built-in Function} {} linspace (@var{base}, @var{limit}, @var{n})\n\ @@ -4392,7 +4437,9 @@ stored in decreasing order. If the number of points is not specified, a\n\ value of 100 is used.\n\ \n\ -The @code{linspace} function always returns a row vector.\n\ +The @code{linspace} function always returns a row vector if both\n\ +@var{base} and @var{limit} are scalars. If one of them or both are column\n\ +vectors, @code{linspace} returns a matrix.\n\ \n\ For compatibility with @sc{matlab}, return the second argument if\n\ fewer than two values are requested.\n\ @@ -4421,60 +4468,17 @@ if (arg_1.is_single_type () || arg_2.is_single_type ()) { if (arg_1.is_complex_type () || arg_2.is_complex_type ()) - { - FloatComplex x1 = arg_1.float_complex_value (); - FloatComplex x2 = arg_2.float_complex_value (); - - if (! error_state) - { - FloatComplexRowVector rv = linspace (x1, x2, npoints); - - if (! error_state) - retval = rv; - } - } + retval = do_linspace<FloatComplexMatrix> (arg_1, arg_2, npoints); else - { - float x1 = arg_1.float_value (); - float x2 = arg_2.float_value (); - - if (! error_state) - { - FloatRowVector rv = linspace (x1, x2, npoints); - - if (! error_state) - retval = rv; - } - } + retval = do_linspace<FloatMatrix> (arg_1, arg_2, npoints); + } else { if (arg_1.is_complex_type () || arg_2.is_complex_type ()) - { - Complex x1 = arg_1.complex_value (); - Complex x2 = arg_2.complex_value (); - - if (! error_state) - { - ComplexRowVector rv = linspace (x1, x2, npoints); - - if (! error_state) - retval = rv; - } - } + retval = do_linspace<ComplexMatrix> (arg_1, arg_2, npoints); else - { - double x1 = arg_1.double_value (); - double x2 = arg_2.double_value (); - - if (! error_state) - { - RowVector rv = linspace (x1, x2, npoints); - - if (! error_state) - retval = rv; - } - } + retval = do_linspace<Matrix> (arg_1, arg_2, npoints); } } else