MatMul

행렬과 벡터의 곱셈을 가능하게 하는 MatMul 메서드에는 몇 가지 오버로드가 있습니다.

Multiplying a matrix by a matrix: matrix[M][K] * matrix[K][N] = matrix[M][N]

matrix  matrix::MatMul(
  const matrix&  b      // second matrix
   );

Multiplying a vector by a matrix: horizontal vector[K] * matrix[K][N] = horizontal vector[N]

vector  vector::MatMul(
  const matrix&  b      // matrix
   );

Multiplying a matrix by a vector: matrix[M][K] * vertical vector[K] = vertical vector[M]

vector  matrix::MatMul(
  const vector&  b      // vector
   );

Scalar vector multiplication: horizontal vector * vertical vector = dot value

scalar  vector::MatMul(
  const vector&  b      // second vector
   );

매개 변수

b

[in]  헹렬 혹은 벡터.

반환값

사용된 메서드에 따라 행렬, 벡터 또는 스칼라.

참조

행렬은 곱셈과 호환되어야 합니다. 즉, 첫 번째 행렬의 열 수가 두 번째 행렬의 행 수와 같아야 합니다. 행렬 곱셈은 비가환적입니다. 일반적인 경우 첫 번째 행렬에 두 번째 행렬을 곱한 결과는 두 번째 행렬에 첫 번째 행렬을 곱한 결과와 같지 않습니다.

행렬 곱은 첫 번째 행렬의 행 벡터와 두 번째 행렬의 열 벡터의 스칼라 곱의 가능한 모든 조합으로 구성됩니다.

스칼라 곱셈에서 벡터는 길이가 같아야 합니다.

벡터와 행렬을 곱할 때 벡터의 길이는 행렬의 열 수와 정확히 일치해야 합니다.

 

MQL5의 간단한 행렬 곱셈 알고리즘:

matrix MatrixProduct(const matrixmatrix_aconst matrixmatrix_b)
  {
   matrix matrix_c;
 
   if(matrix_a.Cols()!=matrix_b.Rows())
      return(matrix_c);
      
   ulong M=matrix_a.Rows();
   ulong K=matrix_a.Cols();
   ulong N=matrix_b.Cols();
   matrix_c=matrix::Zeros(M,N);
 
   for(ulong m=0m<Mm++)
      for(ulong k=0k<Kk++)
         for(ulong n=0n<Nn++)
            matrix_c[m][n]+=matrix_a[m][k]*matrix_b[k][n];
 
   return(matrix_c);
  }

 

행렬 곱셈 예제

   matrix a={{100},
             {010}};
   matrix b={{41},
             {22},
             {13}};
   matrix c1=a.MatMul(b);
   matrix c2=b.MatMul(a);
   Print("c1 = \n", c1);
   Print("c2 = \n", c2);
/*
   c1 = 
   [[4,1]
    [2,2]]
   c2 = 
   [[4,1,0]
    [2,2,0]
    [1,3,0]]
*/

 

수평 벡터에 행렬을 곱하는 예

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 3x5 행렬 생성
   matrix m35;
   m35.Init(35Arange);
//---
   vector v3 = {123};
   Print("Product of horizontal vector v and matrix m[3,5]");
   Print("On the left, vector v3 = "v3);
   Print("On the right, matrix m35 = \n"m35);
   Print("v3.MatMul(m35) = horizontal vector v[5] \n"v3.MatMul(m35));
 
  /* Result
    Product of horizontal vector v3 and matrix m[3,5]
    On the left, vector v3 = [1,2,3]
    On the right, matrix m35 =
    [[0,1,2,3,4]
     [5,6,7,8,9]
     [10,11,12,13,14]]
    v3.MatMul(m35) = horizontal vector v[5]
    [40,46,52,58,64]
   */
  }
//+------------------------------------------------------------------+
//|  Fill the matrix with increasing values                          |
//+------------------------------------------------------------------+
void Arange(matrix & mdouble start = 0double step = 1)
  {
//---
   ulong cols = m.Cols();
   ulong rows = m.Rows();
   double value = start;
   for(ulong r = 0r < rowsr++)
     {
      for(ulong c = 0c < colsc++)
        {
         m[r][c] = value;
         value += step;
        }
     }
//---
  }

 

행렬에 수직 벡터를 곱하는 방법의 예

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 3x5 행렬 생성
   matrix m35;
   m35.Init(35Arange);
//---
   Print("Product of matrix m[3,5] and vertical vector v[5]");
   vector v5 = {1,2,3,4,5};
   Print("On the left, m35 = \n",m35);
   Print("On the right v5 = ",v5);
   Print("m35.MatMul(v5) = vertical vector v[3] \n",m35.MatMul(v5));
 
  /* Result
   Product of matrix m[3,5] and vertical vector v[5]
   On the left, m35 = 
   [[0,1,2,3,4]
    [5,6,7,8,9]
    [10,11,12,13,14]]
   On the right, v5 = [1,2,3,4,5]
   m35.MatMul(v5) = vertical vector v[3
   [40,115,190]
   */
  }
//+------------------------------------------------------------------+
//|  Fill the matrix with increasing values                          |
//+------------------------------------------------------------------+
void Arange(matrix & mdouble start = 0double step = 1)
  {
//---
   ulong cols = m.Cols();
   ulong rows = m.Rows();
   double value = start;
   for(ulong r = 0r < rowsr++)
     {
      for(ulong c = 0c < colsc++)
        {
         m[r][c] = value;
         value += step;
        }
     }
//---
  }

 

벡터의 스칼라(dot) product의 예

void OnStart()
  {
//--- 수평 벡터와 수직 벡터의 스칼라 곱
   vector a= {123};  // horizontal vector
   vector b= {456};  // vertical vector
   Print("a = "a);
   Print("b = "b);
   Print("1) a.MatMul(b) = "a.MatMul(b));
//--- Dot 메서드가 동일한 결과를 생성하는지 확인합니다.
   Print("2) a.Dot(b) = "a.Dot(b));
 
  /* Result
   a = [1,2,3]
   b = [4,5,6]
   1a.MatMul(b) = 32.0
   2a.Dot(b) = 32.0
    */
  }

 

또다른 참조

Dot, GeMM