Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the float values for a sympy point?

Tags:

python

sympy

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.

like image 352
paul-shuvo Avatar asked Dec 06 '25 08:12

paul-shuvo


1 Answers

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.

like image 91
JohanC Avatar answered Dec 08 '25 21:12

JohanC