Open allComputableThings opened 3 years ago
Not sure of the underlying cause but it is to do with having floats in the integrand. This works:
nfloat(nsimplify(distrib).integrate(x))
My guess is that with floats somewhere some rounding causes factorisation to fail.
As an aside it would be better (in this particular case) to compute this integral with symbols and substitute the values in afterwards:
In [32]: integrate(exp(-((x - z)/t)**2), x)
Out[32]:
⎛x - z⎞
√π⋅t⋅erf⎜─────⎟
⎝ t ⎠
───────────────
2
In [33]: _.subs({z:1, t:3})
Out[33]:
⎛x 1⎞
3⋅√π⋅erf⎜─ - ─⎟
⎝3 3⎠
───────────────
2
This is most likely related to the issue that SymPy does not complete the square inside the exponential. This has been brought up several times before, most recently at https://github.com/sympy/sympy/issues/21362 where some fixes are discussed. It's probably an issue with floats in particular because exp(constant)
evaluates to a floating point number if constant
is a float.
So... I can
sympy.integrate
a normal distribution with mean and standard deviation:but not:
Kinds feels like that shouldn't make a big difference. What's up with that?
Full example:
The first problem:
works fine.
The second
Fails with 'NameError: name 'Integral' is not defined' when plotted. I'm aware I can make this 'go away' by importing
sympy.Integral
, but why does one get better evaluated to a simpler form that the other.(And I'm a little suspicious about whether
1.30203374788369e-60* ....
is going to end well).Fails with the latest pip install as of today (python 3.7, sympy 1.18).