Message Boards Message Boards

0
|
3286 Views
|
9 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Is this a correct way of ensuring a preselected precision in calculations?

Posted 1 year ago

Hi, I have the following situation: I need to expand a certain function into an asymptotic series and subsequently integrate (analytically) the series with a certain weight function. The series coefficients are complicated expressions involving the variable x of the expanded function. Apparently for this reason, analytical/symbolic calculations are extremely slow. I therefore decided to convert the symbolically obtained series into a numerical one (that is the various coefficients become now floating point variables, but variable x remains symbolic, if I properly understand), prior to the integration. This speeds up the calculations, and the results appear to be as expected. However, I am not sure if I code this properly. I want to be sure that the results obtained have a prescribed precision (say, 50 accurate significant digits). Is the following code correct from this point of view? Shouldn't I insert somewhere commands like SetPrecision? I don't grasp the differences between SetPrecision[] and N[].

F[x_]:=something
s1=Normal[Series[F[x],{x,Infinity,20}]]
s2=N[s1,50]
Integrate[s2*weight(x),x]

Lesław

POSTED BY: Leslaw Bieniasz
9 Replies

What do you mean by " careful numerical analysis of the inputs and results"? 1) Comparison of results obtained assuming different precisions? 2) Some kind of interval arithmetics? Is any sort of it available in MATHEMATICA? 3) Comparisons of the results with any independently available data? 4) Anything else? Lesław

POSTED BY: Leslaw Bieniasz

Hi, I would add more from my side:
1) I can imagine that maintaining the precision adaptively (whatever this means) can be difficult or impossible in some cases, especially when there is a loss of significant digits in subtraction. But I would at least be happy to learn how MATHEMATICA performs this adaptation. Is this described or published anywhere? Manuals seem to be rather cryptic.
2) The intention of my post was mostly to get some information whether my code was correct from the formal point of view, and I still don't know this. Can anybody explain this to me? If there is no difference between SetPrecision and N, then why there are two such commands?
3) Answering the question of Gianluca, yes, I tried to integrate the terms separately, but this was slow too. The problem seems to be that symbolic expressions for the successive terms become gradually more complicated when presented in a symbolic form, and the complication is eliminated somehow if I calculate the numerical values of the terms (retaining x in symbolic form). If I remember, an attempt to integrate symbolically the series with 4 or 5 terms required 3 days of calculation on a supercomputing cluster (using one node). By applying the trick with the numerical pre-evaluation I was able to integrate 42 terms. Once more I repeat that I don't use NIntegrate, and I don't need this, because my goal is to obtain integrated expansion coefficients, not the value of the integral of F[].

Lesław

POSTED BY: Leslaw Bieniasz

SetPrecision takes all numbers and mathematical constants it sees and assigns them their numerical equivalents at the requested precision. Then the expressions containing them are evaluated, and error is probagated (assuming one did not use MachinePrecision as the second argument). N by contrast attempts to evaluate the entire expression to the requested precision (same caveat). Here is an example.

ee = Sin[10^5];
prec = 4;
{InputForm@N[ee, prec], InputForm@SetPrecision[ee, prec]}

(* Out[829]= {InputForm[0.0357487979720165093`4.], InputForm[0``0.0]} *)

Notice that all precision was lost in the second case, where SetPrecision was used.

There are situations where the attained precision on application of N to a numeric expression might not be correct. The cause I am aware of is in evaluation of special functions, in cases where the error estimates might be off. I believe this is a rare situation though.

Using N in a symbolic computation is a different fish. For example, code in definite integration needs to account for presence of singular points that might only be approximately known. Code in functions such as Together (used by Integrate) might use Rationalize or take other steps just to avoid crashing; such code was not designed for handling approximate numbers (and indeed, the literature on this is far from complete).

With all this in mind, your approach seems reasonable, but I cannot offer any guarantee. Moreover I am fairly sure it would require careful numerical analysis of the inputs and results in order to provide such guarantee.

POSTED BY: Daniel Lichtblau

... if I use floating point numbers with a a user-selected precision, the precision of the calculations will be adaptively maintained to ensure a selected level.

N[f[x], precision] adapts precision if f[x] has infinite precision, but f[N[x, precision]] does not. The error will propagate through f[N[x, precision]].

POSTED BY: Michael Rogers

It is effectively impossible to answer this without full code to replicate it. Also it is not obvious how to gauge error in an antiderivative (indefinite integral), since it really depends on how it is subsequently used.

POSTED BY: Daniel Lichtblau

Have you tried integrating the single terms of the series separately?

POSTED BY: Gianluca Gorni

Hello, Series[] returns its result in a symbolic form, so that I understand the result has an "infinite" acuracy. The question is what to do next in order to convert this result to a preselected finite precision and (most importantly) maintain this precision in subsequent calculations. You write "Any subsequent calculations with each of those might indeed maintain 100 digits of precision (depending on the calculations)". What does this mean? Why "might" and not "will"? Why "depending on the calculations"? In ordinary calculations using IEEE754 floating point numbers, every arithmetic operation adds some error, so that in the worst case, after many operations, the precision of the results gradually deteriorates. But according to some manuals for MATHEMATICA, if I use floating point numbers with a a user-selected precision, the precision of the calculations will be adaptively maintained to ensure a selected level. I presume MATHEMATICA uses some mystic method of adding more bits to numbers, if needed. But how to make this happen? Should I use SetPrecision or N, or both (and how)? Please show me using my example. Please note also that I use Integrate[]. I don't use NIntegrate[] so that the issues of AccuracyGoal, PrecisionGoal, and WorkingPrecision do not arise. Lesław

POSTED BY: Leslaw Bieniasz
Posted 1 year ago

If you have an infinite precision quantity, then I believe applying either SetPrecision or N will produce the same result. As for maintaining precision, I would think that would totally depend on the operations you're going to perform. Unfortunately, I don't know the implementation details of how Mathematica handles precision. I suspect that, yes, extra digits are carried around in an attempt to maintain precision, but I don't know how to explicitly exploit that. I have noticed that extra digits are used, and I generally assume that outputs are correct up to the precision of the inputs, unless I have reason to suspect otherwise. But I'm not an expert on numerical precision, and I would think that if you want high confidence in the actual precision of your results, then you'd need to analyze your specific sequence of computations carefully. (The term "precision" is taking on different meanings here, but hopefully it's not too muddled.)

As for your specific example, you didn't provide F nor s1, so I can just provide generic guidance about Integrate. I would start with N[Integrate[...]...]. Then I would consider NIntegrate[...] and epxeriment with the relevant options. Only then would I consider trying to manage precision "manually" by specifically setting the precision of the inputs. If it got to that stage, I would experiment with different levels of precision until I was satisfied with the results.

But honestly, I hope someone with more sophisticated knowledge of numerical computation will jump in here. I was mostly trying to explain the semantics of the functions you were asking about; I don't claim any expertise in how to analyze computational error.

POSTED BY: Eric Rimbey
Posted 1 year ago

I confess that I don't know much about Series, so I don't know what to expect with the iterator {x,Infinity,20}. Also, this could very well depend on how your F is actually defined. But leaving aside those caveats, I don't think this approach will get you want you want.

The function N cannot actually add any precision. If you try N[1.5,50], you'll notice that it has no effect. On the other hand N[3/2,50] does have an effect, with the result being a number with ~50 digits of precision. So, when you do

s2 = N[s1, 50]

if s1 has precision less than 50 digits, s2 will end up just being the same as s1.

Regarding your questions about SetPrecision... SetPrecision will indeed result in a number with the specified precision, however it cannot know what the original number was supposed to be, so it won't actually improve the accuracy of your calculations. Whatever error there was before you applied SetPrecision will still be there. You'll just have a bunch of garbage digits. So, at the moment you apply SetPrecision, you must somehow know that the new, high precision number is "correct" for using in the subsequent calculations.

For example, compare

SetPrecision[N[Pi, 10], 100]

to

SetPrecision[Pi, 100]

Any subsequent calculations with each of those might indeed maintain 100 digits of precision (depending on the calculations), but the results with the former will be much less "correct" than the results with the latter.

Many built-in functions that do numerical calculations take options like AccuracyGoal, PrecisionGoal, and WorkingPrecision. Using those functions (e.g. NIntegrate) with those options is probably the safest way to get the level of precision/accuracy you're after. You also need to take care not to pollute your computations by mixing lower precision numbers with higher precision ones. The precision of the result will be limited by the lowest precision involved in the calculation. So, start your calculations with infinitely precise quantities (integers, special symbols like Pi, etc) or quantities with sufficiently high precision (you'll have to figure out what that should be), and then never pollute your intermediate results with low-precision quantities (e.g. things like 1.5 will automatically have machine precision, about 15 digits).

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract