Lab_1 0.1.1
Matrix Library
Loading...
Searching...
No Matches
matrix.h
1
44#pragma once
45#include <initializer_list>
46#include <iostream>
47
48namespace linalg {
49
50 class Matrix {
51 public: //Constructors
52 Matrix() noexcept = default;
53 Matrix(size_t rows);
54 Matrix(size_t rows, size_t columns);
55 Matrix(const Matrix& m) noexcept;//copy
56 Matrix(Matrix&& m) noexcept;//move
57 Matrix(std::initializer_list<double> lst) noexcept;
58 Matrix(std::initializer_list<std::initializer_list<double>> lst);
59
60 //Destructor
61 ~Matrix() { delete[] m_ptr; }
62
63 public: //Methods
64 size_t rows() const noexcept { return m_rows; }
65 size_t columns() const noexcept { return m_columns; }
66 bool empty() const noexcept { return (m_rows == 0); }
67 void reshape(size_t new_rows, size_t new_columns);
68
69 //Operators
70 double& operator()(size_t row, size_t column);
71 const double& operator()(size_t row, size_t column) const;
72
73 //Assignment and arifmetic operators
74 Matrix& operator=(Matrix m) noexcept;
75
76 Matrix& operator+=(const Matrix& m);
77 Matrix& operator-=(const Matrix& m);
78 Matrix& operator*=(const double c) noexcept;
79 Matrix& operator*=(const Matrix& m);
80
81 //Methods for matrices
82 double trace() const;
83 double norm() const;
84 Matrix Gauss(bool rref) const; //+
85 double det() const;
86 double minor(size_t rows, size_t columns) const; //+
87
88 private: //Fields
89 double* m_ptr = nullptr;
90 size_t m_rows = 0;
91 size_t m_columns = 0;
92 };
93
94 //The output operator
95 std::ostream& operator<<(std::ostream&, const Matrix&);
96
97 //Arifmetic operators
98 Matrix operator+(const Matrix& m1, const Matrix& m2);
99 Matrix operator-(const Matrix& m1, const Matrix& m2);
100 Matrix operator*(const Matrix& m1, const Matrix& m2);
101 Matrix operator*(const Matrix& m, const double c) noexcept;
102 Matrix operator*(const double c, const Matrix& m) noexcept;
103
104 //Assigment operators out of class
105 bool operator==(const Matrix& m1, const Matrix& m2);
106 bool operator!=(const Matrix& m1, const Matrix& m2);
107
108 //Functions for matrices
109 Matrix concatenate(const Matrix& m1, const Matrix& m2); //Вопрос, почему эти функции лежат вне класса
110 Matrix transpose(const Matrix& m);
111 Matrix uni(size_t n); //Auxiliary function for creating a unit matrix //+
112 const Matrix invert_old(const Matrix& m);
113 Matrix invert(const Matrix& m);
114 Matrix power(const Matrix& m, int deg);
115}
Definition matrix.h:50
double & operator()(size_t row, size_t column)
Accesses the element at the specified position in the matrix.
Definition matrix.cpp:276
Matrix & operator=(Matrix m) noexcept
Copy assignment operator.
Definition matrix.cpp:319
double det() const
Calculates the determinant of the matrix.
Definition matrix.cpp:701
Matrix & operator*=(const double c) noexcept
Scales a matrix by a scalar.
Definition matrix.cpp:383
double minor(size_t rows, size_t columns) const
Calculates the minor of the matrix at the given position.
Definition matrix.cpp:733
Matrix & operator-=(const Matrix &m)
Subtracts one matrix from another.
Definition matrix.cpp:439
Matrix & operator+=(const Matrix &m)
Adds another matrix to the current matrix.
Definition matrix.cpp:340
Matrix Gauss(bool rref) const
Gaussian elimination.
Definition matrix.cpp:652
double trace() const
Calculates the trace of the matrix.
Definition matrix.cpp:628
double norm() const
Calculates the norm of the matrix.
Definition matrix.cpp:604