| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
2.3.1 Array creation
One can put the data in mglData instance by several ways. Let us do it for sinus function:
-
one can create external array, fill it and put to
mglDatavariabledouble *a = new double[50]; for(int i=0;i<50;i++) a[i] = sin(M_PI*i/49.); mglData y; y.Set(a,50); -
another way is to create
mglDatainstance of the desired size and then to work directly with data in this variablemglData y(50); for(int i=0;i<50;i++) y.a[i] = sin(M_PI*i/49.); -
next way is to fill the data in
mglDatainstance by textual formula with the help ofModify()functionmglData y(50); y.Modify("sin(pi*x)"); -
or one may fill the array in some interval and modify it later
mglData y(50); y.Fill(0,M_PI); y.Modify("sin(u)"); -
finally it can be loaded from file
FILE *fp=fopen("sin.dat","wt"); // create file first for(int i=0;i<50;i++) fprintf(fp,"%g\n",sin(M_PI*i/49.)); fclose(fp); mglData y("sin.dat"); // load it -
at this one can read only part of data
FILE *fp-fopen("sin.dat","wt"); // create large file first for(int i=0;i<70;i++) fprintf(fp,"%g\n",sin(M_PI*i/49.)); fclose(fp); mglData y; y.Read("sin.dat",50); // load it
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 float dat[40][30];. Since, formaly this arrays element 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(float **dat, int N1, int N2);. For C this is functions like mgl_data_set_float2(HMDT d, const float **dat, int N1, int N2);. At this, you should keep in mind that nx=N2 and ny=N1 after conversion.
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
