On Sun, Feb 22, 2009 at 12:30 PM, Freddie Witherden <fred...@witherden.org> wrote: > Hi all, > > I am interested if there are currently any plans to add some form of > precision tracking for variables, be it through sympy or mpmath. The current > implementation will more than happily compute sin(0.50) to 100+ decimal > places if asked, even though the argument is only accurate to two decimal > places. > > In Mathematica this feature is implemented as follows: > In: N[Sin[SetPrecision[0.50, 2]], 100] > Out: 0.48 > > Notice how even though the sine of 0.5 was requested to 100 places of > accuracy it was only given to two as that is the maximum allowed by the > input. Naturally combing of numbers of various precisions is also handled > automatically. > > Something like this would be very useful for Sympy and would make scientific > calculations a lot easier.
This is almost supported by the current evalf code. The code for tracking error propagation is already there; the precision for Reals is just assumed infinite (which is by design). I don't have the code in front of me right now, but I think it takes one or two lines of code changed to implement the behavior you're asking for. This could be toggled with a keyword option to N/evalf. However, there are a few problems: There is no way to make this literally work for sin(0.50), because 0.5, 0.50 and 0.5000000000000 are the same to Python. You have to explicitly set the precision for all numbers, as such: sin(Real('0.50', 2)). If x has a precision of 2 digits, you can do N(x, 10) to change the precision of x to 10 digits. This no longer becomes possible: to be consistent, N(x, n) must leave x unchanged. (This is not a problem if toggled by a keyword option.) Finally,the significance arithmetic in evalf.py is designed for evaluation of exact expressions. Errors are measured as an integral number of bits, which can be too crude for tracking errors in initial data, although it can indeed at least give a rough idea of the number of correct digits. Generally, interval arithmetic is a superior way to handle error propagation, and is available in mpmath: >>> from mpmath import sin, mpi >>> sin(mpi(0.5-0.005, 0.5+0.005)) mpi(mpf('0.47503165127095076'), mpf('0.48380744032396017')) (Vinzent is working on code for pretty-printing intervals, so that interval should be possible to print as something like 0.4794 +- 0.0044.) Fredrik --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sympy@googlegroups.com To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sympy?hl=en -~----------~----~----~----~------~----~------~--~---