| [325390] | 1 | /*
|
|---|
| 2 | * Matrix.hpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: Jun 25, 2010
|
|---|
| 5 | * Author: crueger
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #ifndef MATRIX_HPP_
|
|---|
| 9 | #define MATRIX_HPP_
|
|---|
| 10 |
|
|---|
| [986ed3] | 11 | #include <iosfwd>
|
|---|
| [325390] | 12 | #include "defs.hpp"
|
|---|
| 13 |
|
|---|
| [cd82ec] | 14 | /**
|
|---|
| 15 | * Simple class to store matrices with a few extra functions
|
|---|
| 16 | */
|
|---|
| 17 |
|
|---|
| [325390] | 18 | class Vector;
|
|---|
| [fee079] | 19 | class MatrixContent;
|
|---|
| [325390] | 20 |
|
|---|
| 21 | class Matrix
|
|---|
| 22 | {
|
|---|
| 23 | friend Vector operator*(const Matrix&,const Vector&);
|
|---|
| 24 | public:
|
|---|
| 25 | Matrix();
|
|---|
| [cd82ec] | 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * construct a matrix from a 3x3 double array that contains all Elements.
|
|---|
| 29 | *
|
|---|
| 30 | * The elements are laid out in the following way:
|
|---|
| 31 | * array -> matrix
|
|---|
| 32 | * 0 -> (0,0)
|
|---|
| 33 | * 1 -> (1,0)
|
|---|
| 34 | * 2 -> (2,0)
|
|---|
| 35 | * 3 -> (0,1)
|
|---|
| 36 | * 4 -> (1,1)
|
|---|
| 37 | * 5 -> (2,1)
|
|---|
| 38 | * 6 -> (0,2)
|
|---|
| 39 | * 7 -> (1,2)
|
|---|
| 40 | * 8 -> (2,2)
|
|---|
| 41 | *
|
|---|
| 42 | */
|
|---|
| [325390] | 43 | Matrix(const double*);
|
|---|
| 44 | Matrix(const Matrix&);
|
|---|
| [3bc926] | 45 | Matrix(const MatrixContent&);
|
|---|
| [325390] | 46 | virtual ~Matrix();
|
|---|
| 47 |
|
|---|
| [cd82ec] | 48 | /**
|
|---|
| 49 | * Set the matrix to a unit matrix.
|
|---|
| 50 | */
|
|---|
| [9eb7580] | 51 | void setIdentity();
|
|---|
| [1da5f5] | 52 |
|
|---|
| [a439e5] | 53 | /**
|
|---|
| 54 | * Set all matrix entries to zero.
|
|---|
| 55 | */
|
|---|
| [9eb7580] | 56 | void setZero();
|
|---|
| [a439e5] | 57 |
|
|---|
| [31fb1d] | 58 | /**
|
|---|
| 59 | * Sets all matrix corresponding to a rotation matrix,
|
|---|
| 60 | * first around the x-, then the y-, finally the z-axis
|
|---|
| 61 | * with given angles.
|
|---|
| 62 | */
|
|---|
| [9eb7580] | 63 | void setRotation(const double x, const double y, const double z);
|
|---|
| [31fb1d] | 64 |
|
|---|
| [cd82ec] | 65 | /**
|
|---|
| 66 | * Access the matrix at index (i,j)
|
|---|
| 67 | */
|
|---|
| [325390] | 68 | double &at(size_t i, size_t j);
|
|---|
| [cd82ec] | 69 | /**
|
|---|
| 70 | * Access the matrix at index (i,j)
|
|---|
| 71 | */
|
|---|
| [436f04] | 72 | const double at(size_t i, size_t j) const;
|
|---|
| 73 |
|
|---|
| [cd82ec] | 74 | /**
|
|---|
| 75 | * Set the matrix at index (i,j).
|
|---|
| 76 | *
|
|---|
| 77 | * Slightly faster than at(i,j)=x
|
|---|
| 78 | */
|
|---|
| [436f04] | 79 | void set(size_t i, size_t j, const double value);
|
|---|
| [325390] | 80 |
|
|---|
| [3dbb9d] | 81 | /**
|
|---|
| 82 | * get the ith row of the matrix as a vector
|
|---|
| 83 | */
|
|---|
| 84 | Vector &row(size_t);
|
|---|
| 85 | const Vector &row(size_t) const;
|
|---|
| 86 |
|
|---|
| 87 | /**
|
|---|
| 88 | * get the ith column of the matrix as a vector
|
|---|
| 89 | */
|
|---|
| 90 | Vector &column(size_t);
|
|---|
| 91 | const Vector &column(size_t) const;
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * get the diagonal of the matrix as a vector
|
|---|
| 95 | */
|
|---|
| 96 | Vector &diagonal();
|
|---|
| 97 | const Vector &diagonal() const;
|
|---|
| 98 |
|
|---|
| [cd82ec] | 99 | /**
|
|---|
| 100 | * Calculate the determinant of the matrix
|
|---|
| 101 | */
|
|---|
| [cadbc1] | 102 | double determinant() const;
|
|---|
| [325390] | 103 |
|
|---|
| [cd82ec] | 104 | /**
|
|---|
| 105 | * Calculate the inverse of the matrix.
|
|---|
| 106 | *
|
|---|
| 107 | * Rather costly, so use precomputation as often as possible.
|
|---|
| 108 | */
|
|---|
| [cadbc1] | 109 | Matrix invert() const;
|
|---|
| [325390] | 110 |
|
|---|
| [a439e5] | 111 | /**
|
|---|
| 112 | * Diagonalizes a matrix and sets its rows to the resulting eigenvalues.
|
|---|
| 113 | * The eigenvalues are returned as a vector.
|
|---|
| 114 | *
|
|---|
| 115 | * Rather costly, so use precomputation as often as possible.
|
|---|
| 116 | */
|
|---|
| 117 | Vector transformToEigenbasis();
|
|---|
| 118 |
|
|---|
| 119 | /**
|
|---|
| 120 | * Calculate the transpose of the matrix.
|
|---|
| 121 | */
|
|---|
| 122 | Matrix transpose() const;
|
|---|
| [31fb1d] | 123 | Matrix &transpose();
|
|---|
| [a439e5] | 124 |
|
|---|
| [325390] | 125 | // operators
|
|---|
| 126 | Matrix &operator=(const Matrix&);
|
|---|
| 127 |
|
|---|
| [5b4605] | 128 | const Matrix &operator+=(const Matrix&);
|
|---|
| 129 | const Matrix &operator-=(const Matrix&);
|
|---|
| 130 | const Matrix &operator*=(const Matrix&);
|
|---|
| [325390] | 131 |
|
|---|
| [5b4605] | 132 | const Matrix &operator*=(const double);
|
|---|
| [325390] | 133 |
|
|---|
| [5b4605] | 134 | const Matrix operator+(const Matrix&) const;
|
|---|
| 135 | const Matrix operator-(const Matrix&) const;
|
|---|
| 136 | const Matrix operator*(const Matrix&) const;
|
|---|
| [325390] | 137 |
|
|---|
| [0eb2dc] | 138 | bool operator==(const Matrix&) const;
|
|---|
| 139 |
|
|---|
| [325390] | 140 | private:
|
|---|
| [fee079] | 141 | Matrix(MatrixContent*);
|
|---|
| [3dbb9d] | 142 | void createViews();
|
|---|
| [fee079] | 143 | MatrixContent *content;
|
|---|
| [8e17d6] | 144 | // we keep around some Vector views of the matrix, to return references
|
|---|
| [3dbb9d] | 145 | Vector* rows_ptr[NDIM];
|
|---|
| 146 | Vector* columns_ptr[NDIM];
|
|---|
| 147 | Vector* diagonal_ptr;
|
|---|
| [325390] | 148 | };
|
|---|
| 149 |
|
|---|
| [5b4605] | 150 | const Matrix operator*(const double,const Matrix&);
|
|---|
| 151 | const Matrix operator*(const Matrix&,const double);
|
|---|
| [325390] | 152 |
|
|---|
| [cd82ec] | 153 | /**
|
|---|
| 154 | * Takes a symmetric matrix that stores the lower diagonal and produces a
|
|---|
| 155 | * full matrix.
|
|---|
| 156 | *
|
|---|
| 157 | * The array is laid out as follows:
|
|---|
| 158 | *
|
|---|
| 159 | * array -> matrix
|
|---|
| 160 | * 0 -> (0,0)
|
|---|
| 161 | * 1 -> (1,0);(0,1)
|
|---|
| 162 | * 2 -> (1,1)
|
|---|
| 163 | * 3 -> (2,0);(0,2)
|
|---|
| 164 | * 4 -> (2,1);(1,2)
|
|---|
| 165 | * 5 -> (2,2)
|
|---|
| 166 | */
|
|---|
| [d10eb6] | 167 | Matrix ReturnFullMatrixforSymmetric(const double * const cell_size);
|
|---|
| 168 |
|
|---|
| [c49c96] | 169 | std::ostream &operator<<(std::ostream&,const Matrix&);
|
|---|
| [4b94bb] | 170 | Vector operator*(const Matrix&,const Vector&);
|
|---|
| 171 | Vector& operator*=(Vector&,const Matrix&);
|
|---|
| [c49c96] | 172 |
|
|---|
| [325390] | 173 | #endif /* MATRIX_HPP_ */
|
|---|