Inconsistency in vectors and matrizes, workaround class and request to MQ

 

Hello Forum.

There are these shiny new matrix and vector types that are basically classes just without the C in front. But then why can't I create an array of a matrizes to call the layers and weights of a neural net by index? Turns out you can:

#define ARRAYSIZE 2
#define COLS 2
#define ROWS 3

matrix arr_mat[];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   ArrayResize(arr_mat, ARRAYSIZE);
   for(int i = 0; i < ARRAYSIZE; i++)
     {
      arr_mat[i].Resize(COLS, ROWS);
     }

   for(int i = 0; i < ARRAYSIZE; i++)
     {
      for(int j = 0; j < COLS; j++)
        {
         for(int k = 0; k < ROWS; k++)
           {
            arr_mat[i][j][k] = double(i * j * k);
           }
        }
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   ExpertRemove();
  }
//+------------------------------------------------------------------+

This version works and when you run it in debugger it prints a few test numbers as it is supposed to. But you can't watch the values in the observation because it will just show "invalid array access". also when you type "arr[0]." there autocomplete shows no methods. Yet they work.


But if you wrap the matrix or vector type into a class you can use it as an array and observe it in debugger which makes vector and matrix operations so much easier to check.

#define ARRAYSIZE 2
#define COLS 2
#define ROWS 3


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CMatrix
  {
public:
   matrix            _mat;
                     CMatrix(void) {;}
                    ~CMatrix(void) {;}
  };


CMatrix  arr[];

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   ArrayResize(arr, ARRAYSIZE);
   for(int i = 0; i < ARRAYSIZE; i++)
     {
      arr[i]._mat.Resize(COLS, ROWS);
     }


   for(int i = 0; i < ARRAYSIZE; i++)
     {
      for(int j = 0; j < COLS; j++)
        {
         for(int k = 0; k < ROWS; k++)
           {
            arr[i]._mat[j][k] = double(i + j + k);
           }
        }
     }
     
   for(int i = 0; i < ARRAYSIZE; i++)
     {
      for(int j = 0; j < COLS; j++)
        {
         for(int k = 0; k < ROWS; k++)
           {
            Print(string(arr[i]._mat[j][k]));
           }
        }
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   ExpertRemove();
  }
//+------------------------------------------------------------------+

This above code works AND I can have it show everything in observation.


Now when I type

arr[0]._mat.

MetaEditor should proceed to show the matrix type methods available. It doesn't. Yet they still work here.

What it shows:

whatshows

What it should show:

whatshouldbeshown

Documentation on MQL5: Language Basics / Variables
Documentation on MQL5: Language Basics / Variables
  • www.mql5.com
Variables must be declared before they are used. Unique names are used to identify variables. To declare a variable, you must specify its type and...