Next: , Up: Data handling   [Contents][Index]


3.3.1 Array creation

There are many ways in MathGL how data arrays can be created and filled.

One can put the data in mglData instance by several ways. Let us do it for sinus function:

Creation of 2d- and 3d-arrays is mostly the same. But one should keep in mind that class mglData uses flat data representation. For example, matrix 30*40 is presented as flat (1d-) array with length 30*40=1200 (nx=30, ny=40). The element with indexes {i,j} is a[i+nx*j]. So for 2d array we have:

  mglData z(30,40);
  for(int i=0;i<30;i++)   for(int j=0;j<40;j++)
    z.a[i+30*j] = sin(M_PI*i/29.)*sin(M_PI*j/39.);

or by using Modify() function

  mglData z(30,40);
  z.Modify("sin(pi*x)*cos(pi*y)");

The only non-obvious thing here is using multidimensional arrays in C/C++, i.e. arrays defined like mreal dat[40][30];. Since, formally these elements dat[i] can address the memory in arbitrary place you should use the proper function to convert such arrays to mglData object. For C++ this is functions like mglData::Set(mreal **dat, int N1, int N2);. For C this is functions like mgl_data_set_mreal2(HMDT d, const mreal **dat, int N1, int N2);. At this, you should keep in mind that nx=N2 and ny=N1 after conversion.





Next: , Up: Data handling   [Contents][Index]