Mercurial > octave
changeset 25237:ca022a8c4015
linspace: handle ranges with equal Inf endpoints (bug #53489)
* CMatrix.cc (ComplexMatrix::linspace): Change delta calculation to yield 0 if
endpoints are the same, otherwise calculate delta as normal.
* CRowVector.cc (ComplexRowVector::linspace): Likewise.
* dMatrix.cc (Matrix::linspace): Likewise.
* dRowVector.cc (RowVector::linspace): Likewise.
* fCMatrix.cc (FloatComplexMatrix::linspace): Likewise.
* fCRowVector.cc (FloatComplexRowVector::linspace): Likewise.
* fMatrix.cc (FloatMatrix::linspace): Likewise.
* fRowVector.cc (FloatRowVector::linspace): Likewise.
* data.cc (Flinspace): Added Matlab compatibility tests.
* logspace.m: Added same tests as in linspace containing Inf in endpoints.
author | Maor Shutman <maorus12@gmail.com> |
---|---|
date | Fri, 06 Apr 2018 20:25:05 +0300 |
parents | 69b21b8a0e9f |
children | 565d724ecf50 |
files | libinterp/corefcn/data.cc liboctave/array/CMatrix.cc liboctave/array/CRowVector.cc liboctave/array/dMatrix.cc liboctave/array/dRowVector.cc liboctave/array/fCMatrix.cc liboctave/array/fCRowVector.cc liboctave/array/fMatrix.cc liboctave/array/fRowVector.cc scripts/general/logspace.m |
diffstat | 10 files changed, 28 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/libinterp/corefcn/data.cc Thu Apr 12 14:34:10 2018 -0700 +++ b/libinterp/corefcn/data.cc Fri Apr 06 20:25:05 2018 +0300 @@ -5202,6 +5202,16 @@ %!assert (linspace (10, 20, 2.1), [10 20]) %!assert (linspace (10, 20, 2.9), [10 20]) %!assert (1 ./ linspace (-0, 0, 4), [-Inf, Inf, Inf, Inf]) +%!assert (linspace (Inf, Inf, 3), [Inf, Inf, Inf]) +%!assert (linspace (-Inf, -Inf, 3), [-Inf, -Inf, -Inf]) +%!assert (linspace (-Inf, Inf, 3), [-Inf, NaN, Inf]) +%!assert (linspace (Inf + 1i, Inf + 1i, 3), [Inf + 1i, Inf + 1i, Inf + 1i]) +%!assert (linspace (-Inf + 1i, Inf + 1i, 3), [-Inf + 1i, NaN + 1i, Inf + 1i]) +%!assert (linspace (0, Inf, 3), [0, Inf, Inf]) +%!assert (linspace (0, -Inf, 3), [0, -Inf, -Inf]) +%!assert (linspace (-Inf, 0, 3), [-Inf, NaN, 0]) +%!assert (linspace (Inf, 0, 3), [Inf, NaN, 0]) +%!assert (linspace (Inf, -Inf, 3), [Inf, NaN, -Inf]) %!error linspace () %!error linspace (1, 2, 3, 4)
--- a/liboctave/array/CMatrix.cc Thu Apr 12 14:34:10 2018 -0700 +++ b/liboctave/array/CMatrix.cc Fri Apr 06 20:25:05 2018 +0300 @@ -3819,7 +3819,7 @@ // The last column is unused so temporarily store delta there Complex *delta = &retval(0, n-1); for (octave_idx_type i = 0; i < m; i++) - delta[i] = (x2(i) - x1(i)) / (n - 1.0); + delta[i] = (x1(i) == x2(i)) ? 0 : (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++)
--- a/liboctave/array/CRowVector.cc Thu Apr 12 14:34:10 2018 -0700 +++ b/liboctave/array/CRowVector.cc Fri Apr 06 20:25:05 2018 +0300 @@ -432,7 +432,7 @@ retval(0) = x1; - Complex delta = (x2 - x1) / (n - 1.0); + Complex delta = (x1 == x2) ? 0 : (x2 - x1) / (n - 1.0); for (octave_idx_type i = 1; i < n-1; i++) retval(i) = x1 + static_cast<double> (i)*delta;
--- a/liboctave/array/dMatrix.cc Thu Apr 12 14:34:10 2018 -0700 +++ b/liboctave/array/dMatrix.cc Fri Apr 06 20:25:05 2018 +0300 @@ -3210,7 +3210,7 @@ // The last column is unused so temporarily store delta there double *delta = &retval(0, n-1); for (octave_idx_type i = 0; i < m; i++) - delta[i] = (x2(i) - x1(i)) / (n - 1); + delta[i] = (x1(i) == x2(i)) ? 0 : (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++)
--- a/liboctave/array/dRowVector.cc Thu Apr 12 14:34:10 2018 -0700 +++ b/liboctave/array/dRowVector.cc Fri Apr 06 20:25:05 2018 +0300 @@ -275,7 +275,7 @@ retval(0) = x1; - double delta = (x2 - x1) / (n - 1); + double delta = (x1 == x2) ? 0 : ((x2 - x1) / (n - 1)); for (octave_idx_type i = 1; i < n-1; i++) retval(i) = x1 + i*delta;
--- a/liboctave/array/fCMatrix.cc Thu Apr 12 14:34:10 2018 -0700 +++ b/liboctave/array/fCMatrix.cc Fri Apr 06 20:25:05 2018 +0300 @@ -3848,7 +3848,7 @@ // The last column is unused so temporarily store delta there FloatComplex *delta = &retval(0, n-1); for (octave_idx_type i = 0; i < m; i++) - delta[i] = (x2(i) - x1(i)) / (n - 1.0f); + delta[i] = (x1(i) == x2(i)) ? 0 : (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++)
--- a/liboctave/array/fCRowVector.cc Thu Apr 12 14:34:10 2018 -0700 +++ b/liboctave/array/fCRowVector.cc Fri Apr 06 20:25:05 2018 +0300 @@ -434,7 +434,7 @@ retval(0) = x1; - FloatComplex delta = (x2 - x1) / (n - 1.0f); + FloatComplex delta = (x1 == x2) ? 0 : (x2 - x1) / (n - 1.0f); for (octave_idx_type i = 1; i < n-1; i++) retval(i) = x1 + static_cast<float> (i)*delta;
--- a/liboctave/array/fMatrix.cc Thu Apr 12 14:34:10 2018 -0700 +++ b/liboctave/array/fMatrix.cc Fri Apr 06 20:25:05 2018 +0300 @@ -3215,7 +3215,7 @@ // The last column is unused so temporarily store delta there float *delta = &retval(0, n-1); for (octave_idx_type i = 0; i < m; i++) - delta[i] = (x2(i) - x1(i)) / (n - 1); + delta[i] = (x1(i) == x2(i)) ? 0 : (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++)
--- a/liboctave/array/fRowVector.cc Thu Apr 12 14:34:10 2018 -0700 +++ b/liboctave/array/fRowVector.cc Fri Apr 06 20:25:05 2018 +0300 @@ -275,7 +275,7 @@ retval(0) = x1; - float delta = (x2 - x1) / (n - 1); + float delta = (x1 == x2) ? 0 : (x2 - x1) / (n - 1); for (octave_idx_type i = 1; i < n-1; i++) retval(i) = x1 + i*delta;
--- a/scripts/general/logspace.m Thu Apr 12 14:34:10 2018 -0700 +++ b/scripts/general/logspace.m Fri Apr 06 20:25:05 2018 +0300 @@ -97,6 +97,16 @@ %! assert (size (x2) == [1, 10] && abs (x2(1) - 10) < eps && abs (x2(10) - 100) < eps); %! assert (size (x3) == [1, 10] && abs (x3(1) - 10) < eps && abs (x3(10) - 0.01) < eps); %! assert (size (x4) == [1, 10] && abs (x4(1) - 10) < eps && abs (x4(10) - pi) < sqrt (eps)); +%!assert (logspace (Inf, Inf, 3), [Inf, Inf, Inf]) +%!assert (logspace (-Inf, -Inf, 3), [0, 0, 0]) +%!assert (logspace (-Inf, Inf, 3), [0, NaN, Inf]) +%!assert (logspace (Inf + 1i, Inf + 1i, 3), [-Inf + Inf * 1i, -Inf + Inf * 1i, -Inf + Inf * 1i]) +%!assert (logspace (-Inf + 1i, Inf + 1i, 3), [0, NaN + NaN * 1i, -Inf + Inf * 1i]) +%!assert (logspace (0, Inf, 3), [1, Inf, Inf]) +%!assert (logspace (0, -Inf, 3), [1, 0, 0]) +%!assert (logspace (-Inf, 0, 3), [0, NaN, 1]) +%!assert (logspace (Inf, 0, 3), [Inf, NaN, 1]) +%!assert (logspace (Inf, -Inf, 3), [Inf, NaN, 0]) ## Test input validation %!error logspace ()