| 1 | /***************************************************************************
 | 
|---|
| 2 |  * blitz/zero.h          Zero elements
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  * $Id: zero.h,v 1.1.1.1 1999-11-26 16:37:06 ansari Exp $
 | 
|---|
| 5 |  *
 | 
|---|
| 6 |  * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
 | 
|---|
| 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-suggest@cybervision.com
 | 
|---|
| 19 |  * Bugs:                 blitz-bugs@cybervision.com
 | 
|---|
| 20 |  *
 | 
|---|
| 21 |  * For more information, please see the Blitz++ Home Page:
 | 
|---|
| 22 |  *    http://seurat.uwaterloo.ca/blitz/
 | 
|---|
| 23 |  *
 | 
|---|
| 24 |  ***************************************************************************
 | 
|---|
| 25 |  * $Log: not supported by cvs2svn $
 | 
|---|
| 26 |  * Revision 1.1.1.1  1999/04/09  17:59:00  ansari
 | 
|---|
| 27 |  * Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99
 | 
|---|
| 28 |  *
 | 
|---|
| 29 |  * Revision 1.4  1998/03/14 00:04:47  tveldhui
 | 
|---|
| 30 |  * 0.2-alpha-05
 | 
|---|
| 31 |  *
 | 
|---|
| 32 |  * Revision 1.3  1997/07/16 14:51:20  tveldhui
 | 
|---|
| 33 |  * Update: Alpha release 0.2 (Arrays)
 | 
|---|
| 34 |  *
 | 
|---|
| 35 |  * Revision 1.2  1997/01/24 14:42:00  tveldhui
 | 
|---|
| 36 |  * Periodic RCS update
 | 
|---|
| 37 |  *
 | 
|---|
| 38 |  * Revision 1.1  1997/01/13 22:19:58  tveldhui
 | 
|---|
| 39 |  * Periodic RCS update
 | 
|---|
| 40 |  *
 | 
|---|
| 41 |  *
 | 
|---|
| 42 |  ***************************************************************************
 | 
|---|
| 43 |  *
 | 
|---|
| 44 |  * The purpose of the ZeroElement class is to provide an lvalue for
 | 
|---|
| 45 |  * non-const element access of matrices with zero elements.  For
 | 
|---|
| 46 |  * example, a tridiagonal matrix has many elements which are
 | 
|---|
| 47 |  * always zero:
 | 
|---|
| 48 |  *
 | 
|---|
| 49 |  * [ x x 0 0 ]
 | 
|---|
| 50 |  * [ x x x 0 ]
 | 
|---|
| 51 |  * [ 0 x x x ]
 | 
|---|
| 52 |  * [ 0 0 x x ]
 | 
|---|
| 53 |  *
 | 
|---|
| 54 |  * To implement an operator()(int i, int j) for a tridiagonal
 | 
|---|
| 55 |  * matrix which may be used as an lvalue
 | 
|---|
| 56 |  *
 | 
|---|
| 57 |  * e.g. Matrix<double, Tridiagonal> M(4,4);
 | 
|---|
| 58 |  *      M(1,2) = 3.0L;
 | 
|---|
| 59 |  *
 | 
|---|
| 60 |  * some way of returning an lvalue for the zero elements is needed.
 | 
|---|
| 61 |  * (Either that, or an intermediate class must be returned -- but
 | 
|---|
| 62 |  * this is less efficient).  The solution used for the Blitz++
 | 
|---|
| 63 |  * library is to have a unique zero element for each numeric
 | 
|---|
| 64 |  * type (float, double, etc.).  This zero element is then
 | 
|---|
| 65 |  * returned as an lvalue when needed.
 | 
|---|
| 66 |  *
 | 
|---|
| 67 |  * The disadvantage is the possibility of setting the global
 | 
|---|
| 68 |  * zero-element to something non-zero.  
 | 
|---|
| 69 |  */
 | 
|---|
| 70 | 
 | 
|---|
| 71 | #ifndef BZ_ZERO_H
 | 
|---|
| 72 | #define BZ_ZERO_H
 | 
|---|
| 73 | 
 | 
|---|
| 74 | #ifndef BZ_BLITZ_H
 | 
|---|
| 75 |  #include <blitz/blitz.h>
 | 
|---|
| 76 | #endif
 | 
|---|
| 77 | 
 | 
|---|
| 78 | BZ_NAMESPACE(blitz)
 | 
|---|
| 79 | 
 | 
|---|
| 80 | template<class P_numtype>
 | 
|---|
| 81 | class ZeroElement {
 | 
|---|
| 82 | public:
 | 
|---|
| 83 |     typedef P_numtype T_numtype;
 | 
|---|
| 84 | 
 | 
|---|
| 85 |     static T_numtype& zero()
 | 
|---|
| 86 |     { 
 | 
|---|
| 87 |         return zero_; 
 | 
|---|
| 88 |     }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | private:
 | 
|---|
| 91 |     static T_numtype zero_;
 | 
|---|
| 92 | };
 | 
|---|
| 93 | 
 | 
|---|
| 94 | // Specialization of ZeroElement for complex<float>, complex<double>,
 | 
|---|
| 95 | // and complex<long double>
 | 
|---|
| 96 | 
 | 
|---|
| 97 | #define BZZERO_DECLARE(T)            \
 | 
|---|
| 98 |   template<>                         \
 | 
|---|
| 99 |   class ZeroElement<T> {             \
 | 
|---|
| 100 |   public:                            \
 | 
|---|
| 101 |     static T& getZero()              \
 | 
|---|
| 102 |     { return zero_; }                \
 | 
|---|
| 103 |   private:                           \
 | 
|---|
| 104 |     static T zero_;                  \
 | 
|---|
| 105 |   }
 | 
|---|
| 106 | 
 | 
|---|
| 107 | #ifdef BZ_HAVE_COMPLEX
 | 
|---|
| 108 |   BZZERO_DECLARE(complex<float>);
 | 
|---|
| 109 |   BZZERO_DECLARE(complex<double>);
 | 
|---|
| 110 |   BZZERO_DECLARE(complex<long double>);
 | 
|---|
| 111 | #endif // BZ_HAVE_COMPLEX
 | 
|---|
| 112 | 
 | 
|---|
| 113 | BZ_NAMESPACE_END
 | 
|---|
| 114 | 
 | 
|---|
| 115 | #include <blitz/zero.cc>
 | 
|---|
| 116 | 
 | 
|---|
| 117 | #endif // BZ_ZERO_H
 | 
|---|
| 118 | 
 | 
|---|