• Main Page
  • Namespaces
  • Classes
  • Files
  • File List

RDE_lib2/dyadic_interval.h

00001 /* *************************************************************
00002 
00003 Copyright 2010 Terry Lyons, Stephen Buckley, Djalil Chafai, 
00004 Greg Gyurkó and Arend Janssen. 
00005 
00006 Distributed under the terms of the GNU General Public License, 
00007 Version 3. (See accompanying file License.txt)
00008 
00009 ************************************************************* */
00010 
00011 
00012 
00013 #pragma once
00014 
00015 #ifndef __DYADIC_INTERVAL__
00016 #define __DYADIC_INTERVAL__
00017 
00018 
00019 #include <deque>
00020 #include <iostream>
00021 #include "dyadic.h"
00022 
00023 
00025 class dyadic_interval : dyadic // use private derivation to avoid spurious conversions etc.
00026 {
00027 public:
00028         using dyadic::k;
00029         using dyadic::n;
00030         using dyadic::operator ++;
00031 
00033         template<typename S>
00034                 dyadic_interval(S s): dyadic(s)
00035         {}
00036 
00037         template<typename S, typename T>
00038                 dyadic_interval(S s, T t): dyadic(s,t)
00039         {}
00040 
00042         dyadic_interval(void)
00043                 : dyadic()
00044         {
00045         }
00046 
00048         ~dyadic_interval(void)
00049         {
00050         }
00051 
00054         const dyadic & inf(void) const
00055         {
00056                 return (const dyadic &) *this;
00057         }
00058 
00060         dyadic sup(void) const
00061         {
00062                 return ++ dyadic( *this );
00063         }
00064 
00066         dyadic_interval & flip_interval(void)
00067         {
00068                 k = k^1;
00069                 return *this;
00070         }
00071 
00073         int aligned (void) const
00074         {
00075                 return (k+1)%2;
00076         }
00077 
00080         dyadic_interval & shrink_interval_left(const unsigned int Arg = 1)
00081         {
00082                 k <<= Arg;
00083                 n += Arg;
00084                 return *this;
00085         }
00086 
00088         dyadic_interval & shrink_interval_right(void)
00089         {
00090                 ++(k <<= 1);
00091                 n += 1;
00092                 return *this;
00093         }
00094 
00097         dyadic_interval & expand_interval(const unsigned int Arg = 1)
00098         {
00099                 (dyadic::operator< (dyadic())) ? ++k : k;
00100                 k >>= Arg; // rounding always towards zero so must make sure that one is rounding the dyadic on the correct side of zero
00101                 n -= Arg;
00102                 (dyadic::operator< (dyadic())) ? --k : k;
00103                 return *this;
00104         }
00105 
00106 
00108         bool operator < (const dyadic_interval & Arg) const 
00109         {
00110                 // tree leaf order
00111                 return (0 < (Arg.n - n))        
00112                         ? k <= (Arg.k >> (Arg.n - n)) 
00113                         : (k >> (n - Arg.n)) < Arg.k ; 
00114         }
00115 
00117         bool operator != (const dyadic_interval & Arg) const 
00118         {
00119                 return (k != Arg.k || n != Arg.n);
00120         }
00121         
00123         bool operator == (const dyadic_interval & Arg) const
00124         {
00125                 return (k == Arg.k && n == Arg.n);
00126         }
00127 
00129         bool contains (const dyadic_interval & Arg) const 
00130         {
00131                 return 
00132                         // Arg must be shorter to be contained
00133                         (0 <= (Arg.n - n))      
00134 
00135                         ? k == (Arg.k >> (Arg.n - n)) 
00136                         : false ; 
00137         }
00138 
00139 
00142 
00145         static std::deque<dyadic_interval> to_dyadic_intervals(double inf, double sup, int tolerance)
00146         {
00147                 dyadic_interval m_inf = dyadic((int)floor(ldexp(inf,tolerance)),tolerance);
00148                 dyadic_interval m_sup = dyadic((int)floor(ldexp(sup,tolerance)),tolerance);
00149                 std::deque<dyadic_interval> Ans;
00150                 dyadic_interval temp(m_inf);
00151                 while (!(temp.sup() > m_sup))
00152                 {
00153                         temp.expand_interval();
00154                         if ( temp.inf() < m_inf )
00155                         {
00156                                 Ans.push_back(temp.shrink_interval_right());
00157                                 m_inf=temp.inf();
00158                         }
00159                 }
00160                 while ((temp.inf() < sup)&&(temp.sup() > m_sup))
00161                 {
00162                         temp.shrink_interval_left();
00163                         if(!(temp.sup() > m_sup))
00164                         {
00165                                 Ans.push_back(temp);
00166                                 ++ (dyadic &) temp;
00167                         }
00168                 }
00169                 //all done
00170                 return Ans;
00171         }
00172 
00174     static dyadic_interval dyadic_bracket(const double Arg, const int AbsolutePrecision=0)
00175         {
00176                 dyadic_interval temp;
00177         temp.n=AbsolutePrecision;
00178                 temp.k= (int) floor(ldexp(Arg,AbsolutePrecision));
00179                 return temp;
00180         }
00181         
00183         friend inline std::ostream& operator << (std::ostream & os , const dyadic_interval & rhs)
00184         {
00185                 os << "[" << (double) rhs.inf() << ", " << (double) rhs.sup() << ") ";
00186                 return os;
00187         }
00188 };
00189 
00190 #endif // __DYADIC_INTERVAL__
00191 

Generated on Fri Jan 14 2011 17:50:33 for Rough Differential Equation Solver by  doxygen 1.7.1