How to simplify a sympy expression - python

Hello I have this sympy expression 0.5*c**2*m*(v/c)**2.0 and I want to bring it automatically to this form: 0.5*m*v**2. I tried the sympy simplify function but that doesn't seem to work. What can I do? Thank you!

These two expressions are not equivalent, only equivalent if c is not equal to 0. So if you want to get 0.5*m*v**2, you need set c as a positive number:``
>>> import sympy
>>> c= sympy.symbols('c',positive=True)
>>> m,v = sympy.symbols('m v')
>>> sympy.powsimp(0.5*c**2*m*(v/c)**2.0)
0.5*m*v**2.0
Besides, although sympy.simplify will give you what you want, I think sympy.powsimp is better. Because actually we want to reduce expression by combining powers with similar bases and exponents.

Related

Is there a convenient way to add complex numbers in polar form in sympy (python)?

I had some trouble adding complex numbers in polar form in sympy.
The following code
from sympy import I, exp, pi, re, im
a = exp(2*pi/3*I)
b = exp(-2*pi/3*I)
c = a+b
print(c)
print(c.simplify())
print(c.as_real_imag())
print(re(c)+im(c)*I)
print(int(c))
print(complex(c))
gives
exp(-2*I*pi/3) + exp(2*I*pi/3)
-(-1)**(1/3) + (-1)**(2/3)
(-1, 0)
-1
-1
(-1+6.776263578034403e-21j)
What I want, is to get the simplest answer to a+b, which is -1. I can obtain this, by manually rebuilding c=a+b with re(c)+im(c)*I. Why is this necessary? And is there a better way to do this?
Simply printing c retains the polar forms, obfuscating the answer, c.simplify() leaves the polar form, but is not really helpful, and c.as_real_imag() returns a tuple. int(c) does the job, but requires the knowledge, that c is real (otherwise it throws an error) and integer (otherwise, this is not the answer I want). complex(c) kind of works, but I don't want to leave symbolic calculation. Note, that float(c) does not work, since complex(c) has a non-zero imaginary part.
https://stackoverflow.com/users/9450991/oscar-benjamin has given you the solution. If you are in polar coordinates, your expression may have exponential functions. If you don't want these you have to rewrite into trigonometric functions where special values are known for many values. For example, consider a's 2*pi/3 angle:
>>> cos(2*pi/3)
-1/2
>>> sin(2*pi/3)
sqrt(3)/2
When you rewrite a in terms of cos (or sin) it becomes the sum of those two values (with I on the sin value):
>>> a.rewrite(cos)
-1/2 + sqrt(3)*I/2
When you rewrite a more complex expression, you will get the whole expression rewritten in that way and any terms that cancel/combine will do so (or might need some simplification):
>>> c.rewrite(cos)
-1

SymPy outputs a numerical result when a symbolic expression is expected

I'ld like my SymPy results to be displayed as precise results and not as decimal results. I looked through the SymPy documentation, but couldn't find anything helpful.
To illustrate the problem, here is some example code:
from sympy import *
u = symbols("u")
integrate((1+u)**(1/2), (u, 0, 1))
Output:
1.21895141649746
Expected result:
(4/3)*sqrt(2)-(2/3)
The problem is that SymPy will simplify expressions containing floats and so removes those symbolic expressions. Although the expression doesn't look like a float, the division 1/2 is done with standard Python, internally represented as 0.5.
The solution is to first convert one of the parts of the fraction to a SymPy object. The easiest way is with the function S, as in:
from sympy import *
u = symbols("u")
print(integrate((1+u)**(S(1)/2), (u, 0, 1)))
which outputs the desired -2/3 + 4*sqrt(2)/3.
The gotcha section of the documentation tries to explain this behavior.

Sympy allows definition of integer symbols but does not account for their behavior

I have the following set of commands to do a definite integral
n [2]: from sympy import *
init_printing()
x,L = symbols('x, L')
n = symbols('n', integer = True)
exp = sin((n+Rational(1,2))*pi*x/L)**2
integrate(exp, (x,0,L))
The result of these commands shown below:
The first result implies that n=-1/2 which implies that n is not an integer. What is the point of giving an integer attribute to the symbol if it doesn't account for it in operations as shown above? How can I force sympy to recognize that the first part of the piecewise result is impossible if n is integer?
If the equality had evaluated, this condition would have been rejected and the Piecewise would have evaluated to your expected result. Because SymPy didn't know if your L was zero or not, it couldn't evaluate that equality.
So try
>>> n = var('n', integer=True)
>>> L = var('L', nonzero=True)
>>> exp = sin((n+Rational(1,2))*pi*x/L)**2
>>> integrate(exp, (x,0,L))
L/2
And there you go! :-) (Note, however, that it should have been sufficient to just say that L was finite to know that the equality could never be true, but SymPy fails to evaluate that condition, too.)
/c
Declaring the symbol n as an integer has no consequences for the evaluating except for the simplification of expressions:
The assumptions system allows users to specify that symbols have certain common mathematical properties, such as being positive, imaginary, or integer. SymPy is careful to never perform simplifications on an expression unless the assumptions allow them.
and
Assumptions are only needed to restrict a domain so that certain simplifications can be performed. They are not required to make the domain match the input of a function.
So in your case there is no need to specify the symbol n to be an integer.

SymPy, how to perform exact symbolic representation (Rational Issue)?

So I'm planning to write a web application about math, and I need to convert user input to a SymPy expression without modifying it (Simplification), eg. so I'd like to cancel this behaviour like in this example.
>>> srepr(Rational(2,4)) #this is the problem
'Rational(1, 2)'
>>> srepr(Rational(2,4,evaluate=False)) #doesn't work
Traceback...
But I've managed to do it in other types of representations.
>>> srepr(Pow(x,(Mul(e,e,evaluate=False)),evaluate=False)) #nice
"Pow(Symbol('x'), Mul(Symbol('e'), Symbol('e')))"
>>> srepr(sqrt(Integer(8))) #not what I want
'Mul(Integer(2), Pow(Integer(2), Rational(1, 2)))'
>>> srepr(Pow(Integer(8),Rational(1,2),evaluate=False)) #this is the way
'Pow(Integer(8), Rational(1, 2))'
>>> from sympy import E
>>> log(E,evaluate=False)
log(E)
Also isn't there a way to tell SymPy that all representations shouldn't be evaluated?
Something like this, perhaps?
>>> S('2/4',evaluate=False)
2/4
>>> srepr(_)
'Mul(Integer(2), Pow(Integer(4), Integer(-1)))'

Calculating strings as values

Is it possible in Python to calculate a term in a string?
For example:
string_a = "4 ** (3 - 2)"
unknown_function(string_a) = 4
Is this possible? Is there a function that mimics "unknown_function" in my example?
Just like sympy was a useful module for your last question, it can apply here:
>>> import sympy
>>> sympy.sympify("4**(3-2)")
4
and even
>>> sympy.sympify("2*x+y")
2*x + y
>>> sympy.sympify("2*x+y").subs(dict(x=2, y=3))
7
Note though that this will return sympy objects, and if you want to get an integer or a float out of it you should do the conversion explicitly:
>>> type(sympy.sympify("4**(3-2)"))
<class 'sympy.core.numbers.Integer'>
>>> int(sympy.sympify("4**(3-2)"))
4
I hacked together a recipe to turn string expressions into functions here which is kind of cute.
There is eval
eval(string_a)
# 4
But do not use this under any circumstances if string_a comes from anyone but you, because they can easily hack into your system and destroy your files!
Yes, you can use the eval function.
>>> string_a = "4 ** (3 - 2)"
>>> eval(string_a)
4
>>>
You can read more in the documentation
There is a module py-expression-eval, that does not depend on the use of eval. It can be used to evaluate strings as a mathematical expression, even symbolic expressions can be evaluated.
from py_expression_eval import Parser
parser = Parser()
expr = parser.parse("4 ^ (3 - 2)")
expr.evaluate({})
For the use with symbolic expressions see:https://axiacore.com/blog/mathematical-expression-evaluator-python/

Categories

Resources