Skip to content

Tensor Library

Omega Joctan edited this page Apr 14, 2024 · 3 revisions

Tensors.mqh: MQL5 Library for Multidimensional Data Representation

The Tensors.mqh library introduces the CTensors class, offering a structured approach to handling multidimensional data in MQL5. This class can be particularly beneficial for machine learning applications that often involve working with data in higher dimensions.

img

Key Functionalities:

  • Constructor:
    • CTensors(uint DIM): Creates a new CTensors object with the specified number of dimensions (DIM).

Data Storage:

  • The CTensors class internally utilizes an array of CMatrix objects (matrices[]). Each CMatrix instance holds a single MQL5 matrix object, effectively creating a multidimensional structure.

Public Methods:

  • Add (template):
    • Adds an matrix<T> (matrix of type T) to the CTensors object at a specified position (POS).
    • Supports different data types (T) through templating.
  • Append (template):
    • Appends a vector of type T to a specific dimension (POS) of the CTensors object.
    • Useful for creating tensors from data stored in individual vectors.
  • Print_():
    • Prints a human-readable representation of the contents of the CTensors object.
  • Get(ulong POS):
    • Retrieves the matrix object at a specified position (POS) within the CTensors structure.
  • Fill (template):
    • Fills all elements of the CTensors object with a specified value (value) of type T.
  • MemoryClear():
    • Releases memory allocated for the internal CMatrix objects to avoid memory leaks.

Note:

  • The provided documentation serves as an overview. The specific implementation details and error handling mechanisms within the CTensors class are not explicitly explained here.
  • Refer to the Tensors.mqh file for the full implementation and code comments for a comprehensive understanding.

Usage Example:

// Example: Creating a 2D tensor and adding data

#include <MALE5\Tensors.mqh>
CTensors *tensor;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   tensor = new CTensors(2); //Tensor for storing two matrices
   
   matrix matrix1 = {{1,2},
                     {103,23},
                     {42,10}};
                     
   matrix matrix2 = {
                     {10,20},
                     {20,100}};
   
   tensor.Add(matrix1, 0); //adding the first matrix to the first index of the tensor
   tensor.Add(matrix2, 1); //adding the second matrix to the second index of the tensor
   
   Print("Tensor");
   tensor.Print_();
   
   delete (tensor); //Delete the tensor once you are done with it
  }

Outputs:

tensors output