Steppable 0.0.1
A CAS project written from scratch in C++
Loading...
Searching...
No Matches
mat2d.hpp
1/**************************************************************************************************
2 * Copyright (c) 2023-2025 NWSOFT *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy *
5 * of this software and associated documentation files (the "Software"), to deal *
6 * in the Software without restriction, including without limitation the rights *
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *
8 * copies of the Software, and to permit persons to whom the Software is *
9 * furnished to do so, subject to the following conditions: *
10 * *
11 * The above copyright notice and this permission notice shall be included in all *
12 * copies or substantial portions of the Software. *
13 * *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *
20 * SOFTWARE. *
21 **************************************************************************************************/
22
29
30#pragma once
31
32#include "steppable/number.hpp"
33#include "testing.hpp"
34#include "types/point.hpp"
35
36#include <cstddef>
37#include <string_view>
38#include <vector>
39
40namespace steppable
41{
46 template<typename NumberT>
47 using MatVec2D = std::vector<std::vector<NumberT>>;
48
49 namespace prettyPrint::printers
50 {
56 std::string ppMatrix(const MatVec2D<Number>& matrix, int endRows = 0);
57 } // namespace prettyPrint::printers
58
59 namespace __internals::symbols
60 {
61 constexpr std::string_view MATRIX_LEFT_TOP = "\u23A1";
62 constexpr std::string_view MATRIX_LEFT_MIDDLE = "\u23A2";
63 constexpr std::string_view MATRIX_LEFT_BOTTOM = "\u23A3";
64 constexpr std::string_view MATRIX_RIGHT_TOP = "\u23A4";
65 constexpr std::string_view MATRIX_RIGHT_MIDDLE = "\u23A5";
66 constexpr std::string_view MATRIX_RIGHT_BOTTOM = "\u23A6";
67 } // namespace __internals::symbols
68
73 class Matrix
74 {
75 size_t _cols;
76 size_t _rows;
77 size_t prec = 10;
79
84 void _checkIdxSanity(const YXPoint* point) const;
85
92 static void _checkDataSanity(const MatVec2D<Number>& data);
93
100
101 public:
107 [[nodiscard]] Matrix roundOffValues(size_t prec) const;
108
112 Matrix();
113
120 Matrix(size_t rows, size_t cols, const Number& fill = Number("0"));
121
127 Matrix(const MatVec2D<Number>& data, size_t prec = 5);
128
135 template<concepts::Numeric ValueT>
136 Matrix(const MatVec2D<ValueT>& data, const size_t prec) :
137 _cols(data.front().size()), _rows(data.size()), prec(prec)
138 {
139 this->data = std::vector(_rows, std::vector(_cols, Number("0")));
140 for (size_t i = 0; i < _rows; i++)
141 for (size_t j = 0; j < _cols; j++)
142 this->data[i][j] = Number(data[i][j], prec);
143 }
144
149 [[nodiscard]] Matrix rref() const;
150
156 [[nodiscard]] Matrix ref() const;
157
163 [[nodiscard]] Number det() const;
164
169 [[nodiscard]] std::string present(int endRows = 0) const;
170
177 static Matrix ones(size_t rows, size_t cols);
178
185 static Matrix zeros(size_t rows, size_t cols);
186
194 static Matrix diag(size_t colsRows, const Number& fill = 1);
195
204 template<concepts::Numeric NumberT>
205 static Matrix diag(const size_t colsRows, const NumberT& fill = 0)
206 {
207 return diag(colsRows, Number(fill));
208 }
209
214 auto begin() { return data.begin(); }
215
220 auto end() { return data.end(); }
221
227 [[nodiscard]] Number rank() const;
228
234 [[nodiscard]] Matrix transpose() const;
235
243 Matrix operator+(const Matrix& rhs) const;
244
254 Matrix operator+=(const Matrix& rhs);
255
261 Matrix operator+() const;
262
270 Matrix operator-(const Matrix& rhs) const;
271
281 Matrix operator-=(const Matrix& rhs);
282
289 Matrix operator-() const;
290
298 Matrix operator*(const Number& rhs) const;
299
309 Matrix operator*=(const Number& rhs);
310
318 Matrix operator*(const Matrix& rhs) const;
319
329 Matrix operator*=(const Matrix& rhs);
330
339 Matrix operator^(const Number& times) const;
340
349 Matrix operator^=(const Number& times);
350
359 Matrix operator<<(const Matrix& rhs) const;
360
369 Matrix operator<<=(const Matrix& rhs);
370
379 Matrix operator>>(const Matrix& rhs) const;
380
389 Matrix operator>>=(const Matrix& rhs);
390
398 bool operator==(const Matrix& rhs) const;
399
408 bool operator!=(const Matrix& rhs) const;
409
418 Number& operator[](const YXPoint& point);
419
429 Number operator[](const YXPoint& point) const;
430
440 Matrix operator[](const YX2Points& point) const;
441
446 [[nodiscard]] size_t getRows() const { return _rows; }
447
452 [[nodiscard]] size_t getCols() const { return _cols; }
453
458 [[nodiscard]] MatVec2D<Number> getData() const { return data; }
459 };
460} // namespace steppable
Represents a mathematical matrix.
Definition mat2d.hpp:74
bool operator==(const Matrix &rhs) const
Test for equal matrices.
Definition mat2d.cpp:550
static Matrix zeros(size_t rows, size_t cols)
Creates a matrix filled with zeros.
Definition mat2d.cpp:504
auto begin()
Alias for the data begin() method to allow iterating over the matrix rows,.
Definition mat2d.hpp:214
Matrix operator+() const
Unary plus operator.
Definition mat2d.cpp:346
Matrix rref() const
Converts the matrix to its reduced row echelon form.
Definition mat2d.cpp:176
Matrix transpose() const
Transposes a matrix.
Definition mat2d.cpp:541
size_t _rows
The number of rows in the matrix.
Definition mat2d.hpp:76
Number rank() const
Calculates the rank of a matrix.
Definition mat2d.cpp:518
size_t prec
Precision of numbers in the matrix.
Definition mat2d.hpp:77
Matrix(const MatVec2D< ValueT > &data, const size_t prec)
Constructs a matrix from a 2D vector of C++ numbers.
Definition mat2d.hpp:136
Matrix operator+=(const Matrix &rhs)
Adds the other matrix to current matrix and assigns result to this matrix.
Definition mat2d.cpp:348
std::string present(int endRows=0) const
Presents the matrix as a string.
Definition mat2d.cpp:496
Matrix operator>>(const Matrix &rhs) const
Join a matrix to the left of the current matrix.
Definition mat2d.cpp:424
void _checkIdxSanity(const YXPoint *point) const
Checks whether a point is inside the matrix. Errors and exits the program if not.
Definition mat2d.cpp:133
static Matrix ones(size_t rows, size_t cols)
Creates a matrix filled with ones.
Definition mat2d.cpp:498
static Matrix diag(size_t colsRows, const Number &fill=1)
Creates a diagnal matrix.
Definition mat2d.cpp:510
bool operator!=(const Matrix &rhs) const
Test for unequal matrices.
Definition mat2d.cpp:552
Number & operator[](const YXPoint &point)
Gets the element at a point in the matrix.
Definition mat2d.cpp:554
size_t _cols
The number of columns in the matrix.
Definition mat2d.hpp:75
Matrix operator^=(const Number &times)
Raises the current matrix to a certain power, and assigns result to the current matrix.
static void _checkDataSanity(const MatVec2D< Number > &data)
Checks whether the matrix data is correct in format.
Definition mat2d.cpp:101
Matrix operator-=(const Matrix &rhs)
Subtracts the other matrix from current matrix and assigns result to this matrix.
Definition mat2d.cpp:365
size_t getRows() const
Get the number of rows in the matrix.
Definition mat2d.hpp:446
static Matrix diag(const size_t colsRows, const NumberT &fill=0)
Creates a diagnal matrix.
Definition mat2d.hpp:205
Matrix operator>>=(const Matrix &rhs)
Join a matrix to the left of the current matrix, then assign the result to the current one.
Definition mat2d.cpp:455
Matrix operator<<=(const Matrix &rhs)
Join a matrix to the right of the current matrix, then assign the result to the current one.
Definition mat2d.cpp:449
Matrix operator^(const Number &times) const
Raises the current matrix to a certain power.
Definition mat2d.cpp:473
Matrix operator<<(const Matrix &rhs) const
Join a matrix to the right of the current matrix.
Definition mat2d.cpp:399
Matrix ref() const
Converts a matrix to row echelon form.
Definition mat2d.cpp:239
Matrix operator-() const
Unary minus operator.
Definition mat2d.cpp:356
auto end()
Alias for the data end() method to allow iterating over the matrix rows,.
Definition mat2d.hpp:220
MatVec2D< Number > data
The data of the matrix.
Definition mat2d.hpp:78
size_t getCols() const
Get the number of columns in the matrix.
Definition mat2d.hpp:452
static MatVec2D< Number > roundOffValues(const MatVec2D< Number > &data, size_t prec)
Rounds off all data inside a vector to a specified precision.
Definition mat2d.cpp:154
Number det() const
Find the determinant of a matrix.
Definition mat2d.cpp:273
Matrix()
Default constructor for the Matrix class.
Definition mat2d.cpp:116
Matrix operator*(const Number &rhs) const
Scalar multiplication.
Definition mat2d.cpp:371
Matrix operator*=(const Number &rhs)
Multiplies the current matrix by a scalar value and assigns the result to this matrix.
Definition mat2d.cpp:461
MatVec2D< Number > getData() const
Get the data std::vector object from the matrix.
Definition mat2d.hpp:458
Represents a number with arbitrary precision. It basically stores the value as a string.
Definition number.hpp:80
Definition ref.cpp:39
constexpr std::string_view MATRIX_LEFT_BOTTOM
Definition mat2d.hpp:63
constexpr std::string_view MATRIX_RIGHT_BOTTOM
Definition mat2d.hpp:66
constexpr std::string_view MATRIX_RIGHT_TOP
Definition mat2d.hpp:64
constexpr std::string_view MATRIX_LEFT_MIDDLE
Definition mat2d.hpp:62
constexpr std::string_view MATRIX_LEFT_TOP
Definition mat2d.hpp:61
constexpr std::string_view MATRIX_RIGHT_MIDDLE
Definition mat2d.hpp:65
The custom-implemented printer engines for outputting expressions.
Definition baseConvert.cpp:50
std::string ppMatrix(const MatVec2D< Number > &matrix, const int endRows)
Pretty prints a matrix.
Definition mat2d.cpp:53
The public namespace for the Steppable library.
Definition argParse.cpp:40
std::vector< std::vector< NumberT > > MatVec2D
Alias for a 2D matrix represented as a vector of vectors.
Definition mat2d.hpp:47
A points object that represents two points.
Definition point.hpp:55
A point object.
Definition point.hpp:44
Untitled