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