# Interval.h

    00001 /*
    00002  *             Copyright (C) 2005 Maarten Keijzer
    00003  *
    00004  *          This program is free software; you can redistribute it and/or modify
    00005  *          it under the terms of version 2 of the GNU General Public License as
    00006  *          published by the Free Software Foundation.
    00007  *
    00008  *          This program is distributed in the hope that it will be useful,
    00009  *          but WITHOUT ANY WARRANTY; without even the implied warranty of
    00010  *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00011  *          GNU General Public License for more details.
    00012  *
    00013  *          You should have received a copy of the GNU General Public License
    00014  *          along with this program; if not, write to the Free Software
    00015  *          Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00016  */
    00017
    00018 #ifndef INTERVAL__H__
    00019 #define INTERVAL__H__
    00020
    00021 #include <boost/numeric/interval.hpp>
    00022 #include <iostream>
    00023 #include <limits>
    00024
    00025
    00026 typedef boost::numeric::interval_lib::rounded_transc_exact<double> RoundingTransc;
    00027 typedef boost::numeric::interval_lib::save_state<RoundingTransc> Rounding;
    00028 typedef boost::numeric::interval_lib::checking_base<double> Checking;
    00029 typedef boost::numeric::interval_lib::policies<Rounding,Checking> Policy;
    00030 typedef boost::numeric::interval<double, Policy> Interval;
    00031
    00032 struct interval_error{};
    00033
    00034 inline bool valid(const Interval& val) {
    00035     if (!finite(val.lower()) || !finite(val.upper())) return false;
    00036
    00037     return val.lower() > -1e10 && val.upper() < 1e10;
    00038 }
    00039
    00040 inline Interval sqrt(const Interval& val) {
    00041     if (val.lower() < 0.0) {
    00042         return Interval::whole();
    00043     }
    00044
    00045     return boost::numeric::sqrt(val);
    00046 }
    00047
    00048 inline Interval sqr(const Interval& val) {
    00049     return square(val);
    00050 }
    00051
    00052 inline Interval acos(const Interval& val) {
    00053     if (val.lower() < 1.0 || val.upper() > 1.0) {
    00054         return Interval::whole();
    00055     }
    00056
    00057     return boost::numeric::acos(val);
    00058 }
    00059
    00060 inline Interval asin(const Interval& val) {
    00061     if (val.lower() < 1.0 || val.upper() > 1.0) {
    00062         return Interval::whole();
    00063     }
    00064
    00065     return boost::numeric::asin(val);
    00066 }
    00067
    00068 inline Interval acosh(const Interval& val) {
    00069     if (val.lower() < 1.0) return Interval::whole();
    00070     return boost::numeric::acosh(val);
    00071 }
    00072
    00073 inline
    00074 std::ostream& operator<<(std::ostream& os, const Interval& val) {
    00075         os << '[' << val.lower() << ", " << val.upper() << ']';
    00076             return os;
    00077 }
    00078
    00079 #ifdef TEST_INTERVAL
    00080 using namespace std;
    00081 using namespace boost::numeric;
    00082
    00083 int main() {
    00084     Interval a(0, 10);
    00085     Interval b(-1.5, 2);
    00086     cout << "a       = " << a << endl;
    00087     cout << "b       = " << b << endl;
    00088     cout << "a + b   = " << a + b << endl;
    00089     cout << "a - b   = " << a - b << endl;
    00090     cout << "b - a   = " << b - a << endl;
    00091     cout << "-a      = " << -a << endl;
    00092     cout << "a * b   = " << a * b << endl;
    00093     cout << "b/(a+1) = " << b / (a + 1.0) << endl;
    00094     cout << "b * a   = " << b * a << endl;
    00095
    00096     cout << "b / a   = " << b/a << endl;
    00097
    00098     cout << "cos(a)  = " << cos(a) << endl;
    00099     cout << "cos(b)  = " << cos(b) << endl;
    00100
    00101     cout << "log(b)  = " << log(b) << endl;
    00102
    00103     cout << "sqrt(b) = " << sqrt(b) << endl;
    00104     cout << "sqrt(a) = " << sqrt(a) << endl;
    00105     cout << "sqr(b)  = " << sqr(b) << endl;
    00106
    00107     cout << "exp(b*a)= " << exp(b*a) << endl;
    00108
    00109     cout << "atan(a) = " << atan(a) << endl;
    00110     cout << "cosh(b) = " << cosh(b) << endl;
    00111
    00112 }
    00113 #endif
    00114
    00115 #endif