Changeset 0d4424 for src/LinearAlgebra


Ignore:
Timestamp:
Dec 4, 2010, 11:54:32 PM (14 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
c134d9
Parents:
cca9ef
git-author:
Frederik Heber <heber@…> (11/15/10 14:02:38)
git-committer:
Frederik Heber <heber@…> (12/04/10 23:54:32)
Message:

Made gslmatrix obsolete.

Location:
src/LinearAlgebra
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/LinearAlgebra/Line.cpp

    rcca9ef r0d4424  
    2828#include "Helpers/Log.hpp"
    2929#include "Helpers/Verbose.hpp"
    30 #include "LinearAlgebra/gslmatrix.hpp"
     30#include "LinearAlgebra/MatrixContent.hpp"
    3131#include "Helpers/Info.hpp"
    3232#include "Exceptions/LinearDependenceException.hpp"
     
    116116  Vector res;
    117117
    118   auto_ptr<GSLMatrix> M = auto_ptr<GSLMatrix>(new GSLMatrix(4,4));
    119 
    120   M->SetAll(1.);
     118  auto_ptr<MatrixContent> M = auto_ptr<MatrixContent>(new MatrixContent(4,4));
     119
     120  M->setValue(1.);
    121121  for (int i=0;i<3;i++) {
    122     M->Set(0, i, Line1a[i]);
    123     M->Set(1, i, Line1b[i]);
    124     M->Set(2, i, Line2a[i]);
    125     M->Set(3, i, Line2b[i]);
     122    M->set(0, i, Line1a[i]);
     123    M->set(1, i, Line1b[i]);
     124    M->set(2, i, Line2a[i]);
     125    M->set(3, i, Line2b[i]);
    126126  }
    127127
  • src/LinearAlgebra/MatrixContent.cpp

    rcca9ef r0d4424  
    2525#include <gsl/gsl_blas.h>
    2626#include <gsl/gsl_eigen.h>
     27#include <gsl/gsl_linalg.h>
    2728#include <gsl/gsl_matrix.h>
    2829#include <gsl/gsl_multimin.h>
     
    131132    for(int j=columns;j--;){
    132133      set(i,j,0.);
     134    }
     135  }
     136}
     137
     138/** Set all matrix components to a given value.
     139 * \param _value value to set each component to
     140 */
     141void MatrixContent::setValue(double _value)
     142{
     143  for(int i=rows;i--;){
     144    for(int j=columns;j--;){
     145      set(i,j,_value);
    133146    }
    134147  }
     
    196209}
    197210
     211/* ========================== Accessing =============================== */
     212
    198213/** Accessor for manipulating component (i,j).
    199214 * \param i row number
     
    224239}
    225240
     241/** These functions return a pointer to the \a m-th element of a matrix.
     242 *  If \a m or \a n lies outside the allowed range of 0 to GSLMatrix::dimension-1 then the error handler is invoked and a null pointer is returned.
     243 * \param m index
     244 * \return pointer to \a m-th element
     245 */
     246double *MatrixContent::Pointer(size_t m, size_t n)
     247{
     248  return gsl_matrix_ptr (content, m, n);
     249};
     250
     251/** These functions return a constant pointer to the \a m-th element of a matrix.
     252 *  If \a m or \a n lies outside the allowed range of 0 to GSLMatrix::dimension-1 then the error handler is invoked and a null pointer is returned.
     253 * \param m index
     254 * \return const pointer to \a m-th element
     255 */
     256const double *MatrixContent::const_Pointer(size_t m, size_t n) const
     257{
     258  return gsl_matrix_const_ptr (content, m, n);
     259};
     260
     261/* ========================== Initializing =============================== */
     262
    226263/** Setter for component (i,j).
    227264 * \param i row numbr
     
    238275}
    239276
     277/** This function sets the matrix from a double array.
     278 * Creates a matrix view of the array and performs a memcopy.
     279 * \param *x array of values (no dimension check is performed)
     280 */
     281void MatrixContent::setFromDoubleArray(double * x)
     282{
     283  gsl_matrix_view m = gsl_matrix_view_array (x, rows, columns);
     284  gsl_matrix_memcpy (content, &m.matrix);
     285};
     286
     287/* ====================== Exchanging elements ============================ */
     288/** This function exchanges the \a i-th and \a j-th row of the matrix in-place.
     289 * \param i i-th row to swap with ...
     290 * \param j ... j-th row to swap against
     291 */
     292bool MatrixContent::SwapRows(size_t i, size_t j)
     293{
     294  return (gsl_matrix_swap_rows (content, i, j) == GSL_SUCCESS);
     295};
     296
     297/** This function exchanges the \a i-th and \a j-th column of the matrix in-place.
     298 * \param i i-th column to swap with ...
     299 * \param j ... j-th column to swap against
     300 */
     301bool MatrixContent::SwapColumns(size_t i, size_t j)
     302{
     303  return (gsl_matrix_swap_columns (content, i, j) == GSL_SUCCESS);
     304};
     305
     306/** This function exchanges the \a i-th row and \a j-th column of the matrix in-place.
     307 * The matrix must be square for this operation to be possible.
     308 * \param i i-th row to swap with ...
     309 * \param j ... j-th column to swap against
     310 */
     311bool MatrixContent::SwapRowColumn(size_t i, size_t j)
     312{
     313  assert (rows == columns && "The matrix must be square for swapping row against column to be possible.");
     314  return (gsl_matrix_swap_rowcol (content, i, j) == GSL_SUCCESS);
     315};
     316
    240317/** Return transposed matrix.
    241318 * \return new matrix that is transposed of this.
     
    244321{
    245322  std::cout << "const MatrixContent::transpose()." << std::endl;
    246   gsl_matrix *res = gsl_matrix_alloc(rows, columns);
     323  gsl_matrix *res = gsl_matrix_alloc(columns, rows);  // column and row dimensions exchanged!
    247324  gsl_matrix_transpose_memcpy(res, content);
    248325  MatrixContent newContent(res);
     
    286363  return eval;
    287364}
     365
     366/* ============================ Properties ============================== */
     367/** Checks whether matrix' elements are strictly null.
     368 * \return true - is null, false - else
     369 */
     370bool MatrixContent::IsNull() const
     371{
     372  return gsl_matrix_isnull (content);
     373};
     374
     375/** Checks whether matrix' elements are strictly positive.
     376 * \return true - is positive, false - else
     377 */
     378bool MatrixContent::IsPositive() const
     379{
     380  return gsl_matrix_ispos (content);
     381};
     382
     383/** Checks whether matrix' elements are strictly negative.
     384 * \return true - is negative, false - else
     385 */
     386bool MatrixContent::IsNegative() const
     387{
     388  return gsl_matrix_isneg (content);
     389};
     390
     391/** Checks whether matrix' elements are strictly non-negative.
     392 * \return true - is non-negative, false - else
     393 */
     394bool MatrixContent::IsNonNegative() const
     395{
     396  return gsl_matrix_isnonneg (content);
     397};
     398
     399/** This function performs a Cholesky decomposition to determine whether matrix is positive definite.
     400 * We check whether GSL returns GSL_EDOM as error, indicating that decomposition failed due to matrix not being positive-definite.
     401 * \return true - matrix is positive-definite, false - else
     402 */
     403bool MatrixContent::IsPositiveDefinite() const
     404{
     405  if (rows != columns)  // only possible for square matrices.
     406    return false;
     407  else
     408    return (gsl_linalg_cholesky_decomp (content) != GSL_EDOM);
     409};
     410
     411
     412/** Calculates the determinant of the matrix.
     413 * if matrix is square, uses LU decomposition.
     414 */
     415double MatrixContent::Determinant() const
     416{
     417  int signum = 0;
     418  assert (rows == columns && "Determinant can only be calculated for square matrices.");
     419  gsl_permutation *p = gsl_permutation_alloc(rows);
     420  gsl_linalg_LU_decomp(content, p, &signum);
     421  gsl_permutation_free(p);
     422  return gsl_linalg_LU_det(content, signum);
     423};
     424
     425/* ============================= Operators =============================== */
    288426
    289427/** Scalar multiplication operator.
  • src/LinearAlgebra/MatrixContent.hpp

    rcca9ef r0d4424  
    4141  friend Vector operator*(const MatrixContent &mat,const Vector &vec);
    4242  friend class RealSpaceMatrix;
     43  friend class LinearSystemOfEquations;
     44
     45  friend class MatrixContentTest;
     46  friend class MatrixContentSymmetricTest;
    4347public:
    4448  MatrixContent(size_t rows, size_t columns);
     
    5357  const double at(size_t i, size_t j) const;
    5458  void set(size_t i, size_t j, const double value);
     59  void setFromDoubleArray(double * x);
     60
     61  // Exchanging elements
     62  bool SwapRows(size_t i, size_t j);
     63  bool SwapColumns(size_t i, size_t j);
     64  bool SwapRowColumn(size_t i, size_t j);
    5565
    5666  // Transformations
     
    5969  gsl_vector* transformToEigenbasis();
    6070
     71  // Properties
     72  bool IsNull() const;
     73  bool IsPositive() const;
     74  bool IsNegative() const;
     75  bool IsNonNegative() const;
     76  bool IsPositiveDefinite() const;
     77  double Determinant() const;
     78
    6179  // Initializing
    6280  void setIdentity();
    6381  void setZero();
     82  void setValue(double _value);
    6483
    6584  // Operators
     
    7291  bool operator==(const MatrixContent &rhs) const;
    7392
     93protected:
     94  double *Pointer(size_t m, size_t n);
     95  const double *const_Pointer(size_t m, size_t n) const;
    7496
    7597private:
  • src/LinearAlgebra/linearsystemofequations.cpp

    rcca9ef r0d4424  
    2121
    2222#include "defs.hpp"
    23 #include "LinearAlgebra/gslmatrix.hpp"
     23#include "LinearAlgebra/MatrixContent.hpp"
    2424#include "LinearAlgebra/gslvector.hpp"
    2525#include "LinearAlgebra/linearsystemofequations.hpp"
     
    3737LinearSystemOfEquations::LinearSystemOfEquations(int m, int n) : rows(m), columns(n), IsSymmetric(false)
    3838{
    39   A = new GSLMatrix(m, n);
     39  A = new MatrixContent(m, n);
    4040  b = new GSLVector(m);
    4141  x = new GSLVector(n);
     
    8585void LinearSystemOfEquations::SetA(double *x)
    8686{
    87   A->SetFromDoubleArray(x);
     87  A->setFromDoubleArray(x);
    8888};
    8989
     
    129129  if (IsSymmetric) { // use LU
    130130    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);
     131    gsl_linalg_LU_decomp (A->content, p, &s);
     132    gsl_linalg_LU_solve (A->content, p, b->vector, x->vector);
    133133    gsl_permutation_free (p);
    134134  } else {  // use Householder
    135     //GSLMatrix *backup = new GSLMatrix(rows,columns);
     135    //MatrixContent *backup = new MatrixContent(rows,columns);
    136136    //*backup = *A;
    137     gsl_linalg_HH_solve (A->matrix, b->vector, x->vector);
     137    gsl_linalg_HH_solve (A->content, b->vector, x->vector);
    138138    //*A = *backup;
    139139    //delete(backup);
  • src/LinearAlgebra/linearsystemofequations.hpp

    rcca9ef r0d4424  
    2121
    2222class Vector;
    23 class GSLMatrix;
     23class MatrixContent;
    2424class GSLVector;
    2525
     
    4646  GSLVector *b;
    4747  GSLVector *x;
    48   GSLMatrix *A;
     48  MatrixContent *A;
    4949
    5050private:
Note: See TracChangeset for help on using the changeset viewer.