Simplify removes relational term in Sympy? - python

I'm trying to simplify an expression using sympy but the relational terms seem to disappear. A toy example is as follows:
import sympy
from sympy import *
x = Symbol('x')
y = Symbol('y')
z = Symbol('z')
If I run:
z * Eq(x, y)
Then the output is:
z*(x == y)
But if I try to simplify this using:
simplify(z * Eq(x, y))
Then the output is:
z
Which I would not expect - should I expect this behaviour and if so, is there any way to prevent simplify from removing the relational term?
Thanks.

Logic and arithmetic operations cannot be combined to make such operations.
Supposing:
from sympy import *
x, y, z = symbols('x y z')
f = symbols('f', cls=Function)
For arithmetic operation:
xeqy = Piecewise((1,Eq(x,y)),(0,True)) # {1 for x = y, 0 otherwise}
f = z * xeqy # {z for x = y, 0 otherwise}
simplify(f)
For logical operation:
f = And(z,Eq(x,y)) # z ∧ (x = y)
simplify(f)

Related

How to differentiate a expression with respect to a symbol that is not a free symbol of the expression in SymPy?

I have the following equation, like this:
y = 3x2 + x
Then, I want to differentiate the both side w.r.t the variable t with sympy. I try to implement it in the following code in JupyterNotebook:
>>> import sympy as sp
>>> x, y, t = sp.symbols('x y t', real=True)
>>> eq = sp.Eq(y, 3 * x **2 + x)
>>>
>>> expr1 = eq.lhs
>>> expr1
𝑦
>>> expr1.diff(t)
0
>>>
>>> expr2 = eq.rhs
>>> expr2
3𝑥^2+𝑥
>>> expr2.diff(t)
0
As the result, sympy will treat the symbol x and y as a constant. However, the ideal result I want should be the same as the result derived manually like this:
y = 3x2 + x
d/dt (y) = d/dt (3x2 + x)
dy/dt = 6 • x • dx/dt + 1 • dx/dt
dy/dt = (6x + 1) • dx/dt
How can I do the derivative operation on a expression with a specific symbol which is not a free symbol in the expression?
You should declare x and y as functions rather than symbols e.g.:
In [8]: x, y = symbols('x, y', cls=Function)
In [9]: t = symbols('t')
In [10]: eq = Eq(y(t), 3*x(t)**2 + x(t))
In [11]: eq
Out[11]:
2
y(t) = 3⋅x (t) + x(t)
In [12]: Eq(eq.lhs.diff(t), eq.rhs.diff(t))
Out[12]:
d d d
──(y(t)) = 6⋅x(t)⋅──(x(t)) + ──(x(t))
dt dt dt
https://docs.sympy.org/latest/modules/core.html#sympy.core.function.Function
Alternatively, the idiff function was made for this purpose but it works with expressions like f(x, y) and can return the value of dy/dx. So first make your Eq and expression and then calculate the desired derivative:
>>> from sympy import idiff
>>> e = eq.rewrite(Add)
>>> dydx = idiff(e, y, x); dydx
6*x + 1
Note, too, that even in your equation (if you write it explicitly in terms of functions of t) you do not need to isolate y(t) -- you can differentiate and solve for it:
>>> from sympy.abc import t
>>> x,y=map(Function,'xy')
>>> eq = x(t)*(y(t)**2 - y(t) + 1)
>>> yp=y(t).diff(t); Eq(yp, solve(eq.diff(t),yp)[0])
Eq(Derivative(y(t), t), (-y(t)**2 + y(t) - 1)*Derivative(x(t), t)/((2*y(t) - 1)*x(t)))

Substitute one equation into another in SymPy

Suppose I have equations x = z + 2 and y = x + 1 and I wish to substitute the first one into the second one, to eliminate x and get y = z + 3. In SymPy, I can create the first two equations as:
x = sympy.symbols('x')
y = sympy.symbols('y')
z = sympy.symbols('z')
equation_one = sympy.Eq(x, z + 2)
equation_two = sympy.Eq(y, x + 1)
What is the correct way to now substitute equation_one into equation_two? The output should be a new equation.
An approach that works in this case is to use the attributes lhs/rhs ("left hand side" and "right hand side").
import sympy as sp
x = sp.symbols('x')
y = sp.symbols('y')
z = sp.symbols('z')
equation_one = sp.Eq(x, z + 2)
equation_two = sp.Eq(y, x + 1)
print(equation_two.subs(equation_one.lhs,equation_one.rhs))
Result:
Eq(y, z + 3)

How to substract a "y" value from the trendline **numpy and pylab**

I have a 3 point graph with a trendline but I need to find an "x" value for specific y value. Here is what I have:
from numpy import *
from pylab import *
x = ng
y = density
plt.scatter(x, y)
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
plt.plot(x,p(x),"r--")
The x value is basically a DNA concentration whereas the y value is a densitometric value that I calculated. I need to find a DNA concertation for the density of 19159.8.
Can somebody help me, please?
The inverse function of y = a*x + b is simply x = (1/a)*y + (-b/a):
a, b = z
z_inv = np.array([1 / a, -b / a])
p_inv = np.poly1d(z_inv)
print(np.allclose(p_inv(p(x)), x))
# True

How to use Sympy to automatically simplify expressions over a two-valued finite field?

I am trying to construct polynomials over a two-valued finite field {0, 1}, and I want them to automatically simplify using some identities that exist in this setting.
I have tried the following:
from sympy import *
from sympy.polys.domains.finitefield import FiniteField
x, y, z, t = symbols('x y z t')
k = Poly(x+y * z*z + (x + y) + y + 1, domain=FiniteField(2))
This already simplifies to:
Poly(y*z**2 + 1, x, y, z, modulus=2)
However, the z**2 is actually the same as z in the field that I want to use. It does seem to automatically recognize that y + y = 0. How can I implement the other identity, z * z = z (idempotency)?
What you want doesn't seem to implemented for poly but maybe you can simulate the effect:
In [54]: normalise = lambda p: Poly(ratsimpmodprime(p, [n**2-n for n in p.free_symbols]), modulus=2)
In [55]: e = x+y * z*z + (x + y) + y + 1
In [56]: normalise(e)
Out[56]: Poly(y*z + 1, x, y, z, modulus=2)

How to differentiate with dependent variables in sympy

from sympy import *
var('x y')
eqn=x**2+y
print(eqn.diff(x))
Getting 2*x but required 2*x+y'
We have to initialize y as a function of x rather than a Symbol
from sympy import *
var('x')
y = Function('y')(x)
eqn = x**2 + y
x=eqn.diff(x)
print(x)
# 2*x + Derivative(y(x), x)
Source:GUser Jashan
As mention by Smichr(https://stackoverflow.com/users/1089161/smichr) ,to get dy/dx from an equation or expression = 0
from sympy import *
var('x y')
eqn = x**2 + y
x=idiff(eqn,y,x)
print(x)
# -2*x

Categories

Resources