Skip to content
Matrix and Vector Methods

Matrix and Vector Methods

Matrices and vectors

A matrix is a two-dimensional array of double, float, or complex numbers.

A vector is a one-dimensional array of double, float, or complex numbers. The vector has no indication of whether it is vertical or horizontal. It is determined from the use context. For example, the vector operation Dot assumes that the left vector is horizontal and the right one is vertical. If the type indication is required, one-row or one-column matrices can be used. However, this is generally not necessary.

Matrices and vectors allocate memory for data dynamically. In fact, matrices and vectors are objects that have certain properties, such as the type of data they contain and dimensions. Matrix and vector properties can be obtained using methods such as vector_a.Size(), matrix_b.Rows(), vector_c.Norm(), matrix_d.Cond() and others. Any dimension can be changed.

When creating and initializing matrices, so-called static methods are used (these are like static methods of a class). For example: matrix::Eye(), matrix::Identity(), matrix::Ones(), vector::Ones(), matrix: :Zeros(), vector::Zeros(), matrix::Full(), vector::Full(), matrix::Tri().

At the moment, matrix and vector operations do not imply the use of the complex data type, as this development direction has not yet been completed.

MQL5 supports passing of matrices and vectors to DLLs. This enables the import of functions utilizing the relevant types, from external variables.

Matrices and vectors are passed to a DLL as a pointer to a buffer. For example, to pass a matrix of type float, the corresponding parameter of the function exported from the DLL must take a float-type buffer pointer.

MQL5

#import "mmlib.dll"
bool sgemm(uint flags, matrix<float> &C, const matrix<float> &A, const matrix<float> &B, ulong M, ulong N, ulong K, float alpha, float beta);
#import

C++

extern "C" __declspec(dllexport) bool sgemm(UINT flags, float *C, const float *A, const float *B, UINT64 M, UINT64 N, UINT64 K, float alpha, float beta)

In addition to buffers, you should pass matrix and vector sizes for correct processing.

All matrix and vector methods are listed below in alphabetical order.

FunctionActionCategory
ActivationCompute activation function values and write them to the passed vector/matrixMachine learning
ArgMaxReturn the index of the maximum valueStatistics
ArgMinReturn the index of the minimum valueStatistics
ArgSortReturn the sorted indexManipulations
AssignCopies a matrix, vector or array with auto castInitialization
AverageCompute the weighted average of matrix/vector valuesStatistics
CholeskyCompute the Cholesky decompositionTransformations
ClipLimits the elements of a matrix/vector to a given range of valid valuesManipulations
ColReturn a column vector. Write a vector to the specified column.Manipulations
ColsReturn the number of columns in a matrixFeatures
CompareCompare the elements of two matrices/vectors with the specified precisionManipulations
CompareByDigitsCompare the elements of two matrices/vectors with the significant figures precisionManipulations
CondCompute the condition number of a matrixFeatures
ConvolveReturn the discrete, linear convolution of two vectorsProducts
CopyReturn a copy of the given matrix/vectorManipulations
ConcatConcatenate 2 submatrices to one matrix. Concatenate 2 vectors to one vectorManipulations
CopyIndicatorBufferGet the data of the specified indicator buffer in the specified quantity to a vectorInitialization
CopyRatesGets the historical series of the MqlRates structure of the specified symbol-period in the specified amount into a matrix or vectorInitialization
CopyTicksGet ticks from an MqlTick structure into a matrix or a vectorInitialization
CopyTicksRangeGet ticks from an MqlTick structure into a matrix or a vector within the specified date rangeInitialization
CorrCoefCompute the Pearson correlation coefficient (linear correlation coefficient)Products
CorrelateCompute the cross-correlation of two vectorsProducts
CovCompute the covariance matrixProducts
CumProdReturn the cumulative product of matrix/vector elements, including those along the given axisStatistics
CumSumReturn the cumulative sum of matrix/vector elements, including those along the given axisStatistics
DerivativeCompute activation function derivative values and write them to the passed vector/matrixMachine learning
DetCompute the determinant of a square invertible matrixFeatures
DiagExtract a diagonal or construct a diagonal matrixManipulations
DotDot product of two vectorsProducts
EigComputes the eigenvalues and right eigenvectors of a square matrixTransformations
EigValsComputes the eigenvalues of a general matrixTransformations
EyeReturn a matrix with ones on the diagonal and zeros elsewhereInitialization
FillFill an existing matrix or vector with the specified valueInitialization
FlatAccess a matrix element through one index instead of twoManipulations
FullCreate and return a new matrix filled with the given valueInitialization
GeMMThe GeMM (General Matrix Multiply) method implements the general multiplication of two matricesProducts
HasNanReturn the number of NaN values in a matrix/vectorManipulations
HsplitSplit a matrix horizontally into multiple submatrices. Same as Split with axis=0Manipulations
IdentityCreate an identity matrix of the specified sizeInitialization
InitMatrix or vector initializationInitialization
InnerInner product of two matricesProducts
InvCompute the multiplicative inverse of a square invertible matrix by the Jordan-Gauss methodSolutions
KronReturn Kronecker product of two matrices, matrix and vector, vector and matrix or two vectorsProducts
LossCompute loss function values and write them to the passed vector/matrixMachine learning
LstSqReturn the least-squares solution of linear algebraic equations (for non-square or degenerate matrices)Solutions
LUImplement an LU decomposition of a matrix: the product of a lower triangular matrix and an upper triangular matrixTransformations
LUPImplement an LUP factorization with partial permutation, which refers to LU decomposition with row permutations only: PA=LUTransformations
MatMulMatrix product of two matricesProducts
MaxReturn the maximum value in a matrix/vectorStatistics
MeanCompute the arithmetic mean of element valuesStatistics
MedianCompute the median of the matrix/vector elementsStatistics
MinReturn the minimum value in a matrix/vectorStatistics
NormReturn matrix or vector normFeatures
OnesCreate and return a new matrix filled with onesInitialization
OuterCompute the outer product of two matrices or two vectorsProducts
PercentileReturn the specified percentile of values of matrix/vector elements or elements along the specified axisStatistics
PInvCompute the pseudo-inverse of a matrix by the Moore-Penrose methodSolutions
PowerRaise a square matrix to an integer powerProducts
ProdReturn the product of matrix/vector elements, which can also be executed for the given axisStatistics
PtpReturn the range of values of a matrix/vector or of the given matrix axisStatistics
QRCompute the qr factorization of a matrixTransformations
QuantileReturn the specified quantile of values of matrix/vector elements or elements along the specified axisStatistics
RandomStatic function. Create and return a new matrix or vector filled with random values. Random values are generated uniformly within the specified rangeInitialization
RankReturn matrix rank using the Gaussian methodFeatures
RegressionMetricCompute the regression metric as the deviation error from the regression line constructed on the specified data arrayStatistics
ReshapeChange the shape of a matrix without changing its dataManipulations
ResizeReturn a new matrix with a changed shape and sizeManipulations
RowReturn a row vector. Write the vector to the specified rowManipulations
RowsReturn the number of rows in a matrixFeatures
SetSets the value for a vector element by the specified indexManipulations
SizeReturn the size of vectorFeatures
SLogDetCompute the sign and logarithm of the determinant of an matrixFeatures
SolveSolve a linear matrix equation or a system of linear algebraic equationsSolutions
SortSort by placeManipulations
SpectrumCompute spectrum of a matrix as the set of its eigenvalues from the product AT*AFeatures
SplitSplit a matrix into multiple submatricesManipulations
StdReturn the standard deviation of values of matrix/vector elements or elements along the specified axisStatistics
SumReturn the sum of matrix/vector elements, which can also be executed for the given axis (axes)Statistics
SVDSingular value decompositionTransformations
SwapColsSwap columns in a matrixManipulations
SwapRowsSwap rows in a matrixManipulations
TraceReturn the sum along diagonals of the matrixFeatures
TransposeTranspose (swap the axes) and return the modified matrixManipulations
TriConstruct a matrix with ones on a specified diagonal and below, and zeros elsewhereInitialization
TriLReturn a copy of a matrix with elements above the k-th diagonal zeroed. Lower triangular matrixManipulations
TriUReturn a copy of a matrix with the elements below the k-th diagonal zeroed. Upper triangular matrixManipulations
VarCompute the variance of values of matrix/vector elementsStatistics
VsplitSplit a matrix vertically into multiple submatrices. Same as Split with axis=1Manipulations
ZerosCreate and return a new matrix filled with zerosInitialization
Last updated on