source: src/LinearAlgebra/gslmatrix.cpp@ 3bc926

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 3bc926 was 3bc926, checked in by Frederik Heber <heber@…>, 14 years ago

Extended MatrixContent class.

  • MatrixContent formerly has been just a structure to contain the gsl_matrix due to forward declaration reasons.
  • now MatrixContent is a true wrapper to gsl_matrix, i.e. all functionality that is now specific to 3 dimensions has been shifted from Matrix over to MatrixContent.
  • Property mode set to 100644
File size: 9.8 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 <iostream>
30#include <gsl/gsl_linalg.h>
31#include <gsl/gsl_blas.h>
32#include <gsl/gsl_eigen.h>
33#include <gsl/gsl_matrix.h>
34#include <gsl/gsl_vector.h>
35
36/** Constructor of class GSLMatrix.
37 * Allocates GSL structures
38 * \param m dimension of matrix
39 */
40GSLMatrix::GSLMatrix(size_t m, size_t n) : rows(m), columns(n)
41{
42 matrix = gsl_matrix_calloc(rows, columns);
43};
44
45/** Copy constructor of class GSLMatrix.
46 * Allocates GSL structures and copies components from \a *src.
47 * \param *src source matrix
48 */
49GSLMatrix::GSLMatrix(const GSLMatrix * const src) : rows(src->rows), columns(src->columns)
50{
51 matrix = gsl_matrix_alloc(rows, columns);
52 gsl_matrix_memcpy (matrix, src->matrix);
53};
54
55/** Copy constructor of class GSLMatrix.
56 * We take over pointer to gsl_matrix and set the parameter pointer to NULL
57 * afterwards.
58 * \param m row count
59 * \param n column count
60 * \param *&src source gsl_matrix
61 */
62GSLMatrix::GSLMatrix(size_t m, size_t n, gsl_matrix *&src) :
63 rows(m),
64 columns(n)
65{
66 matrix = src;
67 src = NULL;
68};
69
70/** Destructor of class GSLMatrix.
71 * Frees GSL structures
72 */
73GSLMatrix::~GSLMatrix()
74{
75 gsl_matrix_free(matrix);
76 rows = 0;
77 columns = 0;
78};
79
80/** Assignment operator.
81 * \param &rhs right hand side
82 * \return object itself
83 */
84GSLMatrix& GSLMatrix::operator=(const GSLMatrix& rhs)
85{
86 if (this == &rhs) // not necessary here, but identity assignment check is generally a good idea
87 return *this;
88 assert(rows == rhs.rows && columns == rhs.columns && "Number of rows and columns do not match!");
89
90 gsl_matrix_memcpy (matrix, rhs.matrix);
91
92 return *this;
93};
94
95/* ============================ Accessing =============================== */
96/** This function sets the matrix from a double array.
97 * Creates a matrix view of the array and performs a memcopy.
98 * \param *x array of values (no dimension check is performed)
99 */
100void GSLMatrix::SetFromDoubleArray(double * x)
101{
102 gsl_matrix_view m = gsl_matrix_view_array (x, rows, columns);
103 gsl_matrix_memcpy (matrix, &m.matrix);
104};
105
106/** This function returns the i-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 0 is returned.
108 * \param m row index
109 * \param n colum index
110 * \return (m,n)-th element of matrix
111 */
112double GSLMatrix::Get(size_t m, size_t n) const
113{
114 return gsl_matrix_get (matrix, m, n);
115};
116
117/** This function sets the value of the \a m -th element of a matrix to \a x.
118 * If \a m or \a n lies outside the allowed range of 0 to GSLMatrix::dimension-1 then the error handler is invoked.
119 * \param m row index
120 * \param m column index
121 * \param x value to set element (m,n) to
122 */
123void GSLMatrix::Set(size_t m, size_t n, double x)
124{
125 gsl_matrix_set (matrix, m, n, x);
126};
127
128/** These functions return a pointer to the \a m-th element of a matrix.
129 * 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.
130 * \param m index
131 * \return pointer to \a m-th element
132 */
133double *GSLMatrix::Pointer(size_t m, size_t n)
134{
135 return gsl_matrix_ptr (matrix, m, n);
136};
137
138/** These functions return a constant pointer to the \a m-th element of a matrix.
139 * 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.
140 * \param m index
141 * \return const pointer to \a m-th element
142 */
143const double *GSLMatrix::const_Pointer(size_t m, size_t n) const
144{
145 return gsl_matrix_const_ptr (matrix, m, n);
146};
147
148/** Returns the number of rows of the matrix.
149 * \return number of rows
150 */
151size_t GSLMatrix::getRowCount() const
152{
153 return rows;
154}
155
156/** Returns the number of columns of the matrix.
157 * \return number of columns
158 */
159size_t GSLMatrix::getColumnCount() const
160{
161 return columns;
162}
163
164/* ========================== Initializing =============================== */
165/** This function sets all the elements of the matrix to the value \a x.
166 * \param *x
167 */
168void GSLMatrix::SetAll(double x)
169{
170 gsl_matrix_set_all (matrix, x);
171};
172
173/** This function sets all the elements of the matrix to zero.
174 */
175void GSLMatrix::SetZero()
176{
177 gsl_matrix_set_zero (matrix);
178};
179
180/** 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.
181 * This applies to both square and rectangular matrices.
182 */
183void GSLMatrix::SetIdentity()
184{
185 gsl_matrix_set_identity (matrix);
186};
187
188/* ====================== Exchanging elements ============================ */
189/** This function exchanges the \a i-th and \a j-th row of the matrix in-place.
190 * \param i i-th row to swap with ...
191 * \param j ... j-th row to swap against
192 */
193bool GSLMatrix::SwapRows(size_t i, size_t j)
194{
195 return (gsl_matrix_swap_rows (matrix, i, j) == GSL_SUCCESS);
196};
197
198/** This function exchanges the \a i-th and \a j-th column of the matrix in-place.
199 * \param i i-th column to swap with ...
200 * \param j ... j-th column to swap against
201 */
202bool GSLMatrix::SwapColumns(size_t i, size_t j)
203{
204 return (gsl_matrix_swap_columns (matrix, i, j) == GSL_SUCCESS);
205};
206
207/** This function exchanges the \a i-th row and \a j-th column of the matrix in-place.
208 * The matrix must be square for this operation to be possible.
209 * \param i i-th row to swap with ...
210 * \param j ... j-th column to swap against
211 */
212bool GSLMatrix::SwapRowColumn(size_t i, size_t j)
213{
214 assert (rows == columns && "The matrix must be square for swapping row against column to be possible.");
215 return (gsl_matrix_swap_rowcol (matrix, i, j) == GSL_SUCCESS);
216};
217
218/** This function transposes the matrix.
219 * Note that the function is extended to the non-square case, where the matrix is re-allocated and copied.
220 */
221bool GSLMatrix::Transpose()
222{
223 if (rows == columns)// if square, use GSL
224 return (gsl_matrix_transpose (matrix) == GSL_SUCCESS);
225 else { // otherwise we have to copy a bit
226 gsl_matrix *dest = gsl_matrix_alloc(columns, rows);
227 for (size_t i=0;i<rows; i++)
228 for (size_t j=0;j<columns;j++) {
229 gsl_matrix_set(dest, j,i, gsl_matrix_get(matrix, i,j) );
230 }
231 gsl_matrix_free(matrix);
232 matrix = dest;
233 flip(rows, columns);
234 return true;
235 }
236};
237
238/* ============================ Properties ============================== */
239/** Checks whether matrix' elements are strictly null.
240 * \return true - is null, false - else
241 */
242bool GSLMatrix::IsNull() const
243{
244 return gsl_matrix_isnull (matrix);
245};
246
247/** Checks whether matrix' elements are strictly positive.
248 * \return true - is positive, false - else
249 */
250bool GSLMatrix::IsPositive() const
251{
252 return gsl_matrix_ispos (matrix);
253};
254
255/** Checks whether matrix' elements are strictly negative.
256 * \return true - is negative, false - else
257 */
258bool GSLMatrix::IsNegative() const
259{
260 return gsl_matrix_isneg (matrix);
261};
262
263/** Checks whether matrix' elements are strictly non-negative.
264 * \return true - is non-negative, false - else
265 */
266bool GSLMatrix::IsNonNegative() const
267{
268 return gsl_matrix_isnonneg (matrix);
269};
270
271/** This function performs a Cholesky decomposition to determine whether matrix is positive definite.
272 * We check whether GSL returns GSL_EDOM as error, indicating that decomposition failed due to matrix not being positive-definite.
273 * \return true - matrix is positive-definite, false - else
274 */
275bool GSLMatrix::IsPositiveDefinite() const
276{
277 if (rows != columns) // only possible for square matrices.
278 return false;
279 else
280 return (gsl_linalg_cholesky_decomp (matrix) != GSL_EDOM);
281};
282
283
284/** Calculates the determinant of the matrix.
285 * if matrix is square, uses LU decomposition.
286 */
287double GSLMatrix::Determinant() const
288{
289 int signum = 0;
290 assert (rows == columns && "Determinant can only be calculated for square matrices.");
291 gsl_permutation *p = gsl_permutation_alloc(rows);
292 gsl_linalg_LU_decomp(matrix, p, &signum);
293 gsl_permutation_free(p);
294 return gsl_linalg_LU_det(matrix, signum);
295};
296
297
298/* ============================ Properties ============================== */
299
300const GSLMatrix &GSLMatrix::operator+=(const GSLMatrix &rhs)
301{
302 gsl_matrix_add(matrix, rhs.matrix);
303 return *this;
304}
305
306const GSLMatrix &GSLMatrix::operator-=(const GSLMatrix &rhs)
307{
308 gsl_matrix_sub(matrix, rhs.matrix);
309 return *this;
310}
311
312const GSLMatrix &GSLMatrix::operator*=(const GSLMatrix &rhs)
313{
314 (*this) = (*this)*rhs;
315 return *this;
316}
317
318const GSLMatrix GSLMatrix::operator+(const GSLMatrix &rhs) const
319{
320 GSLMatrix tmp = *this;
321 tmp+=rhs;
322 return tmp;
323}
324
325const GSLMatrix GSLMatrix::operator-(const GSLMatrix &rhs) const
326{
327 GSLMatrix tmp = *this;
328 tmp-=rhs;
329 return tmp;
330}
331
332const GSLMatrix GSLMatrix::operator*(const GSLMatrix &rhs) const
333{
334 gsl_matrix *res = gsl_matrix_alloc(rhs.rows, rhs.columns);
335 gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, matrix, rhs.matrix, 0.0, res);
336 return GSLMatrix(rhs.rows, rhs.columns, res);
337}
338
339/** Print matrix in a matlab style manner.
340 * \param &ost reference to output stream
341 * \param &mat reference to matrix to print
342 * \return reference to obtained output stream for concatenation
343 */
344ostream &operator<<(ostream &ost,const GSLMatrix &mat)
345{
346 ost << "[";
347 for(size_t i = 0;i<mat.rows;++i){
348 for(size_t j = 0; j<mat.columns;++j){
349 ost << mat.Get(i,j);
350 if(j!=mat.columns-1)
351 ost << " ";
352 }
353 if(i!=mat.rows-1)
354 ost << "; ";
355 }
356 ost << "]";
357 return ost;
358}
359
Note: See TracBrowser for help on using the repository browser.