1 | /*
|
---|
2 | * $Id: vecnorm.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.5 1998/03/14 00:04:47 tveldhui
|
---|
13 | * 0.2-alpha-05
|
---|
14 | *
|
---|
15 | * Revision 1.4 1997/07/16 14:51:20 tveldhui
|
---|
16 | * Update: Alpha release 0.2 (Arrays)
|
---|
17 | *
|
---|
18 | * Revision 1.3 1997/01/24 14:42:00 tveldhui
|
---|
19 | * Periodic RCS update
|
---|
20 | *
|
---|
21 | */
|
---|
22 |
|
---|
23 | #ifndef BZ_VECNORM_CC
|
---|
24 | #define BZ_VECNORM_CC
|
---|
25 |
|
---|
26 | #ifndef BZ_VECGLOBS_H
|
---|
27 | #error <blitz/vecnorm.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_FLOATTYPE(BZ_SUMTYPE(_bz_typename P_expr::T_numtype))
|
---|
35 | _bz_vec_norm(P_expr vector)
|
---|
36 | {
|
---|
37 | // An extreme use of traits here. It's necessary to
|
---|
38 | // handle odd cases such as the norm of a Vector<char>.
|
---|
39 | // To avoid overflow, BZ_SUMTYPE(char) is int.
|
---|
40 | // To take the sqrt of the sum, BZ_FLOATTYPE(char) is float.
|
---|
41 | // So float is returned for Vector<char>.
|
---|
42 | typedef _bz_typename P_expr::T_numtype T_numtype;
|
---|
43 | typedef BZ_SUMTYPE(T_numtype) T_sumtype;
|
---|
44 | typedef BZ_FLOATTYPE(T_sumtype) T_floattype;
|
---|
45 |
|
---|
46 | T_sumtype sum = 0;
|
---|
47 | int length = vector._bz_suggestLength();
|
---|
48 |
|
---|
49 | if (vector._bz_hasFastAccess())
|
---|
50 | {
|
---|
51 | for (int i=0; i < length; ++i)
|
---|
52 | {
|
---|
53 | T_numtype value = vector._bz_fastAccess(i);
|
---|
54 | sum += value * T_sumtype(value);
|
---|
55 | }
|
---|
56 | }
|
---|
57 | else {
|
---|
58 | for (int i=0; i < length; ++i)
|
---|
59 | {
|
---|
60 | T_numtype value = vector(i);
|
---|
61 | sum += value * T_sumtype(value);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | return _bz_sqrt<T_floattype>::apply(sum);
|
---|
66 | }
|
---|
67 |
|
---|
68 | template<class P_numtype>
|
---|
69 | inline
|
---|
70 | BZ_FLOATTYPE(BZ_SUMTYPE(P_numtype)) norm(const Vector<P_numtype>& x)
|
---|
71 | {
|
---|
72 | return _bz_vec_norm(x._bz_asVecExpr());
|
---|
73 | }
|
---|
74 |
|
---|
75 | // norm(expr)
|
---|
76 | template<class P_expr>
|
---|
77 | inline
|
---|
78 | BZ_FLOATTYPE(BZ_SUMTYPE(_bz_typename P_expr::T_numtype))
|
---|
79 | norm(_bz_VecExpr<P_expr> expr)
|
---|
80 | {
|
---|
81 | return _bz_vec_norm(expr);
|
---|
82 | }
|
---|
83 |
|
---|
84 | // norm(vecpick)
|
---|
85 | template<class P_numtype>
|
---|
86 | inline
|
---|
87 | BZ_FLOATTYPE(BZ_SUMTYPE(P_numtype))
|
---|
88 | norm(const VectorPick<P_numtype>& x)
|
---|
89 | {
|
---|
90 | return _bz_vec_norm(x._bz_asVecExpr());
|
---|
91 | }
|
---|
92 |
|
---|
93 | // norm(TinyVector)
|
---|
94 | template<class P_numtype, int N_dimensions>
|
---|
95 | inline
|
---|
96 | BZ_FLOATTYPE(BZ_SUMTYPE(P_numtype))
|
---|
97 | norm(const TinyVector<P_numtype, N_dimensions>& x)
|
---|
98 | {
|
---|
99 | return _bz_vec_norm(x._bz_asVecExpr());
|
---|
100 | }
|
---|
101 |
|
---|
102 | BZ_NAMESPACE_END
|
---|
103 |
|
---|
104 | #endif // BZ_VECNORM_CC
|
---|
105 |
|
---|