| 1 | /* | 
|---|
| 2 | * $Id: vecdot.cc,v 1.1.1.1 1999-04-09 17:58:58 ansari Exp $ | 
|---|
| 3 | * | 
|---|
| 4 | * Copyright (C) 1997 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca> | 
|---|
| 5 | * All rights reserved.  Please see <blitz/blitz.h> for terms and | 
|---|
| 6 | * conditions of use. | 
|---|
| 7 | * | 
|---|
| 8 | * $Log: not supported by cvs2svn $ | 
|---|
| 9 | * Revision 1.4  1998/03/14 00:04:47  tveldhui | 
|---|
| 10 | * 0.2-alpha-05 | 
|---|
| 11 | * | 
|---|
| 12 | * Revision 1.3  1997/07/16 14:51:20  tveldhui | 
|---|
| 13 | * Update: Alpha release 0.2 (Arrays) | 
|---|
| 14 | * | 
|---|
| 15 | * Revision 1.2  1997/01/24 14:42:00  tveldhui | 
|---|
| 16 | * Periodic RCS update | 
|---|
| 17 | * | 
|---|
| 18 | */ | 
|---|
| 19 |  | 
|---|
| 20 | #ifndef BZ_VECDOT_CC | 
|---|
| 21 | #define BZ_VECDOT_CC | 
|---|
| 22 |  | 
|---|
| 23 | #ifndef BZ_VECGLOBS_H | 
|---|
| 24 | #error <blitz/vecdot.cc> must be included via <blitz/vecglobs.h> | 
|---|
| 25 | #endif | 
|---|
| 26 |  | 
|---|
| 27 | BZ_NAMESPACE(blitz) | 
|---|
| 28 |  | 
|---|
| 29 | template<class P1, class P2> | 
|---|
| 30 | inline | 
|---|
| 31 | BZ_SUMTYPE(BZ_PROMOTE(_bz_typename P1::T_numtype, _bz_typename P2::T_numtype)) | 
|---|
| 32 | _bz_dot(P1 vector1, P2 vector2) | 
|---|
| 33 | { | 
|---|
| 34 | BZPRECONDITION(vector1._bz_suggestLength() == vector2._bz_suggestLength()); | 
|---|
| 35 |  | 
|---|
| 36 | typedef BZ_SUMTYPE(BZ_PROMOTE(_bz_typename P1::T_numtype, | 
|---|
| 37 | _bz_typename P2::T_numtype))  T_sumtype; | 
|---|
| 38 |  | 
|---|
| 39 | T_sumtype sum = 0; | 
|---|
| 40 | int length = vector1._bz_suggestLength(); | 
|---|
| 41 |  | 
|---|
| 42 | if (vector1._bz_hasFastAccess() && vector2._bz_hasFastAccess()) | 
|---|
| 43 | { | 
|---|
| 44 | for (int i=0; i < length; ++i) | 
|---|
| 45 | sum += vector1._bz_fastAccess(i) | 
|---|
| 46 | * vector2._bz_fastAccess(i); | 
|---|
| 47 | } | 
|---|
| 48 | else { | 
|---|
| 49 | for (int i=0; i < length; ++i) | 
|---|
| 50 | sum += vector1[i] * vector2[i]; | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | return sum; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 |  | 
|---|
| 57 | // dot() | 
|---|
| 58 | template<class P_numtype1, class P_numtype2> | 
|---|
| 59 | inline | 
|---|
| 60 | BZ_SUMTYPE(BZ_PROMOTE(P_numtype1,P_numtype2)) | 
|---|
| 61 | dot(const Vector<P_numtype1>& a, const Vector<P_numtype2>& b) | 
|---|
| 62 | { | 
|---|
| 63 | return _bz_dot(a, b); | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | // dot(expr,expr) | 
|---|
| 67 | template<class P_expr1, class P_expr2> | 
|---|
| 68 | inline | 
|---|
| 69 | BZ_SUMTYPE(BZ_PROMOTE(_bz_typename P_expr1::T_numtype, | 
|---|
| 70 | _bz_typename P_expr2::T_numtype)) | 
|---|
| 71 | dot(_bz_VecExpr<P_expr1> expr1, _bz_VecExpr<P_expr2> expr2) | 
|---|
| 72 | { | 
|---|
| 73 | return _bz_dot(expr1, expr2); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | // dot(expr,vec) | 
|---|
| 77 | template<class P_expr1, class P_numtype2> | 
|---|
| 78 | inline | 
|---|
| 79 | BZ_SUMTYPE(BZ_PROMOTE(_bz_typename P_expr1::T_numtype, P_numtype2)) | 
|---|
| 80 | dot(_bz_VecExpr<P_expr1> expr1, const Vector<P_numtype2>& vector2) | 
|---|
| 81 | { | 
|---|
| 82 | return _bz_dot(vector2, expr1); | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | // dot(vec,expr) | 
|---|
| 86 | template<class P_numtype1, class P_expr2> | 
|---|
| 87 | inline | 
|---|
| 88 | BZ_SUMTYPE(BZ_PROMOTE(P_numtype1, _bz_typename P_expr2::T_numtype)) | 
|---|
| 89 | dot(const Vector<P_numtype1>& vector1, _bz_VecExpr<P_expr2> expr2) | 
|---|
| 90 | { | 
|---|
| 91 | return _bz_dot(vector1, expr2); | 
|---|
| 92 | } | 
|---|
| 93 |  | 
|---|
| 94 | // dot(vec,vecpick) | 
|---|
| 95 | template<class P_numtype1, class P_numtype2> | 
|---|
| 96 | inline | 
|---|
| 97 | BZ_SUMTYPE(BZ_PROMOTE(P_numtype1, P_numtype2)) | 
|---|
| 98 | dot(const Vector<P_numtype1>& vector1, const VectorPick<P_numtype2>& vector2) | 
|---|
| 99 | { | 
|---|
| 100 | return _bz_dot(vector1, vector2); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | // dot(vecpick,vec) | 
|---|
| 104 | template<class P_numtype1, class P_numtype2> | 
|---|
| 105 | inline | 
|---|
| 106 | BZ_SUMTYPE(BZ_PROMOTE(P_numtype1, P_numtype2)) | 
|---|
| 107 | dot(const VectorPick<P_numtype1>& vector1, const Vector<P_numtype2>& vector2) | 
|---|
| 108 | { | 
|---|
| 109 | return _bz_dot(vector1, vector2); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | // dot(vecpick,vecpick) | 
|---|
| 113 | template<class P_numtype1, class P_numtype2> | 
|---|
| 114 | inline | 
|---|
| 115 | BZ_SUMTYPE(BZ_PROMOTE(P_numtype1, P_numtype2)) | 
|---|
| 116 | dot(const VectorPick<P_numtype1>& vector1, const VectorPick<P_numtype2>& vector2) | 
|---|
| 117 | { | 
|---|
| 118 | return _bz_dot(vector1, vector2); | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | // dot(expr, vecpick) | 
|---|
| 122 | template<class P_expr1, class P_numtype2> | 
|---|
| 123 | inline | 
|---|
| 124 | BZ_SUMTYPE(BZ_PROMOTE(_bz_typename P_expr1::T_numtype, P_numtype2)) | 
|---|
| 125 | dot(_bz_VecExpr<P_expr1> expr1, const VectorPick<P_numtype2>& vector2) | 
|---|
| 126 | { | 
|---|
| 127 | return _bz_dot(expr1, vector2); | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | // dot(vecpick, expr) | 
|---|
| 131 | template<class P_numtype1, class P_expr2> | 
|---|
| 132 | inline | 
|---|
| 133 | BZ_SUMTYPE(BZ_PROMOTE(P_numtype1, _bz_typename P_expr2::T_numtype)) | 
|---|
| 134 | dot(const VectorPick<P_numtype1>& vector1, _bz_VecExpr<P_expr2> expr2) | 
|---|
| 135 | { | 
|---|
| 136 | return _bz_dot(vector1, expr2); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | BZ_NAMESPACE_END | 
|---|
| 140 |  | 
|---|
| 141 | #endif // BZ_VECDOT_CC | 
|---|
| 142 |  | 
|---|