#include <iostream>
/*m and n is the matrix size and T is the type of the matrix blocks*/
/*the default type of matrix is int */
template<int m, int n, typename T = int>
class Matrix {
private:
T * matrix;/*matrix is a pointer if T type*/
int _m, _n;
public:
///////////////////////////* distructor *////////////////////////////
~Matrix<m,n,T>(){
delete[] matrix;
}
/////*constructor if it get nothing as K it will fill with zeros*////
Matrix<m, n, T>(T k = 0) : _m(m), _n(n) {
matrix = new T[_m*_n];
int i = 0;
for (; i<_m*_n; i++)
matrix[i] = k;
}
/////////////////////*copy constructor *////////////////////////////
Matrix<m, n, T> (Matrix<m, n, T>& other): _m(other._m), _n(other._n)
{
delete[] matrix;
this->matrix = new T[_m*_n];
for (int i = 0; i < _m*_n; i++)
this->matrix[i] = other.getmatrix(i);
}
/////////*function to calculate the trace of the matrix*//////////////
T trace() {
T sum = 0;
int number = (_m>_n) ? _n : _m;
for (int i = 0; i<number; i++) {
sum += matrix[i*(_n)+i];
}
return sum;
}
///////*geters will return the T vale in the i place in matrix*////////
const T getmatrix (const int i) { return matrix[i]; }
T* getDiag(int&number) {
int i = 0;
number = (_m>_n) ? _n : _m;
T* diang = new T[number];
for (; i<number; i++) {
diang[i] = matrix[i*(_n)+i];
}
return diang;
}
///////////////////////*overload Ostream operator*//////////////////////////
friend std::ostream & operator<<(std::ostream & cout,const Matrix<m, n, T>&matrix) {
int i = 0;
for (; i<matrix._m*matrix._n; i++) {
std::cout << matrix.matrix[i];
// if((i+1)%matrix._n)
std::cout << " ";
if (i && !((i + 1) % matrix._n) && i != matrix._m*matrix._n)
std::cout << std::endl;
}
return cout;
}
/////////////*friend overload + operator for T + matrix*//////////////
friend Matrix<m, n, T> operator +(T num, Matrix<m, n, T>&other) {
Matrix result(other);
int i = 0;
for (; i<result._m*result._n; i++)
result.matrix[i] += num;
return result;
}
///////////////*overload + operator matrix + matrix*/////////////////
Matrix<m, n, T> operator+(Matrix<m, n, T>&other) {
Matrix result(*this);
int i = 0;
for (; i<_m*_n; i++)
result.matrix[i] += other.matrix[i];
return result;
}
/////////////////*overload + operator matrix * T/////////////////////
Matrix<m, n, T> operator *(T num) {
Matrix result(*this);
int i = 0;
for (; i<_m*_n; i++)
result.matrix[i] *= num;
return result;
}
////////////////////////*overload = operator*/////////////////////////
/*Matrix<m, n, T>& operator =(Matrix<m, n, T> other) {
for (int i = 0; i<_m*_n; i++)
matrix[i] = other.getmatrix(i);
return this;
}*/
Matrix<m, n, T>& operator =(Matrix<m, n, T> other) {
this->~Matrix<m, n, T>();
this(other);
return this;
}
//////////////////////*overload matrix ++ operator*/////////////////////
Matrix<m, n, T> operator++(int) {
Matrix temp(*this);
++(*this);
return temp;
}
//////////////////////*overload matrix -- operator*/////////////////////
Matrix<m, n, T> operator--(int) {
Matrix temp (*this);
--(*this);
return temp;
}
///////////////////////*overload () operator*/////////////////////////////////
T& operator()(int col, int row) {
return matrix[(col)*(_n)+row];
}
//////////////////////*overload matrix++ operator*////////////////////////////
Matrix<m, n, T>& operator++() {
for(int i=0;i<this->_m*this->_n;i++){
this->matrix[i]++;
}
return *this;
}
//////////////////////*overload matrix-- operator*////////////////////////////
Matrix<m, n, T>& operator--() {
for(int i=0;i<this->_m*this->_n;i++){
this->matrix[i]--;
}
return *this;
}
//////////////////////*overload - operator matrix-T*////////////////////////////
Matrix<m, n, T> operator -(T num) {
Matrix temp(*this);
for(int i=0;i<temp._m*temp._n;i++){
temp.matrix[i]=-num;
}
return temp;
}
//////////////////////*overload -1*matrix operator*//////////////////////////////
Matrix<m, n, T> operator-() {
Matrix temp(*this);
for(int i=0;i<temp._m*temp._n;i++){
temp.matrix[i]*=-1;
}
return temp;
}
};
template <int row, int col, typename T>
void printDiag(Matrix<row, col, T>& mat) {
int number;
T* diag = mat.getDiag(number);
for (int i = 0; i<number; i++)
{
std::cout << diag[i] << " ";
}
std::cout << std::endl;
delete[] diag;
}
std::cout << "Values of mat1 and mat2, before..." << std::endl;
std::cout << mat1++ << std::endl;
std::cout << mat2++ << std::endl;
std::cout << "Values of mat1 and mat2, after..." << std::endl;
std::cout << mat1 << std::endl;
std::cout << mat2 << std::endl;
mat2(0,0) = 13;
/*
* Matrix diagonal is:
* 1. In case of nxn matrix it's simple all (i, i) cell for all
* i between 0 and n - 1.
* 2. In case of nxm matrix it's all cells (i, i) cells for all
* i between 0 and min(n,m).
*/
std::cout << "Printing main diagonal of mat2" << std::endl;
printDiag(mat2);
/*
*
* Trace of any given Matrix nxm is the sum of main
* diagonal entries.
*
* */
std::cout << "trace(mat2) = " << mat2.trace() << std::endl;
std::cout << std::endl;
the operator = got some problems no matter the how i overload it
the main was giving to me i had to make the classes and the function
and i can only make return for values without using new function otherwise i will get memory leak
so the problem i get is with this kind of expression
Matrix<4, 4> res = (identity + 2) * 3;
and the problem i get is
invalid initialization of non-const reference of type ‘Matrix<4, 4>&’ from an rvalue of type ‘Matrix<4, 4>’
i know that i got and temporary value and like i cant send it to operator =
please help me fix it
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/ http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.
I am nor sure how to answer this problem yet. I did find the overloaded "+" operator is expecting (T num, Matrix<m, n, T>&other), but you are sending ( Matrix<m, n, T>&other, T num). It is backwards.
For the line you mentioned Matrix<4, 4> res = (identity + 2) * 3; I believe the problem went away when I changed the overloaded "+" operator.
The only error I have left is with Matrix<3, 2, double> m4 = (m3 + 1) - (m2 * 2) + m1; // <--- Added (). The "-" operator here is the problem. There is no overload for what you are doing here. I believe in the end it is a "Matrix - Matricx".