-
-
Notifications
You must be signed in to change notification settings - Fork 40
Tensor Library
Omega Joctan edited this page Apr 14, 2024
·
3 revisions
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 newCTensors
object with the specified number of dimensions (DIM
).
-
Data Storage:
- The
CTensors
class internally utilizes an array ofCMatrix
objects (matrices[]
). EachCMatrix
instance holds a single MQL5matrix
object, effectively creating a multidimensional structure.
Public Methods:
-
Add (template):
- Adds an
matrix<T>
(matrix of typeT
) to theCTensors
object at a specified position (POS
). - Supports different data types (
T
) through templating.
- Adds an
-
Append (template):
- Appends a vector of type
T
to a specific dimension (POS
) of theCTensors
object. - 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
CTensors
object.
- Prints a human-readable representation of the contents of the
-
Get(ulong POS):
- Retrieves the
matrix
object at a specified position (POS
) within theCTensors
structure.
- Retrieves the
-
Fill (template):
- Fills all elements of the
CTensors
object with a specified value (value
) of typeT
.
- Fills all elements of the
-
MemoryClear():
- Releases memory allocated for the internal
CMatrix
objects 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
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: