michael0196
michael0196

Reputation: 1637

Solve algebraic equation with operations

Is there a practical solution to the following equation with sympy or numpy? I've tried numpy and sympy so far.

from sympy import *
from sympy.solvers.solveset import linsolve

sig = symbols(['sig'])

result = linsolve([(sig * -3) + ((1 - sig) * 1) == (sig * 2) + ((1 - sig) * 0)], sig)

This code returns an error: TypeError: unsupported operand type(s) for -: 'int' and 'list'

Upvotes: 0

Views: 74

Answers (2)

SciDev
SciDev

Reputation: 1

Assuming you are only interested in solving the equation with sympy or numpy and not necessarily restricting yourself to linsolve, here is working code.

from sympy import *
sig = symbols('sig')
eq = Eq((sig * (-3)) + ((1 - sig) * 1), (sig * 2) + ((1 - sig) * 0))
solveset(eq, sig)

One problem with your code is that the return value of symbols(['sig']) is not a Sympy symbol object but a python list containing one such object, so you cannot use it to build Sympy expressions.

Upvotes: 0

esandier
esandier

Reputation: 165

I think you have the wrong syntax for linsolve this works:

sig = symbols(['sig'])

result = linsolve([(sig * -3) + ((1 - sig) * 1) - (sig * 2) - ((1 - sig) * 0)], sig)

Upvotes: 1

Related Questions