Expand square of a Sum into a double Sum in sympy - sympy

The square of a finite sum can be expanded into a double sum, like (fake latex):
(sum_i v_i)^2 = sum_i sum_j (v_i*v_j)
with respective lower and upper bounds. I am trying to get this done in sympy. This is how I start:
from sympy import *
n = symbols('n', integer=True)
i = Idx('i', n)
v = IndexedBase('v')
vsum = Sum(v[i], (i, 0, n-1))
pprint(expand(vsum * vsum))
The expand function does not do what I hoped for. I can guess that the difficulty might be to "invent" a new index variable, j for the second sum. Is there something build in? If not I would be happy to get a pointer how to hook in my own sum-square expander.

I don't know of any ready made function that does this but it's not hard to make one.
You can make this a bit cleaner and maybe use Dummy or numbered_symbols to create the symbols but I think this shows the general idea:
In [15]: expr = 1 + vsum**2
In [16]: expr
Out[16]:
2
⎛n - 1 ⎞
⎜ ___ ⎟
⎜ ╲ ⎟
⎜ ╲ ⎟
⎜ ╱ v[i]⎟ + 1
⎜ ╱ ⎟
⎜ ‾‾‾ ⎟
⎝i = 0 ⎠
In [17]: matcher = lambda e: isinstance(e, Pow) and isinstance(e.base, Sum) and e.exp.is_Integer
In [18]: replacer = lambda e: prod(e.base.xreplace({i:j}) for j in symbols(f'j:{e.exp}'))
In [19]: expr.replace(matcher, replacer)
Out[19]:
⎛n - 1 ⎞ n - 1
⎜ ___ ⎟ ___
⎜ ╲ ⎟ ╲
⎜ ╲ ⎟ ╲
⎜ ╱ v[j0]⎟⋅ ╱ v[j1] + 1
⎜ ╱ ⎟ ╱
⎜ ‾‾‾ ⎟ ‾‾‾
⎝j₀ = 0 ⎠ j₁ = 0
Making something that handles all possible cases for the limits is a bit more complicated.

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)

sympy term rewriting sqrt and binomial formula

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

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