sympy term rewriting sqrt and binomial formula - sympy

I have the following code
import sympy as sp
x, y = sp.symbols('x y', real=True, positive=True)
expr = sp.sqrt(1/(x-y))/sp.sqrt(x+y)
the expression created should be equal to sqrt(1/(x^2 - y^2)) and I would like to rewrite it in that form but I cannot figure out how. I tried .rewrite(sp.sqrt), .rewrite(x**2 - y**2) and similar stuff but it did not help.
Ty for any tips on this,
Franz

The expression that you want is not equivalent to the one that you have:
In [53]: expr1 = sqrt(1/x - y)/sqrt(x + y)
In [54]: expr2 = 1/sqrt(x**2 - y**2)
In [55]: expr1
Out[55]:
________
╱ 1
╱ -y + ─
╲╱ x
────────────
_______
╲╱ x + y
In [56]: expr2
Out[56]:
1
────────────
_________
╱ 2 2
╲╱ x - y
In [58]: expr1.subs({x:1, y:2})
Out[58]:
√3⋅ⅈ
────
3
In [59]: expr2.subs({x:1, y:2})
Out[59]:
-√3⋅ⅈ
──────
3
That being said it should still be possible to do what you want using powsimp(expr1, force=True) but that doesn't seem to work in this case. I recommend opening a bug report about powsimp:
https://github.com/sympy/sympy/issues

Related

sympy substitution of occurences satisfying a given pattern

Is there a way to do a substitution in sympy of all occurences that satisfy a given pattern? For example, if I have an expression that contains multiple occurances of sqrt(anything), is there a way to substitute all those occurences with anything**0.51?
Here is a code example:
import sympy
from sympy import sqrt, exp
from sympy import *
x,y = symbols('x y')
test = sqrt (x+y) + sqrt (exp(y))
in this case I can do substitution manually:
test.subs(sqrt(x+y), (x + y)**0.51).subs(sqrt (exp(y)) , (exp(y))**0.51)
yielding the expected subsitution. But, is there a way to do it one shot so one can easily apply it to long epxressions?
You can use a Wild symbol to do pattern-matched replacements:
In [7]: w = Wild('w')
In [8]: test
Out[8]:
____
_______ ╱ y
╲╱ x + y + ╲╱ ℯ
In [9]: test.replace(sqrt(w), w**0.51)
Out[9]:
0.51
0.51 ⎛ y⎞
(x + y) + ⎝ℯ ⎠
You can use the replace method. Here is the documentation with a few examples.
In your particular case, the square root is a power with exponent 0.5, so we are going to look for those objects:
test.replace(lambda t: t.is_Pow and t.exp is S.Half, lambda t: t.base**0.51)

Merge 2 algebraic expression using Sympy

How to merge 2 or more expressions
example: 3x + 2y - 2 and 2x + 3y -1 (3x + 2y -2 = 2x + 3y -1)
which gives x-y-1
from sympy import *
# declare symbols
var("x, y")
# write the two expressions
expr1 = 3*x + 2*y - 2
expr2 = 2*x + 3*y - 1
# subtract them
result = expr1 - expr2
result
# out: x - y - 1
EDIT: Alternatively, we can also create an Equality object and later rewrite it as an addition:
eqaulity = Eq(expr1, expr2)
# out: 3*x + 2*y - 2 = 2*x + 3*y - 1
eqaulity.rewrite(Add)
# out: x - y - 1

Complex numbers in sympy

I am using lcapy together with sympy and trying to process complex numbers from a circuit.
I have the following sympy expression:
import sympy
from lcapy import j
expr = sympy.parse_expr('L*w_0*(C*R*w_0 - I)')
expr
Output:
L⋅w₀⋅(C⋅R⋅w₀ - ⅉ)
expr above is an complex expression with ⅉ being the imaginary number. Can I use sympy and remove the brackets from expr in order to view it in the more canonical form for complex numbers as
w₀^2⋅R⋅L⋅C - ⅉ⋅w₀⋅L
And does sympy have support for handling complex numbers? I want to get the argument of expr which should be:
arctan(L⋅w₀ / w₀^2⋅R⋅L⋅C)
I can do (in an isympy session):
In [13]: expr
Out[13]: L⋅w₀⋅(C⋅R⋅w₀ - ⅈ)
In [14]: expr.expand()
Out[14]:
2
C⋅L⋅R⋅w₀ - ⅈ⋅L⋅w₀
edit
try
atan2(im(expr), re(expr))
https://docs.sympy.org/latest/modules/functions/elementary.html
Refining the variables:
In [53]: C,L,R,w_0=symbols('C L R w_0',real=True, positive=True)
In [54]: expr=L*w_0*(C*R*w_0-I)
In [55]: expr
Out[55]: L⋅w₀⋅(C⋅R⋅w₀ - ⅈ)
In [56]: expr.expand()
Out[56]:
2
C⋅L⋅R⋅w₀ - ⅈ⋅L⋅w₀
In [57]: im(_),re(_)
Out[57]:
⎛ 2⎞
⎝-L⋅w₀, C⋅L⋅R⋅w₀ ⎠
Now the atan2 is simplified:
In [59]: atan2(*_)
Out[59]:
⎛ 1 ⎞
-atan⎜──────⎟
⎝C⋅R⋅w₀⎠
And arg does the same:
In [60]: arg(_56)
Out[60]: arg(C⋅R⋅w₀ - ⅈ)
In [62]: arg(expr)
Out[62]: arg(C⋅R⋅w₀ - ⅈ)
In [77]: arg(expr)._eval_rewrite_as_atan2(expr)
Out[77]:
⎛ 1 ⎞
-atan⎜──────⎟
⎝C⋅R⋅w₀⎠

Sympy multivariate function analysis

I have some expression expr which is subject for analysis
c, l = symbols('c l', real=True, positive=True)
w = symbols('w', real=True)
expr = (w ** 2) / ((2 * c * l * w**4) - 3 * w**2 + (2/(c*l)))
expr
Output:
2
w
─────────────────────
4 2 2
2⋅c⋅l⋅w - 3⋅w + ───
c⋅l
I want to analyze this function w.r.t w. More specifically, I want to find the maximum value for the expression which should occur when
1
w = ─────
√c⋅√l
I have tried solving it for when the derivative is zero:
solve(diff(expr, w))
Output:
⎡⎧ 1 ⎫ ⎤
⎢⎪c: ────⎪, {w: 0}⎥
⎢⎨ 2⎬ ⎥
⎢⎪ l⋅w ⎪ ⎥
⎣⎩ ⎭ ⎦
I am not sure how to use the output above and express this as a function of l and c. I would prefer
2 1
w = ───
c⋅l
Is there a more standard way of analyzing expr with respect to w ?
You need to tell solve which symbol to solve for:
In [144]: solve(expr.diff(w), w)
Out[144]:
⎡ -1 1 ⎤
⎢0, ─────, ─────⎥
⎣ √c⋅√l √c⋅√l⎦

Simplifying radicals in sympy

Although the radsimp function looks like it's exactly what I want:
from sympy import radsimp, sqrt, symbols
v1, v2 = symbols('v1 v2')
radsimp(1 / (1 + sqrt(v1)))
____
╲╱ v₁ - 1
──────────
v₁ - 1
I can't get it to work with other simple fractions like this:
radsimp(sqrt(v1) / sqrt(v2))
____
╲╱ v₁
──────
____
╲╱ v₂
I need/expect it to do:
______
╲╱ v₁⋅v₂
────────
v₂
This works now
>>> radsimp(sqrt(x)/sqrt(y))
sqrt(x)*sqrt(y)/y