evan54
evan54

Reputation: 3733

python / mpmath - How to get the real values of a complex matrix?

I am trying to get the real values of a complex valued matrix.

import mpmath as mp
A = mp.matrix([[1+1j, 2+2j],[3+2j, 4+2j]])

I've tried both:

mp.re(A)
np.real(A)

but neither work.

I've also tried looking for information here but haven't found anything http://docs.sympy.org/0.6.7/modules/mpmath/basics.html

The first gives an error message: cannot create mpf from matrix ...

The second gives: insufficient indices for matrix

any help appreciated

Upvotes: 0

Views: 2219

Answers (2)

Benjamin
Benjamin

Reputation: 226

Just if anybody else is wondering: The easiest way to loop through the matrix is using the apply function of a mp.matrix.

import mpmath as mp

X = mp.matrix([[1+10j, 2+20j],[3+30j, 4+40j]])
real = X.apply(mp.re)
imag = X.apply(mp.im)

Upvotes: 3

user3654387
user3654387

Reputation: 2330

mp.re(A[0,0]), mp.re(A[0,1]),mp.re(A[1,0]), and mp.re(A[1,1]) all work, but you are right that mp.re(A) doesn't work. For the time being you can loop through the matrix until you find a vectorized solution.

Upvotes: 2

Related Questions