forked from Coding-Club-IITG/Git-Workshop-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearAlgebra.cpp
More file actions
143 lines (116 loc) · 3.98 KB
/
LinearAlgebra.cpp
File metadata and controls
143 lines (116 loc) · 3.98 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
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
//Author : Anmol Abhay Jain
//Date : 29/01/2021
//coding club cpp git task->
#include <bits/stdc++.h>
using namespace std;
template <typename T>
class Matrix {
private:
vector<vector<T>> mat; // Our matrix as vector of vector
int rowSize = -1, colSize = -1; // No. of rows and columns respectively
public:
//constructor
Matrix(int N, int M, T defaultValue); // initialises a MxN matrix with defaultValue
Matrix(Matrix& temp); // Must create a deep copy of the temp matrix
//Logging Functions
void printMatrix();
//Constructor Overloading
//matrix Operation
Matrix<T> operator*(Matrix &);
Matrix<T> operator+(Matrix &);
Matrix<T> operator-(Matrix &);
// Scalar Operations
Matrix<T> operator+(double);
Matrix<T> operator-(double);
Matrix<T> operator*(double);
Matrix<T> operator/(double);
//getter functions
vector<vector<T>>& getMatrix();
int getRowSize();
int getColSize();
//setter functions
void setMatrix(vector<vector<T>>& tempMat);
//utility functions
Matrix transpose(); // returns the transpose of the matrix
Matrix matExponentiation(Matrix& M, int pow); // return M^n for the matrix
void PowerInteration(vector<T> eigenVector, double eigenValue); // Using Power interation method to find
// the most dominant eigenVector and its corresponding eigenvalue
// In this function rather than returning ans we are going to
// fill the appropriate values in eigenVector and eigenValue which are passed as param
// Many functionalites can now be added
// like finding out the PCA of the matrix,
// finding the Null space of the matrix and so on...
};
template <typename T>
Matrix<T>::Matrix(int N, int M, T defaultValue){
mat.resize(N, vector<T>(M, defaultValue)); //initialize the matrix
rowSize = N;
colSize = M;
}
template <typename T>
Matrix<T>::Matrix(Matrix& temp){
*this = temp;
// this->mat = temp.getMatrix();
// this->rowSize = temp.getRowSize();
// this->colSize = temp.getColSize();
}
template <typename T>
void Matrix<T>::printMatrix(){
cout << "Matrix: " << endl;
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
cout << " " << mat[i][j] << " ";
}
cout << endl;
}
}
template <typename T>
vector<vector<T>>& Matrix<T>::getMatrix(){
return this->mat;
}
template <typename T>
int Matrix<T>::getRowSize(){
return this->rowSize;
}
template <typename T>
int Matrix<T>::getColSize(){
return this->colSize;
}
template <typename T>
void Matrix<T>::setMatrix(vector<vector<T>>& tempMat){
mat = tempMat;
rowSize = tempMat.size();
colSize = tempMat[0].size();
}
template <typename T>
Matrix<T> Matrix<T>::operator*(Matrix& rhs){
vector<vector<T>> ansMat(rowSize, vector<T>(rhs.getColSize(), (T)0)); // the matrix which will store the value of multiplication
vector<vector<T>> B = rhs.getMatrix();
assert(colSize == rhs.getRowSize());
int i,j,k;
T temp;
for (i = 0; i < rowSize; i++){
for (j = 0; j < rhs.getColSize(); j++){
temp = (T)0;
for (k = 0; k < colSize; k++){
temp += mat[i][k] * B[k][j];
}
ansMat[i][j] = temp;
}
}
Matrix<T> C(rowSize, rhs.getColSize(), (T)0);
C.setMatrix(ansMat);
return C;
}
int main(){
cout<<"elementry tests ->"<<endl;
Matrix<int> a(4, 2, 2);
a.printMatrix();
Matrix<int> b(a);
b.printMatrix();
vector<vector<int>> tempMat(3, vector<int>(4, 5));
a.setMatrix(tempMat);
a.printMatrix();
(a*b).printMatrix();
return 0;
}