MGL script language

Reference manual

Alexey Balakin, 2007

Contents

  1. Introduction
  2. Graphics setup
  3. Scaling, moving and rotating
  4. Axis functions
  5. Primitive drawing functions
  6. 1d plotting commands
  7. 2d plotting commands
  8. 3d plotting commands
  9. Dual plotting commands
  10. Other plotting function
  11. Data creation and deleting
  12. New data extraction (forming)
  13. Data handling
  14. Color specification
  15. Line style specification
  16. Color scheme specification
  17. Text and font specification
  18. Formulas and functions
  19. Command options
  20. Suffixes for variable

1. Introduction

MathGL library supports (from version 1.3) the simplest scripts for data handling and plotting. These scripts can be used independently (with the help of mgl2png, mgl2eps, mgl2svg programs and others) or in the frame of the library using.

MGL script language is rather simple. Each string is a command. First word of string is the name of command. Other words are command arguments. Command may have up to 1000 arguments (at least for now). Words are separated from each other by space or tabulation symbol. The upper or lower case of words is suficient, i.e. variables a and A are different variables. Symbol # starts the comment (all characters after # will be ignored). The exception is situation when # is a part of some string. Also options can be specified at the end of string (after symbol ';', see Sec. 19).

If string contain references to external parameters (substrings "$0", "$1" ... "$9") then before execution the values of parameter will be substituted instaed of reference. It allows to use the same MGL script for different parameters (filenames, paths, condition and so on).

Argument can be a variable name, a string or a number. Variable name is arbitrary combination of symbols (except spaces and ') started from a letter and with length less than 64. It is possible to use subarrays (like in subdata command) as command argument. For example, a(1) or a(1,:) or a(1,:,:) is second row, a(:,2) or a(:,2,:) is third column, a(:,:,0) is first slice and so on. The string is any symbols between ordinary marks ('). Special variables nan=#QNAN, pi=3.1415926..., on=1, off=0, :=-1 are treated as number if they were not redefined by user. Also variables with suffixes are treated as numbers. Before the first using all variables must bedefined with the help of commands new, var, list, copy or read.

All MGL commands can be divided on several groups. I will use the following notation for a command description: command names are bold, strings are denoted by commas, variable names are italic, numbers are typewriter. Optional arguments are placed in square brackets and default values for them are shown. Detailed description of color, line styles, color schemes, font types , TeX-like symbols and formulas can be found in corresponding section.

[contents]

2. Graphics setup

Commands in this group influences on overall graphics appearance. So all of them should placed before any actual plotting commands.
[contents]

3. Scaling, moving and rotating

These command define the position of plot on the picture and its properties. There is a curtain order of calling of these functions for the better plot view. The first one should be subplot or inplot for specifying of the place. After it a rotate and aspect. And finally any other plotting function may be called.

[contents]

4. Axis functions

Axis functions control and draw the axes. There is the twofold axis representation in MGL. First one consists in normalizing of the data point coordinates in box specified in axis command. If cut is on then the outer point is omitted otherwise it is projected to bounding box. Also the points are omitted if they are inside the box specified in cut command. After it transformation formulas 'fx', 'fy', 'fz' are applied to the data point. Finally, the data point is plotted by one of functions.
[contents]

5. Primitive drawing functions

These functions draw some simple objects like point, line, marker or text string.
[contents]

6. 1d plotting commands

These functions perform plotting of 1D data. 1D means that the data depend on the only 1 parameter (index). Most of commands have similar interface. There are version for drawing in 3d space or in plain. For the last one there is a possibility to use single data array. If command allows to use parametric curve definition (depends on 2 or 3 data arrays) then its minor dimensions should be equal. If one of the arrays is 2d array (matrix) then the curves will be drawn for each of rows. At this the other arrays can be one-dimensional. Optional string parameter 'stl' sets the style and color of line and marks for all 1d plotting commands. By default ('stl'='') solid line with next color from palette is used. See also color and line styles specification.
[contents]

2d plotting commands

These functions perform plotting of 2D data. 2D means that the data depend on 2 independent parameter (indexes). Most of commands have similar interface. There are 2 versions for drawing of the surface of single data array and for parametrically specified surface. If command allows to use parametric surface definition (depends on 3 data arrays xdat, ydat, zdat) then its minor dimensions should be equal. At this arrays xdat, ydat can be one-dimensional. If zdat is 3d array then the surfaces will be drawn for each of slices. Optional string parameter 'stl' sets the color scheme. Previous color scheme is used by default. See also color and color schemes specification.
contents

8. 3d plotting commands

These functions perform plotting of 3D data. 3D means that the data depended on 3 independent parameter (indexes). Most of commands have similar interface. There are 2 versions for drawing single data array and for parametrically specified data. If command allows to use parametric surface definition (depends on 4 data arrays xdat, ydat, zdat, adat) then its minor dimensions should be equal. At this arrays xdat, ydat, zdat can be one-dimensional. Optional string parameter 'stl' sets the color scheme. Previous color scheme is used by default. See also color and color schemes specification.
[contents]

9. Dual plotting commands

These plotting functions draw two (or three) data arrays simultaneously in different forms: as coloring or transparing, vector field, flow chart or mapping. There are 2 versions for drawing single data array and for parametrically specified data. If the command allows to use parametric surface definition (depends on 4, 5 or 6 data arrays xdat, ydat, zdat, adat, bdat, cdat) then its minor dimensions should be equal. At this arrays xdat, ydat, zdat can be one-dimensional. The dimensions of other data arrays (adat, bdat, cdat) must be equal. Optional string parameter 'stl' sets the color scheme. Previous color scheme is used by default. See also color and color schemes specification.

Commands for 2d arrays:

Commands for 3d arrays:
contents

10. Other plotting function

These plotting commands draw density plot or contour lines in x, y, or z plain. If dat is 3-dimensional data array then interpolation to a given sval is performed. These functions are useful for creating projections of the 3D data array to the bounding box. Optional string parameter 'stl' sets the color scheme. Optional parameter val sets the value of coordiante on the slice. Previous color scheme and axis origin are used by default. See also color and color schemes specification.
contents

11. Data creation and deleting

These commands create new variable with specified name. Note, that if a variable with the same name exists then it will be overwritten. The only exception is the command delete which deletes specified variable and makes its memory free.
[contents]

12. New data extraction (forming)

In contrast to the previous group these commands write the result only in existing variables.
[contents]

13. Data handling

[contents]

14. Color specification

MGL script have the only way for color specification. It is a symbols (characters) wkrgbcymhRGBCYMHW each of them define unique color. Normally capital letter gives darker color than lowercase one. The full list of characters is: k – black, rred, Rdark red, ggreen, Gdark green, bblue, Bdark blue, ccyan, Cdark cyan, mmagenta, Mdark magenta, yyellow, Ydark yellow (gold), hgray, Hdark gray, w – white, Wbright gray, lgreen-blue, Ldark green-blue, egreen-yellow, Edark green-yellow, nsky-blue, Ndark sky-blue, ublue-violet, Udark blue-violet, ppurple, Pdark purple, qorange, Qdark orange (brown).

[contents]

15. Line style specification

MGL script use strings for specifying the parameters of lines and marks. The string may contain specification for color (wkrgbcymhRGBCYMHW), dashing style (-|;:ji or space), width (0123456789) and marks (o+xsd.^v). If one of type of information is omitted then the previous values are used. If string is empty then solid line with next color from palette is used. By default palette contain following colors: dark gray, blue, green, red, cyan, magenta, yellow, gray, blue-green, sky-blue, orange, yellow-green, blue-violet, purple.

The color types are defined by symbols as described in Sec. 14.

Dashing styles has the following meaning: space – no line (usable for plotting only marks), – solid line (■■■■■■■■■■■■■■■■), | – dashed line (■■■■■■■■□□□□□□□□), ; – small dashed line (■■■■□□□□■■■■□□□□), : – dotted line (■□□□■□□□■□□□■□□□), j – dash-dotted line (■■■■■■■□□□□■□□□□), i – small dash-dotted line (■■■□□■□□■■■□□■□□).

Marker types are: o – circle, + – cross, x – skew cross, s - square, d - rhomb (or diamond), . – point, ^ – triangle up, v – triangle down.

One may specify to draw special symbol (an arrow) at beginning and at the end of a line. It is possible if specification string contain one of following symbols: A – usual arrow, V – inner arrow, I – transverse hachures (or stop mark), K – arrow with hachures (or dimension mark), T – triangle, S – square, D – rhomb, O – circle, _ – nothing (it is default). At this there is a rule: first symbol specify the arrow at end of line, second symbol specify the arrow at beginning of line. For example, 'r-A' define red solid line with usual arrow at the end, 'b-A|' defien blue dash line with arrow at the end and with hachures at start, '-_O' define the line with current style and with circle at start. This styles are applicable during graphics plotting too (for example, plot).

contents

16. Color scheme specification

The color map scheme is used for coloring surfaces and other and is specified by the string. String may contain several characters which are color id (see Sec. 14) or character 'd' which denotes interpolation by 3d position instead of coloring by amplitude.

For coloring by amplitude (most common) the final color is linear interpolation of color array. The color array is constructed from string ids. The argument is the amplitude normalized between color range (see caxis, crange). For example, string containing 4 characters 'bcyr' corresponds to colorbar from blue (lowest value) through cyan (next value) through yellow (next value) to red (highest value). String 'kw' corresponds to colorbar from black (lowest value) to white (highest value). String 'm' corresponds to simple magenta color.

There are several useful combination. String 'kw' corresponds to simplest gray color scheme when higher values are brighter. String 'wk' presents inverse gray color scheme when higher value is darker. Strings "kRryw", 'kGgw', 'kBbcw' present the well-known "hot", "summer" and "winter" color schemes. Strings 'BbwrR' and 'bBkRr' allow to view bicolor figure on white or black background when negative values are blue and positive values are red. String 'BbcyrR' gives color scheme similar to well-known "jet" color scheme.

For coloring by coordinate the final color is determined by position of point in 3d space and is calculated by formula c=x*c[1] + y*c[2] + z*c[3]. Herec[1], c[2], c[3] are the first three elements of color array; x, y, z are normalized coordinates of the point. This type of coloring is useful for isosurface plot when color may show the exact position of peace of surface. For example, try surf3 a 'bgrd' for some tensor data a.

[contents]

17. Text and font specification

MGL script use strings for specifying the font type and aligning. The string can be any combination of characters: rgbisLCR. The font types are: s – script font/style, r – roman font, g – gothic font, i – italic style, b – bold style. By default roman simplex font (that is '') is used. The align types are: L – align left (default), C – align center, R – align right.

Parsing of the string to special (TeX-like) commands will be done always. There are commands for the font style changing inside the string (for example, use \b for bold font): a – overlined, b – bold, g – gothic, i – italic, r – roman (throw any other attributes), s – script, u – underlined, . – centered. The lower and upper indexes are specified by '_' and '^' symbols. The braces {} save/restore font state. For example, string 'sin(x^2)' will lift up the last bracket ')'. Correct version is 'sin({x^2})' or 'sin( x^2 )'. For drawing square root of some expression one may use '\sqrt{\a x^2 + y^2}'.

The abbreviations of Greek letters are: α – \alpha, β – \beta, γ – \gamma, δ – \delta, ε – \epsilon, η – \eta, ι – \iota, χ – \chi, κ – \kappa, λ – \lambda, μ – \mu, ν – \nu, o – \o, ω – \omega, ϕ – \phi, π – \pi, ψ – \psi, ρ – \rho, σ – \sigma, θ – \theta, τ – \tau, υ – \upsilon, ξ – \xi, ζ – \zeta, ς – \varsigma, ɛ – \varepsilon, ϑ – \vartheta, φ – \varphi, ϰ – \varkappa; A – \Alpha, B – \Beta, Γ – \Gamma, Δ – \Delta, E – \Epsilon, H – \Eta, I – \Iota, C – \Chi, K – \Kappa, Λ – \Lambda, M – \Mu, N – \Nu, O – \O, Ω – \Omega, Φ – \Phi, Π – \Pi, Ψ – \Psi, R – \Rho, Σ – \Sigma, Θ – \Theta, T – \Tau, Υ – \Upsilon, Ξ – \Xi, Z – \Zeta.

The full list of special TeX symbols are: ∠ – \angle,  aleph – \aleph, ⋅ – \cdot, ♣ – \clubsuit, ✓ – \checkmark, ∪ – \cup, ∩ – \cap, ♢ – \diamondsuit, ◇ – \diamond, – \div, ↓ – \downarrow, † – \dag, ‡ – \ddag, ≡ – \equiv, ∃ – \exists, ⌢ – \frown, ♭ – \flat, ≥ – \ge, ≥ – \geq, ≧ – \geqq, ← – \gets, ♡ – \heartsuit, ∞ – \infty, ∈ – \in, ∫ – \int, \Int, ℑ – \Im, ♢ – \lozenge, ⟨ – \langle, ≤ – \le, ≤ – \leq, ≦ – \leqq, ← – \leftarrow, ∓ – \mp, ∇ – \nabla, ≠ – \ne, ≠ – \neq, ♮ – \natural, ∮ – \oint, ⊙ – \odot, ⊕ – \oplus, ∂ – \partial, ∥ – \parallel, ⊥ –\perp, ± – \pm, ∝ – \propto, ∏ – \prod, ℜ – \Re, → – \rightarrow, ⟩ – \rangle, ♠ – \spadesuit, ~ – \sim, ⌣ – \smile, ⊂ – \subset, ⊃ – \supset, √ – \sqrt, § – \S, √ – \surd, ♯ – \sharp, ∑ – \sum, × – \times, → – \to, ∴ – \therefore, ↑ – \uparrow, ℘ – \wp.

[contents]

18. Formulas and functions

Formulas in MGL scripts is string with functions, operators, parentheses, variables, numbers and so on. There is no difference between lower or upper case in formulas. There are a lot of functions and operators available. The operators are: + – addition, – subtraction, * – multiplication, / – division, ^ – integer power. Also there are logical "operators": < – true if x<y, > – true if x>y, = – true if x≡y, & – true if x and y both nonzero, | – true if x or y nonzero. These logical operators have lowest priority and return 1 if true or 0 if false.

The basic functions are: sqrt(x) – square root of x, pow(x,y) power x in y, ln(x) – natural logarithm of x, lg(x) – decimal logarithm of x, log(a,x) – logarithm base a of x, abs(x) – absolute value of x,sign(x) – sign of x, mod(x,y) – x modulo y, step(x) – step function, rnd – random number, pi – number π = 3.1415926…

Trigonometric functions are: sin(x), cos(x), tan(x) (or tg(x)). Inverse trigonometric functions are: asin(x), acos(x), atan(x). Hyperbolic functions are: sinh(x) (or sh(x)), cosh(x) (or ch(x)), tanh(x) (or th(x)). Inverse hyperbolic functions are: asinh(x), acosh(x), atanh(x).

There are a set of special functions: gamma(x) – Gamma function Γ(x) = ∫0 tx-1 exp(-t) dt, psi(x) – digamma function ψ(x) = Γ′(x)/Γ(x) for x≠0, ai(x) – Airy function Ai(x), bi(x) – Airy function Bi(x), cl(x) – Clausen function, li2(x) (or dilog(x)) – dilogarithm Li2(x) = -ℜ∫0xds log(1-s)/s, sinc(x) – compute sinc(x) = sin(πx)/(πx) for any value of x, zeta(x) – Riemann zeta function ζ(s) = ∑k=1k-s for arbitrary s≠1, eta(x) – eta function η(s) = (1 - 21-s)ζ(s) for arbitrary s, lp(l,x) – Legendre polynomial Pl(x), (|x|≤1, l≥0), w0(x) – principal branch of the Lambert W function, w1(x) – principal branch of the Lambert W function. Function W(x) is defined to be solution of the equation: W exp(W) = x.

The exponent integrals are: ci(x) – Cosine integral Ci(x) = ∫0xdt cos(t)/t, si(x) – Sine integral Si(x) = ∫0xdt sin(t)/t, erf(x) – error function erf(x) = (2/√π) ∫0xdt exp(-t2) , ei(x) – exponential integral Ei(x) = -PV(∫-xdt exp(-t)/t) (where PV denotes the principal value of the integral), e1(x) – exponential integral E1(x) = ℜ∫1dt exp(-xt)/t, e2(x) – exponential integral E2(x) = ℜ∫1∞dt exp(-xt)/t2, ei3(x) – exponential integral Ei3(x) = ∫0xdt exp(-t3) for x≥0.

Bessel functions are: j(nu,x) – regular cylindrical Bessel function of fractional order nu, y(nu,x) – irregular cylindrical Bessel function of fractional order nu, i(nu,x) – regular modified Bessel function of fractional order nu, k(nu,x) – irregular modified Bessel function of fractional order nu.

Elliptic integrals are: ee(k) – complete elliptic integral is denoted by E(k) = E(π/2,k), ek(k) – complete elliptic integral is denoted by K(k) = F(π/2,k), e(phi,k) – elliptic integral E(φ,k) = ∫0φdt √(1 - k2sin2(t)), f(phi,k) – elliptic integral F(φ,k) = ∫0φdt 1/√(1 - k2sin2(t))

Jacobi elliptic functions are: sn(u,m), cn(u,m), dn(u,m), sc(u,m), sd(u,m), ns(u,m), cs(u,m), cd(u,m), nc(u,m), ds(u,m), dc(u,m), nd(u,m).

[contents]

19. Command options

Command options allow the easy setup of the plot by changing of global settings only for this plot. Options are specified at the end of string. Each option is separated from the previous text by symbol ';'. Options work so that them remember the current settings, change settings as it being set in the option, execute command and return the original settings back. So, the options usage for data handling commands or for graphics setup commands is useless.

The most useful options are xrange, yrange, zrange. They sets the boundaries for data change. This boundaries are used for automatically filled variables. So, these options allow one to change the position of some plots. For example, in command plot y; xrange 0.1 0.9 the x coordinate will be equidistantly distributed in range 0.1 ... 0.9.

The full list of options are:

[contents]

20. Suffixes for variable

Suffixes can get some numerical value (like its size, maximal or minimal value, the sum of elements and so on) of the data array in variable and use it later as usual number in command arguments. The suffixes start from point '.' right after (without spaces) variable name or its subarray. For example, a.nx give the x-size of data a b(1).max give maximal value of second row of variable b c(:,0).sum give sum of element in first column of c and so on.

The full list of suffixes are:

[contents]