[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
2.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:
-
one can create external array, fill it and put to
mglData
variabledouble *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
mglData
instance 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
mglData
instance 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 you can use textual or HDF files, as well as import values from bitmap image (PNG is supported right now).
-
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 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.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on March 21, 2014 using texi2html 5.0.