Skip to content

Commit c187ce8

Browse files
authored
Merge pull request #1230 from alexvong1995/use-unflatten-in-piecewise
@sym/piecewise: Use sympy function 'unflatten'.
2 parents abfae5e + 90608c1 commit c187ce8

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

inst/@sym/piecewise.m

+8-5
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,14 @@
6868
print_usage ();
6969
end
7070

71-
cmd = {'def chunks_of(ls, n):'
72-
' return itertools.zip_longest(*[ls[k::n] for k in range(n)])'
73-
'args = [(val, cond) if val is not None else (cond, True)'
74-
' for cond, val in chunks_of(_ins, 2)]'
75-
'return Piecewise(*args)'
71+
cmd = {'def pack_args_without_else(args):'
72+
' return [(val, cond) for cond, val in unflatten(args, 2)]'
73+
'def pack_args(args):'
74+
' if len(args) % 2 == 0:'
75+
' return pack_args_without_else(args)'
76+
' else:'
77+
' return pack_args_without_else(args[:-1]) + [(args[-1], True)]'
78+
'return Piecewise(*pack_args(_ins))'
7679
};
7780

7881
args = cellfun (@sym, varargin, 'UniformOutput', false);

0 commit comments

Comments
 (0)