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