casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CExp.h
Go to the documentation of this file.
1 //# CExp.cc: Implementation of CExp (tabulated complex exponential) class
2 //# Copyright (C) 1997,1998,1999,2000,2001,2002,2003
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 #if !defined(CEXP_H)
28 #define CEXP_H
29 
30 #include <casa/aips.h>
31 #include <stdlib.h>
32 #include <math.h>
33 #include <casa/Exceptions/Error.h>
34 #include <casa/iostream.h>
35 #include <casa/Arrays/Vector.h>
36 
37 namespace casa{
38 
39  //#define HASH(A) {(int)(std::fmod(abs((A)+PI2),PI2)/Step)}
40 #define PI2 6.28318530717958623
41 
42 #define HASH(A) {(int)(myhash((A)/PI2)/Step)}
43 #define MYHASH(A) {((int)((((A)<0)?(((A)+1-(int)(A))*PI2):(((A)-(int)(A))*PI2))/Step))}
44 
45 inline double myhash(register double arg)
46 {
47  if (arg < 0) return (arg+1-(int)arg)*PI2;
48  return (arg-(int)arg)*PI2;
49 }
50 
51 // Call this inline as
52 // f(arg/PI2)
53 
54 
55 template <class T> class CExp
56 {
57 public:
58  CExp<T>() {Size=0;};
59  CExp<T>(int n) {Size=n;build(Size);};
60  inline void build(int n)
61  {
62  if (n!=Size)
63  {
64  Size = n;
65  ITable.resize(Size);RTable.resize(Size);
66 
67  Step=PI2/Size;
68  for (casacore::Int i=0;i<Size;i++)
69  {
70  ITable(i)=sin(i*Step);
71  RTable(i)=cos(i*Step);
72  }
73  }
74  }
75 
76  inline double f(register T arg)
77  {
78  if (arg < 0) return (arg+1-(int)arg)*PI2;
79  return (arg-(int)arg)*PI2;
80  }
81 
82  inline int hashFunction(T arg) {return (int)(std::fmod(abs(arg+PI2),PI2)/Step);}
83 //{return (int)(myhash(arg/PI2)/Step);}
84 
85  inline int myhash2(register double arg)
86  { /* Steps must be double here, otherwise compiler will keep converting
87  from int to double everytime, because Steps is a variable.
88  */
89  if (arg < 0) return (int)((arg+1-(int)arg)*Size);
90  return (int)((arg-(int)arg)*Size);
91  }
92 
93 #define MYHASH2(A) {A<0 ? (int)(A+1-(int)A)*Size:(int)(A-(int)A)*Size}
94 #define MYHASH3(a) ((a < 0) ? ((a)-(int)(a)+1)*PI2 : ((a)-(int)(a))*PI2)
95 
96  inline std::complex<T> operator()(T& arg)
97  {
98  // int N=hashFunction(arg);
99  int N=(int)(std::fmod(abs(arg+PI2),PI2)/Step); //Best
100  // int N=HASH(arg);
101  // T t=arg/PI2;int N=MYHASH2(t);
102  // T t=arg/PI2;int N=(int)MYHASH3(t)/Step;
103  // int N=myhash2(arg/PI2);
104  return std::complex<T>(RTable[N],ITable[N]);
105  }
106 
107  inline T imag(T arg) {return ITable[hashFunction(arg)];}
108  inline T real(T arg) {return RTable[hashFunction(arg)];}
109  inline void reim(T& arg,T& re, T&im) {casacore::Int N=HASH(arg);re=RTable[N];im=ITable[N];}
110 
111 private:
113  T Step;
114  int Size;
115 };
116 };
117 #endif
A 1-D Specialization of the Array class.
int Int
Definition: aipstype.h:50
LatticeExprNode arg(const LatticeExprNode &expr)
double myhash(register double arg)
Definition: CExp.h:45
T real(T arg)
Definition: CExp.h:108
std::complex< T > operator()(T &arg)
Definition: CExp.h:96
#define HASH(A)
Definition: CExp.h:42
LatticeExprNode cos(const LatticeExprNode &expr)
int Size
Definition: CExp.h:114
int hashFunction(T arg)
Definition: CExp.h:82
T imag(T arg)
Definition: CExp.h:107
double f(register T arg)
Definition: CExp.h:76
T Step
Definition: CExp.h:113
LatticeExprNode abs(const LatticeExprNode &expr)
Numerical 1-argument functions which result in a real number regardless of input expression type...
casacore::Vector< T > RTable
Definition: CExp.h:112
int myhash2(register double arg)
{return (int)(myhash(arg/PI2)/Step);}
Definition: CExp.h:85
uInt N
Axis number.
Definition: ArrayAccessor.h:60
LatticeExprNode fmod(const LatticeExprNode &left, const LatticeExprNode &right)
void reim(T &arg, T &re, T &im)
Definition: CExp.h:109
casacore::Vector< T > ITable
Definition: CExp.h:112
Call this inline as f(arg/PI2)
Definition: CExp.h:55
#define PI2
Definition: CExp.h:40
void build(int n)
Definition: CExp.h:60
LatticeExprNode sin(const LatticeExprNode &expr)
Numerical 1-argument functions.