| 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 | * LinearSystemOfEquations.cpp | 
|---|
| 10 | * | 
|---|
| 11 | *  Created on: Jan 8, 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 "Helpers/defs.hpp" | 
|---|
| 23 | #include "LinearAlgebra/LinearSystemOfEquations.hpp" | 
|---|
| 24 | #include "LinearAlgebra/MatrixContent.hpp" | 
|---|
| 25 | #include "LinearAlgebra/VectorContent.hpp" | 
|---|
| 26 | #include "Helpers/Assert.hpp" | 
|---|
| 27 | #include "Helpers/logger.hpp" | 
|---|
| 28 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 29 |  | 
|---|
| 30 | #include <gsl/gsl_permutation.h> | 
|---|
| 31 |  | 
|---|
| 32 | /** Constructor for class LinearSystemOfEquations. | 
|---|
| 33 | * Allocates Vector and Matrix classes. | 
|---|
| 34 | * \param m column dimension | 
|---|
| 35 | * \param n row dimension | 
|---|
| 36 | */ | 
|---|
| 37 | LinearSystemOfEquations::LinearSystemOfEquations(int m, int n) : rows(m), columns(n), IsSymmetric(false) | 
|---|
| 38 | { | 
|---|
| 39 | A = new MatrixContent(m, n); | 
|---|
| 40 | b = new VectorContent(m); | 
|---|
| 41 | x = new VectorContent(n); | 
|---|
| 42 | }; | 
|---|
| 43 |  | 
|---|
| 44 | /** Destructor for class LinearSystemOfEquations. | 
|---|
| 45 | * Destructs Vector and Matrix classes. | 
|---|
| 46 | */ | 
|---|
| 47 | LinearSystemOfEquations::~LinearSystemOfEquations() | 
|---|
| 48 | { | 
|---|
| 49 | delete(A); | 
|---|
| 50 | delete(b); | 
|---|
| 51 | delete(x); | 
|---|
| 52 | }; | 
|---|
| 53 |  | 
|---|
| 54 | /** Sets whether matrix is to be regarded as symmetric. | 
|---|
| 55 | * Note that we do not check whether it really is, just take upper diagonal. | 
|---|
| 56 | * \param symmetric true or false | 
|---|
| 57 | */ | 
|---|
| 58 | bool LinearSystemOfEquations::SetSymmetric(bool symmetric) | 
|---|
| 59 | { | 
|---|
| 60 | ASSERT (rows == columns, "Rows and columns don't have equal size! Setting symmetric not possible."); | 
|---|
| 61 | return (IsSymmetric = symmetric); | 
|---|
| 62 | }; | 
|---|
| 63 |  | 
|---|
| 64 | /** Initializes vector b to the components of the given vector. | 
|---|
| 65 | * \param *x Vector with equal dimension (no check!) | 
|---|
| 66 | */ | 
|---|
| 67 | void LinearSystemOfEquations::Setb(Vector *x) | 
|---|
| 68 | { | 
|---|
| 69 | ASSERT  ( columns == NDIM, "Vector class is always three-dimensional, unlike this LEqS!"); | 
|---|
| 70 | b->setFromVector(*x); | 
|---|
| 71 | }; | 
|---|
| 72 |  | 
|---|
| 73 | /** Initializes vector b to the components of the given vector. | 
|---|
| 74 | * \param *x array with equal dimension (no check!) | 
|---|
| 75 | */ | 
|---|
| 76 | void LinearSystemOfEquations::Setb(double *x) | 
|---|
| 77 | { | 
|---|
| 78 | b->setFromDoubleArray(x); | 
|---|
| 79 | }; | 
|---|
| 80 |  | 
|---|
| 81 | /** Initializes matrix a to the components of the given array. | 
|---|
| 82 | * note that sort order should be | 
|---|
| 83 | * \param *x array with equal dimension (no check!) | 
|---|
| 84 | */ | 
|---|
| 85 | void LinearSystemOfEquations::SetA(double *x) | 
|---|
| 86 | { | 
|---|
| 87 | A->setFromDoubleArray(x); | 
|---|
| 88 | }; | 
|---|
| 89 |  | 
|---|
| 90 | /** Returns the solution vector x \f$A \cdot x = b\f$ as an array of doubles. | 
|---|
| 91 | * \param *array pointer allocated array for solution on return (no bounds check, dimension must match) | 
|---|
| 92 | * \return true - solving possible, false - some error occured. | 
|---|
| 93 | */ | 
|---|
| 94 | bool LinearSystemOfEquations::GetSolutionAsArray(double *&array) | 
|---|
| 95 | { | 
|---|
| 96 | bool status = Solve(); | 
|---|
| 97 |  | 
|---|
| 98 | // copy solution | 
|---|
| 99 | for (size_t i=0;i<x->dimension;i++) { | 
|---|
| 100 | array[i] = x->at(i); | 
|---|
| 101 | } | 
|---|
| 102 | return status; | 
|---|
| 103 | }; | 
|---|
| 104 |  | 
|---|
| 105 | /** Returns the solution vector x \f$A \cdot x = b\f$ as an array of doubles. | 
|---|
| 106 | * \param &x solution vector on return (must be 3-dimensional) | 
|---|
| 107 | * \return true - solving possible, false - some error occured. | 
|---|
| 108 | */ | 
|---|
| 109 | bool LinearSystemOfEquations::GetSolutionAsVector(Vector &v) | 
|---|
| 110 | { | 
|---|
| 111 | ASSERT (rows == NDIM, "Solution can only be returned as vector if number of columns is NDIM."); | 
|---|
| 112 | bool status = Solve(); | 
|---|
| 113 |  | 
|---|
| 114 | // copy solution | 
|---|
| 115 | for (size_t i=0;i<x->dimension;i++) | 
|---|
| 116 | v[i] = x->at(i); | 
|---|
| 117 | return status; | 
|---|
| 118 | }; | 
|---|
| 119 |  | 
|---|
| 120 | /** Solves the given system of \f$A \cdot x = b\f$. | 
|---|
| 121 | * Use either LU or Householder decomposition. | 
|---|
| 122 | * Solution is stored in LinearSystemOfEquations::x | 
|---|
| 123 | * \return true - solving possible, false - some error occured. | 
|---|
| 124 | */ | 
|---|
| 125 | bool LinearSystemOfEquations::Solve() | 
|---|
| 126 | { | 
|---|
| 127 | // calculate solution | 
|---|
| 128 | int s; | 
|---|
| 129 | if (IsSymmetric) { // use LU | 
|---|
| 130 | gsl_permutation * p = gsl_permutation_alloc (x->dimension); | 
|---|
| 131 | gsl_linalg_LU_decomp (A->content, p, &s); | 
|---|
| 132 | gsl_linalg_LU_solve (A->content, p, b->content, x->content); | 
|---|
| 133 | gsl_permutation_free (p); | 
|---|
| 134 | } else {  // use Householder | 
|---|
| 135 | //MatrixContent *backup = new MatrixContent(rows,columns); | 
|---|
| 136 | //*backup = *A; | 
|---|
| 137 | gsl_linalg_HH_solve (A->content, b->content, x->content); | 
|---|
| 138 | //*A = *backup; | 
|---|
| 139 | //delete(backup); | 
|---|
| 140 | } | 
|---|
| 141 | return true; | 
|---|
| 142 | }; | 
|---|
| 143 |  | 
|---|