pythonnumpyfloating-pointsympyprecision

Can lambdify return an array with a dtype np.float128?


I am solving a large non-linear system of equations and I need a high degree of numerical precision. I am currently using sympy.lambdify to convert symbolic expressions for the system of equations and its Jacobian into vectorized functions that take ndarrays as inputs and return an ndarray as outputs.

By default, lambdify returns an array with dtype of numpy.float64. Is it possible to have it return an array with dtype numpy.float128? Perhaps this requires the inputs to have dtype of numpy.float128?


Solution

  • The output just reflects the input:

    from numpy import float128
    from sympy.abc import x
    from sympy.utilities import lambdify
    
    f = lambdify(x, x ** 2)
    result = f(float128(2))
    
    result
    #>>> 4.0
    
    type(result)
    #>>> <class 'numpy.float128'>