First time here? Check out the FAQ!

Ask Your Question
1

Matrix Multiplication

asked 14 years ago

mhfrey gravatar image

updated 14 years ago

Mike Hansen gravatar image

I am trying to multiply the following:

Iz=1/2*matrix(2,2,[[1,0],[0,-1]])
a=(1,0)
Iz*a

This should work, what am I doing wrong? I would expect (1/2, 0)

Sage returns the following error:

Traceback (click to the left of this block for traceback)
...
TypeError: 'sage.matrix.matrix_rational_dense.Matrix_rational_dense'
object cannot be interpreted as an index

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_149.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" +    _support_.preparse_worksheet_cell(base64.b64decode("SXo9MS8yKm1hdHJpeCgyLDIsW1sxLDBdLFswLC0xXV0pO0l6CmE9KDEsMCkKSXoqYQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>

  File "/tmp/tmpJoBb2Y/___code___.py", line 5, in <module>
    exec compile(u'Iz*a
  File "", line 1, in <module>

  File "element.pyx", line 2260, in sage.structure.element.Matrix.__mul__     (sage/structure/element.c:14881)
  File "coerce.pyx", line 759, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (sage/structure/coerce.c:6940)
TypeError: 'sage.matrix.matrix_rational_dense.Matrix_rational_dense' object cannot be interpreted as an index

Thanx

Preview: (hide)

2 Answers

Sort by » oldest newest most voted
3

answered 14 years ago

Mike Hansen gravatar image

The issue is that when you do a = (1,0) you are creating a tuple which is one of Python's built-in data types.

sage: type((1,0))
<type 'tuple'>

Sage doesn't "know" how to multiply a matrix and a tuple.

If you make a a vector instead, then thing will work like you want:

sage: Iz=1/2*matrix(2,2,[[1,0],[0,-1]])
sage: a = vector([1,0])
sage: type(a)
<type 'sage.modules.vector_rational_dense.Vector_rational_dense'>
sage: Iz * a
(1/2, 0)
Preview: (hide)
link
0

answered 14 years ago

mhfrey gravatar image

That works, thank you for the quick answer.

Preview: (hide)
link

Comments

If you want to, you can accept Mike's answer, so that other users know this question was answered correctly when they search for a similar problem!

kcrisman gravatar imagekcrisman ( 14 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 14 years ago

Seen: 3,800 times

Last updated: Dec 28 '10