How to use the new matrices/vectors

 

I have been noticing one or two months ago that "matrix" is suggested in blue letters in MetaEditor like a data type but I had not seen anybody mention it on the forum and so it didn't strike me as something inherently useful.

In the meanwhile there have been two version updates where MetaQuotes have explicitely introduced the new feature. Since I am interested in bringing MQL and Tensorflow (Python) together these Python oriented simplified matrices might be of interest and I would like to say thanks to MetaQuotes for this great opportunity.

I was hoping that this could be an ongoing thread to explore and clarify these possibilities.

The reason why it might become important is that more and more developers hop on the datascience/machinlearning train and there are ready solutions in R or Python, so after trying out strategies via matrices or machine learning operations in other languages you might either use the API to have the python program directly control the actions of MetaTrader5 OR you can rebuild Python functions inside MQL which is closer to machine language and therefore faster.

BTW his has been possible before with the AlgLib from Standard Library but the new functions are a lot less clunky.

So our goal is to perform complex calculations and yield data from the matrices. But to achieve this we must first initialise a matrix and put raw data in it.

The documentation says

void matrix.Init()

 

Initialize a matrix


But if we write a little test indicator and say...

OnInit(){
void matrix.Init();      
return(INIT_SUCCEEDED);
}

... It only throws a lot of errors because we haven't declared the matrix.

So let's declare it:

matrix matx1;               //--- so we declare it much like a data type

int OnInit() {
matx1.Init(4,4);            //--- here let's initialise a matrix of four by four fields
return(INIT_SUCCEEDED);
}



You will see after successful declaration there will be methods of the matrices class popping up:

matrix_methods01


Some functions like FromBuffer are not in there yet, but it is announced that they will add this functionality step by step. For now, we are going to fill our matrix by loop:

double InpArray[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

matrix matx1;

int OnInit() {

   matx1.Init(4, 4);

   int cnt = 0;
   for(int i = 0; i < matx1.Rows(); i++) {
      for(int j = 0; j < matx1.Cols(); j++) {
         matx1[i][j] = InpArray[cnt];
         cnt++;
      }
   }

   Print("matx1[0][0]", matx1[0][0]);
   Print("matx1[0][1]", matx1[0][1]);
   Print("matx1[0][2]", matx1[0][2]);
   Print("matx1[0][3]", matx1[0][3]);
   Print("matx1[1][0]", matx1[1][0]);
   Print("matx1[1][1]", matx1[1][1]);
   Print("matx1[1][2]", matx1[1][2]);
   Print("matx1[1][3]", matx1[1][3]);  //--- and so on...
   return(INIT_SUCCEEDED);
}


The matrix looks like this now inside the storage:

(0 , 1 , 2 , 3,

 4 , 5 , 6 , 7,

 8 , 9 ,10,11,

12,13,14,15)

So now that we filled our 4X4 matrix with values from 0 to 15, we can extract single rows or columns as vectors:

double InpArray[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};

matrix matx1;
vector vec_row, vec_col;

int OnInit() {

   vec_col.Init(4);
   vec_row.Init(4);
   matx1.Init(4, 4);

   int cnt = 0;
   for(int i = 0; i < matx1.Rows(); i++) {
      for(int j = 0; j < matx1.Cols(); j++) {
         matx1[i][j] = InpArray[cnt];
         cnt++;
      }   }
   vec_col = matx1.Col(0);
   vec_row = matx1.Row(0);

   Print("vec_col[0]", vec_col[0]);
   Print("vec_col[1]", vec_col[1]);
   Print("vec_col[2]", vec_col[2]);
   Print("vec_col[3]", vec_col[3]);
   Print("vec_row[0]", vec_row[0]);
   Print("vec_row[1]", vec_row[1]);
   Print("vec_row[2]", vec_row[2]);
   Print("vec_row[3]", vec_row[3]);  //--- and so on...

   return(INIT_SUCCEEDED);
}


So after a few initial experiments this stuff is handy to work with and we will see later how to use special functions like eigenvectors and least square procedures. Feel free to contribute.

red pill

Documentation on MQL5: Language Basics / Data Types / Matrices and vectors
Documentation on MQL5: Language Basics / Data Types / Matrices and vectors
  • www.mql5.com
Matrices and vectors - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

I have been working with matrixes for few weeks now and I made some test but they do not seem efficient at all.

On my test, calculating some dot products took twice the time using matrixes comparing using complete loops. I found it weird as everyone on the web says that matrix computation are supposed to be at least 10 time faster. 

Could be that the way MQL5 implements matrixes is not very good?

If someone can relate and know why, I am interested!

 
Hawaii Cashew #:

I have been working with matrixes for few weeks now and I made some test but they do not seem efficient at all.

On my test, calculating some dot products took twice the time using matrixes comparing using complete loops. I found it weird as everyone on the web says that matrix computation are supposed to be at least 10 time faster. 

Could be that the way MQL5 implements matrixes is not very good?

If someone can relate and know why, I am interested!

The problem with computational complexity concerning matrices is not easily solvable.


If you have the option to use DLLs, I suggest using Intels math library.

 
Hawaii Cashew #:

I have been working with matrixes for few weeks now and I made some test but they do not seem efficient at all.

On my test, calculating some dot products took twice the time using matrixes comparing using complete loops. I found it weird as everyone on the web says that matrix computation are supposed to be at least 10 time faster. 

Could be that the way MQL5 implements matrixes is not very good?

If someone can relate and know why, I am interested!

i think the gains start appearing en masse .

Also if you also used loops for the matrices , i.e: did this : matrix[][]= , then yeah there is no gain

 
Lorentzos Roussos #:

i think the gains start appearing en masse .

Also if you also used loops for the matrices , i.e: did this : matrix[][]= , then yeah there is no gain

No I didn't use this kind of loop, only the MatMul functions.

But on investigating a bit more, if you use a vector instead of a matrix that has only one column for example (what I was doing) the time is reduced quiet a bit and became about 30% faster than a complete loop for dot products on my test.

So faster to use Matrix.MatMul(Vector) than Matrix.MatMul(Matrix) even if the matrix seems to have the same size as the vector.

 
Dominik Egert #:
The problem with computational complexity concerning matrices is not easily solvable.

https://youtu.be/QGYvbsHDPxo?si=d23WXhjI5U3e9RIV

If you have the option to use DLLs, I suggest using Intels math library.

Thanks a lot for the direction I will look into it :)
Reason: