Outer

2つの行列または2つのベクトルの外積を計算します。

matrix matrix::Outer(
  const matrix&  b      // 2番目の行列
  );
 
matrix vector::Outer(
  const vector&  b      // 2番目のベクトル
  );
 

パラメータ

b

[in] 行列

戻り値

行列

注意事項

クロネッカー積と同様に、外積もブロック行列 (およびベクトル) の乗算です。

 

次は、MQL5での2つの行列の外積の単純なアルゴリズムです。

matrix MatrixOuter(const matrix& matrix_a, const matrix& matrix_b)
 {
//--- 結果の行列のサイズは、行列サイズに依存する
  ulong rows=matrix_a.Rows()*matrix_a.Cols();
  ulong cols=matrix_b.Rows()*matrix_b.Cols();
  matrix matrix_c(rows,cols);
  ulong cols_a=matrix_a.Cols();
  ulong cols_b=matrix_b.Cols();
//---
  for(ulong i=0; i<rows; i++)
    {
    ulong row_a=i/cols_a;
    ulong col_a=i%cols_a;
    for(ulong j=0; j<cols; j++)
       {
        ulong row_b=j/cols_b;
        ulong col_b=j%cols_b;
        matrix_c[i][j]=matrix_a[row_a][col_a] * matrix_b[row_b][col_b];
       }
    }
//---
  return(matrix_c);
 }

 

MQL5の例

  vector vector_a={0,1,2,3,4,5};
  vector vector_b={0,1,2,3,4,5,6};
  Print("vector_a.Outer\n",vector_a.Outer(vector_b));
  Print("vector_a.Kron\n",vector_a.Kron(vector_b));
 
  matrix matrix_a={{0,1,2},{3,4,5}};
  matrix matrix_b={{0,1,2},{3,4,5},{6,7,8}};
  Print("matrix_a.Outer\n",matrix_a.Outer(matrix_b));
  Print("matrix_a.Kron\n",matrix_a.Kron(matrix_b));
 
 /*
  vector_a.Outer
  [[0,0,0,0,0,0,0]
   [0,1,2,3,4,5,6]
   [0,2,4,6,8,10,12]
   [0,3,6,9,12,15,18]
   [0,4,8,12,16,20,24]
   [0,5,10,15,20,25,30]]
  vector_a.Kron
  [[0,0,0,0,0,0,0,0,1,2,3,4,5,6,0,2,4,6,8,10,12,0,3,6,9,12,15,18,0,4,8,12,16,20,24,0,5,10,15,20,25,30]]
  matrix_a.Outer
  [[0,0,0,0,0,0,0,0,0]
   [0,1,2,3,4,5,6,7,8]
   [0,2,4,6,8,10,12,14,16]
   [0,3,6,9,12,15,18,21,24]
   [0,4,8,12,16,20,24,28,32]
   [0,5,10,15,20,25,30,35,40]]
  matrix_a.Kron
  [[0,0,0,0,1,2,0,2,4]
   [0,0,0,3,4,5,6,8,10]
   [0,0,0,6,7,8,12,14,16]
   [0,3,6,0,4,8,0,5,10]
   [9,12,15,12,16,20,15,20,25]
   [18,21,24,24,28,32,30,35,40]]
  */

 

Pythonの例

import numpy as np
 
A = np.arange(6)
B = np.arange(7)
print("np.outer")
print(np.outer(A, B))
print("np.kron")
print(np.kron(A, B))
 
A = np.arange(6).reshape(2, 3)
B = np.arange(9).reshape(3, 3)
print("np.outer")
print(np.outer(A, B))
print("np.kron")
 
np.outer
[[ 0  0  0  0  0  0  0]
[ 0  1  2  3  4  5  6]
[ 0  2  4  6  8 10 12]
[ 0  3  6  9 12 15 18]
[ 0  4  8 12 16 20 24]
[ 0  5 10 15 20 25 30]]
np.kron
[ 0  0  0  0  0  0  0  0  1  2  3  4  5  6  0  2  4  6  8 10 12  0  3  6
 9 12 15 18  0  4  8 12 16 20 24  0  5 10 15 20 25 30]
np.outer
[[ 0  0  0  0  0  0  0  0  0]
[ 0  1  2  3  4  5  6  7  8]
[ 0  2  4  6  8 10 12 14 16]
[ 0  3  6  9 12 15 18 21 24]
[ 0  4  8 12 16 20 24 28 32]
[ 0  5 10 15 20 25 30 35 40]]
np.kron
[[ 0  0  0  0  1  2  0  2  4]
[ 0  0  0  3  4  5  6  8 10]
[ 0  0  0  6  7  8 12 14 16]
[ 0  3  6  0  4  8  0  5 10]
[ 9 12 15 12 16 20 15 20 25]
[18 21 24 24 28 32 30 35 40]]