| 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 |
|
|---|
| 11 | #include <iosfwd>
|
|---|
| 12 | #include "defs.hpp"
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * Simple class to store matrices with a few extra functions
|
|---|
| 16 | */
|
|---|
| 17 |
|
|---|
| 18 | class Vector;
|
|---|
| 19 | class MatrixContent;
|
|---|
| 20 |
|
|---|
| 21 | class Matrix
|
|---|
| 22 | {
|
|---|
| 23 | friend Vector operator*(const Matrix&,const Vector&);
|
|---|
| 24 | public:
|
|---|
| 25 | Matrix();
|
|---|
| 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 | */
|
|---|
| 43 | Matrix(const double*);
|
|---|
| 44 | Matrix(const Matrix&);
|
|---|
| 45 | Matrix(const MatrixContent&);
|
|---|
| 46 | virtual ~Matrix();
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * Set the matrix to a unit matrix.
|
|---|
| 50 | */
|
|---|
| 51 | void setIdentity();
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * Set all matrix entries to zero.
|
|---|
| 55 | */
|
|---|
| 56 | void setZero();
|
|---|
| 57 |
|
|---|
| 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 | */
|
|---|
| 63 | void setRotation(const double x, const double y, const double z);
|
|---|
| 64 |
|
|---|
| 65 | /**
|
|---|
| 66 | * Access the matrix at index (i,j)
|
|---|
| 67 | */
|
|---|
| 68 | double &at(size_t i, size_t j);
|
|---|
| 69 | /**
|
|---|
| 70 | * Access the matrix at index (i,j)
|
|---|
| 71 | */
|
|---|
| 72 | const double at(size_t i, size_t j) const;
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * Set the matrix at index (i,j).
|
|---|
| 76 | *
|
|---|
| 77 | * Slightly faster than at(i,j)=x
|
|---|
| 78 | */
|
|---|
| 79 | void set(size_t i, size_t j, const double value);
|
|---|
| 80 |
|
|---|
| 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 |
|
|---|
| 99 | /**
|
|---|
| 100 | * Calculate the determinant of the matrix
|
|---|
| 101 | */
|
|---|
| 102 | double determinant() const;
|
|---|
| 103 |
|
|---|
| 104 | /**
|
|---|
| 105 | * Calculate the inverse of the matrix.
|
|---|
| 106 | *
|
|---|
| 107 | * Rather costly, so use precomputation as often as possible.
|
|---|
| 108 | */
|
|---|
| 109 | Matrix invert() const;
|
|---|
| 110 |
|
|---|
| 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;
|
|---|
| 123 | Matrix &transpose();
|
|---|
| 124 |
|
|---|
| 125 | // operators
|
|---|
| 126 | Matrix &operator=(const Matrix&);
|
|---|
| 127 |
|
|---|
| 128 | const Matrix &operator+=(const Matrix&);
|
|---|
| 129 | const Matrix &operator-=(const Matrix&);
|
|---|
| 130 | const Matrix &operator*=(const Matrix&);
|
|---|
| 131 |
|
|---|
| 132 | const Matrix &operator*=(const double);
|
|---|
| 133 |
|
|---|
| 134 | const Matrix operator+(const Matrix&) const;
|
|---|
| 135 | const Matrix operator-(const Matrix&) const;
|
|---|
| 136 | const Matrix operator*(const Matrix&) const;
|
|---|
| 137 |
|
|---|
| 138 | bool operator==(const Matrix&) const;
|
|---|
| 139 |
|
|---|
| 140 | private:
|
|---|
| 141 | Matrix(MatrixContent*);
|
|---|
| 142 | void createViews();
|
|---|
| 143 | MatrixContent *content;
|
|---|
| 144 | // we keep around some Vector views of the matrix, to return references
|
|---|
| 145 | Vector* rows_ptr[NDIM];
|
|---|
| 146 | Vector* columns_ptr[NDIM];
|
|---|
| 147 | Vector* diagonal_ptr;
|
|---|
| 148 | };
|
|---|
| 149 |
|
|---|
| 150 | const Matrix operator*(const double,const Matrix&);
|
|---|
| 151 | const Matrix operator*(const Matrix&,const double);
|
|---|
| 152 |
|
|---|
| 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 | */
|
|---|
| 167 | Matrix ReturnFullMatrixforSymmetric(const double * const cell_size);
|
|---|
| 168 |
|
|---|
| 169 | std::ostream &operator<<(std::ostream&,const Matrix&);
|
|---|
| 170 | Vector operator*(const Matrix&,const Vector&);
|
|---|
| 171 | Vector& operator*=(Vector&,const Matrix&);
|
|---|
| 172 |
|
|---|
| 173 | #endif /* MATRIX_HPP_ */
|
|---|