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