- Tipi Integer
- Tipi Reali (double, float)
- Numero complesso (complex)
- Tipo Stringa
- Strutture, Classi e Interfacce
- Oggetto Array Dinamico
- Matrices and vectors
- Typecasting
- Tipo Void e Costante NULL
- Tipi definiti dall'utente
- Puntatori agli Oggetti
- Riferimento. Il Modifier & e la parola chiave this
Matrices and vectors
Type vector is a special data type in MQL5, which enables operations with vectors. A vector is a one-dimensional array of type double. It is one of the fundamental concepts of linear algebra, which is used in many fields of science, including physics, geometry, and others. Vectors are used to solve systems of linear equations, in 3D graphics and in other application areas. Vectors can be added and multiplied. The length or distance between vectors can be obtained through the Norm. In programming, vectors are usually represented by arrays of homogeneous elements, which may have no regular vector operations, i.e. when arrays cannot be added or multiply, and they have no norm.
Vectors can be represented as row vectors and string vectors when working with matrices. Also, vectors in linear algebra use the concepts of covariance and contravariance. These concepts do not make any difference when writing an MQL5 code, as only the programmer decides what each object of the vector type is. For example, it can be rotation, displacement or compression vector in 3D graphics.
Generally speaking, from the point of view of linear algebra, a number is also a vector, but in a one-dimensional vector space. A vector itself can be considered as a special case of a matrix.
Type matrix is another special data type in MQL5 to represent matrices. A matrix is actually a two-dimensional array of type double. Vectors and matrices have been introduced into MQL5 for easier operations with certain types of data sets. With them, developers can benefit from the linear algebra possibilities in a simple and math-like form. Matrices can be used to compactly write systems of linear or differential equations. The number of matrix rows corresponds to the number of equations, while the the number of columns is equal to the number of unknowns. As a result, systems of linear equations can be solved through matrix operations.
The following algebraic operations are defined for the matrices:
- Addition of same-size matrices
- Multiplication of suitable-size matrices: the number of columns in the left matrix must equal the number of rows in the right matrix
- Matrix multiplication by a column vector; multiplication of a row vector by a matrix according to the matrix multiplication rule. In this sense the vector is a special case of a matrix
- Matrix multiplication by a number, that is, by a scalar
Mathematics considers many different matrix types. For example, identity matrix, symmetric, skew-symmetric, upper and lower triangular matrices, and other types. Various Normal forms play an important role in the matrix theory. They represent a certain canonical form of a matrix, which can be obtained by means of certain transformations. In practice, normal forms that have additional properties, such as for example stability, are used.
The use of vectors and matrices, or rather, of special methods of the relevant types, enables the creation of simpler, briefer and clearer code, which is close to mathematical notation. With these methods, you can avoid the need to create nested loops or to mind correct indexing of arrays in calculations. Therefore, the use of these methods increases reliability and speed in developing complex programs.
List of matrix and vector methods
Types matrix and vector include methods that correspond to the relevant NumPy library methods. Using these methods, you can translate algorithms and codes from Python to MQL5 with minimum efforts. A lot of data processing tasks, mathematical equations, neural networks and machine learning tasks can be solved using ready-made Python methods and libraries.
Method matrix/vector |
Analogous method in NumPy |
Description |
---|---|---|
void matrix.Eye(const int rows, const int cols, const int ndiag=0) |
Construct a matrix with ones on the diagonal and zeros elsewhere |
|
void matrix.Identity(const int rows) |
Construct a square matrix with ones on the main diagonal |
|
void matrix.Ones(const int rows, const int cols) |
Construct a new matrix of given rows and columns, filled with ones |
|
void matrix.Zeros(const int rows, const int cols) |
Construct a new matrix of given rows and columns, filled with zeros |
|
void matrix.Full(const int rows, const int cols, const scalar value) |
Construct a new matrix of given rows and columns, filled with scalar value |
|
void matrix.Copy(const matrix a) |
Construct a copy of the given matrix |
|
void matrix.FromBuffer(const int rows, const int cols, const scalar array[], const int count=-1, const int offset=0) |
Construct a matrix created from a 1-dimensional array |
|
void matrix.FromFile(const int rows, condt int cols, const int file_handle, const int count=-1, const int offset=0) |
Construct a matrix from data in a text or binary file |
|
void vector.FromString(const string source, const string sep=" ") |
Construct a vector initialized from text data in a string |
|
void vector.Arange(const scalar start, const scalar stop, const scalar step=1) |
Construct evenly spaced values within a given interval |
|
void matrix.Diag(const vector v, const int ndiag=0) |
Extract a diagonal or construct a diagonal matrix |
|
void matrix.Tri(const int rows, const int cols, const int ndiag=0) |
Construct a matrix with ones at and below the given diagonal and zeros elsewhere |
|
void matrix.Tril(const int rows, const int cols, const scalar array[], const int ndiag=0) |
Return a copy of a matrix with elements above the k-th diagonal zeroed |
|
void matrix.Triu(const int rows, const int cols, const scalar array[], const int ndiag=0) |
Return a copy of a matrix with the elements below the k-th diagonal zeroed |
|
void matrix.Vander(const vector v, const int cols=-1, const bool increasing=false) |
Generate a Vandermonde matrix |
|
vector matrix.Row(const unsigned nrow) |
|
Return a row vector |
vector matrix.Col(const unsigned ncol) |
|
Return a column vector |
unsigned matrix.Rows() |
|
Return the number of rows in a matrix |
unsigned matrix.Cols() |
|
Return the number of columns in a matrix |
void matrix.Init() |
|
Initialize a matrix |
matrix matrix.Transpose() |
Reverse or permute the axes of a matrix; returns the modified matrix |
|
matrix matrix.Dot(const matrix b) |
Dot product of two matrices |
|
matrix matrix.Inner(const matrix b) |
Inner product of two matrices |
|
matrix matrix.Outer(const matrix b) |
Compute the outer product of two matrices |
|
matrix matrix.MatMul(const matrix b) |
Matrix product of two matrices |
|
matrix matrix.MatrixPower(const int power) |
Raise a square matrix to the (integer) power n |
|
matrix matrix.Kron(const matrix b) |
Return Kronecker product of two matrices |
|
bool matrix.Cholesky(matrix& L) |
Return the Cholesky decomposition |
|
bool matrix.QR(matrix& Q, matrix& R) |
Compute the qr factorization of a matrix |
|
bool matrix.SVD(matrix& U, matrix& V, vector& singular_values) |
Singular value decomposition |
|
bool matrix.Eig(matrix& eigen_vectors, vector& eigen_values) |
Compute the eigenvalues and right eigenvectors of a square matrix |
|
bool matrix.EigH(matrix& eigen_vectors, vector& eigen_values) |
Return the eigenvalues and eigenvectors of a Hermitian matrix |
|
bool matrix.EigVals(vector& eigen_values) |
Compute the eigenvalues of a general matrix |
|
bool matrix.EigValsH(vector& eigen_values) |
Compute the eigenvalues of a Hermitian matrix |
|
bool matrix.LU(matrix& L, matrix& U) |
|
LU decomposition of a matrix as the product of a lower triangular matrix and an upper triangular matrix |
bool matrix.LUP(matrix& L, matrix& U, matrix& P) |
|
LUP decomposition with partial pivoting, which refers to LU decomposition with row permutations only: PA=LU |
double matrix.Norm(const norm) |
Return matrix or vector norm |
|
double matrix.Cond(const norm) |
Compute the condition number of a matrix |
|
vector matrix.Spectrum() |
|
Compute spectrum of a matrix as the set of its eigenvalues from the product AT*A |
double matrix.Det() |
Compute the determinant of an array |
|
int matrix.Rank() |
Return matrix rank of array using the Gaussian method |
|
int matrix.SLogDet(int& sign) |
Compute the sign and logarithm of the determinant of an array |
|
double matrix.Trace() |
Return the sum along diagonals of the matrix |
|
vector matrix.Solve(const vector b) |
Solve a linear matrix equation, or system of linear algebraic equations |
|
vector matrix.LstSq(const vector b) |
Return the least-squares solution of linear algebraic equations (for non-square or degenerate matrices) |
|
matrix matrix.Inv() |
Compute the (multiplicative) inverse of a matrix |
|
matrix matrix.PInv() |
Compute the pseudo-inverse of a matrix by the Moore-Penrose method |
|
int matrix.Compare(const matrix matrix_c, const double epsilon) int matrix.Compare(const matrix matrix_c, const int digits) int vector.Compare(const vector vector_c, const double epsilon) int vector.Compare(const vector vector_c, const int digits) |
|
Confronta gli elementi di due matrici/vettori con la precisione specificata |
double matrix.Flat(const ulong index) bool matrix.Flat(const ulong index,const double value) |
Consente di indirizzare un elemento di matrice tramite un indice anziché due |
|
double vector.ArgMax() double matrix.ArgMax() vector matrix.ArgMax(const int axis) |
Restituisce l'indice del valore massimo |
|
double vector.ArgMin() double matrix.ArgMin() vector matrix.ArgMin(const int axis) |
Restituisce l'indice del valore minimo |
|
double vector.Max() double matrix.Max() vector matrix.Max(const int axis) |
Restituisce il valore massimo in una matrice/vettore |
|
double vector.Mean() double matrix.Mean() vector matrix.Mean(const int axis) |
Calcola la media aritmetica dei valori degli elementi |
|
double vector.Min() double matrix.Min() vector matrix.Min(const int axis) |
Restituisce il valore minimo in una matrice/vettore |
|
double vector.Sum() double matrix.Sum() vector matrix.Sum(const int axis) |
Restituisce la somma degli elementi matrice/vettore che può essere eseguita anche per l'asse dato (assi). |
|
void vector.Clip(const double min_value,const double max_value) void matrix.Clip(const double min_value,const double max_value) |
Limita gli elementi di una matrice/vettore a un intervallo specificato di valori validi |
|
vector vector.CumProd() vector matrix.CumProd() matrix matrix.CumProd(const int axis) |
Restituisce il prodotto cumulativo di elementi matrice/vettoriali, inclusi quelli lungo l'asse specificato |
|
vector vector.CumSum() vector matrix.CumSum() matrix matrix.CumSum(const int axis) |
Restituisce la somma cumulativa di elementi matrice/vettore, inclusi quelli lungo l'asse specificato |
|
double vector.Prod(const double initial=1) double matrix.Prod(const double initial=1) vector matrix.Prod(const int axis,const double initial=1) |
Restituisce il prodotto degli elementi matrice/vettore che può essere eseguito anche per l'asse dato |
|
void matrix.Reshape(const ulong rows, const ulong cols) |
Modifica la forma di una matrice senza modificarne i dati |
|
void matrix.Resize(const ulong rows,const ulong cols) |
Restituisce una nuova matrice con una forma e una dimensione modificate |
|
bool matrix.SwapRows(const ulong row1, const ulong row2) |
|
Scambia le righe in una matrice |
bool matrix.SwapCols(const ulong col1, const ulong col2) |
|
Scambia le colonne in una matrice |
double vector.Ptp() double matrix.Ptp() vector matrix.Ptp(const int axis) |
Restituisce l'intervallo di valori di una matrice/vettore o dell'asse della matrice specificato, equivalente a Max() - Min() |
|
double vector.Percentile(const int percent) double matrix.Percentile(const int percent) vector matrix.Percentile(const int percent,const int axis) |
Restituisce il percentile specificato di valori di elementi matrice/vettore o elementi lungo l'asse specificato. I valori validi del parametro 'percentuale' sono compresi nell'intervallo [0, 100] |
|
double vector.Quantile(const int percent) double matrix.Quantile(const int percent) vector matrix.Quantile(const int percent,const int axis) |
Restituisce il quantile specificato di valori di elementi matrice/vettore o elementi lungo l'asse specificato. Il parametro 'percentuale' assume valori nell'intervallo [0, 1] |
|
double vector.Median() double matrix.Median() vector matrix.Median(const int axis) |
Calcola la mediana degli elementi matrice/vettore. La mediana è il valore medio che separa la metà più alta degli elementi array/vettore dalla metà più bassa degli elementi. |
|
double vector.Average() double matrix.Average() vector matrix.Average(const int axis) |
Calcola la media aritmetica dei valori di matrice/vettore. La somma dei pesi al denominatore non può essere uguale a 0, ma alcuni pesi possono essere 0 |
|
double vector.Std() double matrix.Std() vector matrix.Std(const int axis) |
Restituisce la deviazione standard dei valori degli elementi matrice/vettorie o degli elementi lungo l'asse dato |
|
double vector.Var() double matrix.Var() vector matrix.Var(const int axis) |
Calcola la varianza dei valori degli elementi matrice/vettore |
|
double vector.CorrCoef(const vector& v) matrix matrix.CorrCoef() |
Calcola il coefficiente di correlazione di Pearson (coefficiente di correlazione lineare). Il coefficiente di correlazione è compreso nell'intervallo [-1, 1] |
|
vector vector.Correlate(const vector& v,enum mode) |
Calcola la correlazione incrociata di due vettori. Il parametro 'mode' determina la modalità di calcolo della convoluzione lineare |
|
vector vector.Convolve(const vector& v, enum mode) |
Restituisce la convoluzione lineare e discreta di due vettori Il parametro 'mode' determina la modalità di calcolo della convoluzione lineare |
|
matrix matrix.Cov() matrix vector.Cov(const vector& v); (resulting matrix 2 x 2) |
Calcola la matrice di covarianza. La covarianza di due campioni (due variabili casuali) è una misura della loro dipendenza lineare |