From eaf521a54b387909ea7570edec250d5ccc6283a3 Mon Sep 17 00:00:00 2001 From: Modernbeast02 <89138051+Modernbeast02@users.noreply.github.com> Date: Wed, 8 Feb 2023 21:02:05 +0530 Subject: [PATCH] Resolved Issue #81 (#83) * Resolved Issue #64 * Fixed All Operators Issue#64 * Resolved Issue#81 * Revert "Resolved Issue#81" This reverts commit 16031e49efa80167c6d0e127d89d9eed6aa44b80. * Resolved Issue #81 --------- Co-authored-by: Ishwarendra Jha <75680424+Ishwarendra@users.noreply.github.com> --- .gitignore | 3 + src/slowmokit/ducks/matrix/matrix.cpp | 93 ++++++++++++++++++++++++++- src/slowmokit/ducks/matrix/matrix.hpp | 71 +++++++++++++++----- 3 files changed, 148 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index f849259..4ed0d3d 100644 --- a/.gitignore +++ b/.gitignore @@ -43,5 +43,8 @@ cmake-build-debug # CHelper **/*__tests +# Binary Executive +**/*.bin + # build build diff --git a/src/slowmokit/ducks/matrix/matrix.cpp b/src/slowmokit/ducks/matrix/matrix.cpp index baf04e6..1f02f8d 100644 --- a/src/slowmokit/ducks/matrix/matrix.cpp +++ b/src/slowmokit/ducks/matrix/matrix.cpp @@ -33,7 +33,9 @@ template Matrix::Matrix(const std::vector> in) } } -template Matrix &Matrix::operator*=(const T &scalar) +template +template +Matrix &Matrix::operator*=(const G &scalar) { for (int i = 0; i < n; i++) { @@ -44,6 +46,32 @@ template Matrix &Matrix::operator*=(const T &scalar) return *this; } +template +template +Matrix &Matrix::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 +template +Matrix &Matrix::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 Matrix &Matrix::operator*=(const Matrix &rhs) { auto [n2, m2] = rhs.getShape(); @@ -142,8 +170,7 @@ template const std::vector &Matrix::operator[](int i) const template std::ostream &operator<<(std::ostream &os, const Matrix &matrix) { - int n = std::size(matrix); - int m = std::size(matrix[0]); + auto [n, m] = matrix.getShape(); for (int i = 0; i < n; i++) { @@ -178,3 +205,63 @@ template Matrix operator-(Matrix lhs, const Matrix &rhs) lhs -= rhs; return lhs; } + +template Matrix operator+(G num, const Matrix &matrix) +{ + Matrix res = matrix; + res += num; + return res; +} + +template Matrix operator-(G num, const Matrix &matrix) +{ + Matrix res = matrix; + res -= num; + return res; +} + +template Matrix operator*(G num, const Matrix &matrix) +{ + Matrix res = matrix; + res *= num; + return res; +} + +template Matrix operator+(Matrix matrix, const G &num) +{ + matrix += num; + return matrix; +} + +template Matrix operator-(Matrix matrix, const G &num) +{ + matrix -= num; + return matrix; +} + +template Matrix operator*(Matrix matrix, const G &num) +{ + matrix *= num; + return matrix; +} + +template Matrix Matrix::matmul(const Matrix rhs) +{ + Matrix res = *this; + res *= rhs; + return res; +} + +template Matrix Matrix::add(const Matrix rhs) +{ + Matrix res = *this; + res += rhs; + return res; +} + +template Matrix Matrix::subtract(const Matrix rhs) +{ + Matrix res = *this; + res -= rhs; + return res; +} \ No newline at end of file diff --git a/src/slowmokit/ducks/matrix/matrix.hpp b/src/slowmokit/ducks/matrix/matrix.hpp index 4ccf044..6a6343c 100644 --- a/src/slowmokit/ducks/matrix/matrix.hpp +++ b/src/slowmokit/ducks/matrix/matrix.hpp @@ -55,7 +55,7 @@ template 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 Matrix &operator*=(const G &); /** @@ -69,6 +69,22 @@ template 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 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 Matrix &operator-=(const G &); + + /** * @brief function to get dimension of the matrix */ @@ -84,10 +100,6 @@ template class Matrix */ Matrix &dot(const Matrix &); - /** - * @brief - */ - /** * @brief overloading += operator for adding another matrix to existing matrix @@ -122,21 +134,48 @@ template class Matrix */ const std::vector &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 std::ostream &operator<<(std::ostream &, const Matrix &); + /** * @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 Matrix operator*(T, const Matrix &); -template Matrix operator*(Matrix, const T &); -template Matrix operator*(Matrix lhs, const Matrix &rhs); +template Matrix operator*(G, const Matrix &); +template Matrix operator*(Matrix, const G &); +template Matrix operator*(Matrix, const Matrix &); /** * @brief Free Function to add a matrix to a number or another matrix @@ -144,9 +183,9 @@ template Matrix operator*(Matrix lhs, const Matrix &rhs); * @param rhs: A number (only if lhs is not a number) or a Matrix * @returns Matrix Object */ -template Matrix operator+(T, const Matrix &); -template Matrix operator+(Matrix, const T &); -template Matrix operator+(Matrix lhs, const Matrix &rhs); +template Matrix operator+(G, const Matrix &); +template Matrix operator+(Matrix, const G &); +template Matrix operator+(Matrix, const Matrix &); /** * @brief Free Function to subtract a matrix to a number or another matrix @@ -154,9 +193,9 @@ template Matrix operator+(Matrix lhs, const Matrix &rhs); * @param rhs: A number (only if lhs is not a number) or a Matrix * @returns Matrix Object */ -template Matrix operator-(T, const Matrix &); -template Matrix operator-(Matrix, const T &); -template Matrix operator-(Matrix lhs, const Matrix &rhs); +template Matrix operator-(G, const Matrix &); +template Matrix operator-(Matrix, const G &); +template Matrix operator-(Matrix, const Matrix &); #endif // SLOWMOKIT_IO_HPP