File: DenseMDMatrix.h

package info (click to toggle)
dyssol 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,184 kB
  • sloc: cpp: 53,870; sh: 85; python: 59; makefile: 11
file content (378 lines) | stat: -rw-r--r-- 15,434 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* Copyright (c) 2020, Dyssol Development Team.
 * Copyright (c) 2023, DyssolTEC GmbH.
 * All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */

#pragma once

#include <vector>
#include <cstddef>

/**
 * Multidimensional matrix in dense format.
 */
class CDenseMDMatrix
{
private:
	std::vector<unsigned> m_vDimensions;	///< Types of the distributions
	std::vector<unsigned> m_vClasses;		///< Number of classes of the distributions
	std::vector<double> m_vData;			///< Data itself

public:
	/**
	 * \brief Basic constructor. Creates an empty matrix.
	 */
	CDenseMDMatrix();
	/**
	 * \brief Constructor creating a matrix with the given types and number of classes.
	 * \param _vTypes Distribution types.
	 * \param _vClasses Numbers of classes of distributions.
	 */
	CDenseMDMatrix(const std::vector<unsigned>& _vTypes, const std::vector<unsigned>& _vClasses);

	// ============= Functions to work with DIMENSIONS

	/**
	 * \brief Sets new dimensions set with erasing of old data.
	 * \details Type is one of the #EDistrTypes.
	 * \param _nType Distribution type.
	 * \param _nClasses Number of classes.
	 * \return Error flag.
	 */
	bool SetDimensions(unsigned _nType, unsigned _nClasses);
	/**
	 * \brief Sets new dimensions set with erasing of old data.
	 * \details Types are types of the #EDistrTypes.
	 * \param _nType1 Fisrt distribution type.
	 * \param _nClasses1 Number of classes of first distribution.
	 * \param _nType2 Second distribution type.
	 * \param _nClasses2 Number of classes of second distribution.
	 * \return Error flag.
	 */
	bool SetDimensions(unsigned _nType1, unsigned _nClasses1, unsigned _nType2, unsigned _nClasses2);
	/**
	 * \brief Sets new dimensions set with erasing of old data.
	 * \details Types are one of the #EDistrTypes.
	 * \param _nType1 First distribution type.
	 * \param _nClasses1 Number of classes of first distribution.
	 * \param _nType2 Second distribution type.
	 * \param _nClasses2 Number of classes of second distribution.
	 * \param _nType3 Third distribution type.
	 * \param _nClasses3 Number of classes of third distribution.
	 * \return Error flag.
	 */
	bool SetDimensions(unsigned _nType1, unsigned _nClasses1, unsigned _nType2, unsigned _nClasses2, unsigned _nType3, unsigned _nClasses3);
	/**
	 * \brief Sets new dimensions set with erasing of old data.
	 * \details Types is the vector of #EDistrTypes.
	 * \param _vTypes Distribution types.
	 * \param _vClasses Numbers of classes of distributions.
	 * \return Error flag.
	 */
	bool SetDimensions( const std::vector<unsigned>& _vTypes, const std::vector<unsigned>& _vClasses );
	/**
	 * \brief Returns vector with all current defined dimensions types.
	 * \return Vector of defined dimensions types.
	 */
	std::vector<unsigned> GetDimensions() const;
	/**
	 * \brief Returns vector with current numbers of classes.
	 * \return Vector of defined classes.
	 */
	std::vector<unsigned> GetClasses() const;
	/**
	 * \brief Returns current number of dimensions.
	 * \return Number of defined dimensions
	 */
	size_t GetDimensionsNumber() const;
	/**
	 * \brief Clears all data and information about dimensions.
	 */
	void Clear();

	// ============= Functions to work with DATA

	/**
	 * \brief Clears all data in the matrix by setting all values to 0.
	 * \details Dimensions set won't be erased.
	 */
	void ClearData();

	/**
	 * \brief Returns const pointer to data.
	 * \return Const pointer to data.
	 */
	const double* GetDataPtr() const;
	/**
	 * \brief Returns pointer to data.
	 * \return Pointer to data.
	 */
	double* GetDataPtr();

	/**
	 * \brief Returns length of the plain data array.
	 * \return Total data length.
	 */
	size_t GetDataLength() const;

	// ============= Data GETTERS

	/**
	 * \brief Returns value by specified dimension and coordinate with possible reducing of dimensions set.
	 * \details Returns -1 on error.
	 * \param _nDim Target dimension.
	 * \param _nCoord Coordinate.
	 * \return Value by specified dimension and coordinate.
	 */
	double GetValue( unsigned _nDim, unsigned _nCoord ) const;
	/**
	 * \brief Returns value by specified dimensions and coordinates with possible reducing of dimensions set.
	 * \details Returns -1 on error.
	 * \param _nDim1 First dimension.
	 * \param _nCoord1 First coordinate.
	 * \param _nDim2 Second dimension.
	 * \param _nCoord2 Second coordinate.
	 * \return Value by specified dimensions and coordinates.
	 */
	double GetValue( unsigned _nDim1, unsigned _nCoord1, unsigned _nDim2, unsigned _nCoord2 ) const;
	/**
	 * \brief Returns value by specified dimensions and coordinates with possible reducing of dimensions set.
	 * \details Returns -1 on error.
	 * \param _nDim1 First dimension.
	 * \param _nCoord1 First coordinate.
	 * \param _nDim2 Second dimension.
	 * \param _nCoord2 Second coordinate.
	 * \param _nDim3 Third dimension.
	 * \param _nCoord3 Third coordinate.
	 * \return Value by specified dimensions and coordinates.
	 */
	double GetValue( unsigned _nDim1, unsigned _nCoord1, unsigned _nDim2, unsigned _nCoord2, unsigned _nDim3, unsigned _nCoord3 ) const;
	/**
	 * \brief Returns value by specified coordinates according to the full defined set of dimensions.
	 * \details Returns -1 on error.
	 * \param _vCoords Coordinates.
	 * \return Value by specified coordinates.
	 */
	double GetValue( const std::vector<unsigned>& _vCoords ) const;
	/**
	 * \brief Returns value by specified coordinates and dimensions sequence.
	 * \details Dimensions set can be reduced. Returns -1 on error.
	 * \param _vDims Dimensions.
	 * \param _vCoords Coordinates.
	 * \return Value by specified dimensions and coordinates sequence.
	 */
	double GetValue( const std::vector<unsigned>& _vDims, const std::vector<unsigned>& _vCoords ) const;

	/**
	 * Returns vector of values according to specified dimension with possible reducing of dimensions set.
	 * \param _nDim Target dimension.
	 * \return Vector of values according to specified dimension.
	 */
	std::vector<double> GetVectorValue(unsigned _nDim) const;
	/**
	 * \brief Returns vector of values according to specified dimension with possible reducing of dimensions set.
	 * \param _nDim Target dimension.
	 * \param _vResult Vector of values according to specified dimension.
	 * \return Error flag.
	 */
	bool GetVectorValue( unsigned _nDim, std::vector<double>& _vResult ) const;
	/**
	 * \brief Returns vector of values according to specified dimensions and coordinates with possible reducing of dimensions set.
	 * \param _nDim1 First dimension.
	 * \param _nCoord1 First coordinate.
	 * \param _nDim2 Second dimension.
	 * \param _vResult Vector of values according to specified dimensions and coordinates.
	 * \return Error flag.
	 */
	bool GetVectorValue( unsigned _nDim1, unsigned _nCoord1, unsigned _nDim2, std::vector<double>& _vResult ) const;
	/**
	 * \brief Returns vector of values according to specified dimensions and coordinates with possible reducing of dimensions set.
	 * \param _nDim1 First dimension.
	 * \param _nCoord1 First coordinate.
	 * \param _nDim2 Second dimension.
	 * \param _nCoord2 Second coordinate.
	 * \param _nDim3 Third dimension.
	 * \param _vResult Vector of values according to specified dimensions and coordinates.
	 * \return Error flag.
	 */
	bool GetVectorValue( unsigned _nDim1, unsigned _nCoord1, unsigned _nDim2, unsigned _nCoord2, unsigned _nDim3, std::vector<double>& _vResult ) const;
	/**
	 * Returns vector of values according to specified dimensions and coordinates with possible reducing of dimensions set.
	 * \param _nDim1 First dimension.
	 * \param _nCoord1 First coordinate.
	 * \param _nDim2 Second dimension.
	 * \param _nCoord2 Second coordinate.
	 * \param _nDim3 Third dimension.
	 * \return Vector of values according to specified dimensions and coordinates.
	 */
	std::vector<double> GetVectorValue(unsigned _nDim1, unsigned _nCoord1, unsigned _nDim2, unsigned _nCoord2, unsigned _nDim3) const;
	/**
	 * \brief Returns vector of values by specified coordinates according to a full defined dimensions set.
	 * \param _vCoords Coordinates.
	 * \param _vResult Vector of values by specified coordinates.
	 * \return Error flag.
	 */
	bool GetVectorValue( const std::vector<unsigned>& _vCoords, std::vector<double>& _vResult ) const;
	/**
	 * \brief Returns vector of values according to specified dimensions and coordinates.
	 * \details Dimensions set can be reduced.
	 * \param _vDims Dimensions.
	 * \param _vCoords Coordinates.
	 * \param _vResult Vector of values according to specified dimensions and coordinates.
	 * \return Error flag.
	 */
	bool GetVectorValue( const std::vector<unsigned>& _vDims, const std::vector<unsigned>& _vCoords, std::vector<double>& _vResult ) const;
	/**
	 * Returns vector of values according to specified dimensions and coordinates. Dimensions set can be reduced.
	 * \param _vDims Dimensions.
	 * \param _vCoords Coordinates.
	 * \return Vector of values according to specified dimensions and coordinates.
	 */
	std::vector<double> GetVectorValue(const std::vector<unsigned>& _vDims, const std::vector<unsigned>& _vCoords) const;

	// ============= Data SETTERS

	/**
	 * \brief Sets value in 1D-matrix by specified coordinate.
	 * \details Sets value only if the number of dimensions is the same as in the matrix.
	 * \param _nCoord Coordinate.
	 * \param _dValue New value.
	 * \return Error flag.
	 */
	bool SetValue( unsigned _nCoord, double _dValue );
	/**
	 * \brief Sets value in 2D-matrix by specified dimensions and coordinates.
	 * \details Sets value only if the number of dimensions is the same as in the matrix.
	 * \param _nDim1 First dimension.
	 * \param _nCoord1 First coordinate.
	 * \param _nCoord2 Second coordinate.
	 * \param _dValue New value.
	 * \return Error flag.
	 */
	bool SetValue( unsigned _nDim1, unsigned _nCoord1, unsigned _nCoord2, double _dValue );
	/**
	 * \brief Sets value in 3D-matrix by specified dimensions and coordinates.
	 * \details Sets value only if the number of dimensions is the same as in the matrix.
	 * \param _nDim1 First dimension.
	 * \param _nCoord1 First coordinate.
	 * \param _nDim2 Second dimension.
	 * \param _nCoord2 Second coordinate.
	 * \param _nCoord3 Third coordinate.
	 * \param _dValue New value.
	 * \return Error flag.
	 */
	bool SetValue( unsigned _nDim1, unsigned _nCoord1, unsigned _nDim2, unsigned _nCoord2, unsigned _nCoord3, double _dValue );
	/**
	 * \brief Sets value by specified coordinates according to the full defined set of dimensions.
	 * \details Sets value only if the number of dimensions is the same as in the matrix.
	 * \param _vCoords Coordinates.
	 * \param _dValue New value.
	 * \return Error flag.
	 */
	bool SetValue( const std::vector<unsigned>& _vCoords, double _dValue );
	/**
	 * \brief Sets value by specified coordinates and dimensions sequence.
	 * \details Sets value only if the number of dimensions is the same as in the matrix.
	 * \param _vDims Dimensions.
	 * \param _vCoords Coordinates.
	 * \param _dValue New value.
	 * \return Error flag.
	 */
	bool SetValue( const std::vector<unsigned>& _vDims, const std::vector<unsigned>& _vCoords, double _dValue );

	/**
	 * \brief Sets vector of values in 1D-matrix.
	 * \param _vValue New values.
	 * \return Error flag.
	 */
	bool SetVectorValue( const std::vector<double>& _vValue );
	/**
	 * \brief Sets vector of values according to specified dimensions and coordinates in 2D-matrix.
	 * \param _nDim Target dimension.
	 * \param _nCoord Coordinate.
	 * \param _vValue New values.
	 * \return Error flag.
	 */
	bool SetVectorValue( unsigned _nDim, unsigned _nCoord, const std::vector<double>& _vValue );
	/**
	 * \brief Sets vector of values according to specified dimensions and coordinates in 3D-matrix.
	 * \param _nDim1 First dimension.
	 * \param _nCoord1 First coordinate.
	 * \param _nDim2 Second dimension.
	 * \param _nCoord2 Second coordinate.
	 * \param _vValue New values.
	 * \return Error flag.
	 */
	bool SetVectorValue( unsigned _nDim1, unsigned _nCoord1, unsigned _nDim2, unsigned _nCoord2, const std::vector<double>& _vValue );
	/**
	 * \brief Sets vector of values by specified coordinates according to a full defined dimensions set.
	 * \details Sets values only if the number of coordinates is one less than number of dimensions in the matrix.
	 * \param _vCoords Coordinates.
	 * \param _vValue New values.
	 * \return Whether an error has occurred.
	 * \return Error flag.
	 */
	bool SetVectorValue( const std::vector<unsigned>& _vCoords, const std::vector<double>& _vValue );
	/**
	 * \brief Sets vector of values according to specified dimensions and coordinates.
	 * \details Sets values only if the number of dimensions is one less than in the matrix.
	 * \param _vDims Dimensions.
	 * \param _vCoords Coordinates.
	 * \param _vValue New values.
	 * \return Error flag.
	 */
	bool SetVectorValue( const std::vector<unsigned>& _vDims, const std::vector<unsigned>& _vCoords, const std::vector<double>& _vValue );


	// ========== Normalization

	/**
	 * \brief Checks if the matrix is normalized.
	 * \return Whether the matrix is normalized.
	 */
	bool IsNormalized();
	/**
	 * \brief Normalizes the matrix so that the sum of all elements equals to 1.
	 */
	void Normalize();

	// ========== Overloaded operators

	/**
	 * \brief Adds matrix with the same dimensions.
	 * \details If dimensions are not the same, than the empty matrix will be returned.
	 * \param _matrix Other matrix.
	 */
	CDenseMDMatrix operator+( const CDenseMDMatrix& _matrix );
	/**
	 * \brief Subtracts matrix with the same dimensions.
	 * \details If dimensions are not the same, than the empty matrix will be returned.
	 * \param _matrix Other matrix.
	 */
	CDenseMDMatrix operator-( const CDenseMDMatrix& _matrix );

	/**
	 * \brief Multiplication of the matrix by a coefficient.
	 * \details
	 * \param _dFactor Coefficient.
	 */
	CDenseMDMatrix operator*( double _dFactor );


private:
	/** Checks the duplicates in dimensions vector. Return true if check is passed.*/
	bool CheckDuplicates( const std::vector<unsigned>& _vDims ) const;

	/** Returns index in m_vData according to incoming dimensions and coordinates.
	*	Doesn't check sizes of dimensions and coordinates in parameters. Returns -1 on error.*/
	size_t GetIndex( const std::vector<unsigned>& _vDims, const std::vector<unsigned>& _vCoords ) const;
	/**  If returns true: in _pResIndex will be an index in m_vData according to incoming dimensions and coordinates,
		in _pResStep will be offset between values in m_vData to work with vector, in _pResVecLength - length of the result vector.*/
	bool GetIndexAndStep(const std::vector<unsigned>& _vDims, const std::vector<unsigned>& _vCoords, size_t* _pResIndex, size_t* _pResStep, size_t* _pResVecLength) const;

	/** Returns value by specified dimension and coordinate. Doesn't check sizes of dimensions and coordinates in parameters.
	*	Returns 0 if there is no such combination of dimensions and coordinates.*/
	double GetValueRecursive( std::vector<unsigned> _vDims, std::vector<unsigned> _vCoords ) const;
	/** Returns vector value by specified dimension and coordinate. Doesn't check sizes of dimensions and coordinates in parameters.
	*	Returns false if there is no such combination of dimensions and coordinates.*/
	bool GetVectorValueRecursive( std::vector<unsigned> _vDims, std::vector<unsigned> _vCoords, std::vector<double>& _pResult ) const;
};