Skip to content

Commit

Permalink
Resolved Issue #81 (#83)
Browse files Browse the repository at this point in the history
* Resolved Issue #64

* Fixed All Operators Issue#64

* Resolved Issue#81

* Revert "Resolved Issue#81"

This reverts commit 16031e4.

* Resolved Issue #81

---------

Co-authored-by: Ishwarendra Jha <75680424+Ishwarendra@users.noreply.github.com>
  • Loading branch information
Modernbeast02 and Ishwarendra committed Feb 8, 2023
1 parent 7384387 commit eaf521a
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@ cmake-build-debug
# CHelper
**/*__tests

# Binary Executive
**/*.bin

# build
build
93 changes: 90 additions & 3 deletions src/slowmokit/ducks/matrix/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ template<class T> Matrix<T>::Matrix(const std::vector<std::vector<T>> in)
}
}

template<class T> Matrix<T> &Matrix<T>::operator*=(const T &scalar)
template<class T>
template<class G>
Matrix<T> &Matrix<T>::operator*=(const G &scalar)
{
for (int i = 0; i < n; i++)
{
Expand All @@ -44,6 +46,32 @@ template<class T> Matrix<T> &Matrix<T>::operator*=(const T &scalar)
return *this;
}

template<class T>
template<class G>
Matrix<T> &Matrix<T>::operator+=(const G &scalar)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
mat[i][j] += scalar;
}

return *this;
}

template<class T>
template<class G>
Matrix<T> &Matrix<T>::operator-=(const G &scalar)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
mat[i][j] -= scalar;
}

return *this;
}

template<class T> Matrix<T> &Matrix<T>::operator*=(const Matrix<T> &rhs)
{
auto [n2, m2] = rhs.getShape();
Expand Down Expand Up @@ -142,8 +170,7 @@ template<class T> const std::vector<T> &Matrix<T>::operator[](int i) const
template<class T>
std::ostream &operator<<(std::ostream &os, const Matrix<T> &matrix)
{
int n = std::size(matrix);
int m = std::size(matrix[0]);
auto [n, m] = matrix.getShape();

for (int i = 0; i < n; i++)
{
Expand Down Expand Up @@ -178,3 +205,63 @@ template<class T> Matrix<T> operator-(Matrix<T> lhs, const Matrix<T> &rhs)
lhs -= rhs;
return lhs;
}

template<class T, class G> Matrix<T> operator+(G num, const Matrix<T> &matrix)
{
Matrix<T> res = matrix;
res += num;
return res;
}

template<class T, class G> Matrix<T> operator-(G num, const Matrix<T> &matrix)
{
Matrix<T> res = matrix;
res -= num;
return res;
}

template<class T, class G> Matrix<T> operator*(G num, const Matrix<T> &matrix)
{
Matrix<T> res = matrix;
res *= num;
return res;
}

template<class T, class G> Matrix<T> operator+(Matrix<T> matrix, const G &num)
{
matrix += num;
return matrix;
}

template<class T, class G> Matrix<T> operator-(Matrix<T> matrix, const G &num)
{
matrix -= num;
return matrix;
}

template<class T, class G> Matrix<T> operator*(Matrix<T> matrix, const G &num)
{
matrix *= num;
return matrix;
}

template<class T> Matrix<T> Matrix<T>::matmul(const Matrix<T> rhs)
{
Matrix<T> res = *this;
res *= rhs;
return res;
}

template<class T> Matrix<T> Matrix<T>::add(const Matrix<T> rhs)
{
Matrix<T> res = *this;
res += rhs;
return res;
}

template<class T> Matrix<T> Matrix<T>::subtract(const Matrix<T> rhs)
{
Matrix<T> res = *this;
res -= rhs;
return res;
}
71 changes: 55 additions & 16 deletions src/slowmokit/ducks/matrix/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ template<class T = int> class Matrix
* @param scalar: Number with which you want to multiply matrix with
* @returns: Matrix after multiplying current matrix with scalar
*/
Matrix &operator*=(const T &);
template<class G> Matrix &operator*=(const G &);


/**
Expand All @@ -69,6 +69,22 @@ template<class T = int> class Matrix
Matrix &operator*=(const Matrix &);


/**
* @brief Overloading += to add a matrix with a scalar
* @param scalar: Number with which you want to add matrix with
* @returns: Matrix after adding current matrix with scalar
*/
template<class G> Matrix &operator+=(const G &);


/**
* @brief Overloading -= to subtract a matrix with a scalar
* @param scalar: Number with which you want to subtract matrix with
* @returns: Matrix after subtracting scalar from current matrix
*/
template<class G> Matrix &operator-=(const G &);


/**
* @brief function to get dimension of the matrix
*/
Expand All @@ -84,10 +100,6 @@ template<class T = int> class Matrix
*/
Matrix &dot(const Matrix &);

/**
* @brief
*/


/**
* @brief overloading += operator for adding another matrix to existing matrix
Expand Down Expand Up @@ -122,41 +134,68 @@ template<class T = int> class Matrix
*/
const std::vector<T> &operator[](int) const;


/**
* @brief This function will multiply two matrix
* @param rhs: This is the matrix which will be multiplied with the main
* matrix
* @throw: whatever operator *= throws
*/
Matrix matmul(const Matrix);


/**
* @brief overloading << for easy printing of Matrix
* @brief This function will add 2 matrix
* @param rhs: This is the matrix which will be added with the main matrix
* @throw: whatever operator *= throws
*/
friend std::ostream &operator<<(std::ostream &, const Matrix &);
Matrix add(const Matrix);


/**
* @brief This function will subtract 2 matrix
* @param rhs: This is the matrix which will be subtracted from the main
* matrix
* @throw: whatever operator *= throws
*/
Matrix subtract(const Matrix);
};


/**
* @brief overloading << for easy printing of Matrix
*/
template<class T> std::ostream &operator<<(std::ostream &, const Matrix<T> &);

/**
* @brief Free Function to multiply a matrix to a number or another matrix
* @param lhs: A number or a Matrix
* @param rhs: A number (only if lhs is not a number) or a Matrix
* @returns Matrix Object
*/
template<class T> Matrix<T> operator*(T, const Matrix<T> &);
template<class T> Matrix<T> operator*(Matrix<T>, const T &);
template<class T> Matrix<T> operator*(Matrix<T> lhs, const Matrix<T> &rhs);
template<class T, class G> Matrix<T> operator*(G, const Matrix<T> &);
template<class T, class G> Matrix<T> operator*(Matrix<T>, const G &);
template<class T> Matrix<T> operator*(Matrix<T>, const Matrix<T> &);

/**
* @brief Free Function to add a matrix to a number or another matrix
* @param lhs: A number or a Matrix
* @param rhs: A number (only if lhs is not a number) or a Matrix
* @returns Matrix Object
*/
template<class T> Matrix<T> operator+(T, const Matrix<T> &);
template<class T> Matrix<T> operator+(Matrix<T>, const T &);
template<class T> Matrix<T> operator+(Matrix<T> lhs, const Matrix<T> &rhs);
template<class T, class G> Matrix<T> operator+(G, const Matrix<T> &);
template<class T, class G> Matrix<T> operator+(Matrix<T>, const G &);
template<class T> Matrix<T> operator+(Matrix<T>, const Matrix<T> &);

/**
* @brief Free Function to subtract a matrix to a number or another matrix
* @param lhs: A number or a Matrix
* @param rhs: A number (only if lhs is not a number) or a Matrix
* @returns Matrix Object
*/
template<class T> Matrix<T> operator-(T, const Matrix<T> &);
template<class T> Matrix<T> operator-(Matrix<T>, const T &);
template<class T> Matrix<T> operator-(Matrix<T> lhs, const Matrix<T> &rhs);
template<class T, class G> Matrix<T> operator-(G, const Matrix<T> &);
template<class T, class G> Matrix<T> operator-(Matrix<T>, const G &);
template<class T> Matrix<T> operator-(Matrix<T>, const Matrix<T> &);


#endif // SLOWMOKIT_IO_HPP

0 comments on commit eaf521a

Please sign in to comment.