00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef UTILS_H
00018 #define UTILS_H
00019
00020 #include <odindata/data.h>
00021
00028
00029
00030
00034 template <int N_rank>
00035 Array<float,N_rank> truncate_float(Array<float,N_rank> a){
00036 Array<float,N_rank> result(a.shape());
00037 result=where(a <= 0, ceil(a),floor(a));
00038 return result;
00039 }
00040
00044 template <int N_rank>
00045 void wrapPhase(Array<float,N_rank> ph){
00046 ph=ph-(truncate_float(Array<float,N_rank>(ph/(2.*PII))))*2.*PII;
00047 ph=ph-(truncate_float(Array<float,N_rank>(ph/PII)))*2.*PII;
00048 }
00049
00053 template <int N_rank>
00054 void unwrapPhase1d(Data<float,N_rank> ph){
00055
00056 unsigned long i,j,dim;
00057 float add1;
00058 dim=ph.extent(ph.dimensions()-1);
00059 Array<float,1> phu(dim);
00060 wrapPhase(ph);
00061
00062 for(i=0;i<(unsigned)ph.size()/dim;i++){
00063 phu(0)=ph(ph.create_index(i*dim));
00064 add1=0;
00065 for(j=1;j<dim;j++){
00066 if ((ph(ph.create_index(i*dim+j))-ph(ph.create_index(i*dim+j-1))) > PII) add1=add1-2.*PII;
00067 if ((ph(ph.create_index(i*dim+j))-ph(ph.create_index(i*dim+j-1))) < -PII) add1=add1+2.*PII;
00068 phu(j)=ph(ph.create_index(i*dim+j))+add1;
00069 }
00070 add1=phu(dim/2);
00071 add1=(int)(add1/2./PII)*2.*PII+(int)((add1-(int)(add1/2./PII)*2.*PII)/PII)*2.*PII;
00072 for(j=0;j<dim;j++)
00073 ph(ph.create_index(i*dim+j))=phu(j)-add1;
00074
00075
00076 }
00077 }
00078
00079
00081
00082
00086 template<typename T>
00087 Array<T,1> matrix_product(const Array<T,2>& matrix, const Array<T,1>& vector) {
00088 Log<OdinData> odinlog("","matrix_product");
00089 int nrows=matrix.extent()(0);
00090 int ncols=matrix.extent()(1);
00091
00092 Array<T,1> result(nrows);
00093 result=0;
00094
00095 int vector_extent=vector.extent(0);
00096 if(vector.extent(0)!=ncols) {
00097 ODINLOG(odinlog,errorLog) << "size mismatch (vector_extent=" << vector_extent << ") != (ncols=" << ncols << ")" << STD_endl;
00098 return result;
00099 }
00100
00101
00102 for(int icol=0; icol<ncols; icol++) {
00103 for(int irow=0; irow<nrows; irow++) {
00104 result(irow)+=matrix(irow,icol)*vector(icol);
00105 }
00106 }
00107
00108 return result;
00109 }
00110
00112
00116 template<typename T>
00117 Array<T,2> matrix_product(const Array<T,2>& matrix1, const Array<T,2>& matrix2) {
00118 Log<OdinData> odinlog("","matrix_product");
00119 int nrows=matrix1.extent()(0);
00120 int ncols=matrix2.extent()(1);
00121 ODINLOG(odinlog,normalDebug) << "nrows/ncols=" << nrows << "/" << ncols << STD_endl;
00122
00123 Array<T,2> result(nrows,ncols);
00124 result=0;
00125
00126 if(matrix1.extent(1)!=matrix2.extent(0)) {
00127 ODINLOG(odinlog,errorLog) << "size mismatch (matrix1=" << matrix1.shape() << ", matrix2=" << matrix2.shape() << ")" << STD_endl;
00128 return result;
00129 }
00130
00131 int nprod=matrix1.extent(1);
00132 ODINLOG(odinlog,normalDebug) << "nprod=" << nprod << STD_endl;
00133 for(int irow=0; irow<nrows; irow++) {
00134 for(int icol=0; icol<ncols; icol++) {
00135
00136 T scalprod(0);
00137 for(int iprod=0; iprod<nprod; iprod++) {
00138 scalprod+= matrix1(irow,iprod)*matrix2(iprod,icol);
00139 }
00140 result(irow,icol)=scalprod;
00141 }
00142 }
00143
00144 return result;
00145 }
00146
00148
00149
00153 template<typename T>
00154 Array<T,1> vector_product(const Array<T,1>& u, const Array<T,1>& v) {
00155 Log<OdinData> odinlog("","vector_product");
00156 Array<T,1> result(3);
00157 if(u.extent(0)!=3 || v.extent(0)!=3) {
00158 ODINLOG(odinlog,errorLog) << "input size != 3" << STD_endl;
00159 return result;
00160 }
00161 result(0)=u(1)*v(2)-u(2)*v(1);
00162 result(1)=u(2)*v(0)-u(0)*v(2);
00163 result(2)=u(0)*v(1)-u(1)*v(0);
00164 return result;
00165 }
00166
00167
00169
00173 template<typename T, int N_rank>
00174 bool operator == (const TinyVector<T,N_rank>& t1, const TinyVector<T,N_rank>& t2) {
00175 return sum(abs(t1-t2))==0;
00176 }
00177
00179
00183 template<typename T, int N_rank>
00184 bool operator != (const TinyVector<T,N_rank>& t1, const TinyVector<T,N_rank>& t2) {
00185 return !(t1==t2);
00186 }
00187
00189
00193 template<typename T, int N_rank>
00194 bool same_shape(const Array<T,N_rank>& a1, const Array<T,N_rank>& a2) {
00195 return a1.shape()==a2.shape();
00196 }
00197
00198
00199
00201
00205 template <typename T>
00206 bool check_range(T& val, T min, T max) {
00207 bool in_range=true;
00208 if(val<min) {val=min; in_range=false;}
00209 if(val>max) {val=max; in_range=false;}
00210 return in_range;
00211 }
00212
00213
00214
00215
00220 #endif