1 #ifndef VTESTBED_LINALG_LINEAR_ALGEBRA_HH 2 #define VTESTBED_LINALG_LINEAR_ALGEBRA_HH 6 #include <vtestbed/base/blitz.hh> 7 #include <vtestbed/linalg/types.hh> 24 inline float conj(
float x) {
return x; }
25 inline float conj(
double x) {
return x; }
34 class Vector:
public blitz::Array<T,1> {
37 typedef blitz::Array<T,1> base_type;
40 using base_type::base_type;
41 using base_type::operator=;
42 using base_type::operator();
51 class Matrix:
public blitz::Array<T,2> {
54 typedef blitz::Array<T,2> base_type;
57 using base_type::base_type;
58 using base_type::operator=;
66 return this->rows() == this->cols();
73 is_symmetric(T eps)
const;
84 is_toeplitz(T eps)
const;
95 tmp.base_type::transpose(1, 0);
101 this->base_type::transposeSelf(1, 0);
123 using base_type::base_type;
124 using base_type::operator=;
125 using base_type::operator();
166 using base_type::base_type;
167 using base_type::operator=;
168 using base_type::operator();
185 using base_type::base_type;
186 using base_type::operator=;
187 using base_type::operator();
190 cholesky_indefinite()
const;
212 using base_type::base_type;
213 using base_type::operator=;
214 using base_type::operator();
236 int _npermutations = 0;
239 using base_type::base_type;
240 using base_type::operator=;
241 using base_type::operator();
244 permutations()
const noexcept {
245 return this->_permutations;
262 lower_upper_triangular_self();
271 lower_upper_triangular(
const Matrix<T>& rhs) {
274 tmp.lower_upper_triangular_self();
278 template <
class NewMatrix,
class OldMatrix>
279 inline const NewMatrix&
280 matrix_cast(
const OldMatrix& rhs) {
281 return static_cast<const NewMatrix&>(rhs);
284 template <
class NewMatrix,
class OldMatrix>
286 matrix_cast(OldMatrix& rhs) {
287 return static_cast<NewMatrix&>(rhs);
292 product(
const Matrix<T>& lhs,
const Vector<T>& rhs);
296 operator*(
const Matrix<T>& lhs,
const Vector<T>& rhs) {
297 return product<T>(lhs, rhs);
302 product(
const Matrix<T>& lhs,
const Matrix<T>& rhs);
306 operator*(
const Matrix<T>& lhs,
const Matrix<T>& rhs) {
307 return product<T>(lhs, rhs);
312 det(
const Lower_upper_triangular_matrix<T>& rhs) {
313 return rhs.determinant();
318 det(
const Square_matrix<T>& rhs) {
319 return det(lower_upper_triangular(rhs));
324 inverse(
const Lower_upper_triangular_matrix<T>& rhs) {
325 return rhs.inverse();
330 inverse(
const Square_matrix<T>& rhs) {
331 return inverse(lower_upper_triangular(rhs));
336 trace(
const Square_matrix<T>& rhs) {
338 const int n = rhs.nrows();
339 for (
int i=0; i<n; ++i) {
346 inline Square_matrix<T>
347 Matrix<T>::inverse()
const {
348 return lower_upper_triangular(*this).inverse();
352 inline Square_matrix<T>&
353 Matrix<T>::inverse_self() {
354 this->reference(lower_upper_triangular(*this).inverse());
355 return matrix_cast<Square_matrix<T>>(*this);
361 const Lower_upper_triangular_matrix<T>& lhs,
364 return lhs.solve(rhs);
369 solve(
const Square_matrix<T>& lhs,
const Vector<T>& rhs) {
370 return solve(lower_upper_triangular(lhs), rhs);
375 solve(
const Symmetric_matrix<T>& lhs,
const Vector<T>& rhs) {
376 return lhs.cholesky_indefinite().solve(rhs);
381 solve(
const Positive_definite_matrix<T>& lhs,
const Vector<T>& rhs) {
382 return lhs.cholesky().solve(rhs);
387 eigen_values(
const Symmetric_matrix<T>& rhs, T eps,
int nsweeps) {
388 return rhs.eigen_values(eps, nsweeps);
395 #endif // vim:filetype=cpp
Vector< T > backward_substitution(const Vector< T > &y) const
Backward substitution.
bool is_positive_definite() const
Vector< T > eigen_values(T eps, int nsweeps) const
Compute eigen values.
Lower_triangular_matrix< T > cholesky() const
Cholesky decomposition.
Vector< T > forward_substitution(const Vector< T > &b) const
Forward substitution.