Ask Your Question
1

Matrix Multiplication

asked 2010-12-28 18:15:18 +0100

mhfrey gravatar image

updated 2010-12-28 18:40:58 +0100

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

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2010-12-28 18:44:37 +0100

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)
edit flag offensive delete link more
0

answered 2010-12-28 20:35:54 +0100

mhfrey gravatar image

That works, thank you for the quick answer.

edit flag offensive delete link more

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 ( 2011-01-03 22:03:09 +0100 )edit

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: 2010-12-28 18:15:18 +0100

Seen: 3,670 times

Last updated: Dec 28 '10