source: src/LinearAlgebra/gslmatrix.cpp@ c66537

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
Last change on this file since c66537 was bcf653, checked in by Frederik Heber <heber@…>, 15 years ago

Added copyright note to each .cpp file and an extensive one to builder.cpp.

  • Property mode set to 100644
File size: 7.6 KB
Line 
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 * gslmatrix.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
22using namespace std;
23
24#include "LinearAlgebra/gslmatrix.hpp"
25#include "Helpers/helpers.hpp"
26#include "Helpers/fast_functions.hpp"
27
28#include <cassert>
29#include <gsl/gsl_linalg.h>
30
31/** Constructor of class GSLMatrix.
32 * Allocates GSL structures
33 * \param m dimension of matrix
34 */
35GSLMatrix::GSLMatrix(size_t m, size_t n) : rows(m), columns(n)
36{
37 matrix = gsl_matrix_calloc(rows, columns);
38};
39
40/** Copy constructor of class GSLMatrix.
41 * Allocates GSL structures and copies components from \a *src.
42 * \param *src source matrix
43 */
44GSLMatrix::GSLMatrix(const GSLMatrix * const src) : rows(src->rows), columns(src->columns)
45{
46 matrix = gsl_matrix_alloc(rows, columns);
47 gsl_matrix_memcpy (matrix, src->matrix);
48};
49
50/** Destructor of class GSLMatrix.
51 * Frees GSL structures
52 */
53GSLMatrix::~GSLMatrix()
54{
55 gsl_matrix_free(matrix);
56 rows = 0;
57 columns = 0;
58};
59
60/** Assignment operator.
61 * \param &rhs right hand side
62 * \return object itself
63 */
64GSLMatrix& GSLMatrix::operator=(const GSLMatrix& rhs)
65{
66 if (this == &rhs) // not necessary here, but identity assignment check is generally a good idea
67 return *this;
68 assert(rows == rhs.rows && columns == rhs.columns && "Number of rows and columns do not match!");
69
70 gsl_matrix_memcpy (matrix, rhs.matrix);
71
72 return *this;
73};
74
75/* ============================ Accessing =============================== */
76/** This function sets the matrix from a double array.
77 * Creates a matrix view of the array and performs a memcopy.
78 * \param *x array of values (no dimension check is performed)
79 */
80void GSLMatrix::SetFromDoubleArray(double * x)
81{
82 gsl_matrix_view m = gsl_matrix_view_array (x, rows, columns);
83 gsl_matrix_memcpy (matrix, &m.matrix);
84};
85
86/** This function returns the i-th element of a matrix.
87 * If \a m or \a n lies outside the allowed range of 0 to GSLMatrix::dimension-1 then the error handler is invoked and 0 is returned.
88 * \param m row index
89 * \param n colum index
90 * \return (m,n)-th element of matrix
91 */
92double GSLMatrix::Get(size_t m, size_t n)
93{
94 return gsl_matrix_get (matrix, m, n);
95};
96
97/** This function sets the value of the \a m -th element of a matrix to \a x.
98 * If \a m or \a n lies outside the allowed range of 0 to GSLMatrix::dimension-1 then the error handler is invoked.
99 * \param m row index
100 * \param m column index
101 * \param x value to set element (m,n) to
102 */
103void GSLMatrix::Set(size_t m, size_t n, double x)
104{
105 gsl_matrix_set (matrix, m, n, x);
106};
107
108/** These functions return a pointer to the \a m-th element of a matrix.
109 * 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.
110 * \param m index
111 * \return pointer to \a m-th element
112 */
113double *GSLMatrix::Pointer(size_t m, size_t n)
114{
115 return gsl_matrix_ptr (matrix, m, n);
116};
117
118/** These functions return a constant pointer to the \a m-th element of a matrix.
119 * 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.
120 * \param m index
121 * \return const pointer to \a m-th element
122 */
123const double *GSLMatrix::const_Pointer(size_t m, size_t n)
124{
125 return gsl_matrix_const_ptr (matrix, m, n);
126};
127
128/* ========================== Initializing =============================== */
129/** This function sets all the elements of the matrix to the value \a x.
130 * \param *x
131 */
132void GSLMatrix::SetAll(double x)
133{
134 gsl_matrix_set_all (matrix, x);
135};
136
137/** This function sets all the elements of the matrix to zero.
138 */
139void GSLMatrix::SetZero()
140{
141 gsl_matrix_set_zero (matrix);
142};
143
144/** This function sets the elements of the matrix to the corresponding elements of the identity matrix, \f$m(i,j) = \delta(i,j)\f$, i.e. a unit diagonal with all off-diagonal elements zero.
145 * This applies to both square and rectangular matrices.
146 */
147void GSLMatrix::SetIdentity()
148{
149 gsl_matrix_set_identity (matrix);
150};
151
152/* ====================== Exchanging elements ============================ */
153/** This function exchanges the \a i-th and \a j-th row of the matrix in-place.
154 * \param i i-th row to swap with ...
155 * \param j ... j-th row to swap against
156 */
157bool GSLMatrix::SwapRows(size_t i, size_t j)
158{
159 return (gsl_matrix_swap_rows (matrix, i, j) == GSL_SUCCESS);
160};
161
162/** This function exchanges the \a i-th and \a j-th column of the matrix in-place.
163 * \param i i-th column to swap with ...
164 * \param j ... j-th column to swap against
165 */
166bool GSLMatrix::SwapColumns(size_t i, size_t j)
167{
168 return (gsl_matrix_swap_columns (matrix, i, j) == GSL_SUCCESS);
169};
170
171/** This function exchanges the \a i-th row and \a j-th column of the matrix in-place.
172 * The matrix must be square for this operation to be possible.
173 * \param i i-th row to swap with ...
174 * \param j ... j-th column to swap against
175 */
176bool GSLMatrix::SwapRowColumn(size_t i, size_t j)
177{
178 assert (rows == columns && "The matrix must be square for swapping row against column to be possible.");
179 return (gsl_matrix_swap_rowcol (matrix, i, j) == GSL_SUCCESS);
180};
181
182/** This function transposes the matrix.
183 * Note that the function is extended to the non-square case, where the matrix is re-allocated and copied.
184 */
185bool GSLMatrix::Transpose()
186{
187 if (rows == columns)// if square, use GSL
188 return (gsl_matrix_transpose (matrix) == GSL_SUCCESS);
189 else { // otherwise we have to copy a bit
190 gsl_matrix *dest = gsl_matrix_alloc(columns, rows);
191 for (size_t i=0;i<rows; i++)
192 for (size_t j=0;j<columns;j++) {
193 gsl_matrix_set(dest, j,i, gsl_matrix_get(matrix, i,j) );
194 }
195 gsl_matrix_free(matrix);
196 matrix = dest;
197 flip(rows, columns);
198 return true;
199 }
200};
201
202/* ============================ Properties ============================== */
203/** Checks whether matrix' elements are strictly null.
204 * \return true - is null, false - else
205 */
206bool GSLMatrix::IsNull()
207{
208 return gsl_matrix_isnull (matrix);
209};
210
211/** Checks whether matrix' elements are strictly positive.
212 * \return true - is positive, false - else
213 */
214bool GSLMatrix::IsPositive()
215{
216 return gsl_matrix_ispos (matrix);
217};
218
219/** Checks whether matrix' elements are strictly negative.
220 * \return true - is negative, false - else
221 */
222bool GSLMatrix::IsNegative()
223{
224 return gsl_matrix_isneg (matrix);
225};
226
227/** Checks whether matrix' elements are strictly non-negative.
228 * \return true - is non-negative, false - else
229 */
230bool GSLMatrix::IsNonNegative()
231{
232 return gsl_matrix_isnonneg (matrix);
233};
234
235/** This function performs a Cholesky decomposition to determine whether matrix is positive definite.
236 * We check whether GSL returns GSL_EDOM as error, indicating that decomposition failed due to matrix not being positive-definite.
237 * \return true - matrix is positive-definite, false - else
238 */
239bool GSLMatrix::IsPositiveDefinite()
240{
241 if (rows != columns) // only possible for square matrices.
242 return false;
243 else
244 return (gsl_linalg_cholesky_decomp (matrix) != GSL_EDOM);
245};
246
247
248/** Calculates the determinant of the matrix.
249 * if matrix is square, uses LU decomposition.
250 */
251double GSLMatrix::Determinant()
252{
253 int signum = 0;
254 assert (rows == columns && "Determinant can only be calculated for square matrices.");
255 gsl_permutation *p = gsl_permutation_alloc(rows);
256 gsl_linalg_LU_decomp(matrix, p, &signum);
257 gsl_permutation_free(p);
258 return gsl_linalg_LU_det(matrix, signum);
259};
260
Note: See TracBrowser for help on using the repository browser.