linear-library
Loading...
Searching...
No Matches
VectorMatrix.hpp
Go to the documentation of this file.
1#pragma once
2
3#include<iostream>
4#include <algorithm>
5#include <vector>
6#include <stdexcept>
7
8namespace LinearAlgebra{
31 template <typename T>
33 private:
34 int cols,rows;
35 std::vector<std::vector<T>> matrix; // Матрица
36 public:
37 VectorMatrix(int rows, int cols):rows(rows), cols(cols){
38 matrix.assign(rows,std::vector<T>(cols,0));
39 };
40 VectorMatrix(const std::vector<std::vector<T>>& v):matrix(v) {
41 if (v.empty() || v[0].empty()) {
42 throw std::invalid_argument("Matrix is empty");
43 }
44 rows = v.size();
45 cols = matrix[0].size();
46
47 validation();
48 }
49 VectorMatrix(std::vector<std::vector<T>>&& v)
50 : matrix(std::move(v)) {
51 if (matrix.empty() || matrix[0].empty()) throw std::invalid_argument("Matrix is empty");
52 rows = static_cast<int>(matrix.size());
53 cols = static_cast<int>(matrix[0].size());
54 validation();
55 }
56 void validation() {
57 for (const auto& row : matrix)
58 if (static_cast<int>(row.size()) != cols)
59 throw std::invalid_argument("All rows must have the same size");
60 }
61 VectorMatrix(): rows(0),cols(0){};
62 ~VectorMatrix()=default;
63
65
67
68 T& operator()(int i,int j) {
69
70 if(i < 0 || i >= rows || j < 0 || j >=cols) {
71 throw std::out_of_range("Matrix index out of range");
72 }
73
74 return matrix[i][j];
75 }
76 const T& operator()(int i,int j) const {
77 if(i < 0 || i >= rows || j < 0 || j >=cols) {
78 throw std::out_of_range("Matrix index out of range");
79 }
80 return matrix[i][j];
81 }
82
83 std::vector<T>& operator[](int i) {
84 if (i < 0 || i >= rows)
85 throw std::out_of_range("Row index out of range");
86 return matrix[i];
87 }
88
99 const std::vector<T>& operator[](int i) const {
100 if (i < 0 || i >= rows)
101 throw std::out_of_range("Row index out of range");
102 return matrix[i];
103 }
104
107 int getRows() const {return rows;}
108
110 int getColumns() const {return cols;}
111
112 const std::vector<std::vector<T>>& getMatrix()const {return matrix;}
113 std::vector<std::vector<T>>& getMatrix() { return matrix; }
114
116 template <typename U>
117 friend std::ostream& operator<<(std::ostream& os, const VectorMatrix<U>& M);
118
119 };
120}
121
125template <typename U>
126std::ostream& operator<<(std::ostream& os,const LinearAlgebra::VectorMatrix<U>& M) {
127 int rows = M.getRows();
128 int cols = M.getColumns();
129 for (int i = 0; i < rows; ++i){
130 for (int j = 0; j < cols; ++j){
131 os<<M(i,j)<<" ";
132 }
133 if(i != rows-1)
134 os<<"\n";
135 }
136 return os;
137}
138
147template <typename T>
149 VectorMatrix<T> result(cols,rows);
150 for (int i = 0; i < cols; i++){
151 for (int j = 0; j < rows; j++){
152 auto &Aij = (*this)(i,j);
153 result(j,i) = Aij;
154 }
155 }
156 return result;
157};
158
166template <typename T>
168
169 VectorMatrix<T> result(rows,cols);
170
171 for (int i = 0; i < rows; i++){
172 auto &Ri = result.matrix[i];
173 for (int j = 0; j < cols; j++){
174 auto &Aij = matrix[i][j];
175 Ri[j] = Aij * number;
176 }
177 }
178 return result;
179};
180
181
182
190template <typename T>
192 if(cols != B.getRows()) {
193 throw std::invalid_argument("num columns A not equal num rows B");
194 }
195
196 int colsB = B.getColumns();
198 const int bs = 32;
199
200 for (int i = 0; i < rows; i += bs) {
201 for (int k = 0; k < this->cols; k += bs) {
202 for (int j = 0; j < colsB; j += bs) {
203
204 int i_end = std::min(i + bs, rows);
205 int k_end = std::min(k + bs, this->cols);
206 int j_end = std::min(j + bs, colsB);
207
208 for (int ii = i; ii < i_end; ++ii) {
209 auto &Rii = result.matrix[ii];
210 auto &Aii = matrix[ii];
211
212 for (int kk = k; kk < k_end; ++kk) {
213 T a = T(Aii[kk]);
214 auto &Bkk = B[kk];
215
216 for (int jj = j; jj < j_end; ++jj) {
217 Rii[jj] += a * Bkk[jj];
218 }
219 }
220 }
221 }
222 }
223 }
224
225 return result;
226};
std::ostream & operator<<(std::ostream &os, const LinearAlgebra::VectorMatrix< U > &M)
Definition VectorMatrix.hpp:126
Examples:
Definition VectorMatrix.hpp:32
std::vector< T > & operator[](int i)
Definition VectorMatrix.hpp:83
void validation()
Definition VectorMatrix.hpp:56
T & operator()(int i, int j)
Definition VectorMatrix.hpp:68
friend std::ostream & operator<<(std::ostream &os, const VectorMatrix< U > &M)
Definition VectorMatrix.hpp:126
VectorMatrix(const std::vector< std::vector< T > > &v)
Definition VectorMatrix.hpp:40
const std::vector< T > & operator[](int i) const
Return row of matrix.
Definition VectorMatrix.hpp:99
VectorMatrix< T > operator!() const
Transponse matrix.
Definition VectorMatrix.hpp:148
VectorMatrix< T > operator*(const VectorMatrix< T > &B) const
Matrix multiplication A*B where: A is matrix of VectorMatrix type B is matrix of VectorMatrix type.
Definition VectorMatrix.hpp:191
VectorMatrix(int rows, int cols)
Definition VectorMatrix.hpp:37
VectorMatrix()
Definition VectorMatrix.hpp:61
const std::vector< std::vector< T > > & getMatrix() const
Definition VectorMatrix.hpp:112
VectorMatrix(std::vector< std::vector< T > > &&v)
Definition VectorMatrix.hpp:49
int getColumns() const
return count columns
Definition VectorMatrix.hpp:110
std::vector< std::vector< T > > & getMatrix()
Definition VectorMatrix.hpp:113
int getRows() const
return count rows
Definition VectorMatrix.hpp:107
const T & operator()(int i, int j) const
Definition VectorMatrix.hpp:76
Definition DecomposeLU.hpp:9