Skip to content

Commit d1a2742

Browse files
authored
Merge pull request #760 from cbm755/define_by_grow
subsasgn: fix to define a matrix while also growing it
2 parents 5ec4b71 + 7b6b8d0 commit d1a2742

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

inst/@sym/private/mat_rclist_asgn.m

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%% Copyright (C) 2014, 2016 Colin B. Macdonald
1+
%% Copyright (C) 2014, 2016-2017 Colin B. Macdonald
22
%%
33
%% This file is part of OctSymPy.
44
%%
@@ -33,8 +33,6 @@
3333
%%
3434
%% @end defun
3535

36-
%% Author: Colin B. Macdonald
37-
%% Keywords: symbolic
3836

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

@@ -58,7 +56,7 @@
5856
' B = sp.Matrix([[B]])'
5957
'BT = B.T'
6058
'# copy of A, expanded and padded with zeros'
61-
'if not A.is_Matrix:'
59+
'if not A or not A.is_Matrix:'
6260
' n = max( max(r)+1, 1 )'
6361
' m = max( max(c)+1, 1 )'
6462
'else:'

inst/@sym/subsasgn.m

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
%% @seealso{@@sym/subsref, @@sym/subindex, @@sym/end, symfun}
5757
%% @end deftypeop
5858

59+
5960
function out = subsasgn (val, idx, rhs)
6061

6162
switch idx.type
@@ -76,7 +77,7 @@
7677
all_Symbols = python_cmd (cmd, idx.subs);
7778
end
7879
if (all_syms && all_Symbols)
79-
%% Make a symfun
80+
%% Make a symfun
8081
if (~isa(rhs, 'sym'))
8182
% rhs is, e.g., a double, then we call the constructor
8283
rhs = sym(rhs);
@@ -91,12 +92,12 @@
9192
idx.subs{i} = double(idx.subs{i});
9293
end
9394
end
94-
for i = 1:length(idx.subs)
95+
for i = 1:length(idx.subs)
9596
if (~ is_valid_index(idx.subs{i}))
9697
error('OctSymPy:subsref:invalidIndices', ...
9798
'invalid indices: should be integers or boolean');
9899
end
99-
end
100+
end
100101
out = mat_replace(val, idx.subs, sym(rhs));
101102
end
102103

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

166+
%!test
167+
%! % grow from nothing
168+
%! clear a
169+
%! a(3) = sym (1);
170+
%! b = sym ([0 0 1]);
171+
%! assert (isequal (a, b))
172+
173+
%!test
174+
%! % grow from nothing, 2D
175+
%! clear a
176+
%! a(2, 3) = sym (1);
177+
%! b = sym ([0 0 0; 0 0 1;]);
178+
%! assert (isequal (a, b))
179+
165180
%!test
166181
%! % linear indices of 2D
167182
%! b = 1:4; b = [b; 2*b; 3*b];

0 commit comments

Comments
 (0)