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])
.
Solving with sympy: import sympy as sym x,y = sym. symbols('x,y') eq1 = sym. Eq(x+y,5) eq2 = sym.
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) .
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.
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 .
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).
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 −
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 ().
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.
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
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With