Maksim Zholudev
Maksim Zholudev

Reputation: 671

Multiply matrix of matrices by a scalar in sage

I have created a 2x2 matrix of 2x2 matrices:

a = matrix([[matrix([[ 1,  2], [ 3,  4]]),
             matrix([[ 5,  6], [ 7,  8]])],
            [matrix([[ 9, 10], [11, 12]]),
             matrix([[13, 14], [15, 16]])]])

When I multiple it by number (e.g. type a*3) the following error occurs:

TypeError: unsupported operand parent(s) for '*':
           'Full MatrixSpace of 2 by 2 dense matrices over Integer Ring' and
           'Full MatrixSpace of 2 by 2 dense matrices over Integer Ring'

This looks strange because Full MatrixSpace of 2 by 2 dense matrices over Integer Ring is the parent of the sub-matrices (parent(a[0,0])) and I can multiply them without any problem:

sage: a[0,0]*a[0,0]
[ 7 10]
[15 22]

Any of the following commands works fine:

sage: a[0,0]*3
sage: a[0,0]*a[0,0]
sage: a[0,0]*a
sage: a*a
sage: a[0,0]*3*a
sage: diagonal_matrix([3]*2)*a    # I don't want do this every time!

but these do not:

sage: a*3            #TypeError
sage: a[0,0]*a*3     #TypeError

So here are the questions:

  1. why it says that multiplication of 2x2 matrices is not supported?
  2. why does this multiplication appear when I multiple the matrix by a scalar?
  3. can I multiple the matrix by a scalar without the diagonal_matrix cheat?

Upvotes: 1

Views: 1436

Answers (2)

Harald Schilly
Harald Schilly

Reputation: 1108

I guess there is just a special case missing that's not yet implemented. Why not go with multidimensional arrays from numpy?

sage: import numpy
sage: x = numpy.arange(16)
sage: x.shape = (2,2,2,2)
sage: x
array([[[[ 0,  1],
         [ 2,  3]],
        [[ 4,  5],
         [ 6,  7]]],
       [[[ 8,  9],
         [10, 11]],
        [[12, 13],
         [14, 15]]]])

sage: 3*x
array([[[[ 0,  3],
         [ 6,  9]],
        [[12, 15],
         [18, 21]]],
       [[[24, 27],
         [30, 33]],
        [[36, 39],
         [42, 45]]]])

sage: x[0,0]
array([[0, 1],
       [2, 3]])

sage: x[0,0] * x[1,0]
array([[ 0,  9],
       [20, 33]])

sage: x[0,0].dot(x[1,0]) 
array([[10, 11],
       [46, 51]])

Upvotes: 1

John Palmieri
John Palmieri

Reputation: 1696

You're right that this should work, but as replacements,

sage: (3 * identity_matrix(2)) * a
sage: a * (3 * identity_matrix(2))

both work for me. If you need to do it a lot:

sage: id = identity_matrix(2)
sage: 3 * id * a

You can view this as a bug, or you can view it as a not-yet-implemented feature (combined with a misleading error message). I've reported it on the Sage bug tracker.

Upvotes: 2

Related Questions