devito icon indicating copy to clipboard operation
devito copied to clipboard

numpy floats are eagerly converted to sympy Floats

Open JDBetteridge opened this issue 1 year ago • 0 comments

See https://github.com/devitocodes/devito/blob/v4.8.11/tests/test_symbolics.py#L257-L260 for example of use.

Spitting out the generated code we can see that in the case where the constant value is an np.float64 sympy eagerly turns this into a Float losing the precision information. The generated code is explicitly using a 5.0e-1F single precision float rather than (presumably) the desired double precision float.

from devito import *
from devito.symbolics.inspection import sympy_dtype
from devito.symbolics import ccode
i1 = Dimension(name="i1")

for expr in [
    i1 - 1,
    i1 - 0.5,
    i1 - np.float32(0.5),
    i1 - np.float64(0.5),
    i1 - Constant('half', dtype=np.float64, default_value=0.5)
]:
    print(repr(expr))
    print(sympy_dtype(expr))
    print(ccode(Abs(expr)))
    print()

Produces:

i1 - 1
<class 'numpy.int32'>
abs(i1 - 1)

i1 - 0.5
<class 'numpy.int32'>
fabsf(i1 - 5.0e-1F)

i1 - 0.5
<class 'numpy.int32'>
fabsf(i1 - 5.0e-1F)

i1 - 0.5
<class 'numpy.int32'>
fabsf(i1 - 5.0e-1F)

i1 - half
<class 'numpy.float64'>
fabs(i1 - half)

Could probably be addressed alongside https://github.com/devitocodes/devito/issues/2493 in a printer cleanup.

JDBetteridge avatar Dec 20 '24 16:12 JDBetteridge