Copyright 1994-2004 Sun Microsystems, Inc., 901 San Antonio Road, Palo Alto, CA 94303, U.S.A. All rights reserved. --- CODE EXAMPLE 1-1 ----------------------------- math%: cat ce1-1.cc #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { cout <<"[2,3]+[4,5]=" << (interval("[2,3]") + interval("[4,5]")); cout << endl; } math%: CC -xia -o ce1-1 ce1-1.cc math%: ce1-1 [2,3]+[4,5]=[0.6000000000000000E+001,0.8000000000000000E+001] --- CODE EXAMPLE 1-2 ----------------------------- math%: cat ce1-2.cc #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { interval X("[2,3]"); interval Y("3"); // interval [2,4] is represented cout <<"[2,3]+[2,4]=" << X + Y; cout << endl; } math%: CC -xia -o ce1-2 ce1-2.cc math%: ce1-2 [2,3]+[2,4]=[0.4000000000000000E+001,0.7000000000000000E+001] --- CODE EXAMPLE 1-3 ----------------------------- math%: cat ce1-3.cc #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { interval X, Y; cout << "Press Control/C to terminate!"<< endl; cout <<" X,Y=?"; cin >>X >>Y; for (;;){ cout <>X>>Y; } } math%: CC -xia -o ce1-3 ce1-3.cc math%: ce1-3 Press Control/C to terminate! X,Y=?[1,2] [3,4] For X =[0.1000000000000000E+001,0.2000000000000000E+001] , and Y=[0.3000000000000000E+001,0.4000000000000000E+001] X+Y=[0.4000000000000000E+001,0.6000000000000000E+001] X-Y=[-.3000000000000000E+001,-.1000000000000000E+001] X*Y=[0.3000000000000000E+001,0.8000000000000000E+001] X/Y=[0.2500000000000000E+000,0.6666666666666668E+000] pow(X,Y)=[0.1000000000000000E+001,0.1600000000000000E+002] X,Y=?[1,2] -inf For X =[0.1000000000000000E+001,0.2000000000000000E+001] , and Y=[ -Infinity,-.1797693134862315E+309] X+Y=[ -Infinity,-.1797693134862315E+309] X-Y=[0.1797693134862315E+309, Infinity] X*Y=[ -Infinity,-.1797693134862315E+309] X/Y=[-.1112536929253602E-307,0.0000000000000000E+000] pow(X,Y)=[0.0000000000000000E+000, Infinity] X,Y=? --- CODE EXAMPLE 1-4 ----------------------------- math%: cat ce1-4.cc #include #include #include #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif template< typename T > interval random_interval() { T x = (T)drand48(); T y = (T)drand48(); T t; if (x <= y) return interval(x, y); else return interval(y, x); } int main() { int I, NI, KIND, SN; interval X4[10], XL4[10], XR4[10], XX4[10]; interval X8[10], XL8[10], XR8[10], XX8[10]; interval X16[10], XL16[10], XR16[10], XX16[10]; cout << "Press Control/C to terminate!"<< endl; for (;;){ cout << endl; cout << "Enter number of intervals (less then 10), then 4 - for float,"; cout << "8 - for double, or 16 - for long double,"; cout << " and then 0 for [inf,sup] output and 1 for single-number output: "; cin >> NI >> KIND >> SN; cout << endl; if (NI > 10) { cout << "Number of intervals is greate than 10. " << endl; } I = 0; do { switch (KIND) { case 4: XL4[I] = random_interval(); XX4[I] = random_interval(); XL4[I] = (XL4[I] - XX4[I])* XX4[I]; XR4[I] = random_interval(); X4[I] = interval_hull(interval(XL4[I]), interval(XR4[I])); break; case 8: XL8[I] = random_interval(); XX8[I] = random_interval(); XL8[I] = (XL8[I] - XX8[I])* XX8[I]; XR8[I] = random_interval(); X8[I] = interval_hull(interval(XL8[I]), interval(XR8[I])); break; case 16: XL16[I] = random_interval(); XX16[I] = random_interval(); XL16[I] = (XL16[I] - XX16[I])* XX16[I]; XR16[I] = random_interval(); X16[I] = interval_hull(interval(XL16[I]), interval(XR16[I])); break; default: cout << "KIND should be 4, 8, 16" << endl; break; } I++; } while (I < NI); if (SN == 0) { cout << NI <<" intervals, output mode [inf,sup], KIND = "<< KIND <<":"<)X4[I], cout); cout << endl; I++; } while (I < NI); break; case 8: I = 0; do { single_number_output ((interval)X8[I], cout); cout << endl; I++; } while (I < NI); break; case 16: I = 0; do { single_number_output ((interval)X16[I], cout); cout << endl; I++; } while (I < NI); break; default: cout << "KIND should be 4, 8, 16" << endl; break; }; } else cout << "Interval indicator is 0, number indicator is 1." << endl < #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { char BUFFER[128]; cout << "Press Control/C to terminate!"<< endl; cout << "X=?"; cin >>BUFFER; for(;;) { interval X(BUFFER); cout << endl << "Your input was:" <>BUFFER; } } math%: CC -xia -o ce1-6 ce1-6.cc math%: ce1-6 Press Control/C to terminate! X=?1.37 Your input was:1.37 Resulting stored interval is: [0.1359999999999999E+001,0.1380000000000001E+001] Single number interval output is: 0.13 E+001 X=?1.444 Your input was:1.444 Resulting stored interval is: [0.1442999999999999E+001,0.1445000000000001E+001] Single number interval output is: 0.144 E+001 X=? --- CODE EXAMPLE 1-7 ----------------------------- math%: cat ce1-7.cc #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { interval X1("[0.1]"); interval N(3); interval A (5.0); interval X = X1 * A / N; cout << "[0.1]*[A]/[N]=" < #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { interval X; cout << "Press Control/C to terminate!"<< endl; cout <<"X=?"; cin >>X; for(;;){ cout <>X; } } math%: CC -xia -o ce1-8 ce1-8.cc math%: ce1-8 Press Control/C to terminate! X=?[1.23456,1.234567890] For X =[0.1234559999999999E+001,0.1234567890000001E+001] mid(X)=1.23456 mig(X)=1.23456 mag(X)=1.23457 wid(X)=7.89e-06 X=?[1,10] For X =[0.1000000000000000E+001,0.1000000000000000E+002] mid(X)=5.5 mig(X)=1 mag(X)=10 wid(X)=9 X=? --- CODE EXAMPLE 1-9 ----------------------------- math%: cat ce1-9.cc #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { interval X; cout << "Press Control/C to terminate!"<< endl; cout <<"X=?"; cin >>X; for (;;) { cout <>X; } } math%: CC -xia -o ce1-9 ce1-9.cc math%: ce1-9 Press Control/C to terminate! X=?[1.1,1.2] For X =[0.1099999999999999E+001,0.1200000000000001E+001] abs(X)=[0.1099999999999999E+001,0.1200000000000001E+001] log(X)=[0.9531017980432472E-001,0.1823215567939548E+000] sqrt(X)=[0.1048808848170151E+001,0.1095445115010333E+001] sin(X)=[0.8912073600614351E+000,0.9320390859672266E+000] acos(X)=[EMPTY ] X=?[-0.5,0.5] For X =[-.5000000000000000E+000,0.5000000000000000E+000] abs(X)=[0.0000000000000000E+000,0.5000000000000000E+000] log(X)=[ -Infinity,-.6931471805599452E+000] sqrt(X)=[0.0000000000000000E+000,0.7071067811865476E+000] sin(X)=[-.4794255386042031E+000,0.4794255386042031E+000] acos(X)=[0.1047197551196597E+001,0.2094395102393196E+001] X=? --- CODE EXAMPLE 2-1 ----------------------------- math%: cat ce2-1.cc #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { interval X1("[1,2]"); interval X2("[1]"); interval X3("1"); interval X4("[0.1]"); interval X5("0.1"); interval X6("0.10"); interval X7("0.100"); interval X8("[2,1]"); cout << "X1=" << X1 << endl; cout << "X2=" << X2 << endl; cout << "X3=" << X3 << endl; cout << "X4=" << X4 << endl; cout << "X5=" << X5 << endl; cout << "X6=" << X6 << endl; cout << "X7=" << X7 << endl; cout << "X8=" << X8 << endl; } math%: CC -xia -o ce2-1 ce2-1.cc math%: ce2-1 X1=[0.1000000000000000E+001,0.2000000000000000E+001] X2=[0.1000000000000000E+001,0.1000000000000000E+001] X3=[0.0000000000000000E+000,0.2000000000000000E+001] X4=[0.9999999999999999E-001,0.1000000000000001E+000] X5=[0.0000000000000000E+000,0.2000000000000001E+000] X6=[0.8999999999999999E-001,0.1100000000000001E+000] X7=[0.9899999999999999E-001,0.1010000000000001E+000] X8=[ -Infinity, Infinity] --- CODE EXAMPLE 2-2 ----------------------------- math%: cat ce2-2.cc #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif const interval Y("[0.1]"); const int limit = 100000; int main() { interval RESULT(0.0); clock_t t1= clock(); if(t1==clock_t(-1)){cerr<< "sorry, no clock\n"; exit(1);} for (int i = 0; i < limit; i++){ RESULT += Y; } clock_t t2= clock(); if(t2==clock_t(-1)){cerr<< "sorry, clock overflow\n"; exit(2);} cout << "efficient loop took " << double(t2-t1)/CLOCKS_PER_SEC << " seconds" << endl; cout << "result" << RESULT << endl ; t1= clock(); if(t1==clock_t(-1)){cerr<< "sorry, clock overflow\n"; exit(2);} for (int i = 0; i < limit; i++){ RESULT += interval("[0.1]"); } t2= clock(); if(t2==clock_t(-1)){cerr<< "sorry, clock overflow\n"; exit(2);} cout << "inefficient loop took " << double(t2-t1)/CLOCKS_PER_SEC << " seconds" << endl; cout << "result" << RESULT << endl ; } math%: CC -xia -o ce2-2 ce2-2.cc math%: 35 -> ce2-2 efficient loop took 0.08 seconds result[0.9999999999947978E+004,0.1000000000003054E+005] inefficient loop took 3.15 seconds result[0.1999999999980245E+005,0.2000000000013270E+005] --- CODE EXAMPLE 2-3 ----------------------------- math%: cat ce2-3.cc #include #include #include #include #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { //Compute 0.7-0.1-0.2-0.3-0.1 == 0.0 interval correct_result; const interval x1("[0.1]"), x2("[0.2]"),x3("[0.3]"),x7("[0.7]"); cout << "Exact result:" << 0.0 << endl ; cout << "Incorrect evaluation:" << interval(0.7-0.1-0.2-0.3-0.1, 0.7-0.1-0.2-0.3-0.1) << endl ; correct_result = x7-x1-x2-x3-x1; cout << "Correct evaluation:" << correct_result << endl ; } math%: CC -xia -o ce2-3 ce2-3.cc math%: ce2-3 Exact result:0 Incorrect evaluation:[-.2775557561562892E-016,-.2775557561562891E-016] Correct evaluation:[-.1942890293094024E-015,0.1526556658859591E-015] --- CODE EXAMPLE 2-4 ----------------------------- math%: cat ce2-4.cc #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { interval X; long double a,b; cout << "Press Control/C to terminate!"<< endl; cout <<" a,b =?"; cin >>a >>b; for(;;){ cout <>a >>b; } } math%: CC -xia -o ce2-4 ce2-4.cc math%: ce2-4 Press Control/C to terminate! a,b =?1.0e+400 -0.1 For a =1e+400, and b =-0.1 Check X=[-.10000001E+000, Infinity] a,b =? --- CODE EXAMPLE 2-5 ----------------------------- math%: cat ce2-5.cc #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { interval X, Y; interval DX, DY; float R = 0.1f, S = 0.2f, T = 0.3f; double R8 = 0.1, T1, T2; Y = interval (R,R); X = interval (0.1f); //note 1 if (X == Y) cout <<"Check1"<< endl; X = interval (0.1f, 0.1f); if (X == Y) cout <<"Check2"<< endl; T1 = R + S; T2 = T + R8; DY = interval (T1, T2); DX = interval (double(R+S), double(T+R8)); //note 2 if (DX == DY) cout <<"Check3"<< endl; DX = interval (Y); //note 3 if (ceq(DX,interval (0.1f, 0.1f))) cout <<"Check4"<< endl; } math%: CC -xia -o ce2-5 ce2-5.cc math%: ce2-5 Check1 Check2 Check3 Check4 --- CODE EXAMPLE 2-6 ----------------------------- math%: cat ce2-6.cc #include #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { interval X("[10.E-10,11.E-10]"); interval Y; Y = interval(-MINDOUBLE, MINDOUBLE) + X; cout << "X is " << ((!in_interior(X,Y))? "not": "")<< "in interior of Y" < #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { interval DX; float R=0.0, S=0.0, T; T = R/S; //note 1 cout<< T <(T,S)<(T,T)<(2.,1.)<(1./R)< #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { interval X, Y; double R; R = 1.5; cout << "Press Control/C to terminate!"<< endl; cout <<"X,Y=?"; cin >>X >>Y; for(;;){ cout <>X>>Y; } } math%: CC -xia -o ce2-8 ce2-8.cc math%: ce2-8 Press Control/C to terminate! X,Y=?[1] [2] For X =[0.1000000000000000E+001,0.1000000000000000E+001], and Y =[0.2000000000000000E+001,0.2000000000000000E+001] interval_hull(X,Y)= [0.1000000000000000E+001,0.2000000000000000E+001] intersect(X,Y)=[EMPTY ] disjoint(X,Y)=T in(R,Y)=F in_interior(X,Y)=F proper_subset(X,Y)=F proper_superset(X,Y)=F subset(X,Y)=F superset(X,Y)=F X,Y=?[1,2] [1,3] For X =[0.1000000000000000E+001,0.2000000000000000E+001], and Y =[0.1000000000000000E+001,0.3000000000000000E+001] interval_hull(X,Y)= [0.1000000000000000E+001,0.3000000000000000E+001] intersect(X,Y)=[0.1000000000000000E+001,0.2000000000000000E+001] disjoint(X,Y)=F in(R,Y)=T in_interior(X,Y)=F proper_subset(X,Y)=T proper_superset(X,Y)=F subset(X,Y)=T superset(X,Y)=F X,Y=? --- CODE EXAMPLE 2-9 ----------------------------- math%: cat ce2-9.cc #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { interval X("[2,3]"); interval Y("[4,5]"); if (X+Y == interval ("[6,8]")) cout << "Check." < #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { interval X, Y; cout << "Press Control/C to terminate!"<< endl; cout <<" X,Y =?"; cin >>X >>Y; for(;;){ cout <>X>>Y; } } math%: CC -xia -o ce2-10 ce2-10.cc math%: ce2-10 Press Control/C to terminate! X,Y =?[2] [3] For X =[0.2000000000000000E+001,0.2000000000000000E+001], and Y =[0.3000000000000000E+001,0.3000000000000000E+001] ceq(X,Y),peq(X,Y),seq(X,Y)=F F F cne(X,Y),pne(X,Y),sne(X,Y)=T T T cle(X,Y),ple(X,Y),sle(X,Y)=T T T clt(X,Y),plt(X,Y),slt(X,Y)=T T T cge(X,Y),pge(X,Y),sge(X,Y)=F F F cgt(X,Y),pgt(X,Y),sgt(X,Y)=F F F X,Y =?2 3 For X =[0.1000000000000000E+001,0.3000000000000000E+001], and Y =[0.2000000000000000E+001,0.4000000000000000E+001] ceq(X,Y),peq(X,Y),seq(X,Y)=F T F cne(X,Y),pne(X,Y),sne(X,Y)=F T T cle(X,Y),ple(X,Y),sle(X,Y)=F T T clt(X,Y),plt(X,Y),slt(X,Y)=F T T cge(X,Y),pge(X,Y),sge(X,Y)=F T F cgt(X,Y),pgt(X,Y),sgt(X,Y)=F T F X,Y =? --- CODE EXAMPLE 2-11 ----------------------------- math%: cat ce2-11.cc #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif main() { interval X[8]; for (int i = 0; i < 8 ; i++) { cin >> X[i]; cout << X[i] << endl; } } math%: CC -xia -o ce2-11 ce2-11.cc math%: ce2-11 1.234500 [0.1234498999999999E+001,0.1234501000000001E+001] [1.2345] [0.1234499999999999E+001,0.1234500000000001E+001] [-inf,2] [ -Infinity,0.2000000000000000E+001] [-inf] [ -Infinity,-.1797693134862315E+309] [EMPTY] [EMPTY ] [1.2345,1.23456] [0.1234499999999999E+001,0.1234560000000001E+001] [inf] [0.1797693134862315E+309, Infinity] [Nan] [ -Infinity, Infinity] --- CODE EXAMPLE 2-12 ----------------------------- math%: cat ce2-12.cc #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { interval X(-1, 10); interval Y(1, 6); single_number_output(X, cout); cout << endl; single_number_output(Y, cout); cout << endl; } math%: CC -xia -o ce2-12 ce2-12.cc math%: ce2-12 [ -1.0000 , 10.000 ] [ 1.0000 , 6.0000 ] --- CODE EXAMPLE 2-13 ----------------------------- math%: cat ce2-13.cc #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif main() { interval X[4]; X[0] = interval(1.2345678, 1.23456789); X[1] = interval(1.234567, 1.2345678); X[2] = interval(1.23456, 1.234567); X[3] = interval(1.2345, 1.23456); for (int i = 0; i < 4 ; i++) { single_number_output((interval)X[i], cout); cout << " ndigits =" << ndigits(X[i]) << endl; } } math%: CC -xia -o ce2-13 ce2-13.cc math%: ce2-13 0.12345678 E+001 ndigits =8 0.1234567 E+001 ndigits =7 0.123456 E+001 ndigits =6 0.12345 E+001 ndigits =5 --- CODE EXAMPLE 2-14 ----------------------------- math%: cat ce2-14.cc #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { interval X,Y; cout << "Press Control/C to terminate!"<< endl; cout <<"Y,X=?"; cin >>Y >>X; for(;;) { cout <>Y >>X; } } math%: CC -xia -o ce2-14 ce2-14.cc math%: ce2-14 Press Control/C to terminate! Y,X=?[0] [0] For X =[0.0000000000000000E+000,0.0000000000000000E+000] For Y =[0.0000000000000000E+000,0.0000000000000000E+000] [-.3141592653589794E+001,0.3141592653589794E+001] Y,X=?inf inf For X =[0.1797693134862315E+309, Infinity] For Y =[0.1797693134862315E+309, Infinity] [0.0000000000000000E+000,0.1570796326794897E+001] Y,X=?inf -inf For X =[ -Infinity,-.1797693134862315E+309] For Y =[0.1797693134862315E+309, Infinity] [0.1570796326794896E+001,0.3141592653589794E+001] Y,X=?-inf inf For X =[0.1797693134862315E+309, Infinity] For Y =[ -Infinity,-.1797693134862315E+309] [-.1570796326794897E+001,0.0000000000000000E+000] Y,X=?-inf -inf For X =[ -Infinity,-.1797693134862315E+309] For Y =[ -Infinity,-.1797693134862315E+309] [-.3141592653589794E+001,-.1570796326794896E+001] Y,X=? --- CODE EXAMPLE 2-15 ----------------------------- math%: cat ce2-15.cc #include #include #include #include #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { std::stack > st; //note 1 return 0; } math%: CC -xia -o ce2-15 ce2-15.cc --- CODE EXAMPLE 2-16 ----------------------------- math%: cat ce2-16.cc #include #include #include #include #include #if __cplusplus >= 199711 using namespace SUNW_interval; #endif int main() { std::stack> st; //note 1 return 0; } math%: CC -xia -o ce2-16 ce2-16.cc "ce2-16.cc", line 13: Error: "," expected instead of ">>". "ce2-16.cc", line 13: Error: Illegal value for template parameter. "ce2-16.cc", line 13: Error: "," expected instead of ";". "ce2-16.cc", line 13: Error: Illegal value for template parameter. 4 Error(s) detected. --- CODE EXAMPLE 2-17 ----------------------------- math% cat ce2-17.cc #include #include using namespace SUNW_interval; main () { // create two vectors nvector< interval > v1 ( interval (2.0, 3.0), 10); nvector< double > v2 (10); // compute middle points of v1 elements v2 = mid (v1); // print them out cout << v2 << endl; // print scalar product of vectors v1 and v1*v1 cout << dot_product (v1, v1*v1) << endl; } math% CC -xia -o ce2-17 ce2-17.cc math% ce2-17 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 [0.8000000000000000E+002,0.2700000000000000E+003] --- CODE EXAMPLE 2-18 ----------------------------- math%: cat ce2-18.cc #include #include using namespace SUNW_interval; main() { // create matrix and vector nmatrix< interval > m( interval(2.0, 3.0), 3, 3); nvector< interval > v( interval(2.0, 3.0), 3); // examples of equivalent references to // element at line 2 and column 3 m(1,2) = interval(4.0); cout << m(1)(2)<< endl; cout << m(1)[2]<< endl; cout << m[1](2)<< endl; cout << m[1][2]<< endl; // print result of multiplication of matrix by column cout << matmul(m,v) << endl; // print result of multiplication of line by matrix cout << matmul(v,m) << endl; } math%: CC -xia -o ce2-18 ce2-18.cc math%: ce2-18 [0.4000000000000000E+001,0.4000000000000000E+001] [0.4000000000000000E+001,0.4000000000000000E+001] [0.4000000000000000E+001,0.4000000000000000E+001] [0.4000000000000000E+001,0.4000000000000000E+001] [0.1200000000000000E+002,0.2700000000000000E+002] [0.1600000000000000E+002,0.3000000000000000E+002] [0.1200000000000000E+002,0.2700000000000000E+002] [0.1200000000000000E+002,0.2700000000000000E+002] [0.1200000000000000E+002,0.2700000000000000E+002] [0.1600000000000000E+002,0.3000000000000000E+002]