Ask Your Question
0

Matrices and mpmath

asked 2013-01-15 16:08:51 +0100

Erfan gravatar image

updated 2013-01-15 20:44:04 +0100

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.

edit retag flag offensive close merge delete

6 Answers

Sort by ยป oldest newest most voted
2

answered 2013-01-15 16:37:02 +0100

achrzesz gravatar image

updated 2013-01-15 16:38:03 +0100

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']])
edit flag offensive delete link more
2

answered 2013-01-15 18:25:56 +0100

achrzesz gravatar image

updated 2013-01-15 18:37:00 +0100

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]
edit flag offensive delete link more
1

answered 2013-01-15 16:39:28 +0100

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

answered 2013-01-15 18:16:05 +0100

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.

edit flag offensive delete link more
0

answered 2013-01-15 18:29:40 +0100

Erfan gravatar image

Thanks a lot!

edit flag offensive delete link more
0

answered 2013-01-15 17:13:17 +0100

Erfan gravatar image

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

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

Stats

Asked: 2013-01-15 16:08:51 +0100

Seen: 987 times

Last updated: Jan 15 '13