Skip to content

Commit

Permalink
Merge pull request #760 from cbm755/define_by_grow
Browse files Browse the repository at this point in the history
subsasgn: fix to define a matrix while also growing it
  • Loading branch information
cbm755 authored Mar 1, 2017
2 parents 5ec4b71 + 7b6b8d0 commit d1a2742
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
6 changes: 2 additions & 4 deletions inst/@sym/private/mat_rclist_asgn.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%% Copyright (C) 2014, 2016 Colin B. Macdonald
%% Copyright (C) 2014, 2016-2017 Colin B. Macdonald
%%
%% This file is part of OctSymPy.
%%
Expand Down Expand Up @@ -33,8 +33,6 @@
%%
%% @end defun

%% Author: Colin B. Macdonald
%% Keywords: symbolic

function z = mat_rclist_asgn(A, r, c, B)

Expand All @@ -58,7 +56,7 @@
' B = sp.Matrix([[B]])'
'BT = B.T'
'# copy of A, expanded and padded with zeros'
'if not A.is_Matrix:'
'if not A or not A.is_Matrix:'
' n = max( max(r)+1, 1 )'
' m = max( max(c)+1, 1 )'
'else:'
Expand Down
21 changes: 18 additions & 3 deletions inst/@sym/subsasgn.m
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
%% @seealso{@@sym/subsref, @@sym/subindex, @@sym/end, symfun}
%% @end deftypeop


function out = subsasgn (val, idx, rhs)

switch idx.type
Expand All @@ -76,7 +77,7 @@
all_Symbols = python_cmd (cmd, idx.subs);
end
if (all_syms && all_Symbols)
%% Make a symfun
%% Make a symfun
if (~isa(rhs, 'sym'))
% rhs is, e.g., a double, then we call the constructor
rhs = sym(rhs);
Expand All @@ -91,12 +92,12 @@
idx.subs{i} = double(idx.subs{i});
end
end
for i = 1:length(idx.subs)
for i = 1:length(idx.subs)
if (~ is_valid_index(idx.subs{i}))
error('OctSymPy:subsref:invalidIndices', ...
'invalid indices: should be integers or boolean');
end
end
end
out = mat_replace(val, idx.subs, sym(rhs));
end

Expand Down Expand Up @@ -162,6 +163,20 @@
%! b([1 end+1],end:end+1) = rhs;
%! assert(isequal( a, b ))

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

%!test
%! % grow from nothing, 2D
%! clear a
%! a(2, 3) = sym (1);
%! b = sym ([0 0 0; 0 0 1;]);
%! assert (isequal (a, b))

%!test
%! % linear indices of 2D
%! b = 1:4; b = [b; 2*b; 3*b];
Expand Down

0 comments on commit d1a2742

Please sign in to comment.