Tensor Library - MegaJoctan/MALE5 GitHub Wiki
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.
Key Functionalities:
- Constructor:
CTensors(uint DIM): Creates a newCTensorsobject with the specified number of dimensions (DIM).
Data Storage:
- The
CTensorsclass internally utilizes an array ofCMatrixobjects (matrices[]). EachCMatrixinstance holds a single MQL5matrixobject, effectively creating a multidimensional structure.
Public Methods:
- Add (template):
- Adds an
matrix<T>(matrix of typeT) to theCTensorsobject at a specified position (POS). - Supports different data types (
T) through templating.
- Adds an
- Append (template):
- Appends a vector of type
Tto a specific dimension (POS) of theCTensorsobject. - Useful for creating tensors from data stored in individual vectors.
- Appends a vector of type
- Print_():
- Prints a human-readable representation of the contents of the
CTensorsobject.
- Prints a human-readable representation of the contents of the
- Get(ulong POS):
- Retrieves the
matrixobject at a specified position (POS) within theCTensorsstructure.
- Retrieves the
- Fill (template):
- Fills all elements of the
CTensorsobject with a specified value (value) of typeT.
- Fills all elements of the
- MemoryClear():
- Releases memory allocated for the internal
CMatrixobjects to avoid memory leaks.
- Releases memory allocated for the internal
Note:
- The provided documentation serves as an overview. The specific implementation details and error handling mechanisms within the
CTensorsclass are not explicitly explained here. - Refer to the
Tensors.mqhfile 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: