Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solve equation with sum and index using Sympy

After having tried many things, I thought it would be good to ask on SO. My problem is fairly simple: how can I solve the following equation using Sympy?

Equation

I want to solve this for lambda_0 and q is an array of size J containing elments between 0 and 1 that sum op to 1 (discrete probability distribution). I tried the following:

from sympy.solvers import solve
from sympy import symbols, summation

p = [0.2, 0.3, 0.3, 0.1, 0.1]
l = symbols('l')
j = symbols('j')
eq= summation(j*q[j]/(l-j), (j, 0, 4))

s= solve(eq, l)

But this gives me an error for q[j] as j is a Symbol object here and not an integer. If I don't make j as symbol, I cannot evaluate the eq expression. Does anyone know how to do this?

Edit: p = 1-q in the above, hence q[j] should have been replaced by (1-p[j]).

like image 606
Rutger Avatar asked Oct 31 '20 20:10

Rutger


People also ask

How do you solve equations with SymPy?

Solving with sympy: import sympy as sym x,y = sym. symbols('x,y') eq1 = sym. Eq(x+y,5) eq2 = sym.

How do you solve for two variables in SymPy?

After the symbols and equations are defined, we can use SymPy's solve() function to compute the value of x and y. The first argument passed to the solve() function is a tuple of the two equations (eq1, eq2) . The second argument passed to the solve() function is a tuple of the variables we want to solve for (x, y) .

How do you solve two variable equations in Python?

To solve the two equations for the two variables x and y , we'll use SymPy's solve() function. The solve() function takes two arguments, a tuple of the equations (eq1, eq2) and a tuple of the variables to solve for (x, y) . The SymPy solution object is a Python dictionary.

How do I isolate a variable in SymPy?

To isolate one variable, call the solve function, and pass that variable as the second argument. The brackets surround a list of all solutions—in this case, just one. That solution is that R = P V T n .

How do I create an equation in SymPy?

Let's create the equation: To solve the two equations for the two variables x and y, we'll use SymPy's solve () function. The solve () function takes two arguments, a tuple of the equations (eq1, eq2) and a tuple of the variables to solve for (x, y).

How to solve algebraic equations using SymPy Solver module?

The solver module in SymPy provides soveset () function whose prototype is as follows − The domain is by default S.Complexes. Using solveset () function, we can solve an algebraic equation as follows −

How do I solve two equations for two unknowns using SymPy?

SymPy can be used to solve two equations for two unknowns. Consider the set of two equations containing two variables below: To solve this system of two equations for the two unknows x and y, first the SymPy package needs to be imported. From the SymPy package, we'll use the functions symbols (), Eq (), and solve ().

How to return the roots of the equation using SymPy?

With the help of sympy.solve (expression) method, we can solve the mathematical equations easily and it will return the roots of the equation that is provided as parameter using sympy.solve () method. Return : Return the roots of the equation.


1 Answers

List p needs to be converted into symbolic array before it can be indexed with symbolic value j.

from sympy.solvers import solve
from sympy import symbols, summation, Array

p = Array([0.2, 0.3, 0.3, 0.1, 0.1])
l, j = symbols('l j')
eq = summation(j * (1 - p[j]) / (l - j), (j, 0, 4))

s = solve(eq - 1, l) # [1.13175762143963 + 9.29204634892077e-30*I, 2.23358705810004 - 1.36185313905566e-29*I, 3.4387382449005 + 3.71056356734273e-30*I, 11.5959170755598 + 6.15921474293073e-31*I]

(assuming your p stands for 1 - q)

like image 145
darw Avatar answered Sep 20 '22 12:09

darw