Re: matrix copying policies & implementation
- Subject: Re: matrix copying policies & implementation
- From: Robert Dodier
- Date: Sat, 22 Apr 2006 09:46:25 -0600
Here's a session using a proof-of-concept matrix implementation.
Hope the formatting comes through OK.
Robert
Create a matrix (need to allocate storage first) ...
(%i15) a : ?gensym ();
(%o15) g16688
(%i16) a :: make_array (any, 15);
(%o16) {Array: #(NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NIL NI\
L NIL NIL NIL)}
(%i17) fillarray (''a, makelist (i, i, 1, 15));
(%o17) {Array: #(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15)}
... OK here's the matrix
(%i18) M: mm (5, 0, 1, 3, 0, 5, a, 0);
[ 1 6 11 ]
[ ]
[ 2 7 12 ]
[ ]
(%o18) [ 3 8 13 ]
[ ]
[ 4 9 14 ]
[ ]
[ 5 10 15 ]
(%i19) :lisp (put '|$m| 'matrix_reimpl_assign 'mset_extension_operator)
MATRIX_REIMPL_ASSIGN
Transpose (shares storage with original)
(%i19) M2 : t (M);
[ 1 2 3 4 5 ]
[ ]
(%o19) [ 6 7 8 9 10 ]
[ ]
[ 11 12 13 14 15 ]
(%i20) :lisp (put '|$m2| 'matrix_reimpl_assign 'mset_extension_operator)
MATRIX_REIMPL_ASSIGN
Extract rows and columns of original and transpose. These all share storage
with original
(%i20) M [all, 2];
[ 6 ]
[ ]
[ 7 ]
[ ]
(%o20) [ 8 ]
[ ]
[ 9 ]
[ ]
[ 10 ]
(%i21) M [2, all];
(%o21) [ 2 7 12 ]
(%i22) M2 [all, 2];
[ 2 ]
[ ]
(%o22) [ 7 ]
[ ]
[ 12 ]
(%i23) M2 [2, all];
(%o23) [ 6 7 8 9 10 ]
Modify an element of transpose. Original is unmodified because storage is
copied before writing
(%i24) M2 [3, 3]: FOOBAR;
(%o24) FOOBAR
(%i25) M2;
[ 1 2 3 4 5 ]
[ ]
(%o25) [ 6 7 8 9 10 ]
[ ]
[ 11 12 FOOBAR 14 15 ]
(%i26) M;
[ 1 6 11 ]
[ ]
[ 2 7 12 ]
[ ]
(%o26) [ 3 8 13 ]
[ ]
[ 4 9 14 ]
[ ]
[ 5 10 15 ]
Extract another submatrix
(%i27) M3 : M [all, 3];
[ 11 ]
[ ]
[ 12 ]
[ ]
(%o27) [ 13 ]
[ ]
[ 14 ]
[ ]
[ 15 ]
Unmodified submatrix shares storage with original, but modified submatrix
has its own storage
(%i28) args (M);
(%o28) [5, 0, 1, 3, 0, 5, g16688, 0]
(%i29) args (M3);
(%o29) [5, 0, 1, 1, 2, 5, g16688, 1]
(%i30) args (M2);
(%o30) [3, 0, 5, 5, 0, 1, g16689, 0]