source: Sophya/trunk/SophyaExt/Blitz/blitz/vecsum.cc@ 2846

Last change on this file since 2846 was 221, checked in by ansari, 26 years ago

Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99

File size: 2.5 KB
Line 
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
27BZ_NAMESPACE(blitz)
28
29template<class P_expr>
30inline
31BZ_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
53template<class P_numtype>
54inline
55BZ_SUMTYPE(P_numtype) sum(const Vector<P_numtype>& x)
56{
57 return _bz_vec_sum(x._bz_asVecExpr());
58}
59
60// sum(expr)
61template<class P_expr>
62inline
63BZ_SUMTYPE(_bz_typename P_expr::T_numtype)
64sum(_bz_VecExpr<P_expr> expr)
65{
66 return _bz_vec_sum(expr);
67}
68
69// sum(vecpick)
70template<class P_numtype>
71inline
72BZ_SUMTYPE(P_numtype)
73sum(const VectorPick<P_numtype>& x)
74{
75 return _bz_vec_sum(x._bz_asVecExpr());
76}
77
78// mean(vector)
79template<class P_numtype>
80inline
81BZ_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)
90template<class P_expr>
91inline
92BZ_FLOATTYPE(BZ_SUMTYPE(_bz_typename P_expr::T_numtype))
93mean(_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)
103template<class P_numtype>
104inline
105BZ_FLOATTYPE(BZ_SUMTYPE(P_numtype))
106mean(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
114BZ_NAMESPACE_END
115
116#endif // BZ_VECSUM_CC
117
Note: See TracBrowser for help on using the repository browser.