[73298b]: core / vnl / vnl_diag_matrix.h Maximize Restore History

Download this file

vnl_diag_matrix.h    258 lines (216 with data), 7.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// This is vxl/vnl/vnl_diag_matrix.h
#ifndef vnl_diag_matrix_h_
#define vnl_diag_matrix_h_
#ifdef VCL_NEEDS_PRAGMA_INTERFACE
#pragma interface
#endif
//:
// \file
// \brief Contains class for diagonal matrices
// \author Andrew W. Fitzgibbon (Oxford RRG)
// \date 5/8/96
//
// \verbatim
// Modifications
// IMS (Manchester) 16/03/2001: Tidied up the documentation + added binary_io
// Feb.2002 - Peter Vanroose - brief doxygen comment placed on single line
// \endverbatim
#include <vcl_cassert.h>
#include <vnl/vnl_vector.h>
#include <vnl/vnl_matrix.h>
//: stores a diagonal matrix as a single vector.
// vnl_diag_matrix stores a diagonal matrix for time and space efficiency.
// Specifically, only the diagonal elements are stored, and some matrix
// operations (currently *, + and -) are overloaded to use more efficient
// algorithms.
export
template <class T>
class vnl_diag_matrix
{
public:
vnl_diag_matrix() {}
//: Construct an empty diagonal matrix.
vnl_diag_matrix(unsigned nn) : diagonal_(nn) {}
//: Construct a diagonal matrix with diagonal elements equal to value.
vnl_diag_matrix(unsigned nn, T const& value) : diagonal_(nn, value) {}
//: Construct a diagonal matrix from a vnl_vector.
// The vector elements become the diagonal elements.
vnl_diag_matrix(vnl_vector<T> const& that): diagonal_(that) {}
~vnl_diag_matrix() {}
vnl_diag_matrix& operator=(vnl_diag_matrix<T> const& that) {
this->diagonal_ = that.diagonal_;
return *this;
}
// Operations----------------------------------------------------------------
//: In-place arithmetic operation
vnl_diag_matrix<T>& operator*=(T v) { diagonal_ *= v; return *this; }
//: In-place arithmetic operation
vnl_diag_matrix<T>& operator/=(T v) { diagonal_ /= v; return *this; }
// Computations--------------------------------------------------------------
void invert_in_place();
T determinant() const;
vnl_vector<T> solve(vnl_vector<T> const& b);
void solve(vnl_vector<T> const& b, vnl_vector<T>* out);
// Data Access---------------------------------------------------------------
T operator () (unsigned i, unsigned j) const {
return (i != j) ? T(0) : diagonal_[i];
}
T& operator () (unsigned i, unsigned j) {
assert(i == j);
return diagonal_[i];
}
T& operator() (unsigned i) { return diagonal_[i]; }
T const& operator() (unsigned i) const { return diagonal_[i]; }
T& operator[] (unsigned i) { return diagonal_[i]; }
T const& operator[] (unsigned i) const { return diagonal_[i]; }
// iterators
typedef typename vnl_vector<T>::iterator iterator;
inline iterator begin() { return diagonal_.begin(); }
inline iterator end() { return diagonal_.end(); }
typedef typename vnl_vector<T>::const_iterator const_iterator;
inline const_iterator begin() const { return diagonal_.begin(); }
inline const_iterator end() const { return diagonal_.end(); }
unsigned size() const { return diagonal_.size(); }
unsigned rows() const { return diagonal_.size(); }
unsigned cols() const { return diagonal_.size(); }
unsigned columns() const { return diagonal_.size(); }
// Need this until we add a vnl_diag_matrix ctor to vnl_matrix;
inline vnl_matrix<T> asMatrix() const;
void resize(int n) { diagonal_.resize(n); }
void clear() { diagonal_.clear(); }
void fill(T const &x) { diagonal_.fill(x); }
//: Return pointer to the diagonal elements as a contiguous 1D C array;
T* data_block() { return diagonal_.data_block(); }
T const* data_block() const { return diagonal_.data_block(); }
//: Return diagonal elements as a vector
vnl_vector<T> const& diagonal() const { return diagonal_; }
//: Set diagonal elements using vector
void set(vnl_vector<T> const& v) { diagonal_=v; }
protected:
vnl_vector<T> diagonal_;
private:
#if VCL_NEED_FRIEND_FOR_TEMPLATE_OVERLOAD
friend vnl_vector<T> operator*(vnl_diag_matrix<T> const&,vnl_vector<T> const&);
#endif
};
template <class T>
vcl_ostream& operator<< (vcl_ostream&, vnl_diag_matrix<T> const&);
template <class T> vcl_ostream& operator<< (vcl_ostream&, vnl_diag_matrix<T> const&);
//: Convert a vnl_diag_matrix to a Matrix.
template <class T>
inline vnl_matrix<T> vnl_diag_matrix<T>::asMatrix() const
{
unsigned len = diagonal_.size();
vnl_matrix<T> ret(len, len);
for (unsigned i = 0; i < len; ++i)
{
unsigned j;
for (j = 0; j < i; ++j)
ret(i,j) = T(0);
for (j = i+1; j < len; ++j)
ret(i,j) = T(0);
ret(i,i) = diagonal_[i];
}
return ret;
}
//: Invert a vnl_diag_matrix in-situ.
// Just replaces each element with its reciprocal.
template <class T>
inline void vnl_diag_matrix<T>::invert_in_place()
{
unsigned len = diagonal_.size();
T* d = data_block();
T one = T(1);
for (unsigned i = 0; i < len; ++i)
d[i] = one / d[i];
}
//: Return determinant as product of diagonal values.
template <class T>
inline T vnl_diag_matrix<T>::determinant() const
{
T det = T(1);
T const* d = data_block();
unsigned len = diagonal_.size();
for (unsigned i = 0; i < len; ++i)
det *= d[i];
return det;
}
//: Multiply a Matrix by a vnl_diag_matrix. Just scales the columns - mn flops
template <class T>
inline vnl_matrix<T> operator* (vnl_matrix<T> const& A, vnl_diag_matrix<T> const& D)
{
vnl_matrix<T> ret(A.rows(), A.columns());
for (unsigned i = 0; i < A.rows(); ++i)
for (unsigned j = 0; j < A.columns(); ++j)
ret(i,j) = A(i,j) * D(j,j);
return ret;
}
//: Multiply a vnl_diag_matrix by a Matrix. Just scales the rows - mn flops
template <class T>
inline vnl_matrix<T> operator* (vnl_diag_matrix<T> const& D, vnl_matrix<T> const& A)
{
vnl_matrix<T> ret(A.rows(), A.columns());
T const* d = D.data_block();
for (unsigned i = 0; i < A.rows(); ++i)
for (unsigned j = 0; j < A.columns(); ++j)
ret(i,j) = A(i,j) * d[i];
return ret;
}
//: Add a vnl_diag_matrix to a Matrix. n adds, mn copies.
template <class T>
inline vnl_matrix<T> operator + (vnl_matrix<T> const& A, vnl_diag_matrix<T> const& D)
{
const unsigned n = D.size();
vnl_matrix<T> ret(A);
T const* d = D.data_block();
for (unsigned j = 0; j < n; ++j)
ret(j,j) += d[j];
return ret;
}
//: Add a Matrix to a vnl_diag_matrix. n adds, mn copies.
template <class T>
inline vnl_matrix<T> operator + (vnl_diag_matrix<T> const& D, vnl_matrix<T> const& A)
{
return A + D;
}
//: Subtract a vnl_diag_matrix from a Matrix. n adds, mn copies.
template <class T>
inline vnl_matrix<T> operator - (vnl_matrix<T> const& A, vnl_diag_matrix<T> const& D)
{
const unsigned n = D.size();
vnl_matrix<T> ret(A);
T const* d = D.data_block();
for (unsigned j = 0; j < n; ++j)
ret(j,j) -= d[j];
return ret;
}
//: Subtract a Matrix from a vnl_diag_matrix. n adds, mn copies.
template <class T>
inline vnl_matrix<T> operator - (vnl_diag_matrix<T> const& D, vnl_matrix<T> const& A)
{
const unsigned n = D.size();
vnl_matrix<T> ret(n, n);
T const* d = D.data_block();
for (unsigned i = 0; i < n; ++i)
{
for (unsigned j = 0; j < i; ++j)
ret(i,j) = -A(i,j);
for (unsigned j = i+1; j < n; ++j)
ret(i,j) = -A(i,j);
ret(i,i) = d[i] - A(i,i);
}
return ret;
}
//: Multiply a vnl_diag_matrix by a Vector. n flops.
template <class T>
inline vnl_vector<T> operator* (vnl_diag_matrix<T> const& D, vnl_vector<T> const& A)
{
return element_product(D.diagonal(), A);
}
//: Multiply a Vector by a vnl_diag_matrix. n flops.
template <class T>
inline vnl_vector<T> operator* (vnl_vector<T> const& A, vnl_diag_matrix<T> const& D)
{
return element_product(D.diagonal(), A);
}
#endif // vnl_diag_matrix_h_