Back to index

texmacs  1.0.7.15
ball.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : ball.hpp
00004 * DESCRIPTION: balls with center in C and radius in R (= norm_type (C))
00005 * COPYRIGHT  : (C) 2006  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #ifndef BALL_H
00013 #define BALL_H
00014 #include "properties.hpp"
00015 #include "operators.hpp"
00016 #define TMPL template<typename C>
00017 #define BINARY_TMPL template<typename C1,typename C2>
00018 #define R typename properties<C>::norm_type
00019 #define M typename binary_properties<C1,C2>::product_type
00020 
00021 /******************************************************************************
00022 * The ball class
00023 ******************************************************************************/
00024 
00025 TMPL
00026 class ball {
00027 public:
00028   C cen;
00029   R rad;
00030 public:
00031   inline ball (C c=0, R r=0): cen (c), rad (r) {}
00032 };
00033 
00034 TMPL inline C center (const ball<C>& b) { return b.cen; }
00035 TMPL inline R radius (const ball<C>& b) { return b.rad; }
00036 TMPL inline R upper (const ball<C>& b) { return norm (b.cen) + b.rad; }
00037 TMPL inline R lower (const ball<C>& b) { return norm (b.cen) - b.rad; }
00038 
00039 TMPL inline tree as_tree (const ball<C>& b) {
00040   return compound ("ball", as_tree (center (b)), as_tree (radius (b))); }
00041 TMPL inline tm_ostream& operator << (tm_ostream& out, const ball<C>& b) {
00042   return out << as_math_string (as_tree (b)); }
00043 
00044 TMPL
00045 class properties<ball<C> > {
00046 public:
00047   typedef ball<typename properties<C>::scalar_type> scalar_type;
00048   typedef typename properties<C>::norm_type norm_type;
00049   typedef typename properties<C>::index_type index_type;
00050   static inline tree index_name (index_type i) {
00051     return properties<C>::index_name (i); }
00052   static inline scalar_type access (ball<C> b, index_type var) {
00053     return scalar_type (properties<C>::access (center (b), var), radius (b)); }
00054 };
00055 
00056 BINARY_TMPL
00057 class binary_properties<ball<C1>,ball<C2> > {
00058 public:
00059   typedef ball<M > product_type;
00060 };
00061 
00062 /******************************************************************************
00063 * Basic ball arithmetic
00064 ******************************************************************************/
00065 
00066 TMPL ball<C>
00067 operator - (const ball<C>& b) {
00068   return ball<C> (-center (b), radius (b));
00069 }
00070 
00071 TMPL ball<C>
00072 operator + (const ball<C>& b1, const ball<C>& b2) {
00073   return ball<C> (center (b1) + center (b2), radius (b1) + radius (b2));
00074 }
00075 
00076 TMPL ball<C>
00077 operator - (const ball<C>& b1, const ball<C>& b2) {
00078   return ball<C> (center (b1) - center (b2), radius (b1) + radius (b2));
00079 }
00080 
00081 BINARY_TMPL ball<M >
00082 operator * (const ball<C1>& b1, const ball<C2>& b2) {
00083   return ball<M > (center (b1) * center (b2),
00084                  norm (center (b1)) * radius (b2) +
00085                  radius (b1) * norm (center (b2)) +
00086                  radius (b1) * radius (b2));
00087 }
00088 
00089 TMPL ball<C>
00090 invert (const ball<C>& b) {
00091   return ball<C> (invert (center (b)), radius (b) / square (lower (b)));
00092 }
00093 
00094 BINARY_TMPL ball<M >
00095 operator / (const ball<C1>& b1, const ball<C2>& b2) {
00096   return b1 * invert (b2);
00097 }
00098 
00099 /******************************************************************************
00100 * Special functions
00101 ******************************************************************************/
00102 
00103 TMPL ball<C>
00104 sqrt (const ball<C>& b) {
00105   return ball<C> (sqrt (center (b)), radius (b) / (2 * square (loer (b))));
00106 }
00107 
00108 TMPL ball<C>
00109 exp (const ball<C>& b) {
00110   return ball<C> (exp (center (b)), exp (upper (b)) * radius (b));
00111 }
00112 
00113 TMPL ball<C>
00114 log (const ball<C>& b) {
00115   return ball<C> (log (center (b)), radius (b) / lower (b));
00116 }
00117 
00118 TMPL inline ball<C>
00119 pow (const ball<C>& b1, const ball<C>& b2) {
00120   return exp (b2 * log (b1));
00121 }
00122 
00123 TMPL ball<C>
00124 cos (const ball<C>& z) {
00125   if (radius (z) >= R (3.14159))
00126     return ball<C> (C(0), R(1));
00127   else {
00128     R u1= sin (lower (z));
00129     R u2= sin (upper (z));
00130     if (u1 * u2 <= 0)
00131       return ball<C> (cos (center (z)), radius (z));
00132     return ball<C> (cos (center (z)),
00133                   max (norm (u1), norm (u2)) * radius (z));
00134   }
00135 }
00136 
00137 TMPL ball<C>
00138 sin (const ball<C>& z) {
00139   if (radius (z) >= R (3.14159))
00140     return ball<C> (sin (center (z)), radius (z));
00141   else {
00142     R u1= cos (lower (z));
00143     R u2= cos (upper (z));
00144     if (u1 * u2 <= 0)
00145       return ball<C> (sin (center (z)), radius (z));
00146     return ball<C> (sin (center (z)),
00147                   max (norm (u1), norm (u2)) * radius (z));
00148   }
00149 }
00150 
00151 TMPL inline ball<C>
00152 tan (const ball<C>& b) {
00153   return sin (b) / cos (b);
00154 }
00155 
00156 /******************************************************************************
00157 * Other routines
00158 ******************************************************************************/
00159 
00160 TMPL ball<R>
00161 norm (const ball<C>& b) {
00162   return ball<R> (norm (center (b)), radius (b));
00163 }
00164 
00165 #undef TMPL
00166 #undef BINARY_TMPL
00167 #undef R
00168 #undef M
00169 #endif // defined BALL_H