First time here? Check out the FAQ!

Ask Your Question
0

Matrices and mpmath

asked 12 years ago

Erfan gravatar image

updated 12 years ago

kcrisman gravatar image

Hello, I'm trying to work with matrices. I type matrix(4) and it gives me a 4 by 4 matrix of zeros which is fine. But when I use the command from mpmath import * then try to create matrices with the same command (matrix(4)) it gives an error message as below:

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

  File "/tmp/tmpdJ1MFV/___code___.py", line 3, in <module>
    exec compile(u't=matrix(_sage_const_5 )
  File "", line 1, in <module>

  File "/sagenb/sage_install/sage-5.4-sage.math.washington.edu-x86_64-Linux/local/lib/python2.7/site-packages/mpmath/matrices/matrices.py", line 332, in __init__
    raise TypeError('could not interpret given arguments')
TypeError: could not interpret given arguments

Since I need this command (from mpmath import *) for other operations, I have to use it. On the other hand it disrupts my matrix calculations. Is there anyway which I can have both working together in Sage similar to ipython?

Any help would be appreciated. Thank you.

Preview: (hide)

6 Answers

Sort by » oldest newest most voted
2

answered 12 years ago

achrzesz gravatar image

updated 12 years ago

You can use mpmath without: from mpmath import *

For example:

sage: import mpmath as mp     
sage: matrix(4)          
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
[0 0 0 0]
sage: mp.matrix([[1,2],[3,4]])
matrix(
[['1.0', '2.0'],
 ['3.0', '4.0']])
Preview: (hide)
link
2

answered 12 years ago

achrzesz gravatar image

updated 12 years ago

Use mpmath operations to mpmath matrices

M=mp.matrix([[2,0],[0,2]])
mp.inverse(M)             
matrix(
[['0.5', '0.0'],
 ['0.0', '0.5']])

You can also control precision in Sage:

sage: m=matrix(RealField(100),[[2,0],[0,2]])
sage: m^-1
[0.50000000000000000000000000000 0.00000000000000000000000000000]
[0.00000000000000000000000000000 0.50000000000000000000000000000]
Preview: (hide)
link
1

answered 12 years ago

fredrik gravatar image

Don't use import *. Instead of

from mpmath import *
foo() + bar()

do

from mpmath import foo, bar
foo() + bar()

or

import mpmath
mpmath.foo() + mpmath.bar()
Preview: (hide)
link
0

answered 12 years ago

Erfan gravatar image

I'm trying to due high precision numerical work so I need to use mpf numbers. When we define matrices in sage using mpmath it seems that it loses the linalg operations such as inversion, so you can't calculate the inverse of a matrix as before. Is there a specific command for it?

For example:

Sage: M=matrix([[2,0],[0,2]])

Sage: M**-1

[1/2 0]

[ 0 1/2]

But:

Sage: M=mp.matrix([[2,0],[0,2]])

Sage: M**-1

Gives the error:

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

File "/tmp/tmps6geGH/___code___.py", line 4, in <module> exec compile(u'M**-_sage_const_1 File "", line 1, in <module>

File "/sagenb/sage_install/sage-5.4-sage.math.washington.edu-x86_64-Linux/local/lib/python2.7/site-packages/mpmath/matrices/matrices.py", line 603, in __pow__ raise ValueError('only integer exponents are supported') ValueError: only integer exponents are supported

It seems that Sage is not very friendly with mpmath. Is there something that should be imported or a specific command to use?

Thank you.

Preview: (hide)
link
0

answered 12 years ago

Erfan gravatar image

Thanks a lot!

Preview: (hide)
link
0

answered 12 years ago

Erfan gravatar image

Thank you. That works. I have to then alter my code in ipython a bit..

Preview: (hide)
link

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: 12 years ago

Seen: 1,082 times

Last updated: Jan 15 '13