3

In my Python script for the line

result = sp.solve(eqn)

I get the following output in the format similar to

result = 0.0100503358535014*I

I gather this means the solution is a complex number. So in this case it could also be seen as result = 0.01j.

I want to add formatting to my code to change result = 0.0100503358535014*I to something more like result = 0.01j. However I am finding issues trying to do this as I was trying to use isinstance to check if result was complex

if isinstance(result, complex):
        print "Result is complex!"
        ... my code here...

However this loop is never entered (i.e 0.0100503358535014*I isn't classified as complex here).

What way should I write an if statement to check if result is given in the manner xxxxxxx*I correctly?

6
  • What is I here? Some special constant? If you're not working with complex, what are you working with? Commented Mar 14, 2017 at 16:10
  • I too think that I is an actual symbol (it's probably a SymPy library), and not an indication of a complex number. Commented Mar 14, 2017 at 16:11
  • What is type(result)? Commented Mar 14, 2017 at 16:12
  • @Denziloe type(result) = <class 'sympy.core.mul.Mul'> Commented Mar 14, 2017 at 16:19
  • try if isinstance(result, sympy.core.mul.Mul): Commented Mar 14, 2017 at 16:19

3 Answers 3

4

SymPy supports Python's built-in function complex():

>>> import sympy
>>> x = sympy.sqrt(-2)
>>> x
sqrt(2)*I
>>> complex(x)
1.4142135623730951j

There are some examples in http://docs.sympy.org/latest/modules/evalf.html

A Python complex number can be formatted similar to a float:

>>> format(complex(x), 'e')
'0.000000e+00+1.414214e+00j'
>>> format(complex(x), 'f')
'0.000000+1.414214j'
>>> format(complex(x), 'g')
'0+1.41421j'

If you want to format the real and imag parts separately you can do it yourself.

The conversion would raise a TypeError if it can't be done:

>>> complex(sympy.Symbol('x'))
Traceback (most recent call last):
TypeError: can't convert expression to float
Sign up to request clarification or add additional context in comments.

Comments

1

Here's an alternative which incidentally indicates how to check whether one or both parts of a complex number are available.

>>> from sympy import *
>>> var('x')
x
>>> expr = (x+0.012345678*I)*(x-0.2346678934)*(x-(3-2.67893455*I))
>>> solve(expr)
[0.234667893400000, -0.012345678*I, 3.0 - 2.67893455*I]
>>> roots = solve(expr)
>>> for root in roots:
...     r, i = root.as_real_imag()
...     '%10.3f %10.3f i' % (r,i)
...     
'     0.235      0.000 i'
'     0.000     -0.012 i'
'     3.000     -2.679 i'

You could check the sign of the complex part to decide whether to put a plus sign in. I would like to have been able to use the newfangled formatting but fell afoul of the bug which comes with Py3.4+ mentioned in Python TypeError: non-empty format string passed to object.__format__ for which I have no remedy.

Comments

0

The following checks whether the sympy object result represents a complex number and truncates the float value to 2 decimal points.

import sympy as sp

result = 0.0100503358535014*sp.I
print(result.is_complex)
print(sp.N(result,2))
True
0.01*I

2 Comments

I have tried this way and it works however if I use an if statement like if (result.is_complex) then this return true for both type(result) = <class 'sympy.core.mul.Mul'> and type(result) = <class 'sympy.core.numbers.Float'> which isn't what I want. How do I get a specific if statement only for when the result has an *I part at the end?
is_complex will return True for a large class of SymPy expressions, including symbolic expressions, and even real numbers. x.is_complex just checks if x is a complex number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.