08.02.2015 Views

Sage Reference Manual: Matrices and Spaces of Matrices - Mirrors

Sage Reference Manual: Matrices and Spaces of Matrices - Mirrors

Sage Reference Manual: Matrices and Spaces of Matrices - Mirrors

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Sage</strong> <strong>Reference</strong> <strong>Manual</strong>: <strong>Matrices</strong> <strong>and</strong> <strong>Spaces</strong> <strong>of</strong> <strong>Matrices</strong>, Release 6.1.1<br />

sage: R. = QuaternionAlgebra(-1, -1)<br />

sage: A = matrix(R, 2, [1,i,j,k])<br />

sage: B = matrix(R, 2, [i,i,i,i])<br />

sage: A.elementwise_product(B)<br />

[ i -1]<br />

[-k j]<br />

sage: B.elementwise_product(A)<br />

[ i -1]<br />

[ k -j]<br />

Input that is not a matrix will raise an error.<br />

sage: A = r<strong>and</strong>om_matrix(ZZ,5,10,x=20)<br />

sage: A.elementwise_product(vector(ZZ, [1,2,3,4]))<br />

Traceback (most recent call last):<br />

...<br />

TypeError: oper<strong>and</strong> must be a matrix, not an element <strong>of</strong> Ambient free module <strong>of</strong> rank 4 over th<br />

<strong>Matrices</strong> <strong>of</strong> different sizes for oper<strong>and</strong>s will raise an error.<br />

sage: A = r<strong>and</strong>om_matrix(ZZ,5,10,x=20)<br />

sage: B = r<strong>and</strong>om_matrix(ZZ,10,5,x=40)<br />

sage: A.elementwise_product(B)<br />

Traceback (most recent call last):<br />

...<br />

TypeError: incompatible sizes for matrices from: Full MatrixSpace <strong>of</strong> 5 by 10 dense matrices<br />

Some pairs <strong>of</strong> rings do not have a common parent where multiplication makes sense. This will raise an<br />

error.<br />

sage: A = matrix(QQ, 3, range(6))<br />

sage: B = matrix(GF(3), 3, [2]*6)<br />

sage: A.elementwise_product(B)<br />

Traceback (most recent call last):<br />

...<br />

TypeError: no common canonical parent for objects with parents: ’Full MatrixSpace <strong>of</strong> 3 by 2<br />

We illustrate various combinations <strong>of</strong> sparse <strong>and</strong> dense matrices. Notice how if base rings are unequal,<br />

both oper<strong>and</strong>s must be sparse to get a sparse result.<br />

sage: A = matrix(ZZ, 5, range(30), sparse=False)<br />

sage: B = matrix(ZZ, 5, range(30), sparse=True)<br />

sage: C = matrix(QQ, 5, range(30), sparse=True)<br />

sage: A.elementwise_product(C).is_dense()<br />

True<br />

sage: B.elementwise_product(C).is_sparse()<br />

True<br />

sage: A.elementwise_product(B).is_dense()<br />

True<br />

sage: B.elementwise_product(A).is_dense()<br />

True<br />

TESTS:<br />

Implementation for dense <strong>and</strong> sparse matrices are different, this will provide a trivial test that they are<br />

working identically.<br />

sage: A = r<strong>and</strong>om_matrix(ZZ, 10, x=1000, sparse=False)<br />

sage: B = r<strong>and</strong>om_matrix(ZZ, 10, x=1000, sparse=False)<br />

sage: C = A.sparse_matrix()<br />

168 Chapter 7. Base class for matrices, part 2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!