| 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 "Helpers/MemDebug.hpp"
|
|---|
| 21 |
|
|---|
| 22 | #include <iostream>
|
|---|
| 23 | #include <boost/foreach.hpp>
|
|---|
| 24 |
|
|---|
| 25 | #include "Helpers/Assert.hpp"
|
|---|
| 26 | #include "Helpers/Log.hpp"
|
|---|
| 27 | #include "Helpers/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 | createEigenvectorViews();
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 | /** Constructor for Eigenspace.
|
|---|
| 45 | * @param _Indices index set for columns and rows of EigenspaceMatrix.
|
|---|
| 46 | * @param _matrix matrix to use
|
|---|
| 47 | */
|
|---|
| 48 | Eigenspace::Eigenspace(const indexset &_Indices, const MatrixContent &_matrix) :
|
|---|
| 49 | Indices(_Indices),
|
|---|
| 50 | EigenspaceMatrix(_matrix),
|
|---|
| 51 | EigenvectorMatrix(_matrix.getRows(), _matrix.getColumns())
|
|---|
| 52 | {
|
|---|
| 53 | ASSERT(_Indices.size() == _matrix.getRows(),
|
|---|
| 54 | "Eigenspace::Eigenspace() - number of indices "+toString(_Indices.size())
|
|---|
| 55 | +" and row dimension of matrix "+toString(_matrix.getRows())+" don't match!");
|
|---|
| 56 | ASSERT(_Indices.size() == _matrix.getColumns(),
|
|---|
| 57 | "Eigenspace::Eigenspace() - number of indices "+toString(_Indices.size())
|
|---|
| 58 | +" and column dimension of matrix "+toString(_matrix.getColumns())+" don't match!");
|
|---|
| 59 |
|
|---|
| 60 | createEigenvectorViews();
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 | /** Destructor for Eigenspace.
|
|---|
| 65 | *
|
|---|
| 66 | */
|
|---|
| 67 | Eigenspace::~Eigenspace()
|
|---|
| 68 | {}
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 | /** Create a number of eigenvectors in Eigenspace::Eigenvectors.
|
|---|
| 72 | * These are views on the resepctive column vectors in
|
|---|
| 73 | * Eigenspace::EigenvectorMatrix.
|
|---|
| 74 | */
|
|---|
| 75 | void Eigenspace::createEigenvectorViews()
|
|---|
| 76 | {
|
|---|
| 77 | const size_t ColumnCount = EigenspaceMatrix.getColumns();
|
|---|
| 78 | for (size_t iter = 0; iter < ColumnCount; ++iter) {
|
|---|
| 79 | boost::shared_ptr<VectorContent> ev(EigenspaceMatrix.getColumnVector(iter));
|
|---|
| 80 | Eigenvectors.push_back( ev );
|
|---|
| 81 | Log() << Verbose(1) << iter << " th Eigenvector is " << *ev << std::endl;
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 | /** Obtains the eigenvectors to Eigenspace::EigenspaceMatrix.
|
|---|
| 87 | * We diagonalize the matrix and set the Eigenspace::Eigenvectors and
|
|---|
| 88 | * Eigenspace::Eigenvalues.
|
|---|
| 89 | */
|
|---|
| 90 | void Eigenspace::calculateEigenSubspace()
|
|---|
| 91 | {
|
|---|
| 92 | EigenvectorMatrix = EigenspaceMatrix;
|
|---|
| 93 | VectorContent *Eigenvalues = new VectorContent(EigenvectorMatrix.transformToEigenbasis());
|
|---|
| 94 | Log() << Verbose(2) << "Eigenvector matrix is " << EigenvectorMatrix << std::endl;
|
|---|
| 95 | Log() << Verbose(2) << "Eigenvalues are " << *Eigenvalues << std::endl;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 | /** Getter for constant reference to Eigenspace::EigenspaceMatrix.
|
|---|
| 100 | *
|
|---|
| 101 | * @return Eigenspace::EigenspaceMatrix
|
|---|
| 102 | */
|
|---|
| 103 | const MatrixContent & Eigenspace::getEigenspaceMatrix() const
|
|---|
| 104 | {
|
|---|
| 105 | return EigenspaceMatrix;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 | /** Setter for Eigenspace::EigenspaceMatrix.
|
|---|
| 110 | *
|
|---|
| 111 | * @param _content matrix to use
|
|---|
| 112 | */
|
|---|
| 113 | void Eigenspace::setEigenspaceMatrix(const MatrixContent &_content)
|
|---|
| 114 | {
|
|---|
| 115 | EigenspaceMatrix = _content;
|
|---|
| 116 | EigenvectorMatrix.setZero();
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 | const VectorContent & Eigenspace::getEigenvector(size_t i) const
|
|---|
| 121 | {
|
|---|
| 122 | return *(Eigenvectors[i]);
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /** Checks whether each index of _indexset is contained in this subspace:Indices.
|
|---|
| 126 | *
|
|---|
| 127 | * @param _subspace subspace set to check
|
|---|
| 128 | * @return true - _index is contained, false - at least index is not contained
|
|---|
| 129 | */
|
|---|
| 130 | bool Eigenspace::contains(const Eigenspace &_subspace)
|
|---|
| 131 | {
|
|---|
| 132 | BOOST_FOREACH(size_t iter, _subspace.getIndices()) {
|
|---|
| 133 | if (Indices.count(iter) == 0) { // index not present, then not contained
|
|---|
| 134 | return false;
|
|---|
| 135 | }
|
|---|
| 136 | }
|
|---|
| 137 | return true;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 | /** Getter for Eigenspace::Indices.
|
|---|
| 142 | *
|
|---|
| 143 | * @return indexset of this eigenspace
|
|---|
| 144 | */
|
|---|
| 145 | const Eigenspace::indexset & Eigenspace::getIndices() const
|
|---|
| 146 | {
|
|---|
| 147 | return Indices;
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 | /** Sets the eigenvectors and -values.
|
|---|
| 152 | *
|
|---|
| 153 | * We only implement this for eigenpairs, not eigenvector and eigenvalue
|
|---|
| 154 | * distinctly to avoid mismatches, i.e. old eigenvalues used because only new
|
|---|
| 155 | * eigenvectors have been given.
|
|---|
| 156 | *
|
|---|
| 157 | * @param CurrentEigenvectors eigenvectors to set to
|
|---|
| 158 | * @param CurrentEigenvalues eigenvalues to set to
|
|---|
| 159 | */
|
|---|
| 160 | void Eigenspace::setEigenpairs(const eigenvectorset &CurrentEigenvectors, const eigenvalueset &CurrentEigenvalues)
|
|---|
| 161 | {
|
|---|
| 162 | ASSERT(CurrentEigenvectors.size() == Eigenvectors.size(),
|
|---|
| 163 | "Eigenspace::setEigenpairs() - "
|
|---|
| 164 | +toString(CurrentEigenvectors.size())+" x CurrentEigenvectors is not the same as "
|
|---|
| 165 | +toString(Eigenvectors.size())+"x Eigenvectors!");
|
|---|
| 166 | // set eigenvectors
|
|---|
| 167 | std::vector<boost::shared_ptr<VectorContent> >::const_iterator newev = CurrentEigenvectors.begin();
|
|---|
| 168 | for(std::vector<boost::shared_ptr<VectorContent> >::iterator ev = Eigenvectors.begin();
|
|---|
| 169 | ev != Eigenvectors.end();
|
|---|
| 170 | ++ev, ++newev) {
|
|---|
| 171 | *ev = *newev;
|
|---|
| 172 | }
|
|---|
| 173 | // eigenvalues can be just copied (integral type)
|
|---|
| 174 | Eigenvalues = CurrentEigenvalues;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 | /** Output operator for Eigenspace.
|
|---|
| 179 | *
|
|---|
| 180 | * @param ost output stream
|
|---|
| 181 | * @param _s Eigenspace to print: Indices and contained matrix
|
|---|
| 182 | * @return given output stream
|
|---|
| 183 | */
|
|---|
| 184 | std::ostream & operator<<(std::ostream &ost, const Eigenspace &_s)
|
|---|
| 185 | {
|
|---|
| 186 | BOOST_FOREACH(size_t iter, _s.Indices) {
|
|---|
| 187 | ost << iter << " ";
|
|---|
| 188 | }
|
|---|
| 189 | ost << " with matrix " << _s.EigenspaceMatrix;
|
|---|
| 190 | return ost;
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|