source: XMLIO_V2/external/include/blitz/vecsum.cc @ 80

Last change on this file since 80 was 80, checked in by ymipsl, 14 years ago

ajout lib externe

File size: 2.3 KB
Line 
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
17BZ_NAMESPACE(blitz)
18
19template<typename P_expr>
20inline
21BZ_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
43template<typename P_numtype>
44inline
45BZ_SUMTYPE(P_numtype) sum(const Vector<P_numtype>& x)
46{
47    return _bz_vec_sum(x._bz_asVecExpr());
48}
49
50// sum(expr)
51template<typename P_expr>
52inline
53BZ_SUMTYPE(_bz_typename P_expr::T_numtype)
54sum(_bz_VecExpr<P_expr> expr)
55{
56    return _bz_vec_sum(expr);
57}
58
59// sum(vecpick)
60template<typename P_numtype>
61inline
62BZ_SUMTYPE(P_numtype)
63sum(const VectorPick<P_numtype>& x)
64{
65    return _bz_vec_sum(x._bz_asVecExpr());
66}
67
68// mean(vector)
69template<typename P_numtype>
70inline
71BZ_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)
80template<typename P_expr>
81inline
82BZ_FLOATTYPE(BZ_SUMTYPE(_bz_typename P_expr::T_numtype))
83mean(_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)
93template<typename P_numtype>
94inline
95BZ_FLOATTYPE(BZ_SUMTYPE(P_numtype))
96mean(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
104BZ_NAMESPACE_END
105
106#endif // BZ_VECSUM_CC
107
Note: See TracBrowser for help on using the repository browser.