00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef EXPRESSO_STANDARDOP_HDR
00022 #define EXPRESSO_STANDARDOP_HDR
00023
00024 #include "iterator.h"
00025
00026 namespace esso {
00027
00029
00031
00032 struct cAdd;
00033 template<class TL, class TR>
00034 struct BCalculate<cAdd,TL,TR> {
00035 static inline typename BPromote<cAdd,TL,TR>::T
00036 Do(const TL l, const TR r) { return l+r; }
00037 };
00042 template<int N, class TypeL, class TypeR>
00043 inline Array<N,Bop<cAdd,TypeL,TypeR> >
00044 operator +
00045 (const Array<N,TypeL> &l,
00046 const Array<N,TypeR> &r) {
00047 return Array<N,Bop<cAdd,TypeL,TypeR> >(l,r);
00048 }
00053 template<int N, class TypeL, class TR>
00054 inline Array<N,Bop<cAdd,TypeL,Scalar<TR> > >
00055 operator +
00056 (const Array<N,TypeL> &l,
00057 const TR &r) {
00058 return Array<N,Bop<cAdd,TypeL,Scalar<TR> > >(l,Array<N,Scalar<TR> >(r));
00059 }
00064 template<int N, class TL, class TypeR>
00065 inline Array<N,Bop<cAdd,Scalar<TL>,TypeR > >
00066 operator +
00067 (const TL &l,
00068 const Array<N,TypeR> &r) {
00069 return Array<N,Bop<cAdd,Scalar<TL>,TypeR > >(Array<N,Scalar<TL> >(l),r);
00070 }
00071
00072 struct cSub;
00073 template<class TL, class TR>
00074 struct BCalculate<cSub,TL,TR> {
00075 static inline typename BPromote<cSub,TL,TR>::T
00076 Do(const TL l, const TR r) { return l-r; }
00077 };
00082 template<int N, class TypeL, class TypeR>
00083 inline Array<N,Bop<cSub,TypeL,TypeR> >
00084 operator -
00085 (const Array<N,TypeL> &l,
00086 const Array<N,TypeR> &r) {
00087 return Array<N,Bop<cSub,TypeL,TypeR> >(l,r);
00088 }
00093 template<int N, class TypeL, class TR>
00094 inline Array<N,Bop<cSub,TypeL,Scalar<TR> > >
00095 operator -
00096 (const Array<N,TypeL> &l,
00097 const TR &r) {
00098 return Array<N,Bop<cSub,TypeL,Scalar<TR> > >(l,Array<N,Scalar<TR> >(r));
00099 }
00104 template<int N, class TL, class TypeR>
00105 inline Array<N,Bop<cSub,Scalar<TL>,TypeR > >
00106 operator -
00107 (const TL &l,
00108 const Array<N,TypeR> &r) {
00109 return Array<N,Bop<cSub,Scalar<TL>,TypeR > >(Array<N,Scalar<TL> >(l),r);
00110 }
00111
00112 struct cMul;
00113 template<class TL, class TR>
00114 struct BCalculate<cMul,TL,TR> {
00115 static inline typename BPromote<cMul,TL,TR>::T
00116 Do(const TL l, const TR r) { return l*r; }
00117 };
00122 template<int N, class TypeL, class TypeR>
00123 inline Array<N,Bop<cMul,TypeL,TypeR> >
00124 operator *
00125 (const Array<N,TypeL> &l,
00126 const Array<N,TypeR> &r) {
00127 return Array<N,Bop<cMul,TypeL,TypeR> >(l,r);
00128 }
00133 template<int N, class TypeL, class TR>
00134 inline Array<N,Bop<cMul,TypeL,Scalar<TR> > >
00135 operator *
00136 (const Array<N,TypeL> &l,
00137 const TR &r) {
00138 return Array<N,Bop<cMul,TypeL,Scalar<TR> > >(l,Array<N,Scalar<TR> >(r));
00139 }
00144 template<int N, class TL, class TypeR>
00145 inline Array<N,Bop<cMul,Scalar<TL>,TypeR > >
00146 operator *
00147 (const TL &l,
00148 const Array<N,TypeR> &r) {
00149 return Array<N,Bop<cMul,Scalar<TL>,TypeR > >(Array<N,Scalar<TL> >(l),r);
00150 }
00151
00152 struct cDiv;
00153 template<class TL, class TR>
00154 struct BCalculate<cDiv,TL,TR> {
00155 static inline typename BPromote<cDiv,TL,TR>::T
00156 Do(const TL l, const TR r) { return l/r; }
00157 };
00162 template<int N, class TypeL, class TypeR>
00163 inline Array<N,Bop<cDiv,TypeL,TypeR> >
00164 operator /
00165 (const Array<N,TypeL> &l,
00166 const Array<N,TypeR> &r) {
00167 return Array<N,Bop<cDiv,TypeL,TypeR> >(l,r);
00168 }
00173 template<int N, class TypeL, class TR>
00174 inline Array<N,Bop<cDiv,TypeL,Scalar<TR> > >
00175 operator /
00176 (const Array<N,TypeL> &l,
00177 const TR &r) {
00178 return Array<N,Bop<cDiv,TypeL,Scalar<TR> > >(l,Array<N,Scalar<TR> >(r));
00179 }
00184 template<int N, class TL, class TypeR>
00185 inline Array<N,Bop<cDiv,Scalar<TL>,TypeR > >
00186 operator /
00187 (const TL &l,
00188 const Array<N,TypeR> &r) {
00189 return Array<N,Bop<cDiv,Scalar<TL>,TypeR > >(Array<N,Scalar<TL> >(l),r);
00190 }
00191
00192 struct cMax;
00193 template<class TL, class TR>
00194 struct BCalculate<cMax,TL,TR> {
00195 static inline typename BPromote<cMax,TL,TR>::T
00196 Do(const TL l, const TR r) { return
00197 l>r?l:r;
00198 }
00199 };
00204 template<int N, class TypeL, class TypeR>
00205 inline Array<N,Bop<cMax,TypeL,TypeR> >
00206 Max
00207 (const Array<N,TypeL> &l,
00208 const Array<N,TypeR> &r) {
00209 return Array<N,Bop<cMax,TypeL,TypeR> >(l,r);
00210 }
00215 template<int N, class TypeL, class TR>
00216 inline Array<N,Bop<cMax,TypeL,Scalar<TR> > >
00217 Max
00218 (const Array<N,TypeL> &l,
00219 const TR &r) {
00220 return Array<N,Bop<cMax,TypeL,Scalar<TR> > >(l,Array<N,Scalar<TR> >(r));
00221 }
00226 template<int N, class TL, class TypeR>
00227 inline Array<N,Bop<cMax,Scalar<TL>,TypeR > >
00228 Max
00229 (const TL &l,
00230 const Array<N,TypeR> &r) {
00231 return Array<N,Bop<cMax,Scalar<TL>,TypeR > >(Array<N,Scalar<TL> >(l),r);
00232 }
00233
00234 struct cMin;
00235 template<class TL, class TR>
00236 struct BCalculate<cMin,TL,TR> {
00237 static inline typename BPromote<cMin,TL,TR>::T
00238 Do(const TL l, const TR r) { return
00239 l<r?l:r;
00240 }
00241 };
00246 template<int N, class TypeL, class TypeR>
00247 inline Array<N,Bop<cMin,TypeL,TypeR> >
00248 Min
00249 (const Array<N,TypeL> &l,
00250 const Array<N,TypeR> &r) {
00251 return Array<N,Bop<cMin,TypeL,TypeR> >(l,r);
00252 }
00257 template<int N, class TypeL, class TR>
00258 inline Array<N,Bop<cMin,TypeL,Scalar<TR> > >
00259 Min
00260 (const Array<N,TypeL> &l,
00261 const TR &r) {
00262 return Array<N,Bop<cMin,TypeL,Scalar<TR> > >(l,Array<N,Scalar<TR> >(r));
00263 }
00268 template<int N, class TL, class TypeR>
00269 inline Array<N,Bop<cMin,Scalar<TL>,TypeR > >
00270 Min
00271 (const TL &l,
00272 const Array<N,TypeR> &r) {
00273 return Array<N,Bop<cMin,Scalar<TL>,TypeR > >(Array<N,Scalar<TL> >(l),r);
00274 }
00275
00276 struct cNeg;
00277 template<class T0>
00278 struct UPromote<cNeg,T0> {
00279 typedef T0 T;
00280 static inline T Do(const T s) { return -s; }
00281 };
00285 template<int N, class TypeS>
00286 inline Array<N,Uop<cNeg,TypeS> >
00287 operator -
00288 (const Array<N,TypeS> &s) {
00289 return Array<N,Uop<cNeg,TypeS> >(s);
00290 }
00291
00292 struct cSqrt;
00293 template<class T0>
00294 struct UPromote<cSqrt,T0> {
00295 typedef T0 T;
00296 static inline T Do(const T s) { return sqrt(s); }
00297 };
00301 template<int N, class TypeS>
00302 inline Array<N,Uop<cSqrt,TypeS> >
00303 Sqrt
00304 (const Array<N,TypeS> &s) {
00305 return Array<N,Uop<cSqrt,TypeS> >(s);
00306 }
00307
00308 struct cSqr;
00309 template<class T0>
00310 struct UPromote<cSqr,T0> {
00311 typedef T0 T;
00312 static inline T Do(const T s) { return s*s; }
00313 };
00317 template<int N, class TypeS>
00318 inline Array<N,Uop<cSqr,TypeS> >
00319 Sqr
00320 (const Array<N,TypeS> &s) {
00321 return Array<N,Uop<cSqr,TypeS> >(s);
00322 }
00323
00324 struct cAbs;
00325 template<class T0>
00326 struct UPromote<cAbs,T0> {
00327 typedef T0 T;
00328 static inline T Do(const T s) { return s>0 ? s : -s; }
00329 };
00334 template<int N, class TypeS>
00335 inline Array<N,Uop<cAbs,TypeS> >
00336 Abs
00337 (const Array<N,TypeS> &s) {
00338 return Array<N,Uop<cAbs,TypeS> >(s);
00339 }
00340
00341 template<class Op, class T0>
00342 struct RPromote { public: typedef T0 T; };
00343
00344 struct cSum;
00345 template<class T0>
00346 struct RPromote<cSum,T0> {
00347 typedef T0 T;
00348 template<int N, class TypeS>
00349 static inline T Do(const Array<N,TypeS> &src) {
00350 IXTYPE u=src.U(0);
00351 IXTYPE l=src.L(0);
00352 T ret=T(0);
00353 for(IXTYPE i=l; i<u; i++)
00354 ret=ret+src.CGet(i);
00355 return ret;
00356 }
00357 };
00361 template<int N, class TypeS>
00362 inline Array<N-1,Rop<cSum,TypeS> >
00363 Sum
00364 (const Array<N,TypeS> &s) {
00365 return Array<N-1,Rop<cSum,TypeS> >(s);
00366 }
00369 template<class TypeS>
00370 inline typename RPromote<cSum,typename Array<1,TypeS>::T>::T
00371 Sum
00372 (const Array<1,TypeS> &src0) {
00373 typename Array<1,TypeS>::IteratorType src(src0);
00374 return RPromote<cSum,typename Array<1,TypeS>::T>::Do(src);
00375 }
00376
00379 template<int N, class TypeS>
00380 inline typename Array<N,TypeS>::T Sumall(const Array<N,TypeS> &src) {
00381 return Sumall(Sum(src));
00382 }
00383 template<class TypeS>
00384 inline typename Array<1,TypeS>::T Sumall(const Array<1,TypeS> &src) {
00385 return Sum(src);
00386 }
00387
00388 template<class T0>
00389 struct RPromote<cMax,T0> {
00390 typedef T0 T;
00391 template<int N, class TypeS>
00392 static inline T Do(const Array<N,TypeS> &src) {
00393 IXTYPE u=src.U(0);
00394 IXTYPE l=src.L(0);
00395 T ret=T(0);
00396 if(u>l)
00397 ret=src.CGet(l);
00398 for(IXTYPE i=l; i<u; i++)
00399 if(src.CGet(i)>ret)
00400 ret=src.CGet(i);
00401 return ret;
00402 }
00403 };
00407 template<int N, class TypeS>
00408 inline Array<N-1,Rop<cMax,TypeS> >
00409 Max
00410 (const Array<N,TypeS> &s) {
00411 return Array<N-1,Rop<cMax,TypeS> >(s);
00412 }
00415 template<class TypeS>
00416 inline typename RPromote<cMax,typename Array<1,TypeS>::T>::T
00417 Max
00418 (const Array<1,TypeS> &src0) {
00419 typename Array<1,TypeS>::IteratorType src(src0);
00420 return RPromote<cMax,typename Array<1,TypeS>::T>::Do(src);
00421 }
00424 template<int N, class TypeS>
00425 inline typename Array<N,TypeS>::T Maxall(const Array<N,TypeS> &src) {
00426 return Maxall(Max(src));
00427 }
00428 template<class TypeS>
00429 inline typename Array<1,TypeS>::T Maxall(const Array<1,TypeS> &src) {
00430 return Max(src);
00431 }
00432
00433 template<class T0>
00434 struct RPromote<cMin,T0> {
00435 typedef T0 T;
00436 template<int N, class TypeS>
00437 static inline T Do(const Array<N,TypeS> &src) {
00438 IXTYPE u=src.U(0);
00439 IXTYPE l=src.L(0);
00440 T ret=T(0);
00441 if(u>l)
00442 ret=src.CGet(l);
00443 for(IXTYPE i=l; i<u; i++)
00444 if(src.CGet(i)<ret)
00445 ret=src.CGet(i);
00446 return ret;
00447 }
00448 };
00452 template<int N, class TypeS>
00453 inline Array<N-1,Rop<cMin,TypeS> >
00454 Min
00455 (const Array<N,TypeS> &s) {
00456 return Array<N-1,Rop<cMin,TypeS> >(s);
00457 }
00460 template<class TypeS>
00461 inline typename RPromote<cMin,typename Array<1,TypeS>::T>::T
00462 Min
00463 (const Array<1,TypeS> &src0) {
00464 typename Array<1,TypeS>::IteratorType src(src0);
00465 return RPromote<cMin,typename Array<1,TypeS>::T>::Do(src);
00466 }
00469 template<int N, class TypeS>
00470 inline typename Array<N,TypeS>::T Minall(const Array<N,TypeS> &src) {
00471 return Minall(Min(src));
00472 }
00473 template<class TypeS>
00474 inline typename Array<1,TypeS>::T Minall(const Array<1,TypeS> &src) {
00475 return Min(src);
00476 }
00478
00480
00482
00487 template<int N, class Type>
00488 inline Array<N,Type>
00489 Shift(const Array<N,Type> &src, DIMT n, IXTYPE i) {
00490 return Array<N,Type>(src).Shift(n,i);
00491 }
00492
00497 template<int N, class Type>
00498 inline Array<N,Type>
00499 Shift(const Array<N,Type> &src, const Ix<N> &ix) {
00500 return Array<N,Type>(src).Shift(ix);
00501 }
00502
00507 template<int N, class Type>
00508 inline Array<N,Type>
00509 Reverse(const Array<N,Type> &src, DIMT n) {
00510 return Array<N,Type>(src).Reverse(n);
00511 }
00512
00517 template<int N, class Type>
00518 inline Array<N,Type>
00519 Reverse(const Array<N,Type> &src, const Ix<N,bool> rx) {
00520 return Array<N,Type>(src).Reverse(rx);
00521 }
00522
00527 template<int N, class Type>
00528 inline Array<N,Type>
00529 Flip(const Array<N,Type> &src, DIMT n) {
00530 return Array<N,Type>(src).Flip(n);
00531 }
00532
00537 template<int N, class Type>
00538 inline Array<N,Type>
00539 Flip(const Array<N,Type> &src, const Ix<N,bool> &rx) {
00540 return Array<N,Type>(src).Flip(rx);
00541 }
00542
00548 template<int N, class Type>
00549 inline Array<N,Type>
00550 Restride(const Array<N,Type> &src, DIMT n, IXTYPE s) {
00551 return Array<N,Type>(src).Restride(n,s);
00552 }
00553
00558 template<int N, class Type>
00559 inline Array<N,Type>
00560 Restride(const Array<N,Type> &src, const Ix<N> &sx) {
00561 return Array<N,Type>(src).Restride(sx);
00562 }
00563
00569 template<int N, class Type>
00570 inline Array<N,Type>
00571 RestrictL(const Array<N,Type> &src, DIMT n, IXTYPE l0) {
00572 return Array<N,Type>(src).RestrictL(n,l0);
00573 }
00574
00580 template<int N, class Type>
00581 inline Array<N,Type>
00582 RestrictU(const Array<N,Type> &src, DIMT n, IXTYPE u0) {
00583 return Array<N,Type>(src).RestrictU(n,u0);
00584 }
00585
00592 template<int N, class Type>
00593 inline Array<N,Type>
00594 Restrict(const Array<N,Type> &src, DIMT n, IXTYPE l0, IXTYPE u0) {
00595 return Array<N,Type>(src).Restrict(n,l0,u0);
00596 }
00597
00602 template<int N, class Type>
00603 inline Array<N,Type>
00604 RestrictL(const Array<N,Type> &src, const Ix<N> &l0) {
00605 return Array<N,Type>(src).RestrictL(l0);
00606 }
00607
00612 template<int N, class Type>
00613 inline Array<N,Type>
00614 RestrictU(const Array<N,Type> &src, const Ix<N> &u0) {
00615 return Array<N,Type>(src).RestrictU(u0);
00616 }
00617
00623 template<int N, class Type>
00624 inline Array<N,Type>
00625 Restrict(const Array<N,Type> &src, const Ix<N> &l0, const Ix<N> &u0) {
00626 return Array<N,Type>(src).Restrict(l0,u0);
00627 }
00628
00633 template<int N, class Type>
00634 inline Array<N,Type>
00635 Permute(const Array<N,Type> &src, DIMT n1, DIMT n2) {
00636 return Array<N,Type>(src).Permute(n1,n2);
00637 }
00638
00643 template<int N, class Type>
00644 inline Array<N,Type>
00645 Permute(const Array<N,Type> &src, const Permutation<N> &p) {
00646 return Array<N,Type>(src).Permute(p);
00647 }
00648
00654 template<int N, class Type>
00655 inline Array<N-1,Type>
00656 Rmdim(const Array<N,Type> &src, DIMT n, IXTYPE i) {
00657 return Array<N-1,Type>(src,n,i);
00658 }
00659
00666 template<int N, class Type>
00667 inline Array<N+1,Type>
00668 Extend(const Array<N,Type> &src, IXTYPE l0, IXTYPE u0) {
00669 return Array<N+1,Type>().Extend(src,l0,u0);
00670 }
00671
00675 template<int N, class Type>
00676 inline Array<N,Fix<typename Array<N,Type>::T> >
00677 Clone(const Array<N,Type> &src) {
00678 Array<N,Fix<typename Array<N,Type>::T> > dst;
00679 assert(dst.Clone(src));
00680 return dst;
00681 }
00682
00686 template<int N, class TypeL, class TypeR>
00687 inline bool Matches(const Array<N,TypeL> &l, const Array<N,TypeR> &r) {
00688 for(int n=0; n<N; n++)
00689 if(l.L(n)!=r.L(n) || l.U(n)!=r.U(n))
00690 return false;
00691 return true;
00692 }
00694 }
00695
00696 #endif