Elle
Elle

Reputation: 49

How could I get square root of a symbol in python?

I am using math.sqrt in python to compute the squre root of something, but that "something" is a symbol instead of a known value. I am using that "something" as an intermediate variable for later use.

import math
from math import sqrt
x = Symbol('x')
y = math.sqrt(x)
print(y)

Hower I get the error message

File "/Library/Python/2.7/site-packages/sympy/core/expr.py", line 207, in __float__
    raise TypeError("can't convert expression to float")
TypeError: can't convert expression to float

Looks like there's no other packages for me to compute square root except "math.sqrt", does anyone know how I could get rid of this problem?

Upvotes: 4

Views: 9721

Answers (1)

Simon
Simon

Reputation: 10841

When using sympy, one should use the functions in sympy that operate on symbols, rather than the math.* functions that operate on floating point numbers, e.g.

from sympy import *
x = Symbol('x')
y = sqrt(x)
print(y)

In this case, the code is using sympy's sqrt() function.

Upvotes: 4

Related Questions