shuvo
shuvo

Reputation: 1956

How to get the float values for a sympy point?

I have the following piece of code

from sympy import Point, Line

l1 = Line((3,5,-2), (1,-1,4))
p1 = Point3D((3, 7, 9))

intersection_p = p1.intersection(l1)[0]
print(intersection_p)

I get

Point3D(30/7, 62/7, -41/7)

But, I want the float values, not the rationals.

[4.285714285714286, 8.857142857142858, -5.857142857142857]

I've done this which gives me the desired output.

intersection1 = [float(i) for i in p1.intersection(l1)[0].args]

But I was wondering if there is any sympy method that converts/extracts the Point3D object's coordinate values as float.

Upvotes: 3

Views: 1066

Answers (1)

JohanC
JohanC

Reputation: 80449

You can use evalf(), just as you would do with regular symbolic expressions. You can use tuple(p1.evalf()) or list(p1.evalf()) to have the numbers in a more standard Python way.

from sympy import Point3D, sqrt, pi

p1 = Point3D((sqrt(2), 7, pi/2))

print(p1)
print(p1.evalf())

Output:

Point3D(sqrt(2), 7, pi/2)
Point3D(1.4142135623731, 7.0, 1.5707963267949)

Note that evalf() accepts a parameter indicating the number of digits.

Upvotes: 1

Related Questions