Changeset 0d4424 for src/LinearAlgebra
- Timestamp:
- Dec 4, 2010, 11:54:32 PM (14 years ago)
- 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)
- Location:
- src/LinearAlgebra
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/LinearAlgebra/Line.cpp
rcca9ef r0d4424 28 28 #include "Helpers/Log.hpp" 29 29 #include "Helpers/Verbose.hpp" 30 #include "LinearAlgebra/ gslmatrix.hpp"30 #include "LinearAlgebra/MatrixContent.hpp" 31 31 #include "Helpers/Info.hpp" 32 32 #include "Exceptions/LinearDependenceException.hpp" … … 116 116 Vector res; 117 117 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.); 121 121 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]); 126 126 } 127 127 -
src/LinearAlgebra/MatrixContent.cpp
rcca9ef r0d4424 25 25 #include <gsl/gsl_blas.h> 26 26 #include <gsl/gsl_eigen.h> 27 #include <gsl/gsl_linalg.h> 27 28 #include <gsl/gsl_matrix.h> 28 29 #include <gsl/gsl_multimin.h> … … 131 132 for(int j=columns;j--;){ 132 133 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 */ 141 void MatrixContent::setValue(double _value) 142 { 143 for(int i=rows;i--;){ 144 for(int j=columns;j--;){ 145 set(i,j,_value); 133 146 } 134 147 } … … 196 209 } 197 210 211 /* ========================== Accessing =============================== */ 212 198 213 /** Accessor for manipulating component (i,j). 199 214 * \param i row number … … 224 239 } 225 240 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 */ 246 double *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 */ 256 const 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 226 263 /** Setter for component (i,j). 227 264 * \param i row numbr … … 238 275 } 239 276 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 */ 281 void 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 */ 292 bool 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 */ 301 bool 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 */ 311 bool 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 240 317 /** Return transposed matrix. 241 318 * \return new matrix that is transposed of this. … … 244 321 { 245 322 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! 247 324 gsl_matrix_transpose_memcpy(res, content); 248 325 MatrixContent newContent(res); … … 286 363 return eval; 287 364 } 365 366 /* ============================ Properties ============================== */ 367 /** Checks whether matrix' elements are strictly null. 368 * \return true - is null, false - else 369 */ 370 bool 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 */ 378 bool 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 */ 386 bool 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 */ 394 bool 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 */ 403 bool 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 */ 415 double 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 =============================== */ 288 426 289 427 /** Scalar multiplication operator. -
src/LinearAlgebra/MatrixContent.hpp
rcca9ef r0d4424 41 41 friend Vector operator*(const MatrixContent &mat,const Vector &vec); 42 42 friend class RealSpaceMatrix; 43 friend class LinearSystemOfEquations; 44 45 friend class MatrixContentTest; 46 friend class MatrixContentSymmetricTest; 43 47 public: 44 48 MatrixContent(size_t rows, size_t columns); … … 53 57 const double at(size_t i, size_t j) const; 54 58 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); 55 65 56 66 // Transformations … … 59 69 gsl_vector* transformToEigenbasis(); 60 70 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 61 79 // Initializing 62 80 void setIdentity(); 63 81 void setZero(); 82 void setValue(double _value); 64 83 65 84 // Operators … … 72 91 bool operator==(const MatrixContent &rhs) const; 73 92 93 protected: 94 double *Pointer(size_t m, size_t n); 95 const double *const_Pointer(size_t m, size_t n) const; 74 96 75 97 private: -
src/LinearAlgebra/linearsystemofequations.cpp
rcca9ef r0d4424 21 21 22 22 #include "defs.hpp" 23 #include "LinearAlgebra/ gslmatrix.hpp"23 #include "LinearAlgebra/MatrixContent.hpp" 24 24 #include "LinearAlgebra/gslvector.hpp" 25 25 #include "LinearAlgebra/linearsystemofequations.hpp" … … 37 37 LinearSystemOfEquations::LinearSystemOfEquations(int m, int n) : rows(m), columns(n), IsSymmetric(false) 38 38 { 39 A = new GSLMatrix(m, n);39 A = new MatrixContent(m, n); 40 40 b = new GSLVector(m); 41 41 x = new GSLVector(n); … … 85 85 void LinearSystemOfEquations::SetA(double *x) 86 86 { 87 A-> SetFromDoubleArray(x);87 A->setFromDoubleArray(x); 88 88 }; 89 89 … … 129 129 if (IsSymmetric) { // use LU 130 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);131 gsl_linalg_LU_decomp (A->content, p, &s); 132 gsl_linalg_LU_solve (A->content, p, b->vector, x->vector); 133 133 gsl_permutation_free (p); 134 134 } else { // use Householder 135 // GSLMatrix *backup = new GSLMatrix(rows,columns);135 //MatrixContent *backup = new MatrixContent(rows,columns); 136 136 //*backup = *A; 137 gsl_linalg_HH_solve (A-> matrix, b->vector, x->vector);137 gsl_linalg_HH_solve (A->content, b->vector, x->vector); 138 138 //*A = *backup; 139 139 //delete(backup); -
src/LinearAlgebra/linearsystemofequations.hpp
rcca9ef r0d4424 21 21 22 22 class Vector; 23 class GSLMatrix;23 class MatrixContent; 24 24 class GSLVector; 25 25 … … 46 46 GSLVector *b; 47 47 GSLVector *x; 48 GSLMatrix*A;48 MatrixContent *A; 49 49 50 50 private:
Note:
See TracChangeset
for help on using the changeset viewer.