source: src/gslmatrix.cpp@ 2793ef

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 2793ef was fc3b67, checked in by Frederik Heber <heber@…>, 15 years ago

Wrapper class for gsl_matrix along with working Unit test.

  • new class GSLMatrix wraps all useful functions of the GSL library.
  • new unit tests GSLMatrixSymmetricUnitTest (symmetric case) and GSLMatrixUnitTest tests each part and is working.

Signed-off-by: Frederik Heber <heber@…>

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