Skip to content

Commit

Permalink
Pass ratflag to numeric array conversion
Browse files Browse the repository at this point in the history
Fixes #1273.
  • Loading branch information
cbm755 committed Sep 14, 2023
1 parent 7db57b1 commit 20ff7d2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
18 changes: 14 additions & 4 deletions inst/@sym/private/numeric_array_to_sym.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Copyright (C) 2014, 2015, 2019 Colin B. Macdonald
%% Copyright (C) 2014, 2015, 2019, 2023 Colin B. Macdonald
%%
%% This file is part of OctSymPy.
%%
Expand All @@ -16,9 +16,15 @@
%% License along with this software; see the file COPYING.
%% If not, see <http://www.gnu.org/licenses/>.

function z = numeric_array_to_sym(A)
function z = numeric_array_to_sym(A, ratflag)
%private helper for sym ctor
% convert an array to syms, currently on 1D, 2D.
% convert an array to syms, currently only 1D, 2D.

if nargin < 2
ratwarn = true;
else
ratwarn = false;
end

[n, m] = size(A);

Expand All @@ -33,7 +39,11 @@
% we want all sym creation to go through the ctor.
Ac{i} = cell(m,1);
for j=1:m
Ac{i}{j} = sym(A(i,j));
if ratwarn
Ac{i}{j} = sym (A(i, j));
else
Ac{i}{j} = sym (A(i, j), ratflag);
end
end
end

Expand Down
21 changes: 19 additions & 2 deletions inst/@sym/sym.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Copyright (C) 2014-2019, 2022 Colin B. Macdonald
%% Copyright (C) 2014-2019, 2022-2023 Colin B. Macdonald
%% Copyright (C) 2016 Lagu
%%
%% This file is part of OctSymPy.
Expand Down Expand Up @@ -356,7 +356,11 @@
end

if (~isscalar (x) && isnumber) % Handle octave numeric matrix
s = numeric_array_to_sym (x);
if ratwarn
s = numeric_array_to_sym (x);
else
s = numeric_array_to_sym (x, ratflag);
end
return

elseif (isa (x, 'double')) % Handle double/complex
Expand Down Expand Up @@ -840,6 +844,18 @@
%! x = sym(a, 'r');
%! assert (isequal (x, sym(1)/10))

%!test
%! % sym(double) array with 'r': no warning
%! a = [0.1 0.2];
%! x = sym(a, 'r');
%! assert (isequal (x, [sym(a(1), 'r') sym(a(2), 'r')]))

%!test
%! % sym(double) array with 'f': no warning
%! a = [0.1 0.2];
%! x = sym(a, 'f');
%! assert (isequal (x, [sym(a(1), 'f') sym(a(2), 'f')]))

%!test
%! % sym(double, 'f')
%! a = 0.1;
Expand Down Expand Up @@ -916,6 +932,7 @@
%!warning <dangerous> sym (10.33);
%!warning <dangerous> sym (-5.23);
%!warning <dangerous> sym (sqrt (1.4142135623731));
%!warning <dangerous> sym ([1.2 1.3]);

%!error <is not supported>
%! x = sym ('x', 'positive2');
Expand Down

0 comments on commit 20ff7d2

Please sign in to comment.