source: Sophya/trunk/SophyaExt/Blitz/blitz/vecnorm.cc@ 3088

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

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

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