| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | /* | 
|---|
| 9 | * Eigenspace.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Nov 22, 2010 | 
|---|
| 12 | *      Author: heber | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | // include config.h | 
|---|
| 16 | #ifdef HAVE_CONFIG_H | 
|---|
| 17 | #include <config.h> | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 21 |  | 
|---|
| 22 | #include <iostream> | 
|---|
| 23 | #include <boost/foreach.hpp> | 
|---|
| 24 |  | 
|---|
| 25 | #include "CodePatterns/Assert.hpp" | 
|---|
| 26 | #include "CodePatterns/Log.hpp" | 
|---|
| 27 | #include "CodePatterns/Verbose.hpp" | 
|---|
| 28 |  | 
|---|
| 29 | #include "Eigenspace.hpp" | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | /** Constructor for Eigenspace. | 
|---|
| 33 | * @param _Indices index set for columns and rows of EigenspaceMatrix. | 
|---|
| 34 | */ | 
|---|
| 35 | Eigenspace::Eigenspace(const indexset &_Indices) : | 
|---|
| 36 | Indices(_Indices), | 
|---|
| 37 | EigenspaceMatrix(_Indices.size(),_Indices.size()), | 
|---|
| 38 | EigenvectorMatrix(_Indices.size(),_Indices.size()) | 
|---|
| 39 | { | 
|---|
| 40 | EigenvectorMatrix.setIdentity(); | 
|---|
| 41 | for (size_t i = 0 ; i< _Indices.size(); ++i) | 
|---|
| 42 | Eigenvalues.push_back(0.); | 
|---|
| 43 | createEigenvectorViews(); | 
|---|
| 44 | } | 
|---|
| 45 |  | 
|---|
| 46 |  | 
|---|
| 47 | /** Constructor for Eigenspace. | 
|---|
| 48 | * @param _Indices index set for columns and rows of EigenspaceMatrix. | 
|---|
| 49 | * @param _matrix matrix to use | 
|---|
| 50 | */ | 
|---|
| 51 | Eigenspace::Eigenspace(const indexset &_Indices, const MatrixContent &_matrix) : | 
|---|
| 52 | Indices(_Indices), | 
|---|
| 53 | EigenspaceMatrix(_matrix), | 
|---|
| 54 | EigenvectorMatrix(_matrix.getRows(), _matrix.getColumns()) | 
|---|
| 55 | { | 
|---|
| 56 | ASSERT(_Indices.size() == _matrix.getRows(), | 
|---|
| 57 | "Eigenspace::Eigenspace() - number of indices "+toString(_Indices.size()) | 
|---|
| 58 | +" and row dimension of matrix "+toString(_matrix.getRows())+" don't match!"); | 
|---|
| 59 | ASSERT(_Indices.size() == _matrix.getColumns(), | 
|---|
| 60 | "Eigenspace::Eigenspace() - number of indices "+toString(_Indices.size()) | 
|---|
| 61 | +" and column dimension of matrix "+toString(_matrix.getColumns())+" don't match!"); | 
|---|
| 62 |  | 
|---|
| 63 | EigenvectorMatrix.setIdentity(); | 
|---|
| 64 | for (size_t i = 0 ; i< _Indices.size(); ++i) | 
|---|
| 65 | Eigenvalues.push_back(0.); | 
|---|
| 66 | createEigenvectorViews(); | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 |  | 
|---|
| 70 | /** Destructor for Eigenspace. | 
|---|
| 71 | * | 
|---|
| 72 | */ | 
|---|
| 73 | Eigenspace::~Eigenspace() | 
|---|
| 74 | {} | 
|---|
| 75 |  | 
|---|
| 76 |  | 
|---|
| 77 | /** Create a number of eigenvectors in Eigenspace::Eigenvectors. | 
|---|
| 78 | * These are views on the resepctive column vectors in | 
|---|
| 79 | * Eigenspace::EigenvectorMatrix. | 
|---|
| 80 | */ | 
|---|
| 81 | void Eigenspace::createEigenvectorViews() | 
|---|
| 82 | { | 
|---|
| 83 | const size_t ColumnCount = EigenvectorMatrix.getColumns(); | 
|---|
| 84 | for (size_t iter = 0; iter < ColumnCount; ++iter) { | 
|---|
| 85 | boost::shared_ptr<VectorContent> ev(EigenvectorMatrix.getColumnVector(iter)); | 
|---|
| 86 | Eigenvectors.push_back( ev ); | 
|---|
| 87 | DoLog(1) && (Log() << Verbose(1) << iter << " th Eigenvector is " << *ev << std::endl); | 
|---|
| 88 | } | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 |  | 
|---|
| 92 | /** Obtains the eigenvectors to Eigenspace::EigenspaceMatrix. | 
|---|
| 93 | * We diagonalize the matrix and set the Eigenspace::Eigenvectors and | 
|---|
| 94 | * Eigenspace::Eigenvalues. | 
|---|
| 95 | */ | 
|---|
| 96 | void Eigenspace::calculateEigenSubspace() | 
|---|
| 97 | { | 
|---|
| 98 | ASSERT(0, "Eigenspace::calculateEigenSubspace() - we never want to call this function!"); | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 |  | 
|---|
| 102 | /** Getter for constant reference to Eigenspace::EigenspaceMatrix. | 
|---|
| 103 | * | 
|---|
| 104 | * @return Eigenspace::EigenspaceMatrix | 
|---|
| 105 | */ | 
|---|
| 106 | const MatrixContent & Eigenspace::getEigenspaceMatrix() const | 
|---|
| 107 | { | 
|---|
| 108 | return EigenspaceMatrix; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 |  | 
|---|
| 112 | /** Setter for Eigenspace::EigenspaceMatrix. | 
|---|
| 113 | * | 
|---|
| 114 | * @param _content matrix to use | 
|---|
| 115 | */ | 
|---|
| 116 | void Eigenspace::setEigenspaceMatrix(const MatrixContent &_content) | 
|---|
| 117 | { | 
|---|
| 118 | EigenspaceMatrix = _content; | 
|---|
| 119 | EigenvectorMatrix.setZero(); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 |  | 
|---|
| 123 | const VectorContent & Eigenspace::getEigenvector(const size_t i) const | 
|---|
| 124 | { | 
|---|
| 125 | return *(Eigenvectors[i]); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | /** Checks whether each index of _indexset is contained in this subspace:Indices. | 
|---|
| 129 | * | 
|---|
| 130 | * @param _subspace subspace set to check | 
|---|
| 131 | * @return true - _index is contained, false - at least index is not contained | 
|---|
| 132 | */ | 
|---|
| 133 | bool Eigenspace::contains(const Eigenspace &_subspace) | 
|---|
| 134 | { | 
|---|
| 135 | BOOST_FOREACH(size_t iter, _subspace.getIndices()) { | 
|---|
| 136 | if (Indices.count(iter) == 0) {  // index not present, then not contained | 
|---|
| 137 | return false; | 
|---|
| 138 | } | 
|---|
| 139 | } | 
|---|
| 140 | return true; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 |  | 
|---|
| 144 | /** Getter for Eigenspace::Indices. | 
|---|
| 145 | * | 
|---|
| 146 | * @return indexset of this eigenspace | 
|---|
| 147 | */ | 
|---|
| 148 | const Eigenspace::indexset & Eigenspace::getIndices() const | 
|---|
| 149 | { | 
|---|
| 150 | return Indices; | 
|---|
| 151 | } | 
|---|
| 152 |  | 
|---|
| 153 | const MatrixContent & Eigenspace::getEigenvectorMatrix() const | 
|---|
| 154 | { | 
|---|
| 155 | return EigenvectorMatrix; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | const double Eigenspace::getEigenvalue(const size_t i) const | 
|---|
| 159 | { | 
|---|
| 160 | return Eigenvalues[i]; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | const Eigenspace::eigenvalueset & Eigenspace::getEigenvalues() const | 
|---|
| 164 | { | 
|---|
| 165 | return Eigenvalues; | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 |  | 
|---|
| 169 | /** Sets the eigenvectors and -values. | 
|---|
| 170 | * | 
|---|
| 171 | * We only implement this for eigenpairs, not eigenvector and eigenvalue | 
|---|
| 172 | * distinctly to avoid mismatches, i.e. old eigenvalues used because only new | 
|---|
| 173 | * eigenvectors have been given. | 
|---|
| 174 | * | 
|---|
| 175 | * @param CurrentEigenvectors eigenvectors to set to | 
|---|
| 176 | * @param CurrentEigenvalues eigenvalues to set to | 
|---|
| 177 | */ | 
|---|
| 178 | void Eigenspace::setEigenpairs(const eigenvectorset &CurrentEigenvectors, const eigenvalueset &CurrentEigenvalues) | 
|---|
| 179 | { | 
|---|
| 180 | ASSERT(CurrentEigenvectors.size() == Eigenvectors.size(), | 
|---|
| 181 | "Eigenspace::setEigenpairs() - " | 
|---|
| 182 | +toString(CurrentEigenvectors.size())+" x CurrentEigenvectors is not the same as " | 
|---|
| 183 | +toString(Eigenvectors.size())+"x Eigenvectors!"); | 
|---|
| 184 | // set eigenvectors | 
|---|
| 185 | std::vector<boost::shared_ptr<VectorContent> >::const_iterator newev = CurrentEigenvectors.begin(); | 
|---|
| 186 | for(std::vector<boost::shared_ptr<VectorContent> >::iterator ev = Eigenvectors.begin(); | 
|---|
| 187 | ev != Eigenvectors.end(); | 
|---|
| 188 | ++ev, ++newev) { | 
|---|
| 189 | *ev = *newev; | 
|---|
| 190 | } | 
|---|
| 191 | // eigenvalues can be just copied (integral type) | 
|---|
| 192 | Eigenvalues.clear(); | 
|---|
| 193 | Eigenvalues = CurrentEigenvalues; | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 |  | 
|---|
| 197 | /** Output operator for Eigenspace. | 
|---|
| 198 | * | 
|---|
| 199 | * @param ost output stream | 
|---|
| 200 | * @param _s Eigenspace to print: Indices and contained matrix | 
|---|
| 201 | * @return given output stream | 
|---|
| 202 | */ | 
|---|
| 203 | std::ostream & operator<<(std::ostream &ost, const Eigenspace &_s) | 
|---|
| 204 | { | 
|---|
| 205 | BOOST_FOREACH(size_t iter, _s.Indices) { | 
|---|
| 206 | ost << iter << " "; | 
|---|
| 207 | } | 
|---|
| 208 | ost << " with matrix " << _s.EigenspaceMatrix; | 
|---|
| 209 | return ost; | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|