| [221] | 1 | #ifndef BZ_MATHF2_H
 | 
|---|
 | 2 | #define BZ_MATHF2_H
 | 
|---|
 | 3 | 
 | 
|---|
 | 4 | #ifndef BZ_APPLICS_H
 | 
|---|
 | 5 |  #error <blitz/mathf2.h> should be included via <blitz/applics.h>
 | 
|---|
 | 6 | #endif
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | #ifndef BZ_PRETTYPRINT_H
 | 
|---|
 | 9 |  #include <blitz/prettyprint.h>
 | 
|---|
 | 10 | #endif
 | 
|---|
 | 11 | 
 | 
|---|
 | 12 | BZ_NAMESPACE(blitz)
 | 
|---|
 | 13 | 
 | 
|---|
 | 14 | // cexp(z)     Complex exponential
 | 
|---|
 | 15 | template<class P_numtype1>
 | 
|---|
 | 16 | class _bz_cexp : public OneOperandApplicativeTemplatesBase {
 | 
|---|
 | 17 | public:
 | 
|---|
 | 18 |     typedef P_numtype1 T_numtype1;
 | 
|---|
 | 19 |     typedef P_numtype1 T_numtype;
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 |     static inline T_numtype apply(T_numtype1 x)
 | 
|---|
 | 22 |     { return _bz_exp<T_numtype1>::apply(x); }
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 |     template<class T1>
 | 
|---|
 | 25 |     static void prettyPrint(string& str, prettyPrintFormat& format,
 | 
|---|
 | 26 |         const T1& a)
 | 
|---|
 | 27 |     {
 | 
|---|
 | 28 |         str += "cexp(";
 | 
|---|
 | 29 |         a.prettyPrint(str,format);
 | 
|---|
 | 30 |         str += ")";
 | 
|---|
 | 31 |     }
 | 
|---|
 | 32 | };
 | 
|---|
 | 33 | 
 | 
|---|
 | 34 | // csqrt(z)    Complex square root
 | 
|---|
 | 35 | template<class P_numtype1>
 | 
|---|
 | 36 | class _bz_csqrt : public OneOperandApplicativeTemplatesBase {
 | 
|---|
 | 37 | public:
 | 
|---|
 | 38 |     typedef P_numtype1 T_numtype1;
 | 
|---|
 | 39 |     typedef P_numtype1 T_numtype;
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 |     static inline T_numtype apply(T_numtype1 x)
 | 
|---|
 | 42 |     { return _bz_sqrt<T_numtype1>::apply(x); }
 | 
|---|
 | 43 | 
 | 
|---|
 | 44 |     template<class T1>
 | 
|---|
 | 45 |     static void prettyPrint(string& str, prettyPrintFormat& format,
 | 
|---|
 | 46 |         const T1& a)
 | 
|---|
 | 47 |     {
 | 
|---|
 | 48 |         str += "csqrt(";
 | 
|---|
 | 49 |         a.prettyPrint(str,format);
 | 
|---|
 | 50 |         str += ")";
 | 
|---|
 | 51 |     }
 | 
|---|
 | 52 | };
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | // pow2        Square
 | 
|---|
 | 55 | template<class P_numtype1>
 | 
|---|
 | 56 | class _bz_pow2 : public OneOperandApplicativeTemplatesBase {
 | 
|---|
 | 57 | public:
 | 
|---|
 | 58 |     typedef P_numtype1 T_numtype1;
 | 
|---|
 | 59 |     typedef P_numtype1 T_numtype;
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 |     static inline T_numtype apply(T_numtype1 x)
 | 
|---|
 | 62 |     { 
 | 
|---|
 | 63 |         return BZ_NO_PROPAGATE(x) * BZ_NO_PROPAGATE(x);
 | 
|---|
 | 64 |     }
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 |     template<class T1>
 | 
|---|
 | 67 |     static void prettyPrint(string& str, prettyPrintFormat& format,
 | 
|---|
 | 68 |         const T1& a)
 | 
|---|
 | 69 |     {
 | 
|---|
 | 70 |         str += "pow2(";
 | 
|---|
 | 71 |         a.prettyPrint(str,format);
 | 
|---|
 | 72 |         str += ")";
 | 
|---|
 | 73 |     }
 | 
|---|
 | 74 | };
 | 
|---|
 | 75 | 
 | 
|---|
 | 76 | // pow3        Cube
 | 
|---|
 | 77 | template<class P_numtype1>
 | 
|---|
 | 78 | class _bz_pow3 : public OneOperandApplicativeTemplatesBase {
 | 
|---|
 | 79 | public:
 | 
|---|
 | 80 |     typedef P_numtype1 T_numtype1;
 | 
|---|
 | 81 |     typedef P_numtype1 T_numtype;
 | 
|---|
 | 82 | 
 | 
|---|
 | 83 |     static inline T_numtype apply(T_numtype1 x)
 | 
|---|
 | 84 |     { 
 | 
|---|
 | 85 |         return BZ_NO_PROPAGATE(x) * BZ_NO_PROPAGATE(x) *
 | 
|---|
 | 86 |           BZ_NO_PROPAGATE(x);
 | 
|---|
 | 87 |     }
 | 
|---|
 | 88 | 
 | 
|---|
 | 89 |     template<class T1>
 | 
|---|
 | 90 |     static void prettyPrint(string& str, prettyPrintFormat& format,
 | 
|---|
 | 91 |         const T1& a)
 | 
|---|
 | 92 |     {
 | 
|---|
 | 93 |         str += "pow3(";
 | 
|---|
 | 94 |         a.prettyPrint(str,format);
 | 
|---|
 | 95 |         str += ")";
 | 
|---|
 | 96 |     }
 | 
|---|
 | 97 | };
 | 
|---|
 | 98 | 
 | 
|---|
 | 99 | // pow4        Fourth power
 | 
|---|
 | 100 | template<class P_numtype1>
 | 
|---|
 | 101 | class _bz_pow4 : public OneOperandApplicativeTemplatesBase {
 | 
|---|
 | 102 | public:
 | 
|---|
 | 103 |     typedef P_numtype1 T_numtype1;
 | 
|---|
 | 104 |     typedef P_numtype1 T_numtype;
 | 
|---|
 | 105 | 
 | 
|---|
 | 106 |     static inline T_numtype apply(T_numtype1 x)
 | 
|---|
 | 107 |     { 
 | 
|---|
 | 108 |         T_numtype t1 = BZ_NO_PROPAGATE(x) * BZ_NO_PROPAGATE(x);
 | 
|---|
 | 109 |         return BZ_NO_PROPAGATE(t1) * BZ_NO_PROPAGATE(t1);
 | 
|---|
 | 110 |     }
 | 
|---|
 | 111 | 
 | 
|---|
 | 112 |     template<class T1>
 | 
|---|
 | 113 |     static void prettyPrint(string& str, prettyPrintFormat& format,
 | 
|---|
 | 114 |         const T1& a)
 | 
|---|
 | 115 |     {
 | 
|---|
 | 116 |         str += "pow4(";
 | 
|---|
 | 117 |         a.prettyPrint(str,format);
 | 
|---|
 | 118 |         str += ")";
 | 
|---|
 | 119 |     }
 | 
|---|
 | 120 | };
 | 
|---|
 | 121 | 
 | 
|---|
 | 122 | // pow5        Fifth power
 | 
|---|
 | 123 | template<class P_numtype1>
 | 
|---|
 | 124 | class _bz_pow5 : public OneOperandApplicativeTemplatesBase {
 | 
|---|
 | 125 | public:
 | 
|---|
 | 126 |     typedef P_numtype1 T_numtype1;
 | 
|---|
 | 127 |     typedef P_numtype1 T_numtype;
 | 
|---|
 | 128 | 
 | 
|---|
 | 129 |     static inline T_numtype apply(T_numtype1 x)
 | 
|---|
 | 130 |     {
 | 
|---|
 | 131 |         T_numtype t1 = BZ_NO_PROPAGATE(x) * BZ_NO_PROPAGATE(x);
 | 
|---|
 | 132 |         return BZ_NO_PROPAGATE(t1) * BZ_NO_PROPAGATE(t1)
 | 
|---|
 | 133 |             * BZ_NO_PROPAGATE(t1);
 | 
|---|
 | 134 |     }
 | 
|---|
 | 135 | 
 | 
|---|
 | 136 |     template<class T1>
 | 
|---|
 | 137 |     static void prettyPrint(string& str, prettyPrintFormat& format,
 | 
|---|
 | 138 |         const T1& a)
 | 
|---|
 | 139 |     {
 | 
|---|
 | 140 |         str += "pow5(";
 | 
|---|
 | 141 |         a.prettyPrint(str,format);
 | 
|---|
 | 142 |         str += ")";
 | 
|---|
 | 143 |     }
 | 
|---|
 | 144 | };
 | 
|---|
 | 145 | 
 | 
|---|
 | 146 | // pow6        Sixth power
 | 
|---|
 | 147 | template<class P_numtype1>
 | 
|---|
 | 148 | class _bz_pow6 : public OneOperandApplicativeTemplatesBase {
 | 
|---|
 | 149 | public:
 | 
|---|
 | 150 |     typedef P_numtype1 T_numtype1;
 | 
|---|
 | 151 |     typedef P_numtype1 T_numtype;
 | 
|---|
 | 152 | 
 | 
|---|
 | 153 |     static inline T_numtype apply(T_numtype1 x)
 | 
|---|
 | 154 |     {
 | 
|---|
 | 155 |         T_numtype t1 = BZ_NO_PROPAGATE(x) * BZ_NO_PROPAGATE(x) 
 | 
|---|
 | 156 |             * BZ_NO_PROPAGATE(x);
 | 
|---|
 | 157 |         return BZ_NO_PROPAGATE(t1) * BZ_NO_PROPAGATE(t1);
 | 
|---|
 | 158 |     }
 | 
|---|
 | 159 | 
 | 
|---|
 | 160 |     template<class T1>
 | 
|---|
 | 161 |     static void prettyPrint(string& str, prettyPrintFormat& format,
 | 
|---|
 | 162 |         const T1& a)
 | 
|---|
 | 163 |     {
 | 
|---|
 | 164 |         str += "pow6(";
 | 
|---|
 | 165 |         a.prettyPrint(str,format);
 | 
|---|
 | 166 |         str += ")";
 | 
|---|
 | 167 |     }
 | 
|---|
 | 168 | };
 | 
|---|
 | 169 | 
 | 
|---|
 | 170 | 
 | 
|---|
 | 171 | // pow7        Seventh power
 | 
|---|
 | 172 | template<class P_numtype1>
 | 
|---|
 | 173 | class _bz_pow7 : public OneOperandApplicativeTemplatesBase {
 | 
|---|
 | 174 | public:
 | 
|---|
 | 175 |     typedef P_numtype1 T_numtype1;
 | 
|---|
 | 176 |     typedef P_numtype1 T_numtype;
 | 
|---|
 | 177 | 
 | 
|---|
 | 178 |     static inline T_numtype apply(T_numtype1 x)
 | 
|---|
 | 179 |     {
 | 
|---|
 | 180 |         T_numtype t1 = BZ_NO_PROPAGATE(x) * BZ_NO_PROPAGATE(x) 
 | 
|---|
 | 181 |             * BZ_NO_PROPAGATE(x);
 | 
|---|
 | 182 |         return BZ_NO_PROPAGATE(t1) * BZ_NO_PROPAGATE(t1)
 | 
|---|
 | 183 |             * BZ_NO_PROPAGATE(x);
 | 
|---|
 | 184 |     }
 | 
|---|
 | 185 | 
 | 
|---|
 | 186 |     template<class T1>
 | 
|---|
 | 187 |     static void prettyPrint(string& str, prettyPrintFormat& format,
 | 
|---|
 | 188 |         const T1& a)
 | 
|---|
 | 189 |     {
 | 
|---|
 | 190 |         str += "pow7(";
 | 
|---|
 | 191 |         a.prettyPrint(str,format);
 | 
|---|
 | 192 |         str += ")";
 | 
|---|
 | 193 |     }
 | 
|---|
 | 194 | };
 | 
|---|
 | 195 | 
 | 
|---|
 | 196 | // pow8        Eighth power
 | 
|---|
 | 197 | template<class P_numtype1>
 | 
|---|
 | 198 | class _bz_pow8 : public OneOperandApplicativeTemplatesBase {
 | 
|---|
 | 199 | public:
 | 
|---|
 | 200 |     typedef P_numtype1 T_numtype1;
 | 
|---|
 | 201 |     typedef P_numtype1 T_numtype;
 | 
|---|
 | 202 | 
 | 
|---|
 | 203 |     static inline T_numtype apply(T_numtype1 x)
 | 
|---|
 | 204 |     {
 | 
|---|
 | 205 |         T_numtype t1 = BZ_NO_PROPAGATE(x) * BZ_NO_PROPAGATE(x);
 | 
|---|
 | 206 |         T_numtype t2 = BZ_NO_PROPAGATE(t1) * BZ_NO_PROPAGATE(t1);
 | 
|---|
 | 207 |         return BZ_NO_PROPAGATE(t2) * BZ_NO_PROPAGATE(t2);
 | 
|---|
 | 208 |     }
 | 
|---|
 | 209 | 
 | 
|---|
 | 210 |     template<class T1>
 | 
|---|
 | 211 |     static void prettyPrint(string& str, prettyPrintFormat& format,
 | 
|---|
 | 212 |         const T1& a)
 | 
|---|
 | 213 |     {
 | 
|---|
 | 214 |         str += "pow8(";
 | 
|---|
 | 215 |         a.prettyPrint(str,format);
 | 
|---|
 | 216 |         str += ")";
 | 
|---|
 | 217 |     }
 | 
|---|
 | 218 | };
 | 
|---|
 | 219 | 
 | 
|---|
 | 220 | /*
 | 
|---|
 | 221 |  * These scalar versions of pow2, pow3, ..., pow8 are provided for
 | 
|---|
 | 222 |  * convenience.
 | 
|---|
 | 223 |  *
 | 
|---|
 | 224 |  * NEEDS_WORK -- include BZ_NO_PROPAGATE for these scalar versions.
 | 
|---|
 | 225 |  */
 | 
|---|
 | 226 | 
 | 
|---|
 | 227 | // NEEDS_WORK -- make these templates.  Rely on specialization to
 | 
|---|
 | 228 | // handle expression template versions.
 | 
|---|
 | 229 | 
 | 
|---|
 | 230 | #define BZ_DECLARE_POW(T)  \
 | 
|---|
 | 231 |  inline T pow2(T x) { return x*x; }                  \
 | 
|---|
 | 232 |  inline T pow3(T x) { return x*x*x; }                \
 | 
|---|
 | 233 |  inline T pow4(T x) { T t1 = x*x; return t1*t1; }    \
 | 
|---|
 | 234 |  inline T pow5(T x) { T t1 = x*x; return t1*t1*x; }  \
 | 
|---|
 | 235 |  inline T pow6(T x) { T t1 = x*x*x; return t1*t1; }  \
 | 
|---|
 | 236 |  inline T pow7(T x) { T t1 = x*x; return t1*t1*t1*x; } \
 | 
|---|
 | 237 |  inline T pow8(T x) { T t1 = x*x, t2=t1*t1; return t2*t2; }  
 | 
|---|
 | 238 | 
 | 
|---|
 | 239 | BZ_DECLARE_POW(int)
 | 
|---|
 | 240 | BZ_DECLARE_POW(float)
 | 
|---|
 | 241 | BZ_DECLARE_POW(double)
 | 
|---|
 | 242 | BZ_DECLARE_POW(long double)
 | 
|---|
 | 243 | 
 | 
|---|
 | 244 | #ifdef BZ_HAVE_COMPLEX
 | 
|---|
 | 245 | BZ_DECLARE_POW(complex<float>)
 | 
|---|
 | 246 | BZ_DECLARE_POW(complex<double>)
 | 
|---|
 | 247 | BZ_DECLARE_POW(complex<long double>)
 | 
|---|
 | 248 | #endif
 | 
|---|
 | 249 | 
 | 
|---|
 | 250 | BZ_NAMESPACE_END
 | 
|---|
 | 251 | 
 | 
|---|
 | 252 | #endif
 | 
|---|