ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 01 Feb 2013 13:45:08 -0600empty matrixhttps://ask.sagemath.org/question/9758/empty-matrix/Hello all,
I am currently implementing a numerical algorithm and I need to do a lot of matrix manipulations. I often use submatrices via `B[m:,:]`. The problem is that this operation occasionally yield 0xn matrices. Unfortunately those are handled like 0x0 matrices so I can't multiply them by vectors:
sage: identity_matrix(RDF, 4)[3:,:] * vector(RDF, 4)
(0.0)
sage: identity_matrix(RDF, 4)[4:,:] * vector(RDF, 4)
TypeError: unsupported operand parent(s) for '*':
'Full MatrixSpace of 0 by 0 dense matrices over Real Double Field'
and 'Vector space of dimension 4 over Real Double Field'
I think that the latter operation should be supported and yielding a vector of length 0. Can I force this kind of behaviour (maybe using numpy instead)?Fri, 01 Feb 2013 02:43:52 -0600https://ask.sagemath.org/question/9758/empty-matrix/Answer by John Palmieri for <p>Hello all,</p>
<p>I am currently implementing a numerical algorithm and I need to do a lot of matrix manipulations. I often use submatrices via <code>B[m:,:]</code>. The problem is that this operation occasionally yield 0xn matrices. Unfortunately those are handled like 0x0 matrices so I can't multiply them by vectors:</p>
<pre><code>sage: identity_matrix(RDF, 4)[3:,:] * vector(RDF, 4)
(0.0)
sage: identity_matrix(RDF, 4)[4:,:] * vector(RDF, 4)
TypeError: unsupported operand parent(s) for '*':
'Full MatrixSpace of 0 by 0 dense matrices over Real Double Field'
and 'Vector space of dimension 4 over Real Double Field'
</code></pre>
<p>I think that the latter operation should be supported and yielding a vector of length 0. Can I force this kind of behaviour (maybe using numpy instead)?</p>
https://ask.sagemath.org/question/9758/empty-matrix/?answer=14503#post-id-14503As you note, using `mat[m:,:]` can give the wrong size matrix. This is perhaps a bug.
sage: identity_matrix(RDF, 4)[4:,:].nrows()
0
sage: identity_matrix(RDF, 4)[4:,:].ncols()
0
If you use the `submatrix` method instead, you will get matrices of the correct size:
sage: identity_matrix(RDF, 4).submatrix(4,0,0,4).nrows()
0
sage: identity_matrix(RDF, 4).submatrix(4,0,0,4).ncols()
4
sage: identity_matrix(RDF, 4).submatrix(4,0,0,4) * vector([1,2,3,4])
()Fri, 01 Feb 2013 04:08:02 -0600https://ask.sagemath.org/question/9758/empty-matrix/?answer=14503#post-id-14503Answer by Jason Grout for <p>Hello all,</p>
<p>I am currently implementing a numerical algorithm and I need to do a lot of matrix manipulations. I often use submatrices via <code>B[m:,:]</code>. The problem is that this operation occasionally yield 0xn matrices. Unfortunately those are handled like 0x0 matrices so I can't multiply them by vectors:</p>
<pre><code>sage: identity_matrix(RDF, 4)[3:,:] * vector(RDF, 4)
(0.0)
sage: identity_matrix(RDF, 4)[4:,:] * vector(RDF, 4)
TypeError: unsupported operand parent(s) for '*':
'Full MatrixSpace of 0 by 0 dense matrices over Real Double Field'
and 'Vector space of dimension 4 over Real Double Field'
</code></pre>
<p>I think that the latter operation should be supported and yielding a vector of length 0. Can I force this kind of behaviour (maybe using numpy instead)?</p>
https://ask.sagemath.org/question/9758/empty-matrix/?answer=14505#post-id-14505This is indeed a bug. I've posted a fix here: [trac #14049](http://trac.sagemath.org/sage_trac/ticket/14049), which needs review. The problem was a faulty optimization, and it's a one-line fix. You could try applying it to your version of Sage, if you wanted. Thanks for reporting this!Fri, 01 Feb 2013 13:45:08 -0600https://ask.sagemath.org/question/9758/empty-matrix/?answer=14505#post-id-14505