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 in notebooks. The links will open in the browser. But you can also open all tutorials as notebooks via the help menu.
To fix the layouts of the windows of EMT, you can press F11 at any time. This will use a full layout of the text and the graphics window. Press F11 again to go back to your favorite layout. The layout is remembered across sessions.
You can also use Ctrl-G to hide the graphics window and the TAB key to see the graphics in the text window at any time.
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, * and / bind stronger than + or -, and ^ binds stronger than *, so that pi*r^2 works correctly. If necessary, you should add round brackets, of course.
The command r=1.25 is an assignment to a variable in EMT. You can also write r:=1.25 if you prefer. You can leave spaces as you like.
You can also end a command line with a comment.
>r := 1.25 // This is an optional form for an assignment.
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, or use the function deg() to convert.
EMT knows a great lot of mathematical functions and operators. For details see
EMT can convert units to the international standard system (IS). Append the unit to the number for a simple conversion.
A list of available units can be found on the page below.
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
The default output format prints with around 10 places. Other output formats can be set globally or only for one output. For details on output formats, see
Reference for Output Formats
For short, long, or longest output of a single value, you can 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.
A multi-line command stretches over several lines connected with "..." at the end. To generate a break in a command, use Ctrl-Return. To join a line to the previous line, use "Ctrl-Backspace".
The following multi-line command can be executed whenever the cursor is in any of its lines. It also shows that the ... must be at the very end of the line even if it contains a comment.
>a=4; b=15; c=2; // solve a*x^2+b*x+c=0 manually ... diskr=sqrt(b^2/(a^2*4)-c/a); ... -b/(2*a) + diskr, ... -b/(2*a) - diskr
The percent character % refers to the previous result. It should be used only within a single command line.
To see all user variables use "listvar".
r 1.25 a 4 b 15 c 2 diskr 1.73655549868123
This searches for the user defined variables only. It is possible to search for other variables. For this, a string must be added which must be contained in the name of the variable.
The following finds all units ending with "m" by searching for all variables that contain "m$".
km$ 1000 cm$ 0.01 mm$ 0.001 hquantum$ 6.62606957e-034 nm$ 1853.24496 gram$ 0.001 m$ 1 atm$ 101325
To remove a variable without having to restart EMT use "remvalue".
To get help on any function in EMT, open the help window by F1 and search for the function. You can also double click on a function in the notebook window to open the help window.
Try double clicking on "intrandom" in the following command.
[4, 2, 6, 2, 4, 2, 3, 2, 2, 6]
The result of this functions is a vector of random dice throws.
A vector can be defined by square brackets. See below for matrices.
[4, 5, 6, 3, 2, 1]
Matrices are available too. In fact, EMT has very advanced features to do Linear Algebra, Statistics or Optimization.
For an example, we can define a vector of uniformly spaced values like the following, and compute the mean of the squared values.
[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1] 0.35
EMT can use complex numbers too. Many functions work for complex numbers as expected.
EMT does symbolic math with the help of Maxima. For details, start with the following tutorial, or browse the reference for Maxima. Experts in Maxima should note that there are 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 and printed by Maxima.
4 3 2 x + 4 x + 6 x + 4 x + 1 3 4 (x + 1)
Again, % refers to the previous result.
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;
Variables can be stored as symbolic variables using the assignment "&=".
>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.
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.659697263551068e+7
Hte most elementary 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.
An expression needs not be symbolic. This is necessary, if the expression contains functions, which are only known in the numerical kernel, not in Maxima (albeit Maxima has the incomplete gamma function too, called gamma_incomplete(), but with a different definition).
The easiest way to plot a function is to use an expression in x. This can be a symbolic expression or a simple string. We demonstrate more advanced plots later.
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).
Press TAB to bring the plot window or the plot area to the front.
To demonstrate plots of vector of data, we use the matrix language of EMT to evaluate a function at the data.
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. A 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. We add a label to the function.
If we redefine the function with the keyword "map" it can be used for vectors x. Internally, the function is called for all values of x once, and the results are stored in a vector.
>function map f(x) := integrate("x^x",1,x); >f(0:0.5:2)
[-0.783431, -0.410816, 0, 0.676863, 2.05045]
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.
Assigned parameters are 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.
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.
>function f(x,a) := x^a-a^x;
One way to pass the additional parameter to f() is to use a list with the function name and the parameters (the other way are semicolon 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 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=20°):
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):
Of course, a point cloud is also possible.
EMT can also plot in anaglyph 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.