M2
M2 copied to clipboard
new of from for MutableMatrices
I noticed that the new of from syntax sometimes has unexpected consequences (I can understand why it happens, but maybe it shouldn't?)
In particular, if M is a MutableMatrix and I write
N = new MutableMatrix from M
then if I change an entry in N, it still changes an entry in M. This was not the behavior I was expecting (ie, I was expecting new to allocate a really new object). This does not occur with the mutableMatrix constructor.
For instance:
i1 : M = mutableMatrix{{1,2},{3,4}}
o1 = | 1 2 |
| 3 4 |
o1 : MutableMatrix
i2 : N = new MutableMatrix from M
o2 = | 1 2 |
| 3 4 |
o2 : MutableMatrix
i3 : N_(0,0) = 7
o3 = 7
i4 : N
o4 = | 7 2 |
| 3 4 |
o4 : MutableMatrix
i5 : M
o5 = | 7 2 |
| 3 4 |
o5 : MutableMatrix
However, if I use the mutableMatrix constructor, this doesn't occur.
i1 : M = mutableMatrix{{1,2},{3,4}}
o1 = | 1 2 |
| 3 4 |
o1 : MutableMatrix
i2 : N = mutableMatrix M
o2 = | 1 2 |
| 3 4 |
o2 : MutableMatrix
i3 : N_(0,0) = 7
o3 = 7
i4 : N
o4 = | 7 2 |
| 3 4 |
o4 : MutableMatrix
i5 : M
o5 = | 1 2 |
| 3 4 |
o5 : MutableMatrix