Saturday, February 18, 2023

HP Prime: Interval Arithmetic

HP Prime:  Interval Arithmetic



Interval Arithmetic



In computing, we are working with approximate calculation.  Often a number can be stated as a small interval to account for round off error.  


An interval is a range of two numbers from a to b:  [a, b].  The variable a represents the lower bound, with the variable b represents the upper bound.  There is no restriction on how large or small the interval is.


For example, the interval [ 81.8, 82.2 ] represents the range from 81.8 to 82.2.  This could stand for a calculated value of 82.0 with a plus-or-minus error of 0.2.  


Applying a general function of one function, f(x) to interval, results in this:

f([a, b]) = [ min(f(a), f(b)), max(f(a), f(b)) ]


Applying this to the square root function to intervals:

√([a,b]) = [ min(√a, √b), max(√a, √b) ]


The arithmetic functions for intervals are defined as:


[a, b] + [c, d] = [a + c, b + d]

[a, b] - [c, d] = [a - d, b - c]

[a, b] × [c, d] = [min(a × c, a × d, b × c, b × d), max(a × c, a × d, b × c, b × d)]

[a, b] ÷ [c, d] = [min(a/c, a/d, b/c, b/d), max(a/c, a/d, b/c, b/d)]


Scalar multiplication, given the scalar is positive:


s × [a, b] = [ s × a, s × b ]



HP Prime Interval Programs



INTERVALHELP:  help screen for interval arithmetic functions

INTERVALADD:  adding two intervals

INTERVALSUB:  subtracting two intervals

INTERVALMUL:  multiply two intervals

INTERVALDIV:  divide two intervals


In the Home mode, use the curly brackets (used for lists { }), to type the intervals.  For example, the interval [ 81.8, 82.2 ] would be typed {81.8,82.2}.


EXPORT INTERVALHELP()

BEGIN

// 2022-12-22 EWS

PRINT();

PRINT("Interval Arithmetic");

PRINT("Use curly brackets {}");

PRINT("");

PRINT("INTERVALADD({a,b},{c,d})");

PRINT("add intervals");

PRINT("");

PRINT("INTERVALSUB({a,b},{c,d}");

PRINT("subtract intervals");

PRINT("");

PRINT("INTERVALMUL({a,b},{c,d})");

PRINT("multiply intervals");

PRINT("");

PRINT("INTERVALDIV({a,b},{c,d})");

PRINT("divide intervals");

END;


EXPORT INTERVALADD(v1,v2)

BEGIN

RETURN v1+v2;

END;


EXPORT INTERVALSUB(v1,v2)

BEGIN

RETURN v1-REVERSE(v2);

END;


EXPORT INTERVALMUL(v1,v2)

BEGIN

LOCAL v3;

v3:=CONCAT(v1*v2,v1*REVERSE(v2));

RETURN {MIN(v3),MAX(v3)};

END;


EXPORT INTERVALDIV(v1,v2)

BEGIN

INTERVALMUL(v1,1/REVERSE(v2));

END;


Download a zip file here:  https://drive.google.com/file/d/1L8mmnm3xx58PVjavDROCiZAafBOCsH9o/view?usp=share_link



Example



Interval 1 = {13, 29}

Interval 2 = {10, 14}


Add:  {23, 43}

Subtract:  {-1, 19}

Multiply:  {130, 406}

Divide:  {0.928571428572, 2.9}



Source


Moore, Ramon E.  Mathematical Elements of Scientific Computing  Holt, Rinehart, and Winston, Inc: New York.  1975.  ISBN 0-03-088125-0




Until next time and wish you all well,


Eddie  


All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.