1 | /*************************************************************************** |
---|
2 | * blitz/matdiag.h Declarations for Diagonal matrices |
---|
3 | * |
---|
4 | * $Id: matdiag.h,v 1.4 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 | #ifndef BZ_MATDIAG_H |
---|
27 | #define BZ_MATDIAG_H |
---|
28 | |
---|
29 | #ifndef BZ_MSTRUCT_H |
---|
30 | #error <blitz/matdiag.h> must be included via <blitz/mstruct.h> |
---|
31 | #endif |
---|
32 | |
---|
33 | BZ_NAMESPACE(blitz) |
---|
34 | |
---|
35 | // Diagonal matrix |
---|
36 | // [ 0 . . . ] |
---|
37 | // [ . 1 . . ] |
---|
38 | // [ . . 2 . ] |
---|
39 | // [ . . . 3 ] |
---|
40 | |
---|
41 | class DiagonalIterator { |
---|
42 | public: |
---|
43 | DiagonalIterator(const unsigned rows,const unsigned cols) { |
---|
44 | BZPRECONDITION(rows==cols); |
---|
45 | size_ = rows; |
---|
46 | i_ = 0; |
---|
47 | } |
---|
48 | |
---|
49 | operator bool() const { return i_ < size_; } |
---|
50 | |
---|
51 | void operator++() { ++i_; } |
---|
52 | |
---|
53 | unsigned row() const { return i_; } |
---|
54 | unsigned col() const { return i_; } |
---|
55 | unsigned offset() const { return i_; } |
---|
56 | |
---|
57 | protected: |
---|
58 | unsigned i_, size_; |
---|
59 | }; |
---|
60 | |
---|
61 | class Diagonal : public MatrixStructure { |
---|
62 | public: |
---|
63 | typedef DiagonalIterator T_iterator; |
---|
64 | |
---|
65 | Diagonal(): size_(0) { } |
---|
66 | |
---|
67 | Diagonal(const unsigned size): size_(size) { } |
---|
68 | |
---|
69 | Diagonal(const unsigned rows,const unsigned cols): size_(rows) { |
---|
70 | BZPRECONDITION(rows == cols); |
---|
71 | } |
---|
72 | |
---|
73 | unsigned columns() const { return size_; } |
---|
74 | |
---|
75 | unsigned coordToOffset(const unsigned i,const unsigned j) const |
---|
76 | { |
---|
77 | BZPRECONDITION(inRange(i,j)); |
---|
78 | BZPRECONDITION(i == j); |
---|
79 | return i; |
---|
80 | } |
---|
81 | |
---|
82 | unsigned firstInRow(const unsigned i) const { return i; } |
---|
83 | |
---|
84 | template<typename T_numtype> |
---|
85 | T_numtype get(const T_numtype * restrict data,const unsigned i,const unsigned j) const |
---|
86 | { |
---|
87 | BZPRECONDITION(inRange(i,j)); |
---|
88 | return (i==j) ? data[coordToOffset(i,j)] : ZeroElement<T_numtype>::zero(); |
---|
89 | } |
---|
90 | |
---|
91 | template<typename T_numtype> |
---|
92 | T_numtype& get(T_numtype * restrict data,const unsigned i,const unsigned j) { |
---|
93 | BZPRECONDITION(inRange(i,j)); |
---|
94 | return (i==j) ? data[coordToOffset(i,j)] : ZeroElement<T_numtype>::zero(); |
---|
95 | } |
---|
96 | |
---|
97 | unsigned lastInRow(const unsigned i) const { return i; } |
---|
98 | unsigned firstInCol(const unsigned j) const { return j; } |
---|
99 | unsigned lastInCol(const unsigned j) const { return j; } |
---|
100 | |
---|
101 | bool inRange(const unsigned i,const unsigned j) const { |
---|
102 | return (i < size_) && (j < size_); |
---|
103 | } |
---|
104 | |
---|
105 | unsigned numElements() const { return size_; } |
---|
106 | unsigned rows() const { return size_; } |
---|
107 | |
---|
108 | void resize(const unsigned size) { size_ = size; } |
---|
109 | |
---|
110 | void resize(const unsigned rows,const unsigned cols) { |
---|
111 | BZPRECONDITION(rows == cols); |
---|
112 | size_ = rows; |
---|
113 | } |
---|
114 | |
---|
115 | private: |
---|
116 | unsigned size_; |
---|
117 | }; |
---|
118 | |
---|
119 | BZ_NAMESPACE_END |
---|
120 | |
---|
121 | #endif // BZ_MATSYMM_H |
---|