# [PYTHON MATRIX-SIG] [tbyang@icf.llnl.gov: Some subtlety in reaching items of a matrix]

P. Dubois dubois@kristen.llnl.gov
Fri, 10 Nov 1995 08:53:01 -0800

```The following comment by Brian Yang is not a surprise, as the difference in
returning reference vs. returning copy was documented, but I thought I'd
pass it on.
------- Start of forwarded message -------
I just found something about the matrix module which I was not
aware of earlier:
For a rank-3 matrix `c', there are some subtle difference between
C[0][1] and c[(0,1)]. The following example illustrates this:

----------------------------------------->
>>> c [[[4.0, 5.0, 6.0],
[0.0, 4.0, 5.0]], [[1.0, 6.0, 5.0], [8.0, 5.0, 2.0]]]
>>> c2=c[(0,1)]
>>> c2
[0.0, 4.0, 5.0]
>>> c1=c[0][1]
>>> c1
[0.0, 4.0, 5.0]
>>> c[(0,1,2)]=6
>>> c
[[[4.0, 5.0, 6.0], [0.0, 4.0, 6.0]], [[1.0, 6.0, 5.0], [8.0, 5.0, 2.0]]]
>>> c1
[0.0, 4.0, 6.0]
>>> c2
[0.0, 4.0, 5.0]
<--------------------------------------
Notice when c changes, c1 changes also, but not c2.
The difference occurs because in `c1=c[0][1]' c is treated as
a sequence, while in `c2=c[(0,1)]' c is treated as a mapping. And it
just so happened that in the implementation of the methods, Hugunin
decided to create a new matrix in the latter case, while for the
former case he just created some new pointers pointing to the same
data area as c. It appears very confusing to me. If we can have enough
feed-back about this, maybe we can suggest him to change to one way or
the other.

------- End of forwarded message -------

=================
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org