Cannot do mpmath.sqrtm() in sympy

301 views
Skip to first unread message

Yuxiang Wang

unread,
Nov 24, 2014, 2:05:20 PM11/24/14
to sy...@googlegroups.com

Dear all,

I have the following code to extract the square root of a symmetric second-order tensor.

from sympy import symbols, Matrix, mpmath
import numpy as np

F11, F12, F13, F21, F22, F23, F31, F32, F33 = symbols('F11, F12, F13, F21, F22, F23, F31, F32, F33', real=True)
F = np.array([[F11, F12, F13], [F21, F22, F23], [F31, F32, F33]])
B = F.dot(F.T)
mpmath.sqrtm(Matrix(B))

However, it gave me the error:

TypeError                                 Traceback (most recent call last)
<ipython-input-14-439fed475a57> in <module>()
      5 F = np.array([[F11, F12, F13], [F21, F22, F23], [F31, F32, F33]])
      6 B = F.dot(F.T)
----> 7 mpmath.sqrtm(Matrix(B))

X:\WinPython3\python-3.4.2.amd64\lib\site-packages\sympy\mpmath\matrices\calculus.py in sqrtm(ctx, A, _may_rotate)
    308 
    309         """
--> 310         A = ctx.matrix(A)
    311         # Trivial
    312         if A*0 == A:

X:\WinPython3\python-3.4.2.amd64\lib\site-packages\sympy\mpmath\matrices\matrices.py in __init__(self, *args, **kwargs)
    326                     A[i,j] = convert(A[i,j])
    327         elif hasattr(args[0], 'tolist'):
--> 328             A = self.ctx.matrix(args[0].tolist())
    329             self.__data = A._matrix__data
    330             self.__rows = A._matrix__rows

X:\WinPython3\python-3.4.2.amd64\lib\site-packages\sympy\mpmath\matrices\matrices.py in __init__(self, *args, **kwargs)
    299                 for i, row in enumerate(A):
    300                     for j, a in enumerate(row):
--> 301                         self[i, j] = convert(a)
    302             else:
    303                 # interpret list as row vector

X:\WinPython3\python-3.4.2.amd64\lib\site-packages\sympy\mpmath\ctx_mp_python.py in convert(ctx, x, strings)
    660         if hasattr(x, '_mpmath_'):
    661             return ctx.convert(x._mpmath_(prec, rounding))
--> 662         return ctx._convert_fallback(x, strings)
    663 
    664     def isnan(ctx, x):

X:\WinPython3\python-3.4.2.amd64\lib\site-packages\sympy\mpmath\ctx_mp.py in _convert_fallback(ctx, x, strings)
    612             else:
    613                 raise ValueError("can only create mpf from zero-width interval")
--> 614         raise TypeError("cannot create mpf from " + repr(x))
    615 
    616     def mpmathify(ctx, *args, **kwargs):

TypeError: cannot create mpf from F11**2 + F12**2 + F13**2

May I ask why that is happening? Is this a limitation of sympy or that I am doing something wrong?

Thank you!

Shawn

Tim Lahey

unread,
Nov 24, 2014, 2:17:08 PM11/24/14
to sy...@googlegroups.com
Hi,

mpmath is for multi-precision math, not symbolic math. It's expecting
numbers, not symbols. I don't think there's a square root routine for
symbolic matrices implemented.

Cheers,

Tim.

On 24 Nov 2014, at 14:05, Yuxiang Wang wrote:

> Dear all,
>
> I have the following code to extract the square root of a symmetric
> second-order tensor.
>
> from sympy import symbols, Matrix, mpmathimport numpy as np
>
> F11, F12, F13, F21, F22, F23, F31, F32, F33 = symbols('F11, F12, F13,
> F21, F22, F23, F31, F32, F33', real=True)
> F = np.array([[F11, F12, F13], [F21, F22, F23], [F31, F32, F33]])
> B = F.dot(F.T)
> mpmath.sqrtm(Matrix(B))
>
> However, it gave me the error:
>
> TypeError Traceback (most recent call
> last)<ipython-input-14-439fed475a57> in <module>()
> 5 F = np.array([[F11, F12, F13], [F21, F22, F23], [F31, F32, F33]])
> 6 B = F.dot(F.T)----> 7 mpmath.sqrtm(Matrix(B))
> --
> You received this message because you are subscribed to the Google
> Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/228b5b46-c2fe-4d90-83f6-4dd96927eddd%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Yuxiang Wang

unread,
Nov 24, 2014, 2:23:08 PM11/24/14
to sy...@googlegroups.com
Hi Tim,

Thank you for your response!

And thanks for letting me know that symbolic square root matrix routine was not implemented. 

I will try to do the following:

1) Get eigenvectors first;
2) Take square root of the eigenvalues;
3) Re-construct the square root matrix from the original eigenvectors and the sqrt of eigenvalues.

Thanks again!

Shawn

Aaron Meurer

unread,
Nov 24, 2014, 3:22:34 PM11/24/14
to sy...@googlegroups.com
It seems sqrt(Matrix) (where sqrt is sympy.sqrt) is unevaluated, but
if you do Matrix(B)**(S(1)/2) it tries to evaluate it (but it hangs).

You shouldn't use NumPy if you are dealing with symbolic matrices.
There is no advantage to using NumPy at that point, vs. sympy.Matrix.

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/79209aad-242d-4e63-9d65-b8c2d581aaf8%40googlegroups.com.

Yuxiang Wang

unread,
Nov 24, 2014, 6:30:48 PM11/24/14
to sy...@googlegroups.com
Hi Aaron,

Thank you for the response!

I tried to use numpy.ndarray because I got other orders of tensors, so I wanted to be consistent. But it seems that Matrix object can be higher-order too, so I'll try to use them. Thanks for the tip!

Shawn

Chris Smith

unread,
Nov 25, 2014, 3:33:43 PM11/25/14
to sy...@googlegroups.com
To take the sqrt of each element in B, use B.applyfunc(sqrt).

Yuxiang Wang

unread,
Nov 25, 2014, 4:02:54 PM11/25/14
to sy...@googlegroups.com
Chris,

Thanks for responding! I was actually doing the square-root of the matrix, rather than the element-wise. But thanks still!

-Shawn
Reply all
Reply to author
Forward
0 new messages