(*^ ::[ frontEndVersion = "NeXT Mathematica Notebook Front End Version 2.1"; next21StandardFontEncoding; paletteColors = 128; automaticGrouping; currentKernel; fontset = title, inactive, noPageBreakBelow, noPageBreakInGroup, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, L1, e8, 24, "Times"; ; fontset = subtitle, inactive, noPageBreakBelow, noPageBreakInGroup, nohscroll, preserveAspect, groupLikeTitle, center, M7, bold, L1, e6, 18, "Times"; ; fontset = subsubtitle, inactive, noPageBreakBelow, noPageBreakInGroup, nohscroll, preserveAspect, groupLikeTitle, center, M7, italic, L1, e6, 14, "Times"; ; fontset = section, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, grayBox, M22, bold, L1, a20, 18, "Times"; ; fontset = subsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, blackBox, M19, bold, L1, a15, 14, "Times"; ; fontset = subsubsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, whiteBox, M18, bold, L1, a12, 12, "Times"; ; fontset = text, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12; fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 10, "Times"; ; fontset = input, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeInput, M42, N23, bold, L1, 12, "Courier"; ; fontset = output, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L-5, 12, "Courier"; ; fontset = message, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L1, 12, "Courier"; ; fontset = print, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L1, 12, "Courier"; ; fontset = info, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M42, N23, L1, 12, "Courier"; ; fontset = postscript, PostScript, formatAsPostScript, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeGraphics, M7, l34, w282, h287, L1, 12, "Courier"; ; fontset = name, inactive, noPageBreakInGroup, nohscroll, preserveAspect, M7, italic, B65535, L1, 10, "Times"; ; fontset = header, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, italic, L1, 12, "Times"; ; fontset = Left Header, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, italic, L1, 12, "Times"; ; fontset = footer, inactive, nohscroll, noKeepOnOnePage, preserveAspect, center, M7, italic, L1, 12, "Times"; ; fontset = Left Footer, inactive, nohscroll, noKeepOnOnePage, preserveAspect, center, M7, italic, L1, 12, "Times"; ; fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12; fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12; fontset = completions, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12, "Courier"; ; fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12; fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12; fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12; fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12; fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M7, L1, 12; ] :[font = title; inactive; preserveAspect; startGroup; ] Interval Arithmetic in Mathematica ;[s] 2:0,0;23,1;34,-1; 2:1,21,16,Times,1,24,0,0,0;1,22,17,Times,3,24,0,0,0; :[font = text; inactive; preserveAspect; ] Jerry B. Keiper Wolfram Research, Inc. keiper@wri.com :[font = section; inactive; Cclosed; preserveAspect; startGroup; ] Introduction :[font = text; inactive; preserveAspect; ] Mathematica, like all computer-algebra systems, has several types of arithmetic. In addition to exact arithmetic (i.e., integers and rational numbers) and machine-precision floating-point arithmetic, it has high-precision floating-point arithmetic and interval arithmetic. ;[s] 5:0,0;11,1;115,2;119,3;120,4;273,-1; 5:1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,10,8,Times,3,12,0,0,0;1,11,8,Times,0,12,0,0,0; :[font = subsection; inactive; Cclosed; preserveAspect; startGroup; ] Range Arithmetic :[font = text; inactive; preserveAspect; endGroup; ] The high-precision floating-point arithmetic has two parameters that can be set to control its behavior, but the default behavior is essentially a form of interval arithmetic in which the intervals are assumed to be "short" relative to the magnitude of the numbers represented. This form of arithmetic is sometimes referred to as significance arithmetic or range arithmetic. The length of the interval that is implicitly represented by a high-precision floating-point number is 10-a where a is the "accuracy" of the number, i.e., the number of digits to the right of the decimal point. Internally each high-precision floating-point number has associated with it a machine float that represents the logarithm of the length of the interval; the length is not explicitly calculated nor are the digits actually counted. ;[s] 9:0,0;547,1;548,2;549,3;556,4;557,5;591,6;595,7;596,8;884,-1; 9:1,11,8,Times,0,12,0,0,0;1,11,8,Times,32,12,0,0,0;1,10,8,Times,34,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,10,8,Times,3,12,0,0,0;1,11,8,Times,0,12,0,0,0; :[font = subsection; inactive; Cclosed; preserveAspect; startGroup; ] Interval Arithmetic :[font = text; inactive; preserveAspect; endGroup; endGroup; ] There are problems, however, where range arithmetic is not sufficient and you really do want interval arithmetic. The interval arithmetic in Mathematica version 2.2 is genuine interval arithmetic, complete with outward rounding and multi-intervals. Its deficiencies are that complex intervals have not yet been implemented and the outward rounding is done a posteriori rather than as directed rounding in hardware, which many machines do not support. ;[s] 5:0,0;142,1;153,2;358,3;370,4;453,-1; 5:1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0; :[font = section; inactive; Cclosed; preserveAspect; startGroup; ] Range Arithmetic :[font = text; inactive; preserveAspect; ] The "accuracy" of a number is given by the function Accuracy[ ], but by default its result is rounded to the nearest integer. This rounding can be disabled with the command ;[s] 3:0,0;52,1;63,2;174,-1; 3:1,11,8,Times,0,12,0,0,0;1,10,8,Courier,1,12,0,0,0;1,11,8,Times,0,12,0,0,0; :[font = input; preserveAspect; ] SetPrecision[Round, False]; :[font = text; inactive; preserveAspect; ] The length of the implicit interval associated with a real number can now be found with the function :[font = input; preserveAspect; ] len[x_Real] := 10^(-Accuracy[x]) :[font = text; inactive; preserveAspect; ] Let's find the lengths of the intervals associated with 30-digit representations of the numbers 3 and 4. :[font = input; preserveAspect; ] a = N[3, 30]; len[a] :[font = input; preserveAspect; ] b = N[4, 30]; len[b] :[font = text; inactive; preserveAspect; ] Now let's find the lengths of the intervals resulting from various arithmetic operations on these two numbers. :[font = input; preserveAspect; ] {len[a+b], len[a-b], len[a b], len[a/b]} :[font = text; inactive; preserveAspect; ] Note that from the point of view of rigorous interval arithmetic, the lengths of the intervals implicit in range arithmetic are a bit sloppy. This is because the purpose of range arithmetic is merely to give a good estimate of the number of correct digits in the result. To reduce memory requirements, the logarithm of the length of the interval is stored only as a float. To increase the speed of the length calculations, rational minimax approximations are used. Because interval arithmetic (and hence also range arithmetic) tends to be rather pessimistic and because range arithmetic is not intended to be a substitute for interval arithmetic, sloppy length calculations are not problematic. (Note that a much more serious problem with attempting to view range arithmetic as interval arithmetic is that the length calculations assume that the interval lengths are "short" relative to the magnitude of the numbers they represent. For even moderate precision, say 10 digits or so, this works quite nicely, but for For many calculations significance arithmetic is all that is needed. If we subtract two numbers we can lose many digits. The number of digits displayed in a number corresponds to the logarithm of the relative length of the interval. ;[s] 4:0,0;1219,1;1325,2;1333,3;1358,-1; 4:1,11,8,Times,0,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0; :[font = input; preserveAspect; ] N[Pi, 30] - 31415926535897932384626/10000000000000000000000 :[font = text; inactive; preserveAspect; ] For very well-conditioned functions we can gain many digits. ;[s] 3:0,0;43,1;47,2;61,-1; 3:1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0; :[font = input; preserveAspect; ] Erf[N[20, 20]] :[font = text; inactive; preserveAspect; ] The following example is from S. M. Rump, "Algorithms for verified inclusions---Theory and practice" in Reliability in Computing, R. E. Moore, ed., Academic Press, San Diego, CA, 1988, pp 109-126. The problem is to evaluate c = 333.75 b6 + a2 (11 a2 b2 - b6 - 121 b4 - 2) + 5.5 b8 + a / (2 b) where a = 77617 and b = 33096. With fixed-precision floating-point arithmetic one does not know how many digits of the result are correct. In fact, unless rather high-precision arithmetic is used the answer will be completely wrong. In fact in Rump's calculations, single precision, double precision, and even extended precision gave the same wrong result, even the sign was wrong. We start by defining a function that evaluates c starting with n digits. ;[s] 40:0,0;104,1;129,2;249,3;250,4;260,5;261,6;262,7;265,8;266,9;268,10;272,11;273,12;275,13;276,14;277,15;280,16;281,17;282,18;289,19;290,20;291,21;303,22;304,23;305,24;308,25;310,26;315,27;316,28;345,29;346,30;359,31;360,32;680,33;690,34;772,35;773,36;774,37;788,38;789,39;798,-1; 40:1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,32,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,32,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,32,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,32,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,32,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,32,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,32,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,10,8,Times,3,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0; :[font = input; preserveAspect; ] c[n_] := Block[{a = N[77617, n], b = N[33096, n]}, N[333+3/4, n] b^6 + a^2 (11 a^2 b^2 - b^6 - 121 b^4 - 2) + N[5 + 1/2, n] b^8 + a/(2 b) ] :[font = input; preserveAspect; ] {c[10], c[20], c[30], c[40], c[50]} :[font = text; inactive; preserveAspect; ] 10 digits is less than $MachinePrecision so the first calculation was done using machine numbers and is completely wrong. ;[s] 3:0,0;23,1;40,2;122,-1; 3:1,11,8,Times,0,12,0,0,0;1,10,8,Courier,1,12,0,0,0;1,11,8,Times,0,12,0,0,0; :[font = input; preserveAspect; ] SetPrecision[%[[3]], 10] :[font = text; inactive; preserveAspect; ] The first three digits of the result using 30 digits were in fact correct, but none of the digits was known to be correct so none was displayed. We can let Mathematica worry about how much precision is required to get a specified precision in the result. ;[s] 3:0,0;157,1;168,2;257,-1; 3:1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0; :[font = input; preserveAspect; endGroup; ] n = 20; While[Precision[nc = c[n]] < 10, n += 5]; nc :[font = section; inactive; Cclosed; preserveAspect; startGroup; ] Interval Arithmetic :[font = text; inactive; preserveAspect; ] There are problems where range arithmetic is not sufficient and you really do want interval arithmetic. The interval arithmetic in Mathematica Version 2.2 is genuine interval arithmetic, complete with outward rounding and multi-intervals. Its only deficiency is that the outward rounding is done a posteriori rather than as directed rounding in hardware, which many machines do not support. Thus the intervals tend to grow slightly more rapidly than is absolutely necessary. Also, complex intervals have not yet been implemented. ;[s] 5:0,0;132,1;143,2;298,3;310,4;533,-1; 5:1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0; :[font = input; preserveAspect; ] a = Interval[{2, 3}] :[font = input; preserveAspect; ] b = Interval[{-2, 1}] :[font = input; preserveAspect; ] c = a + b :[font = input; preserveAspect; ] a/b :[font = input; preserveAspect; ] a > b :[font = input; preserveAspect; ] d = IntervalUnion[a, b] :[font = input; preserveAspect; ] IntervalIntersection[c, d] :[font = input; preserveAspect; ] Sin[a] :[font = input; preserveAspect; ] Sin[b] :[font = input; preserveAspect; ] Interval[Pi] :[font = input; preserveAspect; ] d = Interval[2.3] :[font = input; preserveAspect; endGroup; ] d[[1,2]] - d[[1,1]] :[font = section; inactive; Cclosed; preserveAspect; startGroup; ] Interval Rootfinding :[font = text; inactive; preserveAspect; ] The following examples are not intended to be complete, robust algorithms. They are merely intended to illustrate some of the possibilities for interval algorithms in Mathematica. ;[s] 3:0,0;168,1;179,2;180,-1; 3:1,11,8,Times,0,12,0,0,0;1,10,8,Times,2,12,0,0,0;1,11,8,Times,0,12,0,0,0; :[font = subsection; inactive; Cclosed; preserveAspect; startGroup; ] Bisection :[font = text; inactive; preserveAspect; ] In a bisection search a given interval is recursively bisected and each half is tested for roots. In interval arithmetic, testing for roots can be done by testing whether 0 is in the image of the interval. IntervalMemberQ[ ] works nicely for this. ;[s] 3:0,0;207,1;225,2;249,-1; 3:1,11,8,Times,0,12,0,0,0;1,10,8,Courier,1,12,0,0,0;1,11,8,Times,0,12,0,0,0; :[font = input; preserveAspect; ] intervalbisection::rec = "MaxRecursion exceeded."; :[font = input; preserveAspect; ] split[f_, x_, int_Interval, eps_, n_] := Block[{a = int[[1,1]], b = int[[1,2]], c}, If[!IntervalMemberQ[f /. x -> int, 0], Return[{}]]; If[b-a < eps, Return[int]]; If[n == 0, Message[intervalbisection::rec]; Return[int]]; c = (a+b)/2; split[f, x, #, eps, n-1]& /@ {Interval[{a,c}], Interval[{c,b}]} ]; :[font = input; preserveAspect; ] Options[intervalbisection] = {MaxRecursion -> 7}; :[font = input; preserveAspect; ] intervalbisection[f_, x_, intab_, eps_, opts___] := Block[{int, n}, n = MaxRecursion /. {opts} /. Options[intervalbisection]; int = Interval /@ (List @@ intab); IntervalUnion @@ Flatten[split[f, x, #, eps, n]& /@ int] ]; :[font = input; preserveAspect; ] intervalbisection[Sin[x], x, Interval[{2., 20.}], .1] :[font = input; preserveAspect; ] intervalbisection[Sin[x], x, %, .1] :[font = input; preserveAspect; ] intervalbisection[Sin[x], x, Interval[{2., 20.}], .1, MaxRecursion -> 10] :[font = input; preserveAspect; endGroup; ] intervalbisection[Sin[1/x], x, Interval[{-1., 1.}], .01] :[font = subsection; inactive; Cclosed; preserveAspect; startGroup; ] Newton's Method :[font = text; inactive; preserveAspect; ] With Newton's method the idea is to pick a point in the given interval and evaluate the function at that point. Evaluating the derivative of the function on the interval allows us to eliminate parts of the original interval (assuming that the derivative is bounded). :[font = input; preserveAspect; ] intervalnewton::rec = "MaxRecursion exceeded."; :[font = input; preserveAspect; ] intnewt[f_, df_, x_, {a_, b_}, eps_, n_] := Block[{xmid, int = Interval[{a, b}]}, If[b-a < eps, Return[int]]; If[n == 0, Message[intervalnewton::rec]; Return[int]]; xmid = Interval[(a+b)/2]; int = IntervalIntersection[int, xmid - N[f /. x -> xmid]/N[df /. x -> int]]; (intnewt[f, df, x, #, eps, n-1])& /@ (List @@ int) ]; :[font = input; preserveAspect; ] Options[intervalnewton] = {MaxRecursion -> 7}; :[font = input; preserveAspect; ] intervalnewton[f_, x_, int_Interval, eps_, opts___] := Block[{df, n}, n = MaxRecursion/.{opts}/.Options[intervalnewton]; df = D[f, x]; IntervalUnion @@ Select[ Flatten[(intnewt[f, df, x, #, eps, n])& /@ (List @@ int)], IntervalMemberQ[N[f /. x -> #], 0]&] ]; :[font = input; preserveAspect; ] intervalnewton[Sin[x], x, Interval[{2., 20.}], .1] :[font = input; preserveAspect; ] intervalnewton[Sin[x], x, %, 0.00001] :[font = input; preserveAspect; ] intervalnewton[Sin[x]^2, x, Interval[{2., 20.}], .1] :[font = input; preserveAspect; endGroup; ] intervalnewton[Sin[1/x], x, Interval[{-1., 1.}], .01] :[font = subsection; inactive; Cclosed; preserveAspect; startGroup; ] Multidimensional Bisection :[font = text; inactive; preserveAspect; ] In principle, multidimensional bisection is no more difficult than one-dimensional bisection. In practice, however, combinatorial explosion becomes a problem. Here is a simple implementation of the multidimensional bisection method. :[font = input; preserveAspect; ] bis[int_Interval, eps_] := Block[{a = int[[1,1]], b = int[[1,2]], c}, If[b-a < eps, Return[{int}]]; c = (a+b)/2; {Interval[{a, c}], Interval[{c, b}]} ]; :[font = input; preserveAspect; ] split[f_, x_, int_, eps_] := Block[{intlist}, If[Or @@ (!IntervalMemberQ[#, 0]& /@ (f /. Apply[Rule, Transpose[{x, int}], 1])), Return[{}]]; intlist = Flatten[Outer[List, Sequence @@ (bis[#, eps]& /@ int)], Length[int]-1]; If[Length[intlist] == 1, Return[int]]; Sequence @@ (split[f,x,#,eps]& /@ intlist) ]; :[font = input; preserveAspect; ] recombine[ {d1___List, {a___Interval, b1_Interval, c___Interval}, d2___List, {a___Interval, b2_Interval, c___Interval}, d3___List}] := recombine[{d1,{a,IntervalUnion[b1,b2],c},d2,d3}] :[font = input; preserveAspect; ] intervalbisection[f_, x_, intab_, eps_] := Block[{int}, int = (Interval /@ (List @@ #))& /@ intab; int = Flatten[Outer[List, Sequence @@ int], Length[int]-1]; recombine[Select[split[f,x,#,eps]& /@ int, (Length[#]>0)&]][[1]] ]; :[font = input; preserveAspect; endGroup; endGroup; endGroup; ] intervalbisection[{x^2 + y^2 - 2, x^2 - y^2 - 1}, {x, y}, {Interval[{-3., 3.}], Interval[{-3., 3.}]}, .001] ^*)