3

I'm trying to use sympy to solve a polynomial equation, the coefficients of which have uncertainties. So for the uncertainties I'm trying to use the uncertainties module. Is there any way of doing the following:

x=ufloat(10,0.2)  #the xs are coefficients
x1=ufloat(8,0.01)
x3=ufloat(25,2)
L=Symbol("L")
eqn=(x*(L**2))+(x1*(L*1))+(x3*(L**0))
solve(eqn,L) #ideally this should give the value of L with it's propagated uncertainty

without it throwing the error:

TypeError: unsupported operand type(s) for *: 'Variable' and 'Pow'
user2151741
  • 99
  • 1
  • 8

2 Answers2

2

One solution would be to use Symbol('x') and then substitute it for your ufloat (you'll probably need to use lambdify to do this). This should work, assuming that SymPy is able to solve the equation in the general form with the symbolic coefficient. Since this is just a quadratic, it will. For a cubic it would too, but for higher order polynomials, you are out of luck. I'm also assuming that ufloat will do the right thing when plugged into the quadratic equation.

Something like

x, x1, x3 = symbols('x x1 x3')
L=Symbol("L")
eqn=(x*(L**2))+(x1*(L*1))+(x3*(L**0))
s = solve(eqn,L)
lambdify([x, x1, x3], s)(ufloat(10,0.2), ufloat(8,0.01), ufloat(25,2))

(note there are two solutions to the quadratic, so this will give both).

asmeurer
  • 86,894
  • 26
  • 169
  • 240
  • A quartic should work too, except for https://code.google.com/p/sympy/issues/detail?id=4003. – asmeurer Oct 29 '13 at 21:55
  • 2
    I've been trying this with no luck. Could you perhaps be more explicit? I have no experience with lamdify and reading the documentation wasn't very enlightening. – user2151741 Oct 30 '13 at 20:39
2

There is a python package based on uncertainties and sympy which does it: maabara

It is still in beta, but see examples here


update

The links above are already invalid. You may find the package on github. Here's the User Guide


update Mar 2 2019

The links above are dead too. You may find a package of the same name on github. Here's the User Guide

Syrtis Major
  • 3,791
  • 1
  • 30
  • 40