1 | /* |
---|
2 | * $Id: vecsum.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_VECSUM_CC |
---|
11 | #define BZ_VECSUM_CC |
---|
12 | |
---|
13 | #ifndef BZ_VECGLOBS_H |
---|
14 | #error <blitz/vecsum.cc> must be included via <blitz/vecglobs.h> |
---|
15 | #endif |
---|
16 | |
---|
17 | BZ_NAMESPACE(blitz) |
---|
18 | |
---|
19 | template<typename P_expr> |
---|
20 | inline |
---|
21 | BZ_SUMTYPE(_bz_typename P_expr::T_numtype) |
---|
22 | _bz_vec_sum(P_expr vector) |
---|
23 | { |
---|
24 | typedef _bz_typename P_expr::T_numtype T_numtype; |
---|
25 | typedef BZ_SUMTYPE(T_numtype) T_sumtype; |
---|
26 | |
---|
27 | T_sumtype sum = 0; |
---|
28 | int length = vector._bz_suggestLength(); |
---|
29 | |
---|
30 | if (vector._bz_hasFastAccess()) |
---|
31 | { |
---|
32 | for (int i=0; i < length; ++i) |
---|
33 | sum += vector._bz_fastAccess(i); |
---|
34 | } |
---|
35 | else { |
---|
36 | for (int i=0; i < length; ++i) |
---|
37 | sum += vector(i); |
---|
38 | } |
---|
39 | |
---|
40 | return sum; |
---|
41 | } |
---|
42 | |
---|
43 | template<typename P_numtype> |
---|
44 | inline |
---|
45 | BZ_SUMTYPE(P_numtype) sum(const Vector<P_numtype>& x) |
---|
46 | { |
---|
47 | return _bz_vec_sum(x._bz_asVecExpr()); |
---|
48 | } |
---|
49 | |
---|
50 | // sum(expr) |
---|
51 | template<typename P_expr> |
---|
52 | inline |
---|
53 | BZ_SUMTYPE(_bz_typename P_expr::T_numtype) |
---|
54 | sum(_bz_VecExpr<P_expr> expr) |
---|
55 | { |
---|
56 | return _bz_vec_sum(expr); |
---|
57 | } |
---|
58 | |
---|
59 | // sum(vecpick) |
---|
60 | template<typename P_numtype> |
---|
61 | inline |
---|
62 | BZ_SUMTYPE(P_numtype) |
---|
63 | sum(const VectorPick<P_numtype>& x) |
---|
64 | { |
---|
65 | return _bz_vec_sum(x._bz_asVecExpr()); |
---|
66 | } |
---|
67 | |
---|
68 | // mean(vector) |
---|
69 | template<typename P_numtype> |
---|
70 | inline |
---|
71 | BZ_FLOATTYPE(BZ_SUMTYPE(P_numtype)) mean(const Vector<P_numtype>& x) |
---|
72 | { |
---|
73 | BZPRECONDITION(x.length() > 0); |
---|
74 | |
---|
75 | typedef BZ_FLOATTYPE(BZ_SUMTYPE(P_numtype)) T_floattype; |
---|
76 | return _bz_vec_sum(x._bz_asVecExpr()) / (T_floattype) x.length(); |
---|
77 | } |
---|
78 | |
---|
79 | // mean(expr) |
---|
80 | template<typename P_expr> |
---|
81 | inline |
---|
82 | BZ_FLOATTYPE(BZ_SUMTYPE(_bz_typename P_expr::T_numtype)) |
---|
83 | mean(_bz_VecExpr<P_expr> expr) |
---|
84 | { |
---|
85 | BZPRECONDITION(expr._bz_suggestLength() > 0); |
---|
86 | |
---|
87 | typedef BZ_FLOATTYPE(BZ_SUMTYPE(_bz_typename P_expr::T_numtype)) |
---|
88 | T_floattype; |
---|
89 | return _bz_vec_sum(expr) / (T_floattype) expr._bz_suggestLength(); |
---|
90 | } |
---|
91 | |
---|
92 | // mean(vecpick) |
---|
93 | template<typename P_numtype> |
---|
94 | inline |
---|
95 | BZ_FLOATTYPE(BZ_SUMTYPE(P_numtype)) |
---|
96 | mean(const VectorPick<P_numtype>& x) |
---|
97 | { |
---|
98 | BZPRECONDITION(x.length() > 0); |
---|
99 | |
---|
100 | typedef BZ_FLOATTYPE(BZ_SUMTYPE(P_numtype)) T_floattype; |
---|
101 | return _bz_vec_sum(x._bz_asVecExpr()) / (T_floattype) x.length(); |
---|
102 | } |
---|
103 | |
---|
104 | BZ_NAMESPACE_END |
---|
105 | |
---|
106 | #endif // BZ_VECSUM_CC |
---|
107 | |
---|