/usr/share/doc/mathgl/mathgl_en/mathgl_en_12.html is in mathgl-doc-en 2.1.3.1-4ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd">
<html>
<!-- This manual is for MathGL (version 2.1.2), a collection of classes and routines for scientific plotting. Please report any errors in this manual to mathgl.abalakin@gmail.org.
Copyright C 2008-2012 Alexey A. Balakin.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover
Texts. A copy of the license is included in the section entitled "GNU
Free Documentation License."
-->
<!-- Created on December 22, 2013 by texi2html 1.82
texi2html was written by:
Lionel Cons <Lionel.Cons@cern.ch> (original author)
Karl Berry <karl@freefriends.org>
Olaf Bachmann <obachman@mathematik.uni-kl.de>
and many others.
Maintained by: Many creative people.
Send bugs and suggestions to <texi2html-bug@nongnu.org>
-->
<head>
<title>MathGL 2.1.2: 2.3 Data handling</title>
<meta name="description" content="MathGL 2.1.2: 2.3 Data handling">
<meta name="keywords" content="MathGL 2.1.2: 2.3 Data handling">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2html 1.82">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
pre.display {font-family: serif}
pre.format {font-family: serif}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: serif; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: serif; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.roman {font-family:serif; font-weight:normal;}
span.sansserif {font-family:sans-serif; font-weight:normal;}
ul.toc {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Data-handling"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="mathgl_en_11.html#Cutting-sample" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Array-creation" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_20.html#General-concepts" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_88.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Data-handling-1"></a>
<h2 class="section">2.3 Data handling</h2>
<p>Class <code>mglData</code> contains all functions for the data handling in MathGL (see section <a href="mathgl_en_51.html#Data-processing">Data processing</a>). There are several matters why I use class <code>mglData</code> but not a single array: it does not depend on type of data (mreal or double), sizes of data arrays are kept with data, memory working is simpler and safer.
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top"><a href="#Array-creation">2.3.1 Array creation</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Linking-array">2.3.2 Linking array</a></td><td> </td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top"><a href="#Change-data">2.3.3 Change data</a></td><td> </td><td align="left" valign="top">
</td></tr>
</table>
<hr size="6">
<a name="Array-creation"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Data-handling" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Linking-array" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Data-handling" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_20.html#General-concepts" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_88.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Array-creation-1"></a>
<h3 class="subsection">2.3.1 Array creation</h3>
<p>There are many ways in MathGL how data arrays can be created and filled.
</p>
<p>One can put the data in <code>mglData</code> instance by several ways. Let us do it for sinus function:
</p><ul>
<li>
one can create external array, fill it and put to <code>mglData</code> variable
<pre class="verbatim"> double *a = new double[50];
for(int i=0;i<50;i++) a[i] = sin(M_PI*i/49.);
mglData y;
y.Set(a,50);
</pre>
</li><li>
another way is to create <code>mglData</code> instance of the desired size and then to work directly with data in this variable
<pre class="verbatim"> mglData y(50);
for(int i=0;i<50;i++) y.a[i] = sin(M_PI*i/49.);
</pre>
</li><li>
next way is to fill the data in <code>mglData</code> instance by textual formula with the help of <code>Modify()</code> function
<pre class="verbatim"> mglData y(50);
y.Modify("sin(pi*x)");
</pre>
</li><li>
or one may fill the array in some interval and modify it later
<pre class="verbatim"> mglData y(50);
y.Fill(0,M_PI);
y.Modify("sin(u)");
</pre>
</li><li>
finally it can be loaded from file
<pre class="verbatim"> 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
</pre><p>At this you can use textual or HDF files, as well as import values from bitmap image (PNG is supported right now).
</p>
</li><li>
at this one can read only part of data
<pre class="verbatim"> 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
</pre></li></ul>
<p>Creation of 2d- and 3d-arrays is mostly the same. But one should keep in mind that class <code>mglData</code> 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:
</p><pre class="verbatim"> 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.);
</pre><p>or by using <code>Modify()</code> function
</p><pre class="verbatim"> mglData z(30,40);
z.Modify("sin(pi*x)*cos(pi*y)");
</pre>
<p>The only non-obvious thing here is using multidimensional arrays in C/C++, i.e. arrays defined like <code>mreal dat[40][30];</code>. Since, formally these elements <code>dat[i]</code> can address the memory in arbitrary place you should use the proper function to convert such arrays to <code>mglData</code> object. For C++ this is functions like <code>mglData::Set(mreal **dat, int N1, int N2);</code>. For C this is functions like <code>mgl_data_set_mreal2(HMDT d, const mreal **dat, int N1, int N2);</code>. At this, you should keep in mind that <code>nx=N2</code> and <code>ny=N1</code> after conversion.
</p>
<hr size="6">
<a name="Linking-array"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Array-creation" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="#Change-data" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Data-handling" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_20.html#General-concepts" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_88.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Linking-array-1"></a>
<h3 class="subsection">2.3.2 Linking array</h3>
<p>Sometimes the data arrays are so large, that one couldn’t’ copy its values to another array (i.e. into mglData). In this case, he can define its own class derived from <code>mglDataA</code> (see <a href="mathgl_en_75.html#mglDataA-class">User defined types (mglDataA class)</a>) or can use <code>Link</code> function.
</p>
<p>In last case, MathGL just save the link to an external data array, but not copy it. You should provide the existence of this data array for whole time during which MathGL can use it. Another point is that MathGL will automatically create new array if you’ll try to modify data values by any of <code>mglData</code> functions. So, you should use only function with <code>const</code> modifier if you want still using link to the original data array.
</p>
<p>Creating the link is rather simple – just the same as using <code>Set</code> function
</p><pre class="verbatim"> double *a = new double[50];
for(int i=0;i<50;i++) a[i] = sin(M_PI*i/49.);
mglData y;
y.Link(a,50);
</pre>
<hr size="6">
<a name="Change-data"></a>
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Linking-array" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_13.html#Data-plotting" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Data-handling" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_20.html#General-concepts" title="Next chapter"> >> </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en.html#Top" title="Cover (top) of document">Top</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_88.html#Index" title="Index">Index</a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_abt.html#SEC_About" title="About (help)"> ? </a>]</td>
</tr></table>
<a name="Change-data-1"></a>
<h3 class="subsection">2.3.3 Change data</h3>
<p>MathGL has functions for data processing: differentiating, integrating, smoothing and so on (for more detail, see <a href="mathgl_en_51.html#Data-processing">Data processing</a>). 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 ‘<samp>x</samp>’, ‘<samp>y</samp>’ or ‘<samp>z</samp>’. For example, the call of <code>Diff("x")</code> will differentiate data along ‘<samp>x</samp>’ direction; the call of <code>Integral("xy")</code> perform the double integration of data along ‘<samp>x</samp>’ and ‘<samp>y</samp>’ directions; the call of <code>Diff2("xyz")</code> will apply 3d Laplace operator to data and so on. Example of this operations on 2d array a=x*y is presented in code:
</p><pre class="verbatim">int sample(mglGraph *gr)
{
gr->SetRanges(0,1,0,1,0,1);
mglData a(30,40); a.Modify("x*y");
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;
}
</pre>
<img src="../png/dat_diff.png" alt="Example of data differentiation and integration">
<p>Data smoothing (function <a href="mathgl_en_58.html#smooth">smooth</a>) is more interesting and important. This function has single argument which define type of smoothing and its direction. Now 3 methods are supported: ‘<samp>3</samp>’ – linear averaging by 3 points, ‘<samp>5</samp>’ – linear averaging by 5 points, and default one – quadratic averaging by 5 points.
</p>
<p>MathGL also have some amazing functions which is not so important for data processing as useful for data plotting. There are functions for finding envelope (useful for plotting rapidly oscillating data), for data sewing (useful to removing jumps on the phase), for data resizing (interpolation). Let me demonstrate it:
</p><pre class="verbatim">int sample(mglGraph *gr)
{
gr->SubPlot(2,2,0,""); gr->Title("Envelop sample");
mglData d1(1000); gr->Fill(d1,"exp(-8*x^2)*sin(10*pi*x)");
gr->Axis(); gr->Plot(d1, "b");
d1.Envelop('x'); gr->Plot(d1, "r");
gr->SubPlot(2,2,1,""); gr->Title("Smooth sample");
mglData y0(30),y1,y2,y3;
gr->SetRanges(0,1,0,1);
gr->Fill(y0, "0.4*sin(pi*x) + 0.3*cos(1.5*pi*x) - 0.4*sin(2*pi*x)+0.5*rnd");
y1=y0; y1.Smooth("x3");
y2=y0; y2.Smooth("x5");
y3=y0; y3.Smooth("x");
gr->Plot(y0,"{m7}:s", "legend 'none'"); //gr->AddLegend("none","k");
gr->Plot(y1,"r", "legend ''3' style'");
gr->Plot(y2,"g", "legend ''5' style'");
gr->Plot(y3,"b", "legend 'default'");
gr->Legend(); gr->Box();
gr->SubPlot(2,2,2); gr->Title("Sew sample");
mglData d2(100, 100); gr->Fill(d2, "mod((y^2-(1-x)^2)/2,0.1)");
gr->Rotate(50, 60); gr->Light(true); gr->Alpha(true);
gr->Box(); gr->Surf(d2, "b");
d2.Sew("xy", 0.1); gr->Surf(d2, "r");
gr->SubPlot(2,2,3); gr->Title("Resize sample (interpolation)");
mglData x0(10), v0(10), x1, v1;
gr->Fill(x0,"rnd"); gr->Fill(v0,"rnd");
x1 = x0.Resize(100); v1 = v0.Resize(100);
gr->Plot(x0,v0,"b+ "); gr->Plot(x1,v1,"r-");
gr->Label(x0,v0,"%n");
return 0;
}
</pre>
<img src="../png/dat_extra.png" alt="Example of data manipulation">
<p>Also one can create new data arrays on base of the existing one: extract slice, row or column of data (<a href="mathgl_en_57.html#subdata">subdata</a>), summarize along a direction(s) (<a href="mathgl_en_57.html#sum">sum</a>), find distribution of data elements (<a href="mathgl_en_57.html#hist">hist</a>) and so on.
</p>
<p><a name="Solve-sample"></a>
Another interesting feature of MathGL is interpolation and root-finding. There are several functions for linear and cubic spline interpolation (see <a href="mathgl_en_59.html#Interpolation">Interpolation</a>). Also there is a function <a href="mathgl_en_57.html#evaluate">evaluate</a> which do interpolation of data array for values of each data element of index data. It look as indirect access to the data elements.
</p>
<p>This function have inverse function <a href="mathgl_en_57.html#solve">solve</a> which find array of indexes at which data array is equal to given value (i.e. work as root finding). But <a href="mathgl_en_57.html#solve">solve</a> function have the issue – usually multidimensional data (2d and 3d ones) have an infinite number of indexes which give some value. This is contour lines for 2d data, or isosurface(s) for 3d data. So, <a href="mathgl_en_57.html#solve">solve</a> function will return index only in given direction, assuming that other index(es) are the same as equidistant index(es) of original data. If data have multiple roots then second (and later) branches can be found by consecutive call(s) of <a href="mathgl_en_57.html#solve">solve</a> function. Let me demonstrate this on the following sample.
</p>
<pre class="verbatim">int sample(mglGraph *gr)
{
gr->SetRange('z',0,1);
mglData x(20,30), y(20,30), z(20,30), xx,yy,zz;
gr->Fill(x,"(x+2)/3*cos(pi*y)");
gr->Fill(y,"(x+2)/3*sin(pi*y)");
gr->Fill(z,"exp(-6*x^2-2*sin(pi*y)^2)");
gr->SubPlot(2,1,0); gr->Title("Cartesian space"); gr->Rotate(30,-40);
gr->Axis("xyzU"); gr->Box(); gr->Label('x',"x"); gr->Label('y',"y");
gr->SetOrigin(1,1); gr->Grid("xy");
gr->Mesh(x,y,z);
// section along 'x' direction
mglData u = x.Solve(0.5,'x');
mglData v(u.nx); v.Fill(0,1);
xx = x.Evaluate(u,v); yy = y.Evaluate(u,v); zz = z.Evaluate(u,v);
gr->Plot(xx,yy,zz,"k2o");
// 1st section along 'y' direction
mglData u1 = x.Solve(-0.5,'y');
mglData v1(u1.nx); v1.Fill(0,1);
xx = x.Evaluate(v1,u1); yy = y.Evaluate(v1,u1); zz = z.Evaluate(v1,u1);
gr->Plot(xx,yy,zz,"b2^");
// 2nd section along 'y' direction
mglData u2 = x.Solve(-0.5,'y',u1);
xx = x.Evaluate(v1,u2); yy = y.Evaluate(v1,u2); zz = z.Evaluate(v1,u2);
gr->Plot(xx,yy,zz,"r2v");
gr->SubPlot(2,1,1); gr->Title("Accompanied space");
gr->SetRanges(0,1,0,1); gr->SetOrigin(0,0);
gr->Axis(); gr->Box(); gr->Label('x',"i"); gr->Label('y',"j");
gr->Grid(z,"h");
gr->Plot(u,v,"k2o"); gr->Line(mglPoint(0.4,0.5),mglPoint(0.8,0.5),"kA");
gr->Plot(v1,u1,"b2^"); gr->Line(mglPoint(0.5,0.15),mglPoint(0.5,0.3),"bA");
gr->Plot(v1,u2,"r2v"); gr->Line(mglPoint(0.5,0.7),mglPoint(0.5,0.85),"rA");
}
</pre>
<img src="../png/solve.png" alt="Example of data interpolation and root finding">
<hr size="6">
<table cellpadding="1" cellspacing="1" border="0">
<tr><td valign="middle" align="left">[<a href="#Linking-array" title="Previous section in reading order"> < </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_13.html#Data-plotting" title="Next section in reading order"> > </a>]</td>
<td valign="middle" align="left"> </td>
<td valign="middle" align="left">[<a href="mathgl_en_9.html#Examples" title="Beginning of this chapter or previous chapter"> << </a>]</td>
<td valign="middle" align="left">[<a href="#Data-handling" title="Up section"> Up </a>]</td>
<td valign="middle" align="left">[<a href="mathgl_en_20.html#General-concepts" title="Next chapter"> >> </a>]</td>
</tr></table>
<p>
<font size="-1">
This document was generated by <em>Build Daemon user</em> on <em>December 22, 2013</em> using <a href="http://www.nongnu.org/texi2html/"><em>texi2html 1.82</em></a>.
</font>
<br>
</p>
</body>
</html>
|