Ask Your Question

How to create a Matrix with real numbers?

asked 2017-04-24 09:30:12 -0500

Selfes gravatar image

I try to create a matrix with real number. When I try to create one, I get a TypeError message


sage: Z = matrix(2,2)
sage: Z[0,0] = sqrt(2)
TypeError                                 Traceback (most recent call last)
<ipython-input-29-94541bcbfa2b> in <module>()
----> 1 Z[Integer(0),Integer(0)] = sqrt(Integer(2))

sage/matrix/matrix0.pyx in sage.matrix.matrix0.Matrix.__setitem__ (/build/sagemath/src/sage-

sage/matrix/matrix0.pyx in sage.matrix.matrix0.Matrix._coerce_element (/build/sagemath/src/sage-

sage/structure/parent.pyx in sage.structure.parent.Parent.__call__ (/build/sagemath/src/sage-

sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.NamedConvertMap._call_ 

sage/symbolic/expression.pyx in sage.symbolic.expression.Expression._integer_ (/build/sagemath/src/sage-

TypeError: unable to convert sqrt(2) to an integer

Since I couldn't really find a way to create one in the documentation (at least I didn't find one), I would like to ask, if there is a easy solution to this.

Thank you very much

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2017-04-24 10:47:19 -0500

ndomes gravatar image

updated 2017-04-24 10:50:03 -0500

sage: Z = matrix(2,2)
sage: print Z.parent()


Full MatrixSpace of 2 by 2 dense matrices over Integer Ring

That means your matrix can contain only Integers. What you want is a matrix over the Symbolic Ring:

sage: Z = matrix(SR,2,2)
sage: print Z.parent()
sage: Z[0,0] = sqrt(5)
sage: print Z

Alternatively a matrix over a Real Field:

sage: Z = matrix(RR,2,2)
sage: print Z.parent()
sage: Z[0,0] = sqrt(5)
sage: print Z
edit flag offensive delete link more


Thank you very much.

Selfes gravatar imageSelfes ( 2017-04-24 13:22:14 -0500 )edit

answered 2017-04-24 10:49:46 -0500

kcrisman gravatar image

Alternately, you could make the matrix and Sage will "guess" the answer. See here:

L = [[sqrt(2),3],[4,log(2)]]
M = matrix(L)
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools


Asked: 2017-04-24 09:30:12 -0500

Seen: 78 times

Last updated: Apr 24