Matrices and vectors in MQL5 - page 3

 

Forum on trading, automated trading systems and testing of trading strategies

Machine learning in trading: theory, models, practice and algo trading

Renat Fatkhullin , 2025.06.09 14:15

For your information, new for machine learning in MQL5:

  • MQL5: Added matrix multiplication operator @ . It works according to the rules of linear algebra and allows you to multiply matrices and vectors, as well as perform the scalar product of vectors.

    Matrix multiplication (matrix × matrix)
    matrix A( 2 , 3 );
    matrix B( 3 , 2 );
    matrix C = A @ B; // Результат: матрица C размером [2,2]
    Matrix multiplication (matrix × vector)
    matrix M( 2 , 3 );
    vector V( 3 );
    vector R = M @ V; // Результат: вектор R из 2 элементов
    Matrix multiplication (vector x matrix)
    matrix M( 2 , 3 );
    vector V( 1 , 2 );
    vector R = V @ M; // Результат: вектор R из 3 элементов
    Dot product (vector × vector)
     vector V1( 1 , 3 ), V2( 1 , 3 );
    double r = V1 @ V2; // Результат: скаляр
  • MQL5: Added the ddof parameter to the Std , Var and Cov methods. It defines the number of degrees of freedom that are subtracted from the divisor when calculating the standard deviation. For Std and Var, the default parameter is 0, for Cov it is 1.

    How does ddof affect:

    • By default ddof=0, and the standard deviation is calculated for the entire population (population standard deviation).
    • If ddof=1, then the sample standard deviation is used, which corrects the estimate for the final sample (used in statistics when working with a subset of data).

  • MQL5: Added new OpenBLAS methods:

    Calculating eigenvalues and eigenvectors

    • EigenTridiagonalDC - computes eigenvalues and eigenvectors of a symmetric tridiagonal matrix using a divide-and-conquer algorithm (LAPACK function STEVD).
    • EigenTridiagonalQR - computes eigenvalues and eigenvectors of a symmetric tridiagonal matrix using the QR algorithm (LAPACK STEV function).
    • EigenTridiagonalRobust - computes the eigenvalues and eigenvectors of a symmetric tridiagonal matrix using the MRRR (Multiple Relatively Robust Representations) algorithm (LAPACK function STEVR).
    • EigenTridiagonalBisect - computes the eigenvalues and eigenvectors of a symmetric tridiagonal matrix using the bisection algorithm (LAPACK function STEVX).
    • ReduceToBidiagonal — reduces a general m×n real or complex matrix to upper or lower bidiagonal form B using the orthogonal transformation: Q**T * A * P = B. If m≥n, then B is upper bidiagonal, otherwise lower bidiagonal. (LAPACK function GEBRD).
    • ReflectBidiagonalToQP — generates orthogonal matrices Q and P**T (or P**H for complex types) defined by the ReduceToBidiagonal method when reducing a real or complex matrix A to bidiagonal form: A = Q * B * P**T. Q and P**T are products of elementary reflectors H(i) or G(i), respectively. (LAPACK functions ORGBR, UNGBR).
    • ReduceSymmetricToTridiagonal — reduces a real symmetric or complex Hermitian matrix A to tridiagonal form B using an orthogonal similarity transformation: Q**T * A * Q = B. (LAPACK functions SYTRD, HETRD).
    • ReflectTridiagonalToQ — generates an orthogonal matrix Q that is the product of n-1 elementary reflectors of order n returned by the ReduceSymmetricToTridiagonal function.

    • LinearEquationsSolution — calculates a system of linear equations with a square matrix of coefficients A and several right-hand sides.
    • LinearEquationsSolutionTriangular — calculates a system of linear equations with a square-triangular matrix of coefficients A and several right-hand sides.
    • LinearEquationsSolutionSy — computes a system of linear equations with a symmetric or Hermitian conjugate matrix A and several right-hand sides.
    • LinearEquationsSolutionComplexSy — calculates a system of linear equations with a complex symmetric matrix A and several right-hand sides.
    • LinearEquationsSolutionGeTrid — computes a system of linear equations with a symmetric or Hermitian conjugate positive definite matrix A and several right-hand sides.
    • LinearEquationsSolutionSyPD — calculates a system of linear equations with a common (non-symmetric) tridiagonal coefficient matrix A and several right-hand sides.
    • LinearEquationsSolutionSyTridPD — computes a system of linear equations with a symmetric tridiagonal positive definite matrix A and several right-hand sides.
    • FactorizationQR - computes the QR factorization of a general m-by-n matrix: A = Q * R (the LAPACK function of GEQRF).
    • FactorizationQRNonNeg — computes the QR factorization of a general m-by-n matrix: A = Q * R, where R is an upper triangular matrix with non-negative entries on the diagonal (LAPACK function GEQRFP).
    • FactorizationQRPivot - computes the QR factorization of a general m-by-n matrix with column permutation: A * P = Q * R (GEQP3 LAPACK function).
    • FactorizationLQ — performs the LQ factorization of a general m-by-n matrix: A = L * Q (LAPACK function of GELQF).
    • FactorizationQL - performs the QL factorization of a general m-by-n matrix: A = Q * L (the LAPACK function GEQLF).
    • FactorizationRQ - performs the RQ factorization of a general m-by-n matrix: A = R * Q (LAPACK function GERQF).
    • FactorizationPLU - Computes the LU factorization of a general m-by-n matrix A using partial pivot selection and row permutations (LAPACK function GETRF).
    • FactorizationPLUGeTrid - computes the LU factorization of a general (non-symmetric) n-by-n tridiagonal matrix A using partial pivot selection and row permutations (LAPACK function GTTRF).
    • FactorizationLDL — computes the factorization for a real symmetric or complex Hermitian matrix A using diagonal pivot selection according to the Bunch-Kaufman method (LAPACK functions SYTRF and HETRF).
    • FactorizationLDLSyTridPD — computes the factorization of a symmetric positive definite (for real data) or Hermitian positive definite (for complex data) tridiagonal matrix A (LAPACK PTTRF).
    • FactorizationCholesky — computes the factorization for a real symmetric or complex Hermitian positive-definite matrix A (LAPACK POTRF function).
    • FactorizationCholeskySyPS — computes the complete pivoting Cholesky factorization of a real symmetric (or complex Hermitian) positive semidefinite n-by-n matrix A (LAPACK function PSTRF).

  • MQL5: Added the Random function and method for filling vectors and matrices with random values. Random values are generated uniformly in a given range.
     static vector vector ::Random(
       const ulong    size,       // длина вектора
       const double   min= 0.0 ,     // минимальное значение
       const double   max= 1.0      // максимальное значение
       );
    
    static matrix matrix ::Random(
       const ulong    rows,       // количество строк
       const ulong    cols         // количество столбцов
       const float    min= 0.0 ,     // минимальное значение
       const float    max= 1.0      // максимальное значение
       );