Quick question. I am struggling with what I think is a pretty trivial
problem. Say I have a 2x2 matrix of numbers of the form x+I*y where x
and y are doubles. Is there anyway to convert these numbers to the
form x+1j*y (I want to use numpy to calculate some eigenvalues)? Here
is an example:
In [1]: from sympy import I
In [2]: e = 1+I
In [3]: e.subs(I,1j)
Out[3]: 1 + I
In [4]: e.subs(I,numpy.complex(0,1))
Out[4]: 1 + I
Nothing I am trying seems to work. Is there something I am missing?
Thanks!
Bill
that's an interesting question. In principle you can't do e.subs(I, 1j)
or whatever similar because 1j (or equivalent) is sympified (converted)
to I and no substitution occurs. The tool to solve your problem should
be lambdify() function, e.g.:
In [1]: M = Matrix([[1,2],[3,4]])
In [2]: f = lambdify((), M, modules='numpy')
In [3]: f()
Out[3]:
[[1 2]
[3 4]]
In [4]: type(_)
Out[4]: <class 'numpy.core.defmatrix.matrix'>
Unfortunately lambdify() has problem with the imaginary unit:
In [5]: N = Matrix([[1+I,2],[3,4]])
In [6]: g = lambdify((), N, modules='numpy')
In [7]: g()
---------------------------------------------------------------------------
NameError Traceback (most recent call
last)
/home/matt/repo/git/sympy/<ipython console> in <module>()
/home/matt/repo/git/sympy/<string> in <lambda>()
NameError: global name 'I' is not defined
This is rather trivial issue to fix.
> Thanks!
>
> Bill
>
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>
--
Mateusz
On Fri, Apr 02, 2010 at 09:03:58AM -0700, wflynny wrote:
in my local branch (http://github.com/mattpap/sympy-polys/commits/polys8)
I had to extend lambdify(), so with the extension you can do the following:
In [1]: from sympy.printing.lambdarepr import LambdaPrinter
In [2]: class ImaginaryPrinter(LambdaPrinter):
...: def _print_ImaginaryUnit(self, expr):
...: return "1j"
...:
...:
In [3]: N = Matrix([[1+I,2],[3,4]])
In [4]: g = lambdify((), N, modules='numpy', printer=ImaginaryPrinter)
In [5]: g()
Out[5]:
[[ 1.+1.j 2.+0.j]
[ 3.+0.j 4.+0.j]]
In [6]: type(_)
Out[6]: <class 'numpy.core.defmatrix.matrix'>