matrix class in c++
C++
template<class T>
class matrix{
size_t ROW,COL;
vector<vector<T>> mat;
public:
matrix(size_t N, size_t M, int populate = 0){
this->ROW = N;
this->COL = M;
this->mat = vector<vector<T>> (ROW,vector<T> (COL,populate));
}
matrix(size_t N, int populate = 0){
this->ROW = N;
this->COL = N;
this->mat = vector<vector<T>> (ROW,vector<T> (COL,populate));
}
void __init(){
for(int i = 0; i < ROW; ++i){
for(int j = 0; j < COL; ++j){
cin >> this->mat[i][j];
}
}
}
void __display(){
for(int i = 0; i < ROW; ++i){
for(int j = 0; j < COL; ++j){
cout << this->mat[i][j] << " ";
}
cout << "\n";
}
}
matrix<T> operator*(const matrix &rhs)const{
if(this->COL != rhs.ROW){
throw "MATRIX MULTIPLICATION CANNOT HAPPEN WITH THE GIVEN MATRICES"
}
matrix<T> result(this->ROW,rhs.COL);
for(int _i = 0; _i < this->ROW; _i++){
for(int _j = 0; _j < rhs.COL; _j++){
result[_i][_j] = 0;
for(int _k = 0; _k < this->COL; ++_k){
result[_i][_j]+=(this->mat[_i][_k]*rhs.mat[_k][_j]);
}
}
}
return result;
}
matrix<T> power(int n){
if(n == 0)return matrix<T>(this->ROW, this->COL,1);
if(n == 1)return *this;
matrix p = power(n/2);
p = p*p;
if(n%2)p = p*(*this);
return p;
}
};
Also in C++:
- Title
- how to iterate over unordered_map c++
- Category
- C++
- Title
- c++ give options string
- Category
- C++
- Title
- initialize array c++
- Category
- C++
- Title
- error: invalid use of template-name without an argument list
- Category
- C++
- Title
- fill c++
- Category
- C++
- Title
- tuple c++
- Category
- C++
- Title
- c++ triple
- Category
- C++
- Title
- private and public in namespace cpp
- Category
- C++
- Title
- count number of zeros in array in O(logN)
- Category
- C++
- Title
- c++ typedef
- Category
- C++
- Title
- floor() in c++
- Category
- C++
- Title
- 2d vector
- Category
- C++
- Title
- built in popcount c++
- Category
- C++
- Title
- how to return a vector c++
- Category
- C++
- Title
- sfml default program
- Category
- C++
- Title
- GetCurrentThreadId c
- Category
- C++
- Title
- first prime numbers less than
- Category
- C++
- Title
- time function c++
- Category
- C++
- Title
- how to calculate inverse trigonometric values in c++
- Category
- C++
- Title
- maximum possible number atmost k swaps
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- c++ reverse vector
- Category
- C++
- Title
- Shortest Distance in a Maze
- Category
- C++
- Title
- initialise 2d vector in c++
- Category
- C++
- Title
- find all occurrences of a substring in a string c++
- Category
- C++
- Title
- c++ assert
- Category
- C++
- Title
- c++ map insert
- Category
- C++
- Title
- declaring vector c++
- Category
- C++
- Title
- check an stack is empty c++
- Category
- C++
- Title
- setbits
- Category
- C++
- Title
- ternary operator c++
- Category
- C++
- Title
- sqrt cpp
- Category
- C++
- Title
- c++ string
- Category
- C++
- Title
- pass by reference c++
- Category
- C++
- Title
- how to get a letter from the user c++ string
- Category
- C++
- Title
- how to convert int to string c++
- Category
- C++
- Title
- C++ while
- Category
- C++
- Title
- initialize 2d array c++
- Category
- C++
- Title
- c++ split at character
- Category
- C++
- Title
- expected initializer before 'isdigit'|
- Category
- C++
- Title
- sum of vector c++
- Category
- C++
- Title
- c++ compare char array
- Category
- C++
- Title
- how to output to console c++
- Category
- C++
- Title
- if not defined c++
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- Runtime Error: Runtime ErrorBad memory access (SIGBUS)
- Category
- C++
- Title
- hashmap in c++
- Category
- C++
- Title
- C++ and endl
- Category
- C++
- Title
- Operator overloading in C++ Programming
- Category
- C++
- Title
- c++ method name
- Category
- C++
- Title
- how to define a while statement in c++
- Category
- C++
- Title
- c++ dereference a pointer
- Category
- C++
- Title
- string to upper c++
- Category
- C++
- Title
- find in vector in c++
- Category
- C++
- Title
- how to append two vectors in c++
- Category
- C++
- Title
- add two numbers in c++
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- factorial in c++
- Category
- C++
- Title
- mao two drivers c++
- Category
- C++
- Title
- c++ initialise array
- Category
- C++
- Title
- what does count function do in hashmap
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- compile c++ program
- Category
- C++
- Title
- c++ how to skip the last element of vector
- Category
- C++
- Title
- dfenwick tree code c++
- Category
- C++
- Title
- cannot jump from switch statement to this case label c++
- Category
- C++
- Title
- c++ looping through a vector
- Category
- C++
- Title
- c++ loop trhought object
- Category
- C++
- Title
- c++ string manipulation
- Category
- C++
- Title
- c++ create button
- Category
- C++
- Title
- simple timer arduino blynk library error
- Category
- C++
- Title
- c++ class method example
- Category
- C++
- Title
- digitalwrite C++
- Category
- C++
- Title
- c++ vector
- Category
- C++
- Title
- string length c++
- Category
- C++
- Title
- tree traversal c++ in order
- Category
- C++
- Title
- foind th output c++
- Category
- C++
- Title
- convert int to binary string c++
- Category
- C++
- Title
- c++ formatting
- Category
- C++
- Title
- function declerations in C++
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- initialize vector of vector c++
- Category
- C++
- Title
- create copy of range of string c++
- Category
- C++
- Title
- euler's totient function c++
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- Arrays hackerrank solution in c++
- Category
- C++
- Title
- how to allocate on heap in c++
- Category
- C++
- Title
- retu7rn this c++
- Category
- C++
- Title
- how to cin multiple lines of strings c++
- Category
- C++
- Title
- c++ smart pointer 2d array
- Category
- C++
- Title
- c++ modulo make it give only positive numbers
- Category
- C++
- Title
- Get handle in C++
- Category
- C++
- Title
- how to convert n space separated integers in c++
- Category
- C++
- Title
- adding element in vector c++
- Category
- C++
- Title
- sum of integer in array c++
- Category
- C++
- Title
- iterate 2d array c++
- Category
- C++
- Title
- fast io c++ geeksforgeeks
- Category
- C++
- Title
- qt make widget ignore mouse events
- Category
- C++
- Title
- c++ bubble sort array
- Category
- C++
- Title
- array 2d to 1d
- Category
- C++