Skip to content

Commit

Permalink
dot method numpy like matrix multiplication, much flexible and resiliant
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaJoctan committed Mar 3, 2024
1 parent 91208a0 commit 3a1c674
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions linalg.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Omega Joctan"
#property link "https://www.mql5.com/en/users/omegajoctan"

#include "MatrixExtend.mqh"
//+------------------------------------------------------------------+
//| implementations of standard linear algebra algorithms |
//+------------------------------------------------------------------+
Expand Down Expand Up @@ -43,15 +45,47 @@ LinAlg::~LinAlg(void)

}
//+------------------------------------------------------------------+
//| Dot product of two matrices. |
//| Dot product of two matrices | Flexible funciton - numpy like |
//+------------------------------------------------------------------+
template<typename T>
matrix<T> LinAlg::dot(matrix<T> &A,matrix<T> &B)
{
return A.MatMul(B);
matrix<T> LinAlg::dot(matrix<T> &A, matrix<T> &B)
{
matrix Z={};

if (A.Cols() == B.Rows()) //Valid Normal matrix multiplication
{
Z = A.MatMul(B);
return Z;
}
else
{
//---Check for one dimensional matrices | Scalar

if ((A.Rows()==1 && A.Cols()==1))
{
Z = B * A[0][0];
return Z;
}

if (B.Rows()==1 && B.Cols()==1)
{
Z = B[0][0] * A;
return Z;
}

//-- Element wise multiplication

if (A.Rows()==B.Rows() && A.Cols()==B.Cols())
{
Z = A * B;
return Z;
}
}

return Z;
}
//+------------------------------------------------------------------+
//| Matrix or vector<T> norm. | Finds the equlidean distance of the |
//| Matrix or vector<T> norm. | Finds the equlidean distance of the |
//| two matrices |
//+------------------------------------------------------------------+
template<typename T>
Expand Down

0 comments on commit 3a1c674

Please sign in to comment.