| [658] | 1 | /*
 | 
|---|
 | 2 |  * $Id: vecsum.cc,v 1.1.1.1 1999-11-26 16:37:06 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.1.1.1  1999/04/09  17:58:59  ansari
 | 
|---|
 | 10 | // Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99
 | 
|---|
 | 11 | //
 | 
|---|
 | 12 |  * Revision 1.4  1998/03/14 00:04:47  tveldhui
 | 
|---|
 | 13 |  * 0.2-alpha-05
 | 
|---|
 | 14 |  *
 | 
|---|
 | 15 |  * Revision 1.3  1997/07/16 14:51:20  tveldhui
 | 
|---|
 | 16 |  * Update: Alpha release 0.2 (Arrays)
 | 
|---|
 | 17 |  *
 | 
|---|
 | 18 |  * Revision 1.2  1997/01/24 14:42:00  tveldhui
 | 
|---|
 | 19 |  * Periodic RCS update
 | 
|---|
 | 20 |  *
 | 
|---|
 | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | #ifndef BZ_VECSUM_CC
 | 
|---|
 | 24 | #define BZ_VECSUM_CC
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | #ifndef BZ_VECGLOBS_H
 | 
|---|
 | 27 |  #error <blitz/vecsum.cc> must be included via <blitz/vecglobs.h>
 | 
|---|
 | 28 | #endif
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | BZ_NAMESPACE(blitz)
 | 
|---|
 | 31 | 
 | 
|---|
 | 32 | template<class P_expr>
 | 
|---|
 | 33 | inline
 | 
|---|
 | 34 | BZ_SUMTYPE(_bz_typename P_expr::T_numtype)
 | 
|---|
 | 35 | _bz_vec_sum(P_expr vector)
 | 
|---|
 | 36 | {
 | 
|---|
 | 37 |     typedef _bz_typename P_expr::T_numtype T_numtype;
 | 
|---|
 | 38 |     typedef BZ_SUMTYPE(T_numtype)          T_sumtype;
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 |     T_sumtype sum = 0;
 | 
|---|
 | 41 |     int length = vector._bz_suggestLength();
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 |     if (vector._bz_hasFastAccess())
 | 
|---|
 | 44 |     {
 | 
|---|
 | 45 |         for (int i=0; i < length; ++i)
 | 
|---|
 | 46 |             sum += vector._bz_fastAccess(i);
 | 
|---|
 | 47 |     }
 | 
|---|
 | 48 |     else {
 | 
|---|
 | 49 |         for (int i=0; i < length; ++i)
 | 
|---|
 | 50 |             sum += vector(i);
 | 
|---|
 | 51 |     }
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 |     return sum;
 | 
|---|
 | 54 | }
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 | template<class P_numtype>
 | 
|---|
 | 57 | inline
 | 
|---|
 | 58 | BZ_SUMTYPE(P_numtype) sum(const Vector<P_numtype>& x)
 | 
|---|
 | 59 | {
 | 
|---|
 | 60 |     return _bz_vec_sum(x._bz_asVecExpr());
 | 
|---|
 | 61 | }
 | 
|---|
 | 62 | 
 | 
|---|
 | 63 | // sum(expr)
 | 
|---|
 | 64 | template<class P_expr>
 | 
|---|
 | 65 | inline
 | 
|---|
 | 66 | BZ_SUMTYPE(_bz_typename P_expr::T_numtype)
 | 
|---|
 | 67 | sum(_bz_VecExpr<P_expr> expr)
 | 
|---|
 | 68 | {
 | 
|---|
 | 69 |     return _bz_vec_sum(expr);
 | 
|---|
 | 70 | }
 | 
|---|
 | 71 | 
 | 
|---|
 | 72 | // sum(vecpick)
 | 
|---|
 | 73 | template<class P_numtype>
 | 
|---|
 | 74 | inline
 | 
|---|
 | 75 | BZ_SUMTYPE(P_numtype)
 | 
|---|
 | 76 | sum(const VectorPick<P_numtype>& x)
 | 
|---|
 | 77 | {
 | 
|---|
 | 78 |     return _bz_vec_sum(x._bz_asVecExpr());
 | 
|---|
 | 79 | }
 | 
|---|
 | 80 | 
 | 
|---|
 | 81 | // mean(vector)
 | 
|---|
 | 82 | template<class P_numtype>
 | 
|---|
 | 83 | inline
 | 
|---|
 | 84 | BZ_FLOATTYPE(BZ_SUMTYPE(P_numtype)) mean(const Vector<P_numtype>& x)
 | 
|---|
 | 85 | {
 | 
|---|
 | 86 |     BZPRECONDITION(x.length() > 0);
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 |     typedef BZ_FLOATTYPE(BZ_SUMTYPE(P_numtype)) T_floattype;
 | 
|---|
 | 89 |     return _bz_vec_sum(x._bz_asVecExpr()) / (T_floattype) x.length();
 | 
|---|
 | 90 | }
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 | // mean(expr)
 | 
|---|
 | 93 | template<class P_expr>
 | 
|---|
 | 94 | inline
 | 
|---|
 | 95 | BZ_FLOATTYPE(BZ_SUMTYPE(_bz_typename P_expr::T_numtype))
 | 
|---|
 | 96 | mean(_bz_VecExpr<P_expr> expr)
 | 
|---|
 | 97 | {
 | 
|---|
 | 98 |     BZPRECONDITION(expr._bz_suggestLength() > 0);
 | 
|---|
 | 99 | 
 | 
|---|
 | 100 |     typedef BZ_FLOATTYPE(BZ_SUMTYPE(_bz_typename P_expr::T_numtype)) 
 | 
|---|
 | 101 |         T_floattype;
 | 
|---|
 | 102 |     return _bz_vec_sum(expr) / (T_floattype) expr._bz_suggestLength();
 | 
|---|
 | 103 | }
 | 
|---|
 | 104 | 
 | 
|---|
 | 105 | // mean(vecpick)
 | 
|---|
 | 106 | template<class P_numtype>
 | 
|---|
 | 107 | inline
 | 
|---|
 | 108 | BZ_FLOATTYPE(BZ_SUMTYPE(P_numtype))
 | 
|---|
 | 109 | mean(const VectorPick<P_numtype>& x)
 | 
|---|
 | 110 | {
 | 
|---|
 | 111 |     BZPRECONDITION(x.length() > 0);
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 |     typedef BZ_FLOATTYPE(BZ_SUMTYPE(P_numtype)) T_floattype;
 | 
|---|
 | 114 |     return _bz_vec_sum(x._bz_asVecExpr()) / (T_floattype) x.length();
 | 
|---|
 | 115 | }
 | 
|---|
 | 116 | 
 | 
|---|
 | 117 | BZ_NAMESPACE_END
 | 
|---|
 | 118 | 
 | 
|---|
 | 119 | #endif // BZ_VECSUM_CC
 | 
|---|
 | 120 | 
 | 
|---|