blitz  Version 0.9
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
zero.h
Go to the documentation of this file.
1 /***************************************************************************
2  * blitz/zero.h Zero elements
3  *
4  * $Id: zero.h,v 1.5 2003/12/11 03:44:22 julianc Exp $
5  *
6  * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * Suggestions: blitz-dev@oonumerics.org
19  * Bugs: blitz-bugs@oonumerics.org
20  *
21  * For more information, please see the Blitz++ Home Page:
22  * http://oonumerics.org/blitz/
23  *
24  ***************************************************************************/
25 
26 /*
27  * The purpose of the ZeroElement class is to provide an lvalue for
28  * non-const element access of matrices with zero elements. For
29  * example, a tridiagonal matrix has many elements which are
30  * always zero:
31  *
32  * [ x x 0 0 ]
33  * [ x x x 0 ]
34  * [ 0 x x x ]
35  * [ 0 0 x x ]
36  *
37  * To implement an operator()(int i, int j) for a tridiagonal
38  * matrix which may be used as an lvalue
39  *
40  * e.g. Matrix<double, Tridiagonal> M(4,4);
41  * M(1,2) = 3.0L;
42  *
43  * some way of returning an lvalue for the zero elements is needed.
44  * (Either that, or an intermediate class must be returned -- but
45  * this is less efficient). The solution used for the Blitz++
46  * library is to have a unique zero element for each numeric
47  * type (float, double, etc.). This zero element is then
48  * returned as an lvalue when needed.
49  *
50  * The disadvantage is the possibility of setting the global
51  * zero-element to something non-zero.
52  */
53 
54 #ifndef BZ_ZERO_H
55 #define BZ_ZERO_H
56 
57 #ifndef BZ_BLITZ_H
58  #include <blitz/blitz.h>
59 #endif
60 
61 BZ_NAMESPACE(blitz)
62 
63 template<typename P_numtype>
64 class ZeroElement {
65 public:
66  typedef P_numtype T_numtype;
67 
68  static T_numtype& zero()
69  {
70  return zero_;
71  }
72 
73 private:
74  static T_numtype zero_;
75 };
76 
77 // Specialization of ZeroElement for complex<float>, complex<double>,
78 // and complex<long double>
79 
80 #define BZZERO_DECLARE(T) \
81  template<> \
82  class ZeroElement<T > { \
83  public: \
84  static T& zero() \
85  { return zero_; } \
86  private: \
87  static T zero_; \
88  }
89 
90 #ifdef BZ_HAVE_COMPLEX
91  BZZERO_DECLARE(complex<float>);
92  BZZERO_DECLARE(complex<double>);
93  BZZERO_DECLARE(complex<long double>);
94 #endif // BZ_HAVE_COMPLEX
95 
96 // initialization of static data member for general class template
97 
98 template<typename P_numtype>
99 P_numtype ZeroElement<P_numtype>::zero_ = 0;
100 
102 
103 #endif // BZ_ZERO_H
104