Skip to content

Commit

Permalink
Merge pull request #704 from cbm755/cbm_594_part1
Browse files Browse the repository at this point in the history
sym constructor rewrite (594 Part 1)
  • Loading branch information
cbm755 committed Dec 31, 2016
2 parents 4d5355e + 5f380db commit e41406e
Show file tree
Hide file tree
Showing 13 changed files with 524 additions and 275 deletions.
2 changes: 2 additions & 0 deletions NEWS
Expand Up @@ -40,6 +40,8 @@ octsympy 2.5.0-dev

* `solve` now works with inequalities.

* `sym('1.3')` now creates the rational number 13/10.

* Update minimum versions of SymPy to 1.0 and Octave to 4.0.0, so
we can remove some crufty workarounds.

Expand Down
14 changes: 7 additions & 7 deletions inst/@sym/private/cell_array_to_sym.m
Expand Up @@ -16,21 +16,21 @@
%% License along with this software; see the file COPYING.
%% If not, see <http://www.gnu.org/licenses/>.

function s = cell_array_to_sym (L)
function s = cell_array_to_sym (L, varargin)
%private helper for sym ctor
% convert a cell array to syms, recursively when nests cells found

assert(iscell(L))
assert (iscell (L))

s = cell(size(L));
s = cell (size (L));

for i = 1:numel(L)
for i = 1:numel (L)
%s{i} = sym(L{i});
% not strictly necessary if sym calls this but maybe neater this way:
item = L{i};
if iscell(item)
s{i} = cell_array_to_sym(item);
if iscell (item)
s{i} = cell_array_to_sym (item, varargin{:});
else
s{i} = sym(item);
s{i} = sym (item, varargin{:});
end
end
61 changes: 61 additions & 0 deletions inst/@sym/private/check_assumptions.m
@@ -0,0 +1,61 @@
%% Copyright (C) 2016 Lagu
%%
%% This file is part of OctSymPy.
%%
%% OctSymPy is free software; you can redistribute it and/or modify
%% it under the terms of the GNU General Public License as published
%% by the Free Software Foundation; either version 3 of the License,
%% or (at your option) any later version.
%%
%% This software is distributed in the hope that it will be useful,
%% but WITHOUT ANY WARRANTY; without even the implied warranty
%% of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
%% the GNU General Public License for more details.
%%
%% You should have received a copy of the GNU General Public
%% License along with this software; see the file COPYING.
%% If not, see <http://www.gnu.org/licenses/>.

%% -*- texinfo -*-
%% @deftypefun check_assumptions (@var{x})
%% Check if the input have valid assumptions.
%%
%% Private helper function.
%%
%% @seealso{sym}
%% @end deftypefun


function check_assumptions (x)

ca_helper (x)

end

%% Is safer use a helper when private functions call it self for classdef.
function ca_helper (x)

persistent valid_asm

if (isempty (valid_asm))
valid_asm = assumptions ('possible');
end

if (~islogical (x))
if (isa (x, 'char'))
assert (ismember (x, valid_asm), ['sym: the assumption "' x '" is not supported in your Sympy version.'])
elseif (isstruct (x))
fields = fieldnames (x);
for j = 1:numel (fields)
ca_helper (fields{j})
end
elseif (iscell (x))
for j = 1:length (x)
ca_helper (x{j})
end
else
error ('sym: assumption must be a string or struct or cell')
end
end

end
101 changes: 101 additions & 0 deletions inst/@sym/private/const_to_python_str.m
@@ -0,0 +1,101 @@
%% Copyright (C) 2015, 2016 Colin B. Macdonald
%% Copyright (C) 2016 Lagu
%%
%% This file is part of OctSymPy.
%%
%% OctSymPy is free software; you can redistribute it and/or modify
%% it under the terms of the GNU General Public License as published
%% by the Free Software Foundation; either version 3 of the License,
%% or (at your option) any later version.
%%
%% This software is distributed in the hope that it will be useful,
%% but WITHOUT ANY WARRANTY; without even the implied warranty
%% of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
%% the GNU General Public License for more details.
%%
%% You should have received a copy of the GNU General Public
%% License along with this software; see the file COPYING.
%% If not, see <http://www.gnu.org/licenses/>.

%% -*- texinfo -*-
%% @deftypefun {[@var{s}, @var{flag}] =} const_to_python_str (@var{x})
%% Recognize constant and return their python string equivalent.
%%
%% Private helper function.
%%
%% Caution: there are two copies of this file for technical
%% reasons: make sure you modify both of them!
%%
%% @seealso{sym, vpa}
%% @end deftypefun

function [s, flag] = const_to_python_str (x)

persistent list
persistent const

if (isempty (list))
% Table of special doubles and special strings. Format for each row is:
% double value, {list of strings to recognize}, resulting python expr
% Note: case senstive
% Note: python expr should be in list for identity "sym(sympr(x)) == x"
list = {pi {'pi'} 'pi'; ...
inf {'inf' 'Inf' 'oo'} 'oo'; ...
nan {'NaN' 'nan'} 'nan'; ...
i {'i' 'I'} 'I'};
% Special Sympy constants to recognize
const = {'zoo'};
end

flag = 1;

if (isa (x, 'double')) % Number comparison
for j = 1:length (list)
if (isequaln (x, list{j, 1}))
s = list{j, 3};
return
elseif (isequaln (x, -list{j, 1}))
s = ['-' list{j, 3}];
return
end
end
elseif (isa (x, 'char')) % Char comparison
for j = 1:length (list)
for n = 1:length (list{j, 2})
if (strcmp (x, list{j, 2}{n}) || strcmp (x, ['+' list{j, 2}{n}]))
s = list{j, 3};
return
elseif (strcmp (x, ['-' list{j, 2}{n}]))
s = ['-' list{j, 3}];
return
end
end
end
for j = 1:length (const) % Check if is a python constant
if (strcmp (x, const{j}) || strcmp (x, ['+' const{j}]))
s = const{j};
return
elseif (strcmp (x, ['-' const{j}]))
s = ['-' const{j}];
return
end
end
else
error ('Format not supported.')
end

if (isa (x, 'char'))
flag = 0;
s = x;
return
elseif (isa (x, 'double'))
if ((abs (x) < 1e15) && (mod (x,1) == 0))
% special treatment for "small" integers
s = num2str (x); % better than sprintf('%d', large)
else
s = '';
flag = 0;
end
end

end
59 changes: 0 additions & 59 deletions inst/@sym/private/magic_double_str.m

This file was deleted.

0 comments on commit e41406e

Please sign in to comment.