[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
2.3.2 Data changing
MathGL has functions for data processing: differentiating, integrating, smoothing and so on (for more detail, see section mglData class). Let us consider some examples. The simplest ones are integration and differentiation. The direction in which operation will be performed is specified by textual string, which may contain symbols ‘x’, ‘y’ or ‘z’. For example, the call of Diff("x")
will differentiate data along ‘x’ direction; the call of Integral("xy")
perform the double integration of data along ‘x’ and ‘y’ directions; the call of Diff2("xyz")
will apply 3d Laplace operator to data and so on. Example of this operations on 2d array a=x*y is presented in code:
int sample(mglGraph *gr, void *) { mglData a(30,40); a.Modify("x*y"); gr->Axis(mglPoint(0,0,0),mglPoint(1,1,1)); gr->SubPlot(2,2,0); gr->Rotate(60,40); gr->Surf(a); gr->Box(); gr->Puts(mglPoint(0.7,1,1.2),"a(x,y)"); gr->SubPlot(2,2,1); gr->Rotate(60,40); a.Diff("x"); gr->Surf(a); gr->Box(); gr->Puts(mglPoint(0.7,1,1.2),"da/dx"); gr->SubPlot(2,2,2); gr->Rotate(60,40); a.Integral("xy"); gr->Surf(a); gr->Box(); gr->Puts(mglPoint(0.7,1,1.2),"\\int da/dx dxdy"); gr->SubPlot(2,2,3); gr->Rotate(60,40); a.Diff2("y"); gr->Surf(a); gr->Box(); gr->Puts(mglPoint(0.7,1,1.2),"\\int {d^2}a/dxdy dx"); return 0; }
Example of data differentiation and integration
Data smoothing (function Smooth()
) is more interesting and important. This function has 2 main arguments: type of smoothing and its direction. Now 4 methods are supported: SMOOTH_NONE
does nothing for delta=0 or approaches data to zero with the step delta, SMOOTH_LINE_3
linear averaging by 3 points, SMOOTH_LINE_5
linear averaging by 5 points, SMOOTH_QUAD_5
quadratic averaging by 5 points. Let me demonstrate it for 1d case:
int sample(mglGraph *gr, void *) { mglData y0(30),y1,y2,y3; y0.Modify("0.4*sin(2*pi*x)+0.3*cos(3*pi*x)-0.4*sin(4*pi*x)+0.2*rnd"); y1=y0; y1.Smooth(SMOOTH_LINE_3); y2=y0; y2.Smooth(SMOOTH_LINE_5); y3=y0; y3.Smooth(SMOOTH_QUAD_5); gr->Plot(y0,"k"); gr->AddLegend("NONE","k"); gr->Plot(y1,"r"); gr->AddLegend("LINE_3","r"); gr->Plot(y2,"g"); gr->AddLegend("LINE_5","g"); gr->Plot(y3,"b"); gr->AddLegend("QUAD_5","b"); gr->Legend(); gr->Box(); return 0; }
Example of data smoothing
Finally one can create new data arrays on base of the existing one: extract slice, row or column of data (SubData()
), summarize along some of direction(s) (Sum()
), find distribution of data elements (Hist()
). Note, that all these functions are not thread-safe because they use static internal variable for output array. In particular, the using of several of them in arguments of the same function will lead to unpredictable result.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |