-
-
Notifications
You must be signed in to change notification settings - Fork 27
Tensor Library
Omega Joctan edited this page Mar 3, 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
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.