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
|
/* 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>
/**
* \brief This class describes a two-dimensional matrix in dense format.
*/
class CMatrix2D
{
private:
typedef std::vector<std::vector<double>> d_matr_t; ///< std::vector<std::vector<double>>
typedef std::vector<double> d_vect_t; ///< std::vector<double>
d_matr_t m_data; ///< The data itself
size_t m_rows; ///< Number of rows in the matrix
size_t m_cols; ///< Number of columns in the matrix
public:
/**
* \brief Basic constructor.
* \details Creates empty matrix with zero in all rows and columns.
*/
CMatrix2D();
/**
* \brief Creates a matrix with the specified dimensions and sets all cells to zero.
* \param _rows Number of rows.
* \param _cols Number of columns.
*/
CMatrix2D(size_t _rows, size_t _cols);
//////////////////////////////////////////////////////////////////////////
// Work with dimensions
/**
* \brief Sets new dimensions of the matrix.
* \param _rows Number of rows.
* \param _cols Number of columns.
*/
void Resize(size_t _rows, size_t _cols);
/**
* Sets new dimensions of the matrix and fills it with the specified value.
* \param _rows Number of rows.
* \param _cols Number of columns.
* \param _val Target value.
*/
void Assign(size_t _rows, size_t _cols, double _val);
/**
* \brief Returns the current number of rows.
* \return Number of rows.
*/
size_t Rows() const;
/**
* \brief Returns the current number of columns.
* \return Number of columns.
*/
size_t Cols() const;
//////////////////////////////////////////////////////////////////////////
// Work with data
/**
* \brief Returns reference to the vector of values of the specified row.
* \param _row Index of row.
* \return Reference to the vector of values.
*/
d_vect_t& operator[](size_t _row);
/**
* Returns const reference to the vector of values of the specified row.
* \param _row Index of row.
* \return Target reference to the vector of values.
*/
const d_vect_t& operator[](size_t _row) const;
/**
* \brief Returns the vector of values for the specified row.
* \details If there is no such row, returns an empty vector.
* \param _row Index of row.
* \return Vector of values.
*/
d_vect_t GetRow(size_t _row) const;
/**
* \brief Returns the vector of values for the specified column.
* \details If there is no such column, returns an empty vector.
* \param _col Index of column.
* \return Vector of values.
*/
d_vect_t GetCol(size_t _col) const;
/**
* \brief Returns all values in the vector-of-vectors form.
* \return All values in the vector-of-vectors form.
*/
d_matr_t GetMatrix() const;
/**
* \brief Sets data to the existing row.
* \details If dimensions do not match, does nothing.
* \param _row Index of row.
* \param _vector Vector of values.
*/
void SetRow(size_t _row, const d_vect_t& _vector);
/**
* \brief Sets data to the existing column.
* \details If dimensions do not match, does nothing.
* \param _col Index of column.
* \param _vector Vector of values.
*/
void SetCol(size_t _col, const d_vect_t& _vector);
/**
* \brief Sets all values to the matrix in the vector-of-vectors form.
* \details If dimensions do not match, does nothing.
* \param _matrix All values in the vector-of-vectors form.
*/
void SetMatrix(const d_matr_t& _matrix);
/**
* \brief Fills the matrix with the specified value.
* \param _val Target value.
*/
void Fill(double _val);
/**
* \brief Removes all data and information about dimensions from the matrix.
*/
void Clear();
/**
* \brief Returns a vector with all values joining all rows one after another.
* \return Vector of all data.
*/
d_vect_t ToVector() const;
//////////////////////////////////////////////////////////////////////////
// Arithmetic
/**
* \brief Normalizes values of the matrix.
*/
void Normalize();
/**
* \brief Returns identity matrix with the specified dimensions.
* \param _size Size of the square matrix.
* \return Identity matrix.
*/
static CMatrix2D Identity(size_t _size);
/**
* \brief Adds a value to all elements of the matrix.
* \param _val Value.
* \return Reference to this matrix.
*/
CMatrix2D& operator+=(double _val);
/**
* \brief Adds a value to all elements of the matrix.
* \param _val Value.
* \return Copy of this matrix.
*/
CMatrix2D operator+(double _val) const;
/**
* \brief Subtracts a value from all elements of the matrix.
* \param _val Value.
* \return Reference to this matrix.
*/
CMatrix2D& operator-=(double _val);
/**
* \brief Subtracts a value from all elements of the matrix.
* \param _val Value.
* \return Copy of this matrix.
*/
CMatrix2D operator-(double _val) const;
/**
* \brief Multiplies a value with all elements of the matrix.
* \param _val Value.
* \return Reference to this matrix.
*/
CMatrix2D& operator*=(double _val);
/**
* \brief Multiplies a value with all elements of the matrix.
* \param _val Value.
* \return Copy of this matrix.
*/
CMatrix2D operator*(double _val) const;
/**
* \brief Divides all elements of the matrix by a value .
* \param _val Value.
* \return Reference to this matrix.
*/
CMatrix2D& operator/=(double _val);
/**
* \brief Divides all elements of the matrix by a value .
* \param _val Value.
* \return Copy of this matrix.
*/
CMatrix2D operator/(double _val) const;
/**
* \brief Adds two matrices element-wise.
* \details If dimensions of the matrices do not match, std::runtime_error exception is thrown.
* \param _matrix Other matrix.
* \return Reference to this matrix.
*/
CMatrix2D& operator+=(const CMatrix2D& _matrix);
/**
* \brief Adds two matrices element-wise.
* \details If dimensions of the matrices do not match, std::runtime_error exception is thrown.
* \param _matrix Other matrix.
* \return Copy of this matrix.
*/
CMatrix2D operator+(const CMatrix2D& _matrix) const;
/**
* \brief Subtracts two matrices element-wise.
* \details If dimensions of the matrices do not match, std::runtime_error exception is thrown.
* \param _matrix Other matrix.
* \return Reference to this matrix.
*/
CMatrix2D& operator-=(const CMatrix2D& _matrix);
/**
* \brief Subtracts two matrices element-wise.
* \details If dimensions of the matrices do not match, std::runtime_error exception is thrown.
* \param _matrix Other matrix.
* \return Copy of this matrix.
*/
CMatrix2D operator-(const CMatrix2D& _matrix) const;
/**
* \brief Multiplies two matrices element-wise.
* \details If dimensions of the matrices do not match, std::runtime_error exception is thrown.
* \param _matrix Other matrix.
* \return Copy of this matrix.
*/
CMatrix2D operator*(const CMatrix2D& _matrix) const;
};
|