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