Is this a bug

27 views
Skip to first unread message

Ash

unread,
Sep 5, 2019, 12:05:04 PM9/5/19
to sympy
Hello All,
Please see the code below. Is this a bug in simpy?

from sympy import *

a = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = a
a.col_del(0)

print(a, b)

Expected answer: a = [ [2, 3]
                                   [5, 6]
                                   [8, 9]]

b = [ [1, 2, 3]
        [4, 5, 6]
        [7, 8, 9]]

Actual answer: a = [ [2, 3]
                              [5, 6]
                              [8, 9]]

b = [ [2, 3]
        [5, 6]
        [8, 9]]

Thanks
Ash


Aaron Meurer

unread,
Sep 5, 2019, 1:14:09 PM9/5/19
to sympy
The default Matrix class is mutable, which means that col_del modifies
a in-place. Since b points to the same matrix as a, it is also
modified in-place. See https://nedbatchelder.com/text/names.html.

It may be simpler to reason about an immutable matrix. Use
ImmutableMatrix instead of Matrix. In that case, a.col_del(0) will
return a new matrix and not modify a in-place.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/bc6d9386-82d2-4a4e-bad6-675c914ea574%40googlegroups.com.

Ashith Shyam

unread,
Sep 6, 2019, 4:13:19 AM9/6/19
to sy...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages