📄 convolve.h
字号:
{ dim1(B(i,all,all), C, tmp); Array<TA,2> tmp2D = tmp3D(i,all,all); dim2(tmp, C, tmp2D); } } for (int j=B.lbound(1); j <= B.ubound(1); ++j) for (int k=B.lbound(2); k <= B.ubound(2); ++k) { (*this)(tmp3D(all,j,k),C,A(all,j,k)); } return A; }// }}} /** * @brief Convolve 3D array with 1D arrays. * * C : smooth kernel along dimension 1 (smallest) * D : smooth kernel along dimension 2 * E : smooth kernel along dimension 3 (highest) */ template<class TB, class TC> inline blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, 3>& operator()( const blitz::Array<TB,3>& B, const blitz::Array<TC,1>& C, const blitz::Array<TC,1>& D, const blitz::Array<TC,1>& E, blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, 3>& A) const {// {{{ using namespace blitz; typedef typename blitz::promote_trait<TB,TC>::T_promote TA; Range all = Range::all(); Array<TA,3> tmp3D (B.lbound(), B.extent()); { Array<TA,2> tmp ( TinyVector<int,2> (B.lbound(1), B.lbound(2)), TinyVector<int,2> (B.extent(1), B.extent(2)) ); for (int i=B.lbound(0); i <= B.ubound(0); ++i) { dim1(B(i,all,all), C, tmp); Array<TA,2> tmp2D = tmp3D(i,all,all); dim2(tmp, D, tmp2D); } } for (int j=B.lbound(1); j <= B.ubound(1); ++j) for (int k=B.lbound(2); k <= B.ubound(2); ++k) { blitz::Array<TA,1> tA = A(all,j,k); (*this)(tmp3D(all,j,k),E,tA); } return A; }// }}} /** * @brief Convolve 3-D array with 1D. * Creates new array for result. */ template<class TB, class TC, int N> inline blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, N> operator()( const blitz::Array<TB,N>& B, const blitz::Array<TC,1>& C) const {// {{{ typedef typename blitz::promote_trait<TB,TC>::T_promote TA; blitz::Array<TA,N> A (B.lbound(),B.extent()) ; return (*this)(B,C,A); }// }}}};template<class Policy, class TB, class TC, int N> inlineblitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, N> convolveExact( const blitz::Array<TB,N>& B, const blitz::Array<TC,1>& C, blitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, N>& A){ ConvolveExact<Policy> op; return op(B,C,A);}template<class Policy, class TB, class TC, int N> inlineblitz::Array<typename blitz::promote_trait<TB,TC>::T_promote, N> convolveExact( const blitz::Array<TB,N>& B, const blitz::Array<TC,1>& C){ ConvolveExact<Policy> op; return op(B,C);}BZ_NAMESPACE_END#endif //#ifdef _LEAR_CONVOLVE_H_//{{{ extra code/*template<class T>blitz::Array<T,1> convolve(const blitz::Array<T,1>& B, const blitz::Array<T,1>& C, blitz::Array<T,1> A);*//** Defines convolution operation. Return array has same * shape as B *//*template<class T>blitz::Array<T,2> convolve(const blitz::Array<T,2>& B, const blitz::Array<T,1>& C, int dimension) ;*//** Defines convolution operation. Return array has same * shape as B *//*template<class T>blitz::Array<T,1> convolveExact(const blitz::Array<T,1>& B, const blitz::Array<T,1>& C, blitz::Array<T,1> A) ; *//** Defines convolution operation. Return array has same * shape as B *//*template<class T>blitz::Array<T,2> convolveExact(const blitz::Array<T,2>& B, const blitz::Array<T,1>& C, int dimension) ;*//** Defines convolution operation. Array B is mirror accross boundaries. * Array C is assumed to be centered around 0. * * NOTE: Extent of B along all dimensions must be greater than half * the extent of C. *//*template<class T>blitz::Array<T,1> convolveMirror(const blitz::Array<T,1>& B, const blitz::Array<T,1>& C, blitz::Array<T,1> A) ; *//** Defines convolution operation. Array B is mirror accross boundaries. * Array C is assumed to be centered around 0. * * NOTE: Extent of B must be greater than half * the extent of C. *//*template<class T>blitz::Array<T,2> convolveMirror(const blitz::Array<T,2>& B, const blitz::Array<T,1>& C, int dimension) ;*/// Definitions starting now.../*template<class T>blitz::Array<T,1> convolve(const blitz::Array<T,1>& B, const blitz::Array<T,1>& C, blitz::Array<T,1> A = blitz::Array<T,1>( Range( B.lbound(0)+C.lbound(0), B.ubound(0)+C.ubound(0)) ) ){ int Bl = B.lbound(0), Bh = B.ubound(0); int Cl = C.lbound(0), Ch = C.ubound(0); for (int i=A.lbound(0); i <= A.ubound(0); ++i) { int jl = i - Ch; if (jl < Bl) jl = Bl; int jh = i - Cl; if (jh > Bh) jh = Bh; T result = 0; for (int j=jl; j <= jh; ++j) result += B(j) * C(i-j); A(i) = result; } return A;}template<class T>blitz::Array<T,2> convolve(const blitz::Array<T,2>& B, const blitz::Array<T,1>& C, int dimension){ using namespace blitz; TinyVector<int,2> Bl = B.lbound(), Be = B.extent(); int Cl = C.lbound(0), Cu = C.ubound(0); TinyVector<int,2> lbound = Bl; lbound[dimension] += Cl; TinyVector<int,2> extent = Be; extent[dimension] += Cu - Cl; Array<T,2> A(lbound,extent); Range all = Range::all(); if (dimension == 0) { int l = B.lbound(1), u = B.ubound(1); for (int i=l; i <= u; ++i) { convolve( B(all,i), C, A(all,i)); } } else //if (dimension == 1) { int l = B.lbound(0), u = B.ubound(0); for (int i=l; i <= u; ++i) { convolve( B(i,all), C, A(i, all)); } } return A;}template<class T>blitz::Array<T,1> convolveExact(const blitz::Array<T,1>& B, const blitz::Array<T,1>& C, blitz::Array<T,1> A = blitz::Array<T,1>( Range(B.lbound(0),B.ubound(0)) ) ){ using namespace blitz; int Bl = B.lbound(0), Bh = B.ubound(0); int Cl = C.lbound(0), Ch = C.ubound(0); for (int i=Bl; i <= Bh; ++i) { int jl = i - Ch; if (jl < Bl) jl = Bl; int jh = i - Cl; if (jh > Bh) jh = Bh; T result = 0; for (int j=jl; j <= jh; ++j) result += B(j) * C(i-j); A(i) = result; } return A;}template<class T>blitz::Array<T,2> convolveExact(const blitz::Array<T,2>& B, const blitz::Array<T,1>& C, int dimension){ using namespace blitz; TinyVector<int,2> Bl = B.lbound(), Be = B.extent(); int Cl = C.lbound(0), Cu = C.ubound(0); Array<T,2> A(Bl,Be); if (dimension == 0) { Range all = Range(B.lbound(0),B.ubound(0)); int l = B.lbound(1), u = B.ubound(1); for (int i=l; i <= u; ++i) { convolveExact( B(all,i), C, A(all,i)); } } else //if (dimension == 1) { Range all = Range(B.lbound(1),B.ubound(1)); int l = B.lbound(0), u = B.ubound(0); for (int i=l; i <= u; ++i) { convolveExact( B(i,all), C, A(i, all)); } } return A;}template<class T>blitz::Array<T,1> convolveMirror(const blitz::Array<T,1>& B, const blitz::Array<T,1>& C, blitz::Array<T,1> A = blitz::Array<T,1>( Range( B.lbound(0)+C.lbound(0), B.ubound(0)+C.ubound(0) ) ) ){ using namespace blitz; int Bl = B.lbound(0), Bh = B.ubound(0); int Cl = C.lbound(0), Ch = C.ubound(0); for (int i=A.lbound(0); i <= A.ubound(0); ++i) { T result = 0; int tj=0; for (int j=i-Ch; j <= i-Cl; ++j) { if (j<Bl) tj = 2*Bl - j; else if (j>Bh) tj = 2*Bh - j; else tj = j; result += B(tj) * C(i-j); } A(i) = result; } return A;}template<class T>blitz::Array<T,2> convolveMirror(const blitz::Array<T,2>& B, const blitz::Array<T,1>& C, int dimension){ using namespace blitz; TinyVector<int,2> Bl = B.lbound(), Be = B.extent(); int Cl = C.lbound(0), Cu = C.ubound(0); TinyVector<int,2> lbound = Bl; lbound[dimension] += Cl; TinyVector<int,2> extent = Be; extent[dimension] += Cu - Cl; Array<T,2> A(lbound,extent); Range all = Range::all(); if (dimension == 0) { int l = B.lbound(1), u = B.ubound(1); for (int i=l; i <= u; ++i) { convolveMirror( B(all,i), C, A(all,i)); } } else if (dimension == 1) { int l = B.lbound(0), u = B.ubound(0); for (int i=l; i <= u; ++i) { convolveMirror( B(i,all), C, A(i, all)); } } return A;}*//*template<class T>blitz::Array<T,2> convolveMirrorExact(const blitz::Array<T,2>& B, const blitz::Array<T,1>& C){ using namespace blitz; TinyVector<int,2> Bl = B.lbound(), Be = B.extent(); int Cl = C.lbound(0), Ch = C.ubound(0); Array<T,2> D(Bl,Be); for (int k=B.lbound(1); k <= B.ubound(1); ++k) { int l = B.lbound(0), h = B.ubound(0); for (int i=l; i <= h; ++i) { T result = 0; int tj=0; for (int j=i-Ch; j <= i-Cl; ++j) { if (j<l) tj = 2*l - j; else if (j>h) tj = 2*h - j; else tj =j; result += B(tj,k) * C(i-j); } D(i,k) = result; } } Array<T,2> A(Bl,Be); for (int k=D.lbound(0); k <= D.ubound(0); ++k) { int l = D.lbound(1), h = D.ubound(1); for (int i=l; i <= h; ++i) { int tj=0; T result = 0; for (int j=i-Ch; j <= i-Cl; ++j) { if (j<l) tj = 2*l - j; else if (j>h) tj = 2*h - j; else tj =j; result += D(k,tj) * C(i-j); } A(k,i) = result; } } return A;}*/ //}}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -