source: src/gslmatrix.cpp@ 796aa6

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 796aa6 was 112b09, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added #include "Helpers/MemDebug.hpp" to all .cpp files

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