Search code examples
scipysympylambdify

Use Sum Indexed and Lambdify, and scipy to minimize a large expression


I have a huge expression around 231 terms and each of these expressions has some power of cos(e) or sin(e) and they can be mixed as well, each term also has an r(distance) term in the denominator raised to some power as well.

Here is a small portion of the expression Equation Image

What I'd like to do is sum the expression over all angle e's and then over all r's and use lambdify and scipy to minimize the expression with respect to 4 other parameters present in the equation.

Things I tried

  • I have tried to do the sums using sum indexed in scipy but am not able to make it work, the power bit is tricky also once I have the sum indexed expression and I expand it how do i pass the list of angle values at which to calculate the expression

  • Also since the expression is pretty large I'd like to do the sum indexing etc. in a loop without individually resolving expression for each power.

(If my question is not clear, let me know.)


Solution

  • This is how I finally managed to solve my problem -

    • Replaced cos(e) and sin(e) with variable cose and sine.
    • Iterated over the list of angles and used sympy.subs to replace cose and sine with math.cos(e) and math.sin(e) and kept adding the expressions obtained ditto for r as well.
    • This left me with only p1 and p2 and Q1 and Q2 which was required.
    • I couldnt use sympy lambdify and sum indexed but this got the job done.