Python sympy symbols - python

When I use "x" and "z" as symbols, I have no problem with this code:
from sympy import *
x, z = symbols('x z')
y = -6*x**2 + 2*x*z**0.5 + 50*x - z
solve((diff(y, x), diff(y, z)))
y.subs({x: 5, z: 25})
But when I use "q" and "a", solve does not give me any solution.
q, a = symbols('q a')
y = -6*q**2 + 2*q*a**0.5 + 50*q - a
solve((diff(y, q), diff(y, a)))
y.subs({q: 5, a: 25})
As you can see I use "subs" to check that there is no typo in the objective function.
UPDATE: I used "Symbol" to set each variable individually, but again using "q" and "a" does not work.
# This works
x = Symbol('x')
z = Symbol('z')
y = -6*x**2 + 2*x*z**0.5 + 50*x - z
solve((diff(y, x), diff(y, z)))
# This does not work
q = Symbol('q')
a = Symbol('a')
y = -6*q**2 + 2*q*a**0.5 + 50*q-a
solve((diff(y, q), diff(y, a)))
Thank you.

Got it!
It all depends on an alphabetic order of your variables.
If you substitute x for z and z for x in your first example it will also stop working.
Internally solve sends the expression to the function _solve in sympy.solvers which then tries to solve your equation and fails many times.
Finally as a last effort what it does is it tries to solve -sqrt(a) + q or x - sqrt(z) by picking symbols from it through an internal function _ok_syms, with an argument that sorts those alphabetically (even without this argument it still would, but if wrapped with reversed it magically makes your examples works in the exactly opposite way).
And so it does solve x - sqrt(z) as x: sqrt(z) and -sqrt(a) + q as a: q**2.
While in the first case it ends up with an easily solvable 50 - 10*sqrt(z), in the second case it is lost on -12*q + 2*sqrt(q**2) + 50 as it is not able to simplify sqrt(q**2).
source:
a lot of testing on:
https://github.com/sympy/sympy/blob/master/sympy/solvers/solvers.py

Related

Collect sinusoids of same frequency

I have an expression of many sinusoids. Some frequencies are repeated, so I would like to collect the coefficients for identical frequencies. Seems like either collect() or factor() should so the trick but they only work on simple expressions and fail when many variables are involved. Is there a better way to collect terms? Is there a way to help guide factor() to what variable to operate on like exists for collect()? I am trying to manually simplify the equation into a particular form and I think cse() goes too far in reworking terms. Examples below:
w, x, y, z = symbols('w x y z')
factor(z*sin(2*pi*x)+y*sin(2*pi*x), deep=True)
(y + z)⋅sin(2⋅π⋅x) #WORKS
factor(z*sin(2*pi*x*w)+y*sin(2*pi*x*w), deep=True)
(y + z)⋅sin(2⋅π⋅w⋅x) #WORKS
factor(z*sin(2*pi*x)+y*sin(2*pi*x)+z*sin(w)+2*y*sin(w), deep=True)
2⋅y⋅sin(w) + y⋅sin(2⋅π⋅x) + z⋅sin(w) + z⋅sin(2⋅π⋅x) # FAILS – expected Expected (2y + z)*sin(w) + (y+z)⋅sin(2⋅π⋅x)
collect(z*sin(2*pi*x)+y*sin(2*pi*x), x)
y⋅sin(2⋅π⋅x) + z⋅sin(2⋅π⋅x) # FAILS expected (y+z)⋅sin(2⋅π⋅x)
collect(z*sin(2*pi*x)+y*sin(2*pi*x)+z*sin(w)+2*y*sin(w), x)
2⋅y⋅sin(w) + y⋅sin(2⋅π⋅x) + z⋅sin(w) + z⋅sin(2⋅π⋅x) #FAILS Expected (2y + z)*sin(w) + (y+z)⋅sin(2⋅π⋅x)
It is:
>>> from sympy import *
>>> w, x, y, z = symbols('w x y z')
>>> ex= z*sin(2*pi*x)+y*sin(2*pi*x)+z*sin(w)+2*y*sin(w)
>>> ex
2*y*sin(w) + y*sin(2*pi*x) + z*sin(w) + z*sin(2*pi*x)
>>> collect(ex, [sin(w), sin(2*pi*x)])
(y + z)*sin(2*pi*x) + (2*y + z)*sin(w)
Reference: https://docs.sympy.org/latest/modules/simplify/simplify.html?highlight=collect

Sympy.solve returning: " [] "

I was working on a program that takes an input, and solves it. I used Sympy. I assumed because this works:
from sympy import symbols, Eq, solve
x, y = symbols("x y")
eq1 = Eq(5 + x)
eq2 = Eq(5 + y)
sol = solve((eq1, eq2),(x, y))
print(sol)
Gives the result:
{x: -5, y: -5}
This should work too, because I'm splitting it, formatting the code correctly, and it has the inputs of "a" and "z":
from sympy import symbols, Eq, solve
Input = input("Please give the two variables the names 'a' and 'z': ").replace("x", "*").replace("^", "**").upper().split(" = ")
a, z = symbols("a z")
othersol = solve((Input[0], Input[1]),(a, z))
print(othersol)
Though, when given the input:
5 + a = 5 + z
It gives the result:
[]
I want to know why it is not solving, why it doesn't work, and how I can make a program that takes an input and solves the equation given. If I need to, I will even switch my Python Library. If not, can you give the code that takes an input, and solves that equation?
Can anybody do that?
Thanks.
You see what is wrong in here when you compare what you are giving as first parameter to your solve function.
In your working code you do like this:
eq1 = Eq(5 + x)
eq2 = Eq(5 + y)
sol = solve((eq1, eq2),(x, y))
First parameter is two Eq objects.
Let's see what is going on in your non-working code.
>>> Input = input("Please give the two variables the names 'a' and 'z': ").replace("x", "*").replace("^", "**").upper().split(" = ")
Please give the two variables the names 'a' and 'z': a+1 = zx3
>>> Input
['A+1', 'Z*3']
>>> type(Input[0])
<class 'str'>
>>> solve((Input[0], Input[1]),(a, z))
[]
There first parameter is tuple of two strings.
So you'd need to parse your Input-strings to Eq objects. Then you'd have working code. It may be challenging task though if you want to allow your user enter freetext input. Input in polish notation or some other more strcutured way would be much easier to parse.
--- EDIT ----
With exec it could be something like this. I Added a function to secure input a bit. This will fail if there is unknown characters in input.
from sympy import symbols, Eq, solve
A, Z = symbols("A Z")
def secure_input(inputstring):
known_symbols = {" ","A","Z","+","*","**","-"}
if not all([x in known_symbols or x.isdigit() for x in inputstring]):
raise Exception("Illegal strings in input %s"%inputstring)
return inputstring
Input = input("Please give the two variables the names 'a' and 'z': ").replace("x", "*").replace("^", "**").upper().split(" = ")
i1 = secure_input(Input[0])
i2 = secure_input(Input[1])
exec("eq1 = Eq(%s)" % i1)
exec("eq2 = Eq(%s)" % i2)
solve((eq1,eq2),(A, Z))
Also, you used lower-case variable names but you said upper() for your input string. I changed that as well, now the code should work.
from sympy import symbols, Eq, solve
A, Z = symbols("A Z")
def secure_input(inputstring):
known_symbols = {" ","A","Z","+","*","**","-"}
if not all([x in known_symbols or x.isdigit() for x in inputstring]):
raise Exception("Illegal strings in input %s"%inputstring)
return inputstring
Input = input("Please give the two variables the names 'a' and 'z': ").replace("x", "*").replace("^", "**").upper().split(" = ")
i1 = secure_input(Input[0])
i2 = secure_input(Input[1])
exec("eq1 = Eq(%s)" % i1)
exec("eq2 = Eq(%s)" % i2)
A = solve((eq1,eq2),A)
Z = solve((eq1,eq2),Z)
print(str(A).replace("{","").replace("}", ""))
print(str(Z).replace("{","").replace("}", ""))

How to edit the order of sympy latex print? [duplicate]

I have the following code:
from sympy import *
init_printing()
x,y = symbols('x y')
u = Function('u')(x,y)
ux,uy,uxx,uxy,uyy = symbols("u_x u_y u_xx u_xy u_yy")
mainEvaluation = uxx - 2*sin(x)*uxy - (cos(x) ** 2) * uyy - 2*ux + (2 - cos(x) + 2*sin(x) )*uy
And when the output of print(mainExpression) is
-2*u_x + u_xx - 2*u_xy*sin(x) + u_y*(2*sin(x) - cos(x) + 2) - u_yy*cos(x)**2
The problem is: I want the original order of variables.
u_xx - 2*u_xy*sin(x) - u_yy*cos(x)**2 - 2*u_x + u_y*(2*sin(x) - cos(x) + 2)
All this is done in IPython notebook.
Is there any way to keep order?
Sadly, SymPy does not keep track of the input order (see the other question I linked in a comment on the question). You can define your own ordering function that orders expressions however you want, but there's no way to order things exactly as they were input, since that information isn't saved.
If you know what your arguments/terms are then you can manually create the Add with evaluate=False to keep them in order and print them with a printer initialized to not change the order:
x,y = symbols('x y')
u = Function('u')(x,y)
ux,uy,uxx,uxy,uyy = symbols("u_x u_y u_xx u_xy u_yy")
args = uxx , -2*sin(x)*uxy, -cos(x)**2*uyy, -2*ux, +(2-cos(x)+2*sin(x))*uy
expr = Add(*args, evaluate=False)
from sympy.printing.str import StrPrinter # or LatexPrinter from .latex)
StrPrinter(dict(order='none'))._print_Add(expr)
This outputs
u_xx - 2*u_xy*sin(x) - u_yy*cos(x)**2 - 2*u_x + u_y*(2 - cos(x) + 2*sin(x))
try read this http://docs.sympy.org/0.7.2/modules/utilities/misc.html, may be could help you
Note:
The key returned is useful for getting items into a canonical order that will be the same across platforms. It is not directly useful for sorting lists of expressions:
>>> a, b = x, 1/x
Since a has only 1 term, its value of sort_key is unaffected by order:
>>> a.sort_key() == a.sort_key('rev-lex')
True
If a and b are combined then the key will differ because there are terms that can be ordered:
>>> eq = a + b
>>> eq.sort_key() == eq.sort_key('rev-lex')
False
>>> eq.as_ordered_terms()
[x, 1/x]
>>> eq.as_ordered_terms('rev-lex')
[1/x, x]
But since the keys for each of these terms are independent of order‘s value, they don’t sort differently when they appear separately in a list:
>>> sorted(eq.args, key=default_sort_key)
[1/x, x]
>>> sorted(eq.args, key=lambda i: default_sort_key(i, order='rev-lex'))
[1/x, x]
The order of terms obtained when using these keys is the order that would be obtained if those terms were factors in a product.
For the teaching purpose, I also don't want to simpilify or change the order of terms too early.
I'm using pytexit with jupyter notebook: https://pytexit.readthedocs.io/en/latest/
from pytexit import py2tex
def showeq(str):
py2tex(str, print_formula=False);
eq1 = "angle = acos((side_A**2 + side_B**2 - side_C**2)/(2*side_A*side_B))"
show(eq1)
side_A = 14.8
side_B = 16.3
side_C = 13.2
exec(eq1)

Collecting like term of an expression in Sympy

I am currently dealing with functions of more than one variable and need to collect like terms in an attempt to simplify an expression.
Say the expression is written as follows:
x = sympy.Symbol('x')
y = sympy.Symbol('y')
k = sympy.Symbol('k')
a = sympy.Symbol('a')
z = k*(y**2*(a + x) + (a + x)**3/3) - k((2*k*y*(a + x)*(n - 1)*(-k*(y**2*(-a + x) + (-a + x)**3/3) + k*(y**2*(a + x) + (a + x)**3/3)) + y)**2*(-a + k*(n - 1)*(y**2 + (a + x)**2)*(-k*(y**2*(-a + x)))))
zEx = z.expand()
print type(z)
print type(zEx)
EDIT: Formatting to add clarity and changed the expression z to make the problem easier to understand.
Say z contains so many terms, that sifting through them by eye. and selecting the appropriate terms, would take an unsatisfactory amount of time.
I want to collect all of the terms which are ONLY a multiple of a**1. I do not care for quadratic or higher powers of a, and I do not care for terms which do not contain a.
The type of z and zEx return the following:
print type(z)
print type(zEx)
>>>
<class 'sympy.core.add.Add'>
<class 'sympy.core.mul.Mul'>
Does anyone know how I can collect the terms which are a multiple of a , not a^0 or a^2?
tl'dr
Where z(x,y) with constants a and k described by z and zEx and their type(): How can one remove all non-a terms from z AND remove all quadratic or higher terms of a from the expression? Such that what is left is only the terms which contain a unity power of a.
In addition to the other answers given, you can also use collect as a dictionary.
print(collect(zEx,a,evaluate=False)[a])
yields the expression
k*x**2 + k*y**2
In the end it is just an one-liner. #asmeurer brought me on the right track (check the comments below this post). Here is the code; explanations can be found below:
from sympy import *
from sympy.parsing.sympy_parser import parse_expr
import sys
x, y, k, a = symbols('x y k a')
# modified string: I added a few terms
z = x*(k*a**9) + (k**1)*x**2 - k*a**8 + y*x*(k**2) + y*(x**2)*k**3 + x*(k*a**1) - k*a**3 + y*a**5
zmod = Add(*[argi for argi in z.args if argi.has(a)])
Then zmod is
a**9*k*x - a**8*k + a**5*y - a**3*k + a*k*x
So let's look at this more carefully:
z.args
is just a collection of all individual terms in your expression (please note, that also the sign is parsed which makes things easier):
(k*x**2, a**5*y, -a**3*k, -a**8*k, a*k*x, a**9*k*x, k**2*x*y, k**3*x**2*y)
In the list comprehension you then select all the terms that contain an a using the function has. All these terms can then be glued back together using Add which gives you the desired output.
EDIT
The above returns all all the expressions that contain an a. If you only want to filter out the expressions that contain a with unity power, you can use collect and Mul:
from sympy import *
from sympy.parsing.sympy_parser import parse_expr
import sys
x, y, k, a = symbols('x y k a')
z2 = x**2*(k*a**1) + (k**1)*x**2 - k*a**8 + y*x*(k**2) + y*(x**2)*k**3 + x*k*a - k*a**3 + y*a**1
zc = collect(z2, a, evaluate=False)
zmod2 = Mul(zc[a], a)
then zmod2 is
a*(k*x**2 + k*x + y)
and zmod2.expand()
a*k*x**2 + a*k*x + a*y
which is correct.
With the updated z you provide I run:
z3 = k*(y**2*(a + x) + (a + x)**3/3) - k((2*k*y*(a + x)*(n - 1)*(-k*(y**2*(-a + x) + (-a + x)**3/3) + k*(y**2*(a + x) + (a + x)**3/3)) + y)**2*(-a + k*(n - 1)*(y**2 + (a + x)**2)*(-k*(y**2*(-a + x)))))
zc3 = collect(z3.expand(), a, evaluate=False)
zmod3 = Mul(zc3[a], a)
and then obtain for zmod3.expand():
a*k*x**2 + a*k*y**2
Is this the result you were looking for?
PS: Thanks to #asmeurer for all these helpful comments!
To iterate over the terms of an expression use expr.args.
I'm unclear what a is supposed to be, but the collect function may do what you want.

Sympy: working with equalities manually

I'm currently doing a maths course where my aim is to understand the concepts and process rather than crunch through problem sets as fast as possible. When solving equations, I'd like to be able to poke at them myself rather than have them solved for me.
Let's say we have the very simple equation z + 1 = 4- if I were to solve this myself, I would obviously subtract 1 from both sides, but I can't figure out if sympy provides a simple way to do this. At the moment the best solution I can come up with is:
from sympy import *
z = symbols('z')
eq1 = Eq(z + 1, 4)
Eq(eq1.lhs - 1, eq1.rhs - 1)
# Output:
# z == 3
Where the more obvious expression eq1 - 1 only subtracts from the left-hand side. How can I use sympy to work through equalities step-by-step like this (i.e. without getting the solve() method to just given me the answer)? Any pointers to the manipulations that are actually possible with sympy equalities would be appreciated.
There is a "do" method and discussion at https://github.com/sympy/sympy/issues/5031#issuecomment-36996878 that would allow you to "do" operations to both sides of an Equality. It's not been accepted as an addition to SymPy but it is a simple add-on that you can use. It is pasted here for convenience:
def do(self, e, i=None, doit=False):
"""Return a new Eq using function given or a model
model expression in which a variable represents each
side of the expression.
Examples
========
>>> from sympy import Eq
>>> from sympy.abc import i, x, y, z
>>> eq = Eq(x, y)
When the argument passed is an expression with one
free symbol that symbol is used to indicate a "side"
in the Eq and an Eq will be returned with the sides
from self replaced in that expression. For example, to
add 2 to both sides:
>>> eq.do(i + 2)
Eq(x + 2, y + 2)
To add x to both sides:
>>> eq.do(i + x)
Eq(2*x, x + y)
In the preceding it was actually ambiguous whether x or i
was to be added but the rule is that any symbol that are
already in the expression are not to be interpreted as the
dummy variable. If we try to add z to each side, however, an
error is raised because now it is unclear whether i or z is being
added:
>>> eq.do(i + z)
Traceback (most recent call last):
...
ValueError: not sure what symbol is being used to represent a side
The ambiguity must be resolved by indicating with another parameter
which is the dummy variable representing a side:
>>> eq.do(i + z, i)
Eq(x + z, y + z)
Alternatively, if only one Dummy symbol appears in the expression then
it will be automatically used to represent a side of the Eq.
>>> eq.do(2*Dummy() + z)
Eq(2*x + z, 2*y + z)
Operations like differentiation must be passed as a
lambda:
>>> Eq(x, y).do(lambda i: i.diff(x))
Eq(1, 0)
Because doit=False by default, the result is not evaluated. to
evaluate it, either use the doit method or pass doit=True.
>>> _.doit == Eq(x, y).do(lambda i: i.diff(x), doit=True)
True
"""
if not isinstance(e, (FunctionClass, Lambda, type(lambda:1))):
e = S(e)
imaybe = e.free_symbols - self.free_symbols
if not imaybe:
raise ValueError('expecting a symbol')
if imaybe and i and i not in imaybe:
raise ValueError('indicated i not in given expression')
if len(imaybe) != 1 and not i:
d = [i for i in imaybe if isinstance(i, Dummy)]
if len(d) != 1:
raise ValueError(
'not sure what symbol is being used to represent a side')
i = set(d)
else:
i = imaybe
i = i.pop()
f = lambda side: e.subs(i, side)
else:
f = e
return self.func(*[f(side) for side in self.args], evaluate=doit)
from sympy.core.relational import Equality
Equality.do = do

Categories

Resources