comparison libinterp/corefcn/data.cc @ 31927:fef004fa8575

Improve Matlab compatibility of linspace for Inf endpoints. Return an N-element vector if both START and END are the same. When N is odd and [START, END] are [-Inf, Inf] return 0 for middle return value. For all other elements besides START and END return NaN when one endpoint is not finite. * data.cc (Flinspace): Update and add new BIST tests for linspace. * CRowVector.cc, dRowVector.cc, fCRowVector.cc, fRowVector.cc (linspace): Add special case in input validation for START==END and return an N-element vector of value END. Check whether variable delta is Inf value and change delta to NaN if it is. For complex calculations, a separate isinf() check is done for real and imaginary parts of delta. For middle point when N is odd, return 0 if START == -END, NaN if delta was NaN, or otherwise (START + END) / 2. * logspace.m: Update BIST tests.
author Rik <rik@octave.org>
date Fri, 24 Mar 2023 14:42:24 -0700
parents b6e0171c3f44
children 39700c1ea93e
comparison
equal deleted inserted replaced
31926:cd5be42a81ab 31927:fef004fa8575
5743 %!assert (linspace (10, 20, -1), zeros (1, 0)) 5743 %!assert (linspace (10, 20, -1), zeros (1, 0))
5744 %!assert (numel (linspace (0, 1, 2+eps)), 2) 5744 %!assert (numel (linspace (0, 1, 2+eps)), 2)
5745 %!assert (numel (linspace (0, 1, 2-eps)), 1) 5745 %!assert (numel (linspace (0, 1, 2-eps)), 1)
5746 %!assert (linspace (10, 20, 2.1), [10 20]) 5746 %!assert (linspace (10, 20, 2.1), [10 20])
5747 %!assert (linspace (10, 20, 2.9), [10 20]) 5747 %!assert (linspace (10, 20, 2.9), [10 20])
5748 %!assert (1 ./ linspace (-0, 0, 4), [-Inf, Inf, Inf, Inf])
5749 %!assert (linspace (Inf, Inf, 3), [Inf, Inf, Inf]) 5748 %!assert (linspace (Inf, Inf, 3), [Inf, Inf, Inf])
5750 %!assert (linspace (-Inf, -Inf, 3), [-Inf, -Inf, -Inf]) 5749 %!assert (linspace (-Inf, -Inf, 3), [-Inf, -Inf, -Inf])
5751 %!assert (linspace (-Inf, Inf, 3), [-Inf, 0, Inf]) 5750 %!assert (linspace (-Inf, Inf, 3), [-Inf, 0, Inf])
5751 ## Octave prefers to return NaN which indicates failure of algorithm.
5752 %!assert (linspace (-Inf, Inf, 4), [-Inf, NaN, NaN, Inf])
5753 %!assert (linspace (-Inf, 0, 3), [-Inf, NaN, 0])
5754 %!assert (linspace (-Inf, 0, 4), [-Inf, NaN, NaN, 0])
5752 %!assert (linspace (Inf + 1i, Inf + 1i, 3), [Inf + 1i, Inf + 1i, Inf + 1i]) 5755 %!assert (linspace (Inf + 1i, Inf + 1i, 3), [Inf + 1i, Inf + 1i, Inf + 1i])
5753 %!assert (linspace (-Inf + 1i, Inf + 1i, 3), [-Inf + 1i, NaN + 1i, Inf + 1i]) 5756 %!assert (linspace (-Inf - 1i, Inf + 1i, 3), [-Inf - 1i, 0 + 0i, Inf + 1i])
5757 %!assert (linspace (-Inf - 1i, Inf + 2i, 3), [-Inf - 1i, NaN + 0.5i, Inf + 2i])
5758 %!assert (linspace (-Inf - 3i, Inf + 0i, 4),
5759 %! [-Inf - 3i, NaN - 2i, NaN - 1i, Inf + 0i])
5760 %!assert (linspace (complex (-1, -Inf), complex (1, Inf), 3),
5761 %! [complex(-1, -Inf), 0 + 0i, complex(1, Inf)])
5762 %!assert (linspace (complex (-1, -Inf), complex (2, Inf), 3),
5763 %! [complex(-1, -Inf), complex(0.5, NaN), complex(2, Inf)])
5764 %!assert (linspace (complex (-3, -Inf), complex (0, Inf), 4),
5765 %! [complex(-3, -Inf), complex(-2, NaN), complex(-1, NaN), complex(0, Inf)])
5754 5766
5755 ## FIXME: Octave is not fully Matlab-compatible for some combinations of 5767 ## FIXME: Octave is not fully Matlab-compatible for some combinations of
5756 ## Inf/-Inf endpoints. See bug #56933. This was dubbed "Won't Fix" 5768 ## Inf/-Inf endpoints. See bug #56933. This was dubbed "Won't Fix"
5757 ## so these tests have been removed from the test suite by commenting 5769 ## as Octave prefers to return NaN for some of these conditions to
5758 ## them out. If the behavior in the future is made compatible these 5770 ## better reflect that the algorithm has failed. If the behavior in
5759 ## tests can be re-instated. 5771 ## the future is made compatible these tests can be re-instated.
5760 ##%!assert <56933> (linspace (-Inf, Inf, 4), [-Inf, -Inf, Inf, Inf]) 5772 ##%!assert <56933> (linspace (-Inf, Inf, 4), [-Inf, -Inf, Inf, Inf])
5761 ##%!assert <56933> (linspace (-Inf, Inf, 5), [-Inf, -Inf, 0, Inf, Inf]) 5773 ##%!assert <56933> (linspace (-Inf, Inf, 5), [-Inf, -Inf, 0, Inf, Inf])
5762 ##%!assert <56933> (linspace (0, Inf, 4), [0, Inf, Inf, Inf]) 5774 ##%!assert <56933> (linspace (0, Inf, 4), [0, Inf, Inf, Inf])
5763 ##%!assert <56933> (linspace (0, -Inf, 4), [0, -Inf, -Inf, -Inf]) 5775 ##%!assert <56933> (linspace (0, -Inf, 4), [0, -Inf, -Inf, -Inf])
5764 ##%!assert <56933> (linspace (-Inf, 0, 4), [-Inf, NaN, NaN, 0]) 5776 ##%!assert <56933> (linspace (-Inf, 0, 4), [-Inf, NaN, NaN, 0])
5765 ##%!assert <56933> (linspace (Inf, 0, 4), [Inf, NaN, NaN, 0]) 5777 ##%!assert <56933> (linspace (Inf, 0, 4), [Inf, NaN, NaN, 0])
5766 5778 ##%!assert (1 ./ linspace (-0, 0, 4), [-Inf, Inf, Inf, Inf])
5767 %!error linspace () 5779
5768 %!error linspace (1, 2, 3, 4) 5780 ## Test input validation
5781 %!error <Invalid call> linspace ()
5782 %!error <Invalid call> linspace (1, 2, 3, 4)
5769 %!error <N must be a scalar> linspace (1, 2, [3, 4]) 5783 %!error <N must be a scalar> linspace (1, 2, [3, 4])
5770 %!error <START, END must be scalars or vectors> linspace (ones (2,2), 2, 3) 5784 %!error <START, END must be scalars or vectors> linspace (ones (2,2), 2, 3)
5771 %!error <START, END must be scalars or vectors> linspace (2, ones (2,2), 3) 5785 %!error <START, END must be scalars or vectors> linspace (2, ones (2,2), 3)
5772 %!error <START, END must be scalars or vectors> linspace (1, [], 3) 5786 %!error <START, END must be scalars or vectors> linspace (1, [], 3)
5773 */ 5787 */