Skip to content

Tensor Library

Omega Joctan edited this page Mar 3, 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

CTensors tensor(2); // 2 dimensions

matrix<double> mat_1 = {{1.2, 3.4}, {5.6, 7.8}};
vector<int> vec_2 = {10, 20, 30};

tensor.Add(mat_1, 0); // Add matrix to the first dimension
tensor.Append(vec_2, 1); // Append vector to the second dimension

tensor.Print_(); // Print the contents of the tensor

matrix retrieved_matrix = tensor.Get(0); // Get the matrix from the first dimension
Print("Retrieved matrix:", retrieved_matrix);

tensor.Fill(0.0); // Fill the tensor with zeros
tensor.Print_(); // Print the modified tensor

tensor.MemoryClear(); // Release memory used by the tensor

This documentation provides a solid foundation for understanding and utilizing the CTensors class for handling multidimensional data in your MQL5 projects, particularly within the domain of machine learning.

Clone this wiki locally