Skip to content

Linear Algebra Library

Omega Joctan edited this page Mar 3, 2024 · 1 revision

linalg.mqh: MQL5 Library for Linear Algebra Operations

The linalg.mqh library provides various functions for performing essential linear algebra operations in MQL5, It extends some of the MQL5 builtin functions. These operations are fundamental building blocks for many machine learning algorithms and other numerical computations.

Key Functionalities:

  • Dot Product:
    • template<typename T> matrix<T> dot(matrix<T> &A, matrix<T> &B): Calculates the dot product of two matrices (A and B), numpy like: Unlike MatMul it supports both matrix multiplication and element-wise multiplication based on the dimensions of the input matrices.
  • Norm:
    • template<typename T> matrix<T> norm(const matrix<T> &A, const matrix<T> &B): Calculates the Euclidean norm (distance) between two matrices (A and B). Handles cases where matrices have different dimensions.
    • template<typename T> double norm(const vector<T> &v1, const vector<T> &v2): Calculates the Euclidean norm (distance) between two vectors (v1 and v2).
  • Outer Product:
    • template<typename T> matrix<T> outer(const matrix<T> &A,const matrix<T> &B): Computes the outer product of two matrices (A and B).
  • Singular Value Decomposition (SVD):
    • bool svd(matrix &mat, matrix &U,matrix &V,vector &singular_value): Decomposes a matrix (mat) into its singular value decomposition (SVD) components, returning the left singular vectors (U), right singular vectors (V), and singular values (singular_value).

Additional Notes:

  • The library utilizes templates to ensure function compatibility with different data types (T).
  • Error handling is implemented for specific cases like mismatched matrix dimensions.
  • The svd function directly utilizes the built-in MQL5 SVD method of the matrix class for efficiency.

Incorporating the Library:

#include "linalg.mqh"

Example Usage:

// Example: Calculating the dot product of two matrices

matrix A = {{1, 2}, {3, 4}};
matrix B = {{5, 6}, {7, 8}};

matrix C = LinAlg::dot(A, B); // Matrix multiplication
Print("Dot product (matrix multiplication):");
Print(C);

// Example: Calculating the Euclidean norm of two vectors

vector<double> v1 = {1.2, 3.4};
vector<double> v2 = {2.5, 1.8};

double distance = LinAlg::norm(v1, v2);
Print("Euclidean norm (distance) between vectors:", distance);

// Example: Performing SVD on a matrix

matrix M = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
matrix U, V;
vector<double> singular_values;

bool svd_success = LinAlg::svd(M, U, V, singular_values);

if (svd_success) {
  Print("Singular value decomposition successful:");
  // Use the decomposed components (U, V, singular_values) for further calculations
} else {
  Print("SVD failed!");
}

By understanding and using the linalg.mqh library, MQL5 users can effectively perform various linear algebra operations, enhancing their ability to implement and work with machine learning algorithms and numerical computations within their MQL5 programs.

Clone this wiki locally