Probably the word "efficient" shouldn't be there. The key thing is
that it can potentially be more accurate, because it is able to be
better at avoiding loss of significance due to numerical cancellation.
For example
>>> (x + y + z).subs({x: 1, y: 1e-100, z: -1})
0
>>> (x + y + z).evalf(subs={x: 1, y: 1e-100, z: -1})
1.00000000000000e-100
Aaron Meurer
On Mon, Oct 12, 2020 at 5:44 AM s5s <[email protected]> wrote:
>
> Hi,
>
> First, apologies if this has been discussed before. I'm browsing through the
> tutorial and encountered what appears to be an inaccuracy in the
> documentation. In particular, I am reading Basic Operations section which
> states that
>
> To numerically evaluate an expression with a Symbol at a point, we might use
> subs followed by evalf, but it is more efficient and numerically stable to
> pass the substitution to evalf using the subs flag, which takes a dictionary
> of Symbol: point pairs.
>
> However, when I test this in jupyter lab using "%timeit" it appears the
> reverse is true. I'll avoid posting images, but I got the following results:
>
> expr = cos(x) - 2*sin(exp(x))
> %timeit expr.subs(x, 0).evalf()
> 92.9 µs ± 628 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>
> %timeit expr.evalf(subs=dict(x=0))
> 264 µs ± 7.21 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
>
> expr = cos(2*x)
> %timeit expr.evalf(subs={x: 2.4})
> 73.8 µs ± 3.28 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>
> %timeit expr.subs(x, 2.4).evalf()
> 35.1 µs ± 434 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
>
> Perhaps the documentation needs to elaborate more what it means by "stable"
> and "efficient" and under what conditions using one is preferable over the
> other because from the tests I performed, it appears using subs().evalf() is
> the way to go. The docs are either incorrect or out of date.
>
> Best regards
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/03831493-431b-45e7-809f-6cc691cab191n%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/CAKgW%3D6LbcaBHvLCLAH87C73k%3DyDNjj6%3DDXPhsF7CxcmQf3t9mA%40mail.gmail.com.