This is a really fast introduction into Euler Math Toolbox (EMT) with lots of links and examples.
If you need a more thorough overview or a starter tutorial, see the following notebooks.
00 - A first Welcome
00 - The Syntax of Euler
These are links to HTML exports of tutorials which are installed with EMT. You can double click such links notebooks. The links will open in the browser. But you can also open all tutorials as notebooks via the help menu.
EMT is command line oriented. It allows one or more commands in one command line. The commands have to be ended by comma or semicolon, besides the last command, where a comma is assumed.
In the following example, we define a variable r. The output of this definition would be the value of the variable. But because of the semicolon this output is not printed.
>r=1.25; pi*r^2, 2*pi*r
Make sure to use a decimal dot, not a decimal comma for numbers! Use * for multiplication and ^ for exponentials. As usual, * binds stronger than +, and ^ binds stronger than *, so that pi*r^2 works correctly. If necessary, you can add round brackets, of course.
The command r=1.25 is an assignment in EMT. You can also write r:=1.25 if you prefer. You can leave spaces as you like.
>r := 1.25
Functions are called with round brackets.
As you see, trigonometric functions work with radians, and degrees can be converted with °. If your keyboard does not have the degree character press F7.
EMT knows very many mathematical functions and operators. For details see
Units are translated to the international standard system (IS).
A list of available units is here.
Reference for Units
For the conversion to and between units, EMT has a special operator -> which can convert to a number or to a string.
>4km->miles, 4inch->" mm"
2.48548476895 101.6 mm
It is possible to place a special blank in numbers, between numbers and units, and in Euler names. You get this special blank with Ctrl-Space. It looks just like an ordinary blank.
>1 234 miles -> " km"
The default output format prints with around 10 places. Other output formats can be set globally or only for one result. For details on output formats, see
Reference for Output Formats
For short, long, or longest output of a value, use the following operators.
>short 1200*1.03^10, long E, longest pi
1612.7 2.71828182846 3.141592653589793
You can switch the output format for all subsequent commands. Reset the format with defformat() or reset().
>longestformat; pi, defformat;
The numerical kernel of EMT works with floats (in contrast to the symbolic part of EMT). But numerical results can be converted to fractions.
Another interesting format is for currencies.
11 234 567.89
Complex numbers use the variable I or the syntax a+bi.
For an introduction to complex numbers, see the following tutorial.
Tutorial on Complex Numbers
Many functions work with complex numbers. The complex logarithm branches at the negative x-axis.
A vector can be defined using square brackets with comma separated values.
[1, 2, 3, 5] [-1, 3, 4, 5]
The operator show can print a variable with its name.
>show v; show w;
v = [1, 2, 3, 5] w = [-1, 3, 4, 5]
To find all user variables use "listvar".
v Type: Real Matrix (1x4) r 1.25 w Type: Real Matrix (1x4)
This searches for the user defined variables only. It is possible to search for other variables. For this, a string must be added which should be contained in the name of the variable.
The following finds all units ending with "m".
km$ 1000 cm$ 0.01 mm$ 0.001 hquantum$ 6.62606957e-034 gram$ 0.001 m$ 1 atm$ 101325
Simple linear algebra works with vectors. See below for more.
>3*v-2*w, scalp(v,w), norm(v)
[5, 0, 1, 5] 42 6.2449979984
Two or more command lines can be connected with ... to a multi-line command. You can
You can execute a multi-line command with the return key in any of its line.
>v=[3,4,1]; ... w=[5,6,7]; ... v+w
[8, 10, 8]
EMT does symbolic math with the help of Maxima. For details, start with the following tutorial, or browse the reference for Maxima. Note that there are slight differences in the syntax between the original syntax of Maxima and the default syntax of symbolic expressions in EMT.
Tutorial on Symbolic Math with Euler
Symbolic math is integrated seamlessly into Euler with &. Any expression starting with & is a symbolic expression. It is evaluated by Maxima.
4 3 2 x + 4 x + 6 x + 4 x + 1 3 4 (x + 1)
In this command line, % refers to the previous result. You should use % only within a line or multi-line.
A direct input of Maxima commands is available too. Start the command line with "::". The syntax of Maxima is adapted to the syntax of EMT (called the "compatibility mode").
18 8 4 2 2 3 5 7 11 13 17 19
If you are an expert in Maxima, you may wish to use the original syntax of Maxima. You can do this with ":::".
>::: av:g$ av^2;
In compatibility mode, the same line is much closer to the syntax of Euler.
>:: av:=g; av^2
However, one should prefer the seamless integration of Maxima into EMT.
>av &= g; &av^2
As you see, variables can be stored as symbolic variables using the assignment "&=". Symbolic variables contain a symbolic expression.
>fx &= x^3*exp(x)
3 x x E
Such variables can be used in other symbolic expressions. Note, that in the following command the right hand side of &= is evaluated before the assignment to Fx.
>Fx &= integrate(fx,x)
3 2 x (x - 3 x + 6 x - 6) E
For the evaluation of an expression with specific values of the variables, you can use the "with" operator. For Maxima experts: The "with" operator is translated to at() for Maxima.
The following command line also demonstrates that Maxima can evaluate an expression numerically with float().
>&(Fx with x=10)-(Fx with x=5), &float(%)
10 5 754 E - 74 E 1.6596972635510674E+7
As we will see in the following section, EMT can also evaluate symbolic or non-symbolic expressions numerically.
One way to define a simple function is to store its formula in a symbolic or numerical expression. If the main variable is x, the expression can be evaluated just like a function.
As you see in the following example, global variables are visible during the evaluation.
>fx &= x^3-a*x; ... a=1.2; fx(0.5)
All other variables in the expression can be specified in the evaluation using an assigned parameter.
Note that the items "x" and "a" must not have a symbolic value for this. If they have a value the definition might not be as intended.
>a &= 3; &x^3-a*x
3 x - 3 x
You can remove the values of variables to make sure. Of course, all variables you might use lose their value, if EMT restarts.
An expression needs not be symbolic. It can also be stored in a simple string. This is necessary, if the expression contains functions, which are only known in the numerical kernel, not in Maxima.
At this point, we should remember that evaluation of expressions is done by the numerical kernel of Euler.
To evaluate in Maxima, we need a symbolic expression and the "with" operator. The evaluation can be symbolic or numerical in Maxima. For a numerical evaluation in Maxima the variables in the symbolic expression have to be floating point.
>fx&=exp(-x^2)*x; &fx with x=2, &fx with x=2.0
- 4 2 E 0.036631277777468
The same expression can be evaluated by the numerical kernel.
The easiest way to plot a function is to use an expression in x. This can be symbolic expression or a simple string.
In the following example, we also demonstrate that a colon at the end of the command line inserts the current plot into the notebook. By default, the plot is visible in a separate window only. (EMT can also use a one-window interface - press Ctrl-G).
In any case, you may wish to insert the plot into the notebook text. Such plots will be saved and loaded with notebooks, and will be exported to HTML. Simply end the line containing the plot command with a colon ":".
We can add a text box explaining the function. In the following example, we use a Unicode string of the form u"..." with the HTML entity sup3.
>expr &= x^3-x*sin(x); ... plot2d(&diff(expr,x),-1,1,grid=6); ... textbox(u"d/dx x³ - x sin(x)",style="t"):
In this crash course, we can only show one-line functions. For multi-line functions refer to the following tutorial. EMT contains a complete programming language.
Tutorial for EMT Programs
A one-line function can be numerical or symbolic.
>function f(x) &= x^3-x
3 x - x
With &= the function is symbolic, and can be used in other symbolic expressions.
4 2 x x -- - -- 4 2
With := the function is numerical. An good example is a numerical integral like
which can not be evaluated symbolically. (See below for numerical integration.)
>function f(x) := integrate("x^x",1,x);
A function can be plotted by its name.
Functions can have default values for parameters.
>function mylog (x,base=10) := ln(x)/ln(base);
Now the function can be called with or without a parameter "base".
Moreover, it is possible to use assigned parameters.
This is used by many functions and algorithms in EMT. E.g., the plot2d function can get parameters in exactly this way.
For boolean parameters there is a shortcut. There, >param is equivalent to param=true, and <param is equivalent to param=false. By the way, "false" is defined as 0, and "true" is defined as 1 in EMT. There is no boolean data type.
Often, we want to use functions for vectors at one place, and for individual elements at other places. This is possible with vector parameters.
>function f([a,b]) &= a^2+b^2-a*b+b
2 2 b - a b + b + a
Such a symbolic function can be used for symbolic variables. In the example we find the critical point of the gradient.
[2 x - y, 2 y - x + 1] 2 1 [[y = - -, x = - -]] 3 3
But the function can also be used for a numerical vector.
Some numerical algorithms, like the Nelder-Mead method, require a numerical function working for vectors. The Nelder-Mead method determines the minimum of a function.
>nelder("f",[0,0]), fraction f(%)
[-0.333333, -0.666667] -1/3
There are also purely symbolic functions, which cannot be used numerically.
>function lapl(expr,x,y) &&= diff(expr,x,2)+diff(expr,y,2)
diff(expr, y, 2) + diff(expr, x, 2)
4 2 2 4 y - 6 x y + x 0
But of course, they can be used in symbolic expressions or in the definition of symbolic functions.
>function f(x,y) &= factor(lapl((x+y^2)^5,x,y))
2 3 2 10 (y + x) (9 y + x + 2)
Expressions can be solved numerically and symbolically.
To solve a simple expression of one variable, we can use the solve() function. It needs a start value to start the search. Internally, solve() uses the secant method.
This works for symbolic expression too. Take the following function.
>px &= 4*x^8+x^7-x^4-x, plot2d(px,-1,1):
8 7 4 4 x + x - x - x
Now we search the point, where the polynomial is 2. In solve(), the
default target value y=0 can be changed with an assigned variable.
We use y=2 and check by evaluating the polynomial at the previous result.
Solving a symbolic expression in symbolic form returns a list of solutions. We use the symbolic solver solve() provided by Maxima.
>sol &= solve(x^2-x-1,x)
1 - sqrt(5) sqrt(5) + 1 [x = -----------, x = -----------] 2 2
The easiest way to get the numerical values is to evaluate the solution numerically just like an expression.
To use the solutions symbolically in other expressions, the easiest way is "with".
>&x^2 with sol, &expand(x^2-x-1 with sol)
2 (sqrt(5) - 1) -------------- 4 0
There are many more functions to solve equations, like the Newton method or the bisection method. Moreover, there are interval methods, which return guaranteed inclusions of the solution.
Reference on Numerical Algorithms
Tutorial on Numerical Analysis
Tutorial on Interval Methods
Solving systems of equations symbolically can be done with vectors of equations and the symbolic solver solve(). The answer is a list of lists of equations.
[[x = - 1, y = 3], [x = 1, y = 1], [x = 0, y = 2]]
Solving systems of nonlinear equations numerically requires advanced numerical methods.
In general, a function is required for this. Here is just one short example.
>function f([x,y]) := [sin(x)+y-2,x^3+2*y+x-4];
The Broyden method is a good general method to solve such equations. But there are more methods. See
Newton and Quasi-Newton Methods
The Broyden algorithm does not need the derivative of the function.
[0.870078, 1.23562] [0, 0]
We can also use a function instead of an expression. We define a tough integration, and like to solve
>function f(x) := integrate("x^x",1,x);
The secant method solves this very well.
The function f() can see global variables. But often we want to use local parameters.
In EMT, there are semicolon parameters. These parameters are passed to the function f() from solve() or similar routines. All functions which accept function names do this.
Semicolon parameters have to be entered after all normal parameters and before any assigned parameters. Let us solve
>function f(x,a) := x^a-a^x;
The initial guess is 2.
Another alternative is to use a list with the function name and additional parameters.
This does also work with expressions. But then, a named list element has to be used. (More on lists in the tutorial about the syntax of EMT).
The documentation of the EMT core contains a detailed discussion on the matrix language of Euler.
The Matrix Language
Vectors and matrices are entered with square brackets, elements separated by commas, rows separated by semicolons.
1 2 3 4
The matrix product is denoted by a dot.
The main point of a matrix language is that all functions and operators work element for element.
1 4 9 16
This is not the matrix product, but a multiplication element by element. The same works for vectors.
With this and the colon operator a:delta:b, vectors of values of functions can be generated easily.
In the following example, we generate a vector of values t[i] with spacing 0.1 from -1 to 1. Then we generate a vector of values of the function
[0, 0.171, 0.288, 0.357, 0.384, 0.375, 0.336, 0.273, 0.192, 0.099, 0, -0.099, -0.192, -0.273, -0.336, -0.375, -0.384, -0.357, -0.288, -0.171, 0]
In the following plot, we plot each point of the graph of the function. I.e., we plot the points [t[i],s[i]].
The parameter >points is a shortcut for the assigned parameter points=true. If we omit this flag, we get a coarse image of the function. To get more details, we simply add more points.
The function linspace(-10,10,1000) computes 1001 points with 1000 intervals between -10 and 10.
>t=linspace(-10,10,1000); s=sin(t/(t^2+1)); plot2d(t,s):
Of course, it is easier to use symbolic or numerical expressions. Moreover, EMT can use an adaptive algorithm to improve the plot in critical areas for expressions or functions.
EMT expands operators for scalars, vectors, and matrices in the obvious way.
E.g., a column vector times a row vector expands to matrix, if an an operator is applied. In the following, v' is the transposed vector (a column vector).
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
Note, that this is quite different from the matrix product. The matrix product is denoted with a dot "." in EMT.
Using the matrix language, very complicated tables can be generated and plotted. We try
for x between 0 and 1, n=10, and k from 0 to 10. These polynomials are called the Bernstein polynomials.
In the computation of the matrix y, each row belongs to one value of k.
>n=10; k=0:n; ... x=linspace(0,1,100); y=bin(n,k')*x^k'*(1-x)^(n-k'); ... plot2d(x,y):
A function which does not work for vector input should be "vectorized". This can be achieved by the "map" keyword in the function definition. Then the function will be evaluated for each element of a vector parameter.
The numerical integration integrate() works only for scalar interval bounds. So we need to vectorize it.
>function map f(x) := integrate("x^x",1,x)
The "map" keyword vectorizes the function. The function will now work
for vectors of numbers.
[0, 2.05045, 13.7251, 113.336, 1241.03]
Note that plots vectorize the functions automatically. This is necessary to compute adaptive plots anyway.
EMT has lots of functions to solve linear systems, sparse systems, or regression problems.
Reference for Linear Systems
For linear systems Ax=b, you can use the Gauss algorithm, the inverse matrix or a linear fit. The operator A\b uses a version of the Gauss algorithm.
>A=[1,2;3,4]; b=[5;6]; A\b
For another example, we generate a 200x200 matrix and the sum of its rows. Then we solve Ax=b using the inverse matrix. We measure the error as the maximal deviation of all elements from 1, which of course is the correct solution.
>A=normal(200,200); b=sum(A); longest totalmax(abs(inv(A).b-1))
If the system does not have a solution, a linear fit minimizes the norm of the error Ax-b.
1 2 3 4 5 6 7 8 9
The determinant of this matrix is 0.
We can only get an optimal fit. That is a vector x which minimizes the norm of Ax-b. Euler uses orthogonal transformations to find this fit.
0.166667 0.333333 0
The same fit is used for polynomial fits to data. We fit a polynomial of degree 2 to the following data.
>x=1:5; y=[2,3,3,4,5.2]; p=polyfit(x,y,2)
[1.92, 0.14, 0.1]
The we can plot the points and the polynomial into one plot using two calls to plot2d and the parameter >add.
Since EMT contains strong algorithms for minimization, we can also fit an exponential function or a mixed exponential and linear function to the data.
The function modelfit() makes this easy. It needs a model function.
>function fexp (x,[a,b,c]) := a*x+b*exp(c*x)
Then it takes either the Nelder-Mead or Powell's algorithm to minimize the error.
[0.978624, 1.92445, -0.535012]
EMT can very well be used for statistical experiments or evaluations.
There are many functions, which generate a distribution. For a normal distribution with mean and variance use randnormal(n,m,mean,dev). It produces a nxm matrix of random values. The plot2d() functions can plot distributions of values.
>a=randnormal(1,1000,5,1); plot2d(a,>distribution, ... style="/",fillcolor=darkgray):
A cumulative sum of a 0-1-normal distributed values produces a random walk.
Using two rows shows a walk in two dimensions.
There are special plots for statistics. One is columnsplot(). We use it to display the number of 1s to 6s in 600 dice throws.
To simulate dice throws, we can use intrandom(n,m,6). Then we need to count the multiplicities of the values 1 to 6 in this vector.
>w=intrandom(1,600,6); ... columnsplot(getmultiplicities(1:6,w),style="-",color=darkgray):
Together with the matrix language, this can be used for effective analysis of statistical data.
For a simple example, we generate a million events uniformly between 0 and 1 using random(n,m) or random(m). Then we plot the distribution of the waiting times between the events. To get the waiting times, we sort the random values and get our vector of events. Then the function differences() computes successive differences between the elements of this vector.
>n=1 000 000; s=random(n); d=differences(sort(s)); ... plot2d(d*n,>distribution):
The mean waiting time is 1/n.
The standard deviation of the waiting time is less obvious. But it is also 1/n.
The distribution of the differences (scaled with the number of data n) is indeed an exponential distribution.
A boxplot shows the quartiles of this distribution and lots of outliers. By definition, outliers in a boxplot are data which exceed 1.5 times the middle 50% range of the plot.
The main functions are plot2d and plot3d.
Tutorial for plot2d
Tutorial for plot3d
For 2D plots of data, functions, curves, points or polygons we use plot2d(). We have already shown how to plot expressions or functions.
>function f(x,s) := 1/(s*sqrt(2*pi))*exp(-x^2/(2*s^2))
To plot functions, we can use the function name.
If necessary, additional parameters are passed by semicolon parameters. In the following, we call f(x,s) with s=2. The semicolon parameter belongs between the parameters and the assigned parameters.
All utility functions in EMT, which accept function names, do also accept semicolon parameters. These extra parameters will be passed to the function f(x,...).
For plots of data, we can use two vectors.
Again, we add another plot with >add in the second plot2d() command.
>n=10; k=0:n; b=bin(n,k)*2^-n; ... plot2d(k,b); plot2d(k,b,>points,>add):
The function statplot() adds lines and points automatically and is designed for statistics.
In the example, we generate a matrix with two rows for two different binomial distributions using the matrix language of EMT. We plot each row with a different color.
>n=10; k=0:n; p=[0.4,0.7]'; b=bin(n,k)*p^k*(1-p)^(n-k); ... statplot(k,b,color=[green,blue]):
Polygons are drawn with a vector of x and y coordinates. They can be filled. Use the matrix language to get vectors elegantly!
In this example, we plot the curve
>t=linspace(0,2pi,1000); r=1+sin(3*t)/2; x=r*cos(t); y=r*sin(t); ... plot2d(x,y,>filled,fillcolor=red,style="/",r=1.5):
For column plots, we already saw the function columnsplot(). But with the parameter >bar plot2d() can produce column plot of all kinds.
Note that you are not restricted to square plots. You can either set a non-square aspect globally, or for only a few plots.
You need to release the 1:2 aspect to revert to the default square ratio.
A 3D plot of a function uses plot3d. The easiest way is to plot an expression in x and y. The parameter r set the range of the plot around (0,0).
The default style can be changed. For the following contour plot, we use a finer grid fo 100x100 points, scale the function and the plot, and use different angle of view.
>plot3d("exp(-x^2-y^2)",r=2,n=100,level="thin", ... >contour,>spectral,fscale=1,scale=1.1,angle=45°,height=40°):
For 3D objects, you need to provide a matrix of x-, y- and z-values, or three functions or expressions fx(x,y), fy(x,y), fz(x,y).
In the following example, we use a vector of t values and a column vector of s values to parameterize the surface of the ball. In the drawing we can mark regions, in our case the polar region.
>t=linspace(0,2pi,180); s=linspace(-pi/2,pi/2,90)'; ... x=cos(s)*cos(t); y=cos(s)*sin(t); z=sin(s); ... plot3d(x,y,z,>hue, ... color=blue,<frame,grid=[10,20], ... values=s,contourcolor=red,level=[90°-24°;90°-22°], ... scale=1.4,height=50°):
Here is a plot with three functions.
It is also possible to plot a curve in 3D. In this case, it is easier to precompute the points of the curve.
>t=linspace(0,8pi,500); ... plot3d(sin(t),cos(t),t/10,>wire,zoom=3.5):
Of course, a point cloud is also possible.
EMT can also plot in analglyph mode. To view such a plot, you need red/cyan glasses.
>X=cumsum(normal(3,100)); ... plot3d(X,X,X,>anaglyph,>wire):
Special characters can be used in plots, comments and output. For this, the easiest way is to use Unicode entities of the form &text; in string constants of the form u"...". These Unicode strings can be concatenated like other strings.
>u"α = " + deg(acos(0.6)) + u"°"
α = 53.1301023542°
In comments, the same entities can be inserted. Thus greek letters like α, β or symbols like Windows® can be used.
In plots, titles, labels, label boxes, text boxes and other text can contain Unicode characters.
>plot2d("x^3-x",title=u"x → x³-x"):
This does also work in vertical labels.
EMT contains an implementation of the Simplex algorithm for linear optimization, a simple implementation of the branch and bound algorithm for integer optimization, and algorithms from the LPSOLVE package.
Additionally, there are some non-linear algorithms. For a start, we solve a very simple problem
1 2 2 1 1 1
3 3 2
The default for the simplex is to assume conditions of type <=, but other conditions are possible too (>= or =). Let us try changing the first condition to
We need to change b, and add an additional parameter, which denotes the type of condition (0=equal, -1=less equal, 1=greater equal).
0 -1 -1
EMT has also a very simple implementation of the branch-and-bound algorithm for integer problems.
The algorithm can relax the integer condition for specific variables, or relax the condition x>=0 etc. For more information
Tutorial on Optimization
There is also an efficient implementation of algorithms for optimization in the LPSOLVE package. The interface is very similar to intsimplex().
EMT has symbolic and numerical algorithms to solve a differential equation.
Tutorial on Differential Equations
To solve the first order equation
numerically, we use ode() and an expression. By default, the LSODA algorithm is used.
>x=linspace(0,4pi,100); y=ode("sin(y)*x",x,1); plot2d(x,y):
We can also solve to the left side of the initial point 0.
To plot both into the same plot, we use add.
For a symbolic example, we solve
exactly. The main symbolic function for this is ode2(). We first get a general solution.
>sol &= ode2('diff(y,x,2)+'diff(y,x)+y=x^2,y,x)
- x/2 sqrt(3) x sqrt(3) x 2 y = E (%k1 sin(---------) + %k2 cos(---------)) + x 2 2 - 2 x
With ic2, we can determine the constants.
>yx &= y with ic2(sol,x=0,y=0,'diff(y,x)=1)
- x/2 sqrt(3) x 2 2 sqrt(3) E sin(---------) + x - 2 x 2
Let us evaluate this in x=2.
For a numerical solution, we need to rewrite the second order equation into a system of equations.
>function f(x,y) := [y,x^2-y-y]
Then we can solve the equation with ode. Note that the default adaptive algorithm does not need intermediate points. We get only the values at x=0 and x=2.
>x=[0,2]; longest ode("f",x,[0,1])
0 1.257838889010009 1 1.193884206433507
For a more instructive example, we plot a vector field for the autonomic equation
>function f(x,y) := [-y,y+y^2]; ... vectorfield2("-y","x+x^2",-2,2,-2,2):
Now, we solve the equation, starting from
>x=0:0.1:2pi; y=ode("f",x,[0,-1]); ... plot2d(y,y,color=red,thickness=2,>add):
The author wishes you a lot of fun with math and EMT! There is a lot to learn. So keep experimenting. That's what EMT was made for.