Geant4 11.4.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
GIDI_matrix.cc
Go to the documentation of this file.
1/*
2# <<BEGIN-copyright>>
3# Copyright 2019, Lawrence Livermore National Security, LLC.
4# This file is part of the gidiplus package (https://github.com/LLNL/gidiplus).
5# gidiplus is licensed under the MIT license (see https://opensource.org/licenses/MIT).
6# SPDX-License-Identifier: MIT
7# <<END-copyright>>
8*/
9
10#include "GIDI.hpp"
11
12namespace GIDI {
13
14/*! \class Matrix
15 * This class stores a mathematical matrix and has methods that perform several matrix operations (e.g., addition, subtraction).
16 */
17
18/* *********************************************************************************************************//**
19 *
20 * @param a_rows [in] Number of rows of the matrix.
21 * @param a_columns [in] Number of columns of the matrix.
22 ***********************************************************************************************************/
23
24Matrix::Matrix( std::size_t a_rows, std::size_t a_columns ) {
25
26 m_matrix.resize( a_rows );
27
28 for( std::size_t i1 = 0; i1 < a_rows; ++i1 ) m_matrix[i1].resize( a_columns );
29}
30
31/* *********************************************************************************************************//**
32 *
33 * @param a_matrix [in] Matrix to copy.
34 ***********************************************************************************************************/
35
36Matrix::Matrix( Matrix const &a_matrix ) {
37
38 std::size_t rows = a_matrix.size( );
39
40 m_matrix.resize( rows );
41
42 for( std::size_t i1 = 0; i1 < rows; ++i1 ) m_matrix[i1] = a_matrix[i1];
43}
44
45/* *********************************************************************************************************//**
46 ***********************************************************************************************************/
47
49
50}
51
52/* *********************************************************************************************************//**
53 * The assignment operator. This method sets the members of *this* to those of *a_rhs*.
54 *
55 * @param a_rhs [in] Instance whose member are used to set the members of *this*.
56 *
57 * @return A reference to the updated Matrix instance.
58 ***********************************************************************************************************/
59
61
62 if( this != &a_rhs ) {
63 m_matrix = a_rhs.matrix( );
64 }
65
66 return( *this );
67}
68
69/* *********************************************************************************************************//**
70 * Returns a new Matrix whose cells are *this* plus *a_value*.
71 *
72 * @param a_value [in] The value to add to each cell.
73 * @return New Matrix whose cells are *this* plus *a_value*.
74 ***********************************************************************************************************/
75
76Matrix Matrix::operator+( double a_value ) const {
77
78 Matrix gidiMatrix( *this );
79
80 gidiMatrix += a_value;
81 return( gidiMatrix );
82}
83
84/* *********************************************************************************************************//**
85 * Adds *a_value* to each cell of *this*.
86 *
87 * @param a_value [in] The value to add to each cell.
88 * @return Returns reference to *this*.
89 ***********************************************************************************************************/
90
91Matrix &Matrix::operator+=( double a_value ) {
92
93 for( std::vector<Vector>::iterator iter = m_matrix.begin( ); iter < m_matrix.end( ); ++iter ) *iter += a_value;
94
95 return( *this );
96}
97
98/* *********************************************************************************************************//**
99 * Adds two Matrices.
100 *
101 * @param a_rhs [in] Matrix to add to *this*.
102 * @return New Matrix that is the matrix sum of *this* and *a_rhs*.
103 ***********************************************************************************************************/
104
105Matrix Matrix::operator+( Matrix const &a_rhs ) const {
106
107 Matrix gidiMatrix( *this );
108
109 gidiMatrix += a_rhs;
110 return( gidiMatrix );
111}
112
113/* *********************************************************************************************************//**
114 * Adds *a_rhs* to *this*.
115 *
116 * @param a_rhs [in] Matrix to add to *this*.
117 * @return Returns reference to *this*.
118 ***********************************************************************************************************/
119
121
122 std::size_t i1, rhs_size = a_rhs.size( );
123
124 if( rhs_size == 0 ) return( *this ); // Do nothing if rhs is empty.
125
126 if( size( ) == 0 ) {
127 for( i1 = 0; i1 < rhs_size; ++i1 ) m_matrix.push_back( Vector( a_rhs.numberOfColumns( ) ) );
128 }
129
130 if( size( ) != a_rhs.size( ) ) throw Exception( "matrix sizes differ." );
131 if( m_matrix[0].size( ) != a_rhs[0].size( ) ) throw Exception( "matrix colums numbers differ." );
132
133 i1 = 0;
134 for( std::vector<Vector>::iterator iter = m_matrix.begin( ); iter < m_matrix.end( ); ++iter, ++i1 ) *iter += a_rhs[i1];
135
136 return( *this );
137}
138
139/* *********************************************************************************************************//**
140 * Returns a new Matrix whose cells are *this* minus *a_value*.
141 *
142 * @param a_value [in] The value to subtract from each cell.
143 * @return New Matrix whose cells are *this* plus *a_value*.
144 ***********************************************************************************************************/
145
146Matrix Matrix::operator-( double a_value ) const {
147
148 Matrix gidiMatrix( *this );
149
150 gidiMatrix -= a_value;
151 return( gidiMatrix );
152}
153
154/* *********************************************************************************************************//**
155 * Subtracts *a_value* from each cell of *this*.
156 *
157 * @param a_value [in] The value to subtract from each cell.
158 * @return Returns reference to *this*.
159 ***********************************************************************************************************/
160
161Matrix &Matrix::operator-=( double a_value ) {
162
163 for( std::vector<Vector>::iterator iter = m_matrix.begin( ); iter < m_matrix.end( ); ++iter ) *iter -= a_value;
164
165 return( *this );
166}
167
168/* *********************************************************************************************************//**
169 * Subtracts *a_rhs* from *this*.
170 *
171 * @param a_rhs [in] Matrix to subtract from *this*.
172 * @return New Matrix that is *this* minus *a_rhs*.
173 ***********************************************************************************************************/
174
175Matrix Matrix::operator-( Matrix const &a_rhs ) const {
176
177 Matrix gidiMatrix( *this );
178
179 gidiMatrix -= a_rhs;
180 return( gidiMatrix );
181}
182
183/* *********************************************************************************************************//**
184 * Subtracts *a_rhs* to *this*.
185 *
186 * @param a_rhs [in] Matrix to subtract from *this*.
187 * @return Returns reference to *this*.
188 ***********************************************************************************************************/
189
191
192 std::size_t i1, rhs_size = a_rhs.size( );
193
194 if( rhs_size == 0 ) return( *this ); // Do nothing if rhs is empty.
195
196 if( size( ) == 0 ) {
197 for( i1 = 0; i1 < rhs_size; ++i1 ) m_matrix.push_back( Vector( a_rhs.numberOfColumns( ) ) );
198 }
199
200 if( size( ) != a_rhs.size( ) ) throw Exception( "matrix sizes differ." );
201 if( m_matrix[0].size( ) != a_rhs[0].size( ) ) throw Exception( "matrix colums numbers differ." );
202
203 i1 = 0;
204 for( std::vector<Vector>::iterator iter = m_matrix.begin( ); iter < m_matrix.end( ); ++iter, ++i1 ) *iter -= a_rhs[i1];
205
206 return( *this );
207}
208
209/* *********************************************************************************************************//**
210 * Returns a new Matrix whose cells are *this* multiplied by *a_value*.
211 *
212 * @param a_value [in] The value to multiply each cell by.
213 * @return New Matrix whose cells are *this* multiply by *a_value*.
214 ***********************************************************************************************************/
215
216Matrix Matrix::operator*( double a_value ) const {
217
218 Matrix gidiMatrix( *this );
219
220 gidiMatrix *= a_value;
221 return( gidiMatrix );
222}
223
224/* *********************************************************************************************************//**
225 * Multiplies each cell of *this* by *a_value*.
226 *
227 * @param a_value [in] The value to multiply each cell by.
228 * @return Returns reference to *this*.
229 ***********************************************************************************************************/
230
231Matrix &Matrix::operator*=( double a_value ) {
232
233 for( std::vector<Vector>::iterator iter = m_matrix.begin( ); iter < m_matrix.end( ); ++iter ) *iter *= a_value;
234
235 return( *this );
236}
237
238/* *********************************************************************************************************//**
239 * Returns a new Matrix whose cells are *this* divided by *a_value*.
240 *
241 * @param a_value [in] The value to divide each cell by.
242 * @return New Matrix whose cells are *this* divided by *a_value*.
243 ***********************************************************************************************************/
244
245Matrix Matrix::operator/( double a_value ) const {
246
247 Matrix gidiMatrix( *this );
248
249 gidiMatrix /= a_value;
250 return( gidiMatrix );
251}
252
253/* *********************************************************************************************************//**
254 * Divides each cell of *this* by *a_value*.
255 *
256 * @param a_value [in] The value to divide each cell by.
257 * @return Returns reference to *this*.
258 ***********************************************************************************************************/
259
260Matrix &Matrix::operator/=( double a_value ) {
261
262 if( a_value == 0 ) throw Exception( "divide by zero." );
263 for( std::vector<Vector>::iterator iter = m_matrix.begin( ); iter < m_matrix.end( ); ++iter ) *iter /= a_value;
264
265 return( *this );
266}
267
268/* *********************************************************************************************************//**
269 * Returns the number of columns of *this*.
270 *
271 * @return The number of columns of *this*.
272 ***********************************************************************************************************/
273
274std::size_t Matrix::numberOfColumns( ) const {
275
276 if( size( ) == 0 ) return( 0 );
277 return( m_matrix[0].size( ) );
278}
279
280/* *********************************************************************************************************//**
281 * Adds a row to *this*.
282 *
283 * @param a_vector [in] The Vector to add to *this* as another row.
284 ***********************************************************************************************************/
285
286void Matrix::push_back( Vector const &a_vector ) {
287
288 if( size( ) > 0 ) {
289 if( (*this)[0].size( ) != a_vector.size( ) ) throw Exception( "matrix::push_back: size different" );
290 }
291 m_matrix.push_back( a_vector );
292}
293
294/* *********************************************************************************************************//**
295 * Transposes the cells of *this*.
296 ***********************************************************************************************************/
297
299
300 std::size_t __numberOfColumns( numberOfColumns( ) );
301 Matrix __matrix( __numberOfColumns, size( ) );
302
303 for( std::size_t i1 = 0; i1 < size( ); ++i1 ) {
304 for( std::size_t i2 = 0; i2 < __numberOfColumns; ++i2 ) __matrix( i2, i1, (*this)[i1][i2] );
305 }
306 return( __matrix );
307}
308
309/* *********************************************************************************************************//**
310 * Reverse the rows of *this*.
311 ***********************************************************************************************************/
312
314
315 std::size_t i2 = size( ), n_2 = i2 / 2;
316
317 for( std::size_t i1 = 0; i1 < i2; ++i1 ) m_matrix[i1].reverse( );
318 --i2;
319 for( std::size_t i1 = 0; i1 < n_2; ++i1, --i2 ) {
320 Vector temp = m_matrix[i1];
321
322 m_matrix[i1] = m_matrix[i2];
323 m_matrix[i2] = temp;
324 }
325}
326
327/* *********************************************************************************************************//**
328 * Prints the contents of *this* by calling the *print* method of each row with **a_prefixForRow** as an argument.
329 *
330 * @param a_prefixForRow [in] Argument passed to each row's print method.
331 ***********************************************************************************************************/
332
333void Matrix::print( std::string const &a_prefixForRow ) const {
334
335 for( std::vector<Vector>::const_iterator iter = m_matrix.begin( ); iter < m_matrix.end( ); ++iter ) iter->print( a_prefixForRow );
336}
337
338}
Matrix operator*(double a_value) const
Matrix & operator/=(double a_value)
Matrix & operator+=(double a_value)
Matrix & operator*=(double a_value)
std::vector< Vector > const & matrix() const
std::size_t size() const
Matrix transpose()
Matrix operator/(double a_value) const
Matrix & operator=(Matrix const &a_rhs)
Matrix operator-(double a_value) const
Matrix(std::size_t a_rows, std::size_t a_columns)
Matrix operator+(double a_value) const
void print(std::string const &a_prefixForRow) const
std::size_t numberOfColumns() const
void push_back(Vector const &a_vector)
Matrix & operator-=(double a_value)
std::size_t size() const
Definition GIDI_data.hpp:79
Definition GIDI.hpp:32