| 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 "defs.hpp"
 | 
|---|
| 23 | #include "LinearAlgebra/gslmatrix.hpp"
 | 
|---|
| 24 | #include "LinearAlgebra/gslvector.hpp"
 | 
|---|
| 25 | #include "LinearAlgebra/linearsystemofequations.hpp"
 | 
|---|
| 26 | #include "Helpers/logger.hpp"
 | 
|---|
| 27 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 28 | 
 | 
|---|
| 29 | #include <cassert>
 | 
|---|
| 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 GSLMatrix(m, n);
 | 
|---|
| 40 |   b = new GSLVector(m);
 | 
|---|
| 41 |   x = new GSLVector(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->Get(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->Get(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->matrix, p, &s);
 | 
|---|
| 132 |     gsl_linalg_LU_solve (A->matrix, p, b->vector, x->vector);
 | 
|---|
| 133 |     gsl_permutation_free (p);
 | 
|---|
| 134 |   } else {  // use Householder
 | 
|---|
| 135 |     //GSLMatrix *backup = new GSLMatrix(rows,columns);
 | 
|---|
| 136 |     //*backup = *A;
 | 
|---|
| 137 |     gsl_linalg_HH_solve (A->matrix, b->vector, x->vector);
 | 
|---|
| 138 |     //*A = *backup;
 | 
|---|
| 139 |     //delete(backup);
 | 
|---|
| 140 |   }
 | 
|---|
| 141 |   return true;
 | 
|---|
| 142 | };
 | 
|---|
| 143 | 
 | 
|---|