Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] change @sym to a classdef #950

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ac27887
Convert sym to classdef. Move private files.
genuinelucifer Jan 13, 2017
7235925
Fix some tests. Improve sym constructor.
genuinelucifer Jan 14, 2017
f713b30
Fix almost all sym tests. Improve sym.symarray
genuinelucifer Jan 14, 2017
ee65900
Prelim symfun. Non working!
genuinelucifer Jan 15, 2017
67c865a
Symfun finally working! Tests to be edited...
genuinelucifer Jan 17, 2017
ff299ae
Fix almost all symfun tests!
genuinelucifer Jan 18, 2017
24817dd
Fix more symfun problems
genuinelucifer Jan 19, 2017
416c83c
symfun: implement (poorly?) cast to sym
cbm755 Jan 20, 2017
400164b
symfun: use "formula" instead of broken cast to sym
cbm755 Jan 20, 2017
d98a54e
symfun: call subclass methods
cbm755 Jan 20, 2017
e7b4f75
Merge pull request #6 from cbm755/genuinelucifer_classdef_fixes
genuinelucifer Jan 20, 2017
e0851d7
Fix symarray and few tests.
genuinelucifer Jan 20, 2017
ba12a0e
Mark tests with xtest as indexing double with classdef is not yet sup…
genuinelucifer Jan 20, 2017
336eddf
Add proper copyright info and rewrite parts of code.
genuinelucifer Jan 20, 2017
80ca5e2
avoid assuming sym(cell) gives cell for varargin processing
cbm755 Jan 17, 2017
ff46cdc
Merge remote-tracking branch 'origin/master' into classdefupdate2
cbm755 Mar 24, 2019
2436d80
more private files to private class methods
cbm755 Mar 24, 2019
5aa68d8
revert the symarray thing: not needed anymore
cbm755 Mar 24, 2019
21f8ddd
num2cell is broken?
cbm755 Mar 24, 2019
4c00dc4
Merge branch 'symfun_cleanup' into classdefupdate3
cbm755 Mar 24, 2019
28203f0
remove cell_array_to_sym
cbm755 Mar 24, 2019
974d80e
xtest a few things that are unexpectedly broken on classdef
cbm755 Mar 24, 2019
46f1a55
use private property for size
cbm755 Mar 24, 2019
812a084
finiteset: temporarily xfail a doctest for classdef
cbm755 Mar 24, 2019
adb427d
Merge remote-tracking branch 'origin/master' into classdef
cbm755 Apr 14, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
%% If not, see <http://www.gnu.org/licenses/>.

function t = assert_same_shape(x,y)
if ~(is_same_shape(x,y))
if ~(sym.is_same_shape (x, y))
error('array inputs must have same size and shape');
end

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion inst/@sym/eye.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
end

if (isa (varargin{end}, 'char'))
varargin = cell2nosyms (varargin);
varargin = sym.cell2nosyms (varargin);
y = eye (varargin{:});
return
end
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion inst/@sym/isNone.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
%!assert (isequal (None(1), None));
%!error None(None);
%!error <must be integer> x=sym('x'); x(None);
%!error <must be integer> x=1; x(None);
%!xtest <must be integer> x=1; x(None); % NOT supported for integer(classdef)
%!error None(None);
%!error 1 + None;
%!error None - 1;
Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 3 additions & 2 deletions inst/@sym/logical.m
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,17 @@
%%! w = logical(x);
%%! assert (w)

%!test
%!xtest
%! % older Octave (< 4.2) didn't automatically do "if (logical(obj))"
%! % re-broken on classdef?
%! e = sym(true);
%! if (e)
%! assert(true);
%! else
%! assert(false);
%! end

%!test
%!xtest
%! % more of above
%! e2 = sym(1) == sym(1);
%! if (e2)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion inst/@sym/ones.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
end

if (isa (varargin{end}, 'char'))
varargin = cell2nosyms (varargin);
varargin = sym.cell2nosyms (varargin);
y = ones (varargin{:});
return
end
Expand Down
2 changes: 1 addition & 1 deletion inst/@sym/size.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

% Note: symbolic sized matrices should return double, not sym/string.

n = x.size;
n = x._size;

% FIXME: for now, we artificially force symbolic sized objects
% (where one or more dimension is recorded as NaN) to be 1x1.
Expand Down
10 changes: 6 additions & 4 deletions inst/@sym/subsasgn.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@
end
end
for i = 1:length(idx.subs)
if (~ is_valid_index(idx.subs{i}))
if (~ sym.is_valid_index (idx.subs{i}))
error('OctSymPy:subsref:invalidIndices', ...
'invalid indices: should be integers or boolean');
end
end
out = mat_replace(val, idx.subs, sym(rhs));
out = sym.mat_replace (val, idx.subs, sym(rhs));
end

case '.'
Expand Down Expand Up @@ -163,15 +163,17 @@
%! b([1 end+1],end:end+1) = rhs;
%! assert(isequal( a, b ))

%!test
%!xtest
%! % grow from nothing
%! % broken on classdef?
%! clear a
%! a(3) = sym (1);
%! b = sym ([0 0 1]);
%! assert (isequal (a, b))

%!test
%!xtest
%! % grow from nothing, 2D
%! % broken on classdef?
%! clear a
%! a(2, 3) = sym (1);
%! b = sym ([0 0 0; 0 0 1;]);
Expand Down
12 changes: 6 additions & 6 deletions inst/@sym/subsindex.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@
end


%!test
%!xtest
%! i = sym(1);
%! a = 7;
%! assert(a(i)==a);
%! i = sym(2);
%! a = 2:2:10;
%! assert(a(i)==4);

%!test
%!xtest
%! i = sym([1 3 5]);
%! a = 1:10;
%! assert( isequal (a(i), [1 3 5]))
Expand All @@ -105,12 +105,12 @@
%! end
%! assert(waserr)

%!test
%!xtest
%! syms x
%! assert (isequal (x(sym (true)), x))
%! assert (isequal (x(sym (false)), sym ([])))

%!test
%!xtest
%! x = 6;
%! assert (isequal (x(sym (true)), 6))
%! assert (isequal (x(sym (false)), []))
Expand All @@ -120,14 +120,14 @@
%! assert (isequal (a(sym ([true false true])), a([1 3])))
%! assert (isequal (a(sym ([false false false])), sym (ones(1,0))))

%!test
%!xtest
%! a = [10 11; 12 13];
%! p = [true false; true true];
%! assert (isequal (a(sym (p)), a(p)))
%! p = [false false false];
%! assert (isequal (a(sym (p)), a(p)))

%!error <indices must be integers or boolean>
%!xtest <indices must be integers or boolean>
%! a = [10 12];
%! I = [sym(true) 2];
%! b = a(I);
8 changes: 5 additions & 3 deletions inst/@sym/subsref.m
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@
end
end
for i = 1:length(idx.subs)
if (~ is_valid_index(idx.subs{i}))
if (~ sym.is_valid_index (idx.subs{i}))
error('OctSymPy:subsref:invalidIndices', ...
'invalid indices: should be integers or boolean');
end
end
out = mat_access(f, idx.subs);
out = sym.mat_access (f, idx.subs);

case '.'
fld = idx.subs;
Expand All @@ -82,7 +82,9 @@
% out = f.extra;
% not part of the interface
%elseif (strcmp (fld, 'size'))
% out = f.size;
% out = f._size;
elseif (strcmp (fld, '_priv_size')) % XXX only for symfun?
out = f._size;
else
error ('@sym/subsref: invalid or nonpublic property ''%s''', fld);
end
Expand Down
66 changes: 55 additions & 11 deletions inst/@sym/sym.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
%% Copyright (C) 2014-2018 Colin B. Macdonald
%% Copyright (C) 2016 Lagu
%% Copyright (C) 2017 Abhinav Tripathi
%%
%% This file is part of OctSymPy.
%%
Expand Down Expand Up @@ -246,9 +247,46 @@
%% @seealso{syms, assumptions, @@sym/assume, @@sym/assumeAlso}
%% @end deftypeop

classdef sym < handle
properties
% none?
end

properties (Access = private)
_size
pickle
flat
ascii
unicode
extra
end

function s = sym(x, varargin)
methods (Static, Access = private)
assert_same_shape (x, y);
cell2nosyms (x);
check_assumptions (x);
codegen (varargin);
detect_special_str (x);
double_to_sym_exact (x);
double_to_sym_heuristic (x, ratwarn, argnstr);
elementwise_op(scalar_fcn, varargin);
ineq_helper (op, fop, lhs, rhs, nanspecial);
is_same_shape (x, y);
is_valid_index (x);
make_sym_matrix (As, sz);
mat_access (A, subs);
mat_rccross_access (A, r, c);
mat_rclist_access (A, r, c);
mat_rclist_asgn (A, r, c, B);
mat_replace (A, subs, b);
numeric_array_to_sym (A);
uniop_bool_helper (x, scalar_fcn, opt, varargin);
end

methods
function s = sym (x, varargin)

%% Should be INDENTED 4 spaces: just trying to keep the diffs small for now...
if (nargin == 0)
x = 0;
end
Expand All @@ -260,12 +298,11 @@
% that "sym([])" is valid but "sym([], ...)" is otherwise not.
if (isempty (x) && nargin == 6)
s.pickle = varargin{1};
s.size = varargin{2};
s._size = varargin{2};
s.flat = varargin{3};
s.ascii = varargin{4};
s.unicode = varargin{5};
s.extra = [];
s = class (s, 'sym');
return
end

Expand Down Expand Up @@ -325,7 +362,7 @@
if (ismatrix (varargin{1}) && ~ischar (varargin{1}) && ~isstruct (varargin{1}) && ~iscell (varargin{1}))
%% Handle MatrixSymbols
assert (nargin < 3, 'MatrixSymbol do not support assumptions')
s = make_sym_matrix (x, varargin{1});
s = sym.make_sym_matrix (x, varargin{1});
return
elseif (nargin == 2 && isnumber && ischar (varargin{1}) && isscalar (varargin{1}))
%% explicit ratflag given
Expand All @@ -350,13 +387,13 @@
else
sclear = false;
assert (~isnumber, 'Only symbols can have assumptions.')
check_assumptions (varargin); % Check if assumptions exist - Sympy don't check this
sym.check_assumptions (varargin); % Check if assumptions exist - Sympy don't check this
asm = varargin;
end
end

if (~isscalar (x) && isnumber) % Handle octave numeric matrix
s = numeric_array_to_sym (x);
s = sym.numeric_array_to_sym (x);
return

elseif (isa (x, 'double')) % Handle double/complex
Expand All @@ -374,9 +411,9 @@
x = xx{n};
switch ratflag
case 'f'
y = double_to_sym_exact (x);
y = sym.double_to_sym_exact (x);
case 'r'
y = double_to_sym_heuristic (x, ratwarn, []);
y = sym.double_to_sym_heuristic (x, ratwarn, []);
otherwise
error ('sym: this case should not be possible')
end
Expand Down Expand Up @@ -423,7 +460,7 @@
% end
%end

y = detect_special_str (x);
y = sym.detect_special_str (x);
if (~ isempty (y))
assert (isempty (asm), 'Only symbols can have assumptions.')
s = python_cmd (['return ' y]);
Expand Down Expand Up @@ -568,6 +605,13 @@

error ('Conversion to symbolic with those arguments not (yet) supported')

%% END TEMPORARY INDENT
end
end

methods (Static)
% none yet
end
end


Expand Down Expand Up @@ -948,7 +992,7 @@
%!error <unexpected in assumptions>
%! n = sym ('n', {{'negative', 'even'}});

%!test
%!xtest
%! % save/load sym objects
%! syms x
%! y = 2*x;
Expand Down Expand Up @@ -990,7 +1034,7 @@
%! assert (isequal (a, sym (1)))
%! assert (isequal (b, sym (-1)))

%!test
%!xtest
%! % num2cell works on sym arrays
%! syms x
%! C1 = num2cell ([x 2 3; 4 5 6*x]);
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion inst/@sym/zeros.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
end

if (isa (varargin{end}, 'char'))
varargin = cell2nosyms (varargin);
varargin = sym.cell2nosyms (varargin);
y = zeros (varargin{:});
return
end
Expand Down
1 change: 1 addition & 0 deletions inst/@symfun/diff.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@

function z = diff(f, varargin)

%z = diff@sym(f, varargin{:});
z = diff(formula (f), varargin{:});
z = symfun(z, f.vars);

Expand Down
2 changes: 1 addition & 1 deletion inst/@symfun/formula.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
%% @end defmethod

function g = formula(f)
g = f.sym;
g = sym(f);
end


Expand Down
1 change: 1 addition & 0 deletions inst/@symfun/int.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
indefinite = false;
end

%F = int@sym(f, varargin{:});
F = int(formula (f), varargin{:});
if (indefinite)
F = symfun(F, f.vars);
Expand Down
1 change: 1 addition & 0 deletions inst/@symfun/isequal.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
%% Copyright (C) 2017 NVS Abhilash
%% Copyright (C) 2017 Abhinav Tripathi
%%
%% This file is part of OctSymPy.
%%
Expand Down
3 changes: 2 additions & 1 deletion inst/@symfun/minus.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@
%! assert( isa(f - f, 'symfun'))
%! assert( isa(f - x, 'symfun'))

%!test
%!xtest
%! % Octave bug #42735 fixed in 4.4.2
%! % TODO: and broken again on classdef 4.2.2--6.0.0?
%! syms x
%! f(x) = x^2;
%! g = x^2;
Expand Down
Loading