-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.hpp
More file actions
67 lines (55 loc) · 1.18 KB
/
Matrix.hpp
File metadata and controls
67 lines (55 loc) · 1.18 KB
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
#pragma once
#include<iostream>
#include<vector>
#include<random>
class Matrix {
private:
std::vector<std::vector<double>> data;
size_t rows, cols;
public:
Matrix(size_t r, size_t c)
{
(*this).rows = r;
(*this).cols = c;
(*this).data.resize(r, std::vector<double>(c, 0.0));
};
size_t const getCols() { return (*this).cols; };
size_t const getRows() { return (*this).rows; };
std::vector<double>& operator[] (size_t i) { return data[i]; };
const std::vector<double>& operator[] (size_t i) const { return data[i]; };
void fillRandom(double min = -1.0, double max = 1.0)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dist(min, max);
for (size_t i = 0; i < rows; ++i)
{
for (size_t j = 0; j < cols; ++j)
{
(*this).data[i][j] = dist(gen);
}
}
}
void operator*= (size_t scalar)
{
for (size_t i = 0; i < rows; i++)
{
for (size_t j = 0; j < cols; j++)
{
(*this).data[i][j] *= scalar;
}
}
}
void print()
{
std::cout << "--Matrix--" << '\n';
for (size_t i = 0; i < rows; ++i)
{
for (size_t j = 0; j < cols; ++j)
{
std::cout << data[i][j] << ' ';
}
std::cout << '\n';
}
}
};