casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
CExp.h
Go to the documentation of this file.
00001 //# CExp.cc: Implementation of CExp (tabulated complex exponential) class
00002 //# Copyright (C) 1997,1998,1999,2000,2001,2002,2003
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id$
00027 #if !defined(CEXP_H)
00028 #define CEXP_H
00029 
00030 #include <casa/aips.h>
00031 #include <stdlib.h>
00032 #include <math.h>
00033 #include <casa/Exceptions/Error.h>
00034 #include <casa/iostream.h>
00035 #include <casa/Arrays/Vector.h>
00036 
00037 namespace casa{
00038 
00039   //#define HASH(A) {(int)(std::fmod(abs((A)+PI2),PI2)/Step)}
00040 #define PI2 6.28318530717958623
00041 
00042 #define HASH(A) {(int)(myhash((A)/PI2)/Step)}
00043 #define MYHASH(A)   {((int)((((A)<0)?(((A)+1-(int)(A))*PI2):(((A)-(int)(A))*PI2))/Step))}
00044 
00045 inline double myhash(register double arg)
00046 {
00047   if (arg < 0) return (arg+1-(int)arg)*PI2;
00048   return (arg-(int)arg)*PI2;
00049 }
00050 
00051 // Call this inline as
00052 //  f(arg/PI2)
00053 
00054 
00055 template <class T> class CExp
00056 {
00057 public:
00058   CExp<T>() {Size=0;};
00059   CExp<T>(int n) {Size=n;build(Size);};
00060   inline void build(int n)
00061   {
00062     if (n!=Size)
00063       {
00064         Size = n;
00065         ITable.resize(Size);RTable.resize(Size);
00066 
00067         Step=PI2/Size;
00068         for (Int i=0;i<Size;i++) 
00069           {
00070             ITable(i)=sin(i*Step);
00071             RTable(i)=cos(i*Step);
00072           }
00073       }
00074   }
00075 
00076   inline double f(register T arg)
00077   {
00078     if (arg < 0) return (arg+1-(int)arg)*PI2;
00079     return (arg-(int)arg)*PI2;
00080   }
00081 
00082   inline int hashFunction(T arg)   {return (int)(std::fmod(abs(arg+PI2),PI2)/Step);}
00083 //{return (int)(myhash(arg/PI2)/Step);}
00084 
00085   inline int myhash2(register double arg)
00086   { /* Steps must be double here, otherwise compiler will keep converting
00087        from int to double everytime, because Steps is a variable.
00088     */
00089     if (arg < 0) return (int)((arg+1-(int)arg)*Size);
00090     return (int)((arg-(int)arg)*Size);
00091   }
00092 
00093 #define MYHASH2(A) {A<0     ? (int)(A+1-(int)A)*Size:(int)(A-(int)A)*Size}
00094 #define MYHASH3(a) ((a < 0) ? ((a)-(int)(a)+1)*PI2 : ((a)-(int)(a))*PI2)
00095 
00096   inline std::complex<T> operator()(T& arg)
00097   {
00098     //    int N=hashFunction(arg);
00099     int N=(int)(std::fmod(abs(arg+PI2),PI2)/Step); //Best
00100     //    int N=HASH(arg);
00101     //    T t=arg/PI2;int N=MYHASH2(t);
00102     //    T t=arg/PI2;int N=(int)MYHASH3(t)/Step;
00103     //    int N=myhash2(arg/PI2);
00104     return std::complex<T>(RTable[N],ITable[N]);
00105   }
00106 
00107   inline T imag(T arg) {return ITable[hashFunction(arg)];}
00108   inline T real(T arg) {return RTable[hashFunction(arg)];}
00109   inline void reim(T& arg,T& re, T&im) {Int N=HASH(arg);re=RTable[N];im=ITable[N];}
00110 
00111 private:
00112   Vector<T> RTable, ITable;
00113   T Step;
00114   int Size;
00115 };
00116 };  
00117 #endif