WEBVTT

00:01.120 --> 00:05.100
So hello, welcome back to the Python course for the vehicle

00:05.100 --> 00:11.860
technicians and we are now here in the part Tools and Packages, in the

00:11.860 --> 00:14.220
fifth part at SymPy.

00:18.530 --> 00:22.370
And yes, SymPy is a library with which you can do symbolic

00:22.370 --> 00:22.710
calculations.

00:23.830 --> 00:28.170
Maybe you have already worked with other programs that can solve

00:28.170 --> 00:30.630
mathematical problems on a computer symbolically.

00:31.350 --> 00:36.630
There are, for example, Mavcat or Maple, these are the most famous, I

00:36.630 --> 00:36.770
think.

00:37.310 --> 00:41.150
Of course there are also other programs or extensions.

00:43.670 --> 00:47.430
At MATLAB, for example, there is also such an extra tool that you can

00:47.430 --> 00:47.870
turn on.

00:50.230 --> 00:54.590
So symbolic computing is also possible in Python with this library and

00:54.590 --> 01:00.570
it offers a lot of possibilities and we will just take a look at what

01:00.570 --> 01:01.370
you can do with SymPy.

01:03.270 --> 01:06.090
The nice thing about SymPy is that you can simply integrate it into

01:06.090 --> 01:10.510
your other programming projects and then don't have such a break in

01:10.510 --> 01:12.390
the tools, so that you have to switch to a different place for certain

01:12.390 --> 01:13.310
calculations.

01:15.190 --> 01:18.550
So you can simply install that as a package and then embed it directly

01:18.550 --> 01:20.050
into your tasks.

01:23.670 --> 01:29.710
There is a live demonstrator where you can find the help very quickly.

01:30.510 --> 01:36.170
It can be found at live.sympy.org and it looks like this.

01:36.610 --> 01:44.110
On the left side you have a console where you can type in commands and

01:44.110 --> 01:47.350
on the right side there are also a few small example sessions.

01:47.350 --> 01:50.630
The help is really good.

01:50.910 --> 02:01.090
You can look at small example tasks and then try it out interactively.

02:01.550 --> 02:05.570
If you don't want to put a Python installation on your computer and

02:05.570 --> 02:09.410
can learn SymPy step by step.

02:09.550 --> 02:14.270
But let's take a look at a few features in this part.

02:15.270 --> 02:20.270
First, let's take a look at how to import it and a few simple things.

02:21.770 --> 02:25.690
If you have installed it, you can do it with conda or pip, simply with

02:25.690 --> 02:27.150
conda install sympy.

02:27.750 --> 02:30.430
Then you can import it as normal as any other package.

02:31.610 --> 02:35.570
Here I did import sympy as sp, so that it is a bit shorter.

02:36.610 --> 02:41.790
The second line is for the notebooks, so that you can see the plots

02:41.790 --> 02:42.030
directly.

02:43.570 --> 02:45.550
You can define symbols.

02:46.270 --> 02:51.550
Of course, you have to tell the Python interpreter that an assignment

02:51.550 --> 03:00.070
is not a variable or a pointer that points to an address in the

03:00.070 --> 03:05.010
computer's memory, but that it should be a symbol with completely

03:05.010 --> 03:05.770
different properties.

03:06.550 --> 03:09.790
This works with the symbols function.

03:10.610 --> 03:16.430
You can also define several symbols at once, for example separated

03:16.430 --> 03:16.430
with a comma.

03:17.070 --> 03:18.810
That's how it works on the right side.

03:18.910 --> 03:21.830
You see a high comma there, you can define that.

03:22.250 --> 03:25.910
And then this x, y and t has certain properties.

03:30.850 --> 03:33.890
Such symbols are also rendered specifically.

03:33.890 --> 03:42.090
If you work in Jupyter Notebooks, you get a math font.

03:43.950 --> 03:51.270
The font is rendered and displayed.

03:51.510 --> 03:55.490
With such a display command you can work in notebooks.

03:56.110 --> 03:58.630
The x is shown in such a math font.

04:01.450 --> 04:08.130
Then you can define such expressions and equations.

04:09.390 --> 04:11.370
Equations have a left and right side.

04:14.250 --> 04:17.010
Equations can be read from left to right or from right to left.

04:17.430 --> 04:21.190
That's a difference to such an expression.

04:22.310 --> 04:27.350
Now let's look at an expression that you can define.

04:29.530 --> 04:32.510
Such an expression is equal to x squared.

04:33.790 --> 04:42.510
That means we have such an x squared in math font, if you execute the

04:42.510 --> 04:43.230
object there.

04:43.910 --> 04:47.410
And here is such an equation shown, how it works.

04:48.050 --> 04:52.970
That's this simple dot, capital E and small q.

04:53.630 --> 04:56.870
You see in the brackets that it is separated by a comma.

04:59.730 --> 05:05.390
The first argument is the left side of an equation and the second the

05:05.390 --> 05:05.870
right side.

05:06.590 --> 05:12.290
You see that when it is executed, 3x is equal to minus 10.

05:12.610 --> 05:16.590
That's how you can define equations in Symbi.

05:21.010 --> 05:23.010
There are already built-in plot functions.

05:23.010 --> 05:27.190
You don't necessarily need an extra plot library.

05:28.990 --> 05:34.030
You can also make some settings there, set the value range and so on.

05:35.850 --> 05:37.850
We wrote such an expression before.

05:39.650 --> 05:44.130
That was the abbreviation expression is equal to x squared.

05:45.910 --> 05:50.790
If you call this Symbi plot and type in the expression there, then you

05:50.790 --> 05:55.630
can specify the second argument in which value range the x should be

05:55.630 --> 05:55.630
executed.

05:56.710 --> 05:58.350
This is very practical.

05:58.670 --> 06:03.850
In the previous part or in the NumPy part, we also made similar plots,

06:04.170 --> 06:04.890
that we had generated a time series.

06:06.310 --> 06:16.430
It was important to create a time vector, for example with numpy

06:16.430 --> 06:16.950
.arrange.

06:18.390 --> 06:25.810
Then apply a mathematical function to it, for example x squared.

06:26.490 --> 06:31.670
Then you can plot these two time series with a plot command.

06:31.970 --> 06:37.110
Here it is a bit shorter and easier because it is calculated

06:37.110 --> 06:37.110
symbolically.

06:37.210 --> 06:43.170
You just have to say in which value range this expression should be

06:43.170 --> 06:43.650
executed.

06:43.650 --> 06:46.190
Then you get such a plot with one line.

06:48.070 --> 06:51.050
You can also tune the axis label.

06:51.790 --> 06:54.830
You can already see a small x on the x-axis.

06:55.510 --> 07:00.270
On the y-axis you can see a f of x.

07:02.210 --> 07:06.390
You get plots of mathematical functions pretty quickly.

07:07.350 --> 07:09.150
This is the first advantage.

07:09.390 --> 07:13.610
If you have larger functions that you want to type in and also with a

07:13.610 --> 07:19.630
display or a print command, or you just have to call the expression,

07:20.810 --> 07:26.370
then you can see what you typed in as mathematical functions in such a

07:26.370 --> 07:27.310
mathematical font.

07:28.230 --> 07:30.310
And then just plot it.

07:30.770 --> 07:35.630
That means you probably get to the goal faster with SymPy, if you type

07:35.630 --> 07:42.110
a certain formula from a book or a paper, just to show it graphically.

07:46.550 --> 07:50.610
That would be the first reason why you should calculate symbolically.

07:51.350 --> 07:54.510
But the main reason why you should look at it, the topic of

07:54.510 --> 07:58.730
calculating symbolically on a PC, is the accuracy.

07:59.250 --> 08:00.710
We talked about it before.

08:01.950 --> 08:06.090
It was about comparisons of floating point numbers.

08:06.770 --> 08:11.550
That was two parts before in the lecture.

08:12.390 --> 08:16.090
That you can't make precise comparisons.

08:17.950 --> 08:20.650
It's only possible with certain tolerance ranges.

08:21.610 --> 08:26.770
Simply because the values of the floating point numbers differ

08:26.770 --> 08:29.990
minimally in the comma places and in the decimal places.

08:32.490 --> 08:34.490
That has nothing to do with Python.

08:34.490 --> 08:38.350
That's just the computer architecture and how floating point numbers

08:38.350 --> 08:38.910
are represented on a computer.

08:40.390 --> 08:43.110
And of course you can do that with symbolic, if you do such

08:43.110 --> 08:47.770
conversions with symbolic mathematics, then again exactly.

08:49.230 --> 08:50.170
This is shown here.

08:50.630 --> 08:56.490
We get the mathematics module from the Python Basics Library and just

08:56.490 --> 08:57.930
calculate the root of 8.

08:59.010 --> 09:01.770
And that's where a floating point number comes out.

09:04.770 --> 09:11.240
So a floating point number.

09:11.720 --> 09:19.960
And if we do that with sympi, then the equation is solved correctly

09:19.960 --> 09:21.640
and the two are drawn in front of the root.

09:22.260 --> 09:24.280
And then you have the exact solution.

09:25.200 --> 09:26.980
This may not be very interesting for a one-liner.

09:29.180 --> 09:33.600
But if you imagine that you have a longer algorithm, then you can do

09:33.600 --> 09:33.600
that.

09:33.600 --> 09:37.320
Where you perform many of these mathematical operations one after the

09:37.320 --> 09:43.000
other and then use them again and have such intermediate results, that

09:43.000 --> 09:43.880
makes a difference.

09:44.700 --> 09:50.060
And then it gets even more exciting if you have an algorithm that you

09:50.060 --> 09:50.960
call up again and again.

09:51.800 --> 09:53.760
This is also the case with optimizers, for example.

09:55.580 --> 09:58.500
That you have such a preliminary loop that iterates up to a certain

09:58.500 --> 09:59.440
termination criterion.

10:00.580 --> 10:05.420
And if you can calculate that symbolically, then you have a much

10:05.420 --> 10:12.360
higher accuracy and only have to convert the result into such a

10:12.360 --> 10:20.380
floating point number As if you were always working with floating

10:20.380 --> 10:24.160
point numbers and then make such a numerical error again and again,

10:24.380 --> 10:26.440
which accumulates over time.

10:28.740 --> 10:34.680
So that would be a reason why you should take a look at this, what you

10:34.680 --> 10:37.060
can do with symbolic mathematics on a computer.

10:42.280 --> 10:49.560
Of course, you can simplify such expressions and equations, also pull

10:49.560 --> 10:50.280
them apart again.

10:50.500 --> 10:52.540
There are a few built-in commands.

10:53.360 --> 10:57.260
Here is an expression shown.

10:57.660 --> 10:59.900
So sin²x and cos²x.

11:00.820 --> 11:06.420
And if you just make a simplification, then the known 1 comes out.

11:11.080 --> 11:19.440
Here is another expression, that you use a bracket and you can

11:19.440 --> 11:20.520
calculate that with the expand.

11:22.040 --> 11:24.260
These are of course very, very simple examples.

11:24.820 --> 11:26.600
It's pretty clear what comes out.

11:26.640 --> 11:30.840
But if you calculate bigger problems and then have very, very long

11:30.840 --> 11:35.380
formulas, then such commands are of course very helpful, before you

11:35.380 --> 11:36.420
calculate it all on paper.

11:40.460 --> 11:46.160
And with the factor command, you can then pull the multiplier out

11:46.160 --> 11:46.160
again.

11:51.380 --> 11:54.640
Of course, it is also built-in to differentiate and integrate.

11:58.020 --> 11:59.800
Here we have a very simple example.

12:01.940 --> 12:05.200
You can also see that in SymPy there are of course all these

12:05.200 --> 12:06.160
functions, i.e.

12:06.220 --> 12:09.020
sine and exponential function and root and so on.

12:09.520 --> 12:14.000
Of course, you also have to use them from SymPy, so that it all works.

12:15.420 --> 12:23.960
And then you can differentiate and also integrate, and you always have

12:23.960 --> 12:30.780
the expression in such a mathematical type and can then work very well

12:30.780 --> 12:31.080
with it.

12:37.920 --> 12:44.580
Of course, you can also do this with a fixed interval and then get an

12:44.580 --> 12:46.580
exact solution for it.

12:47.200 --> 12:53.840
There are also functions for converting this into a floating point

12:53.840 --> 12:57.080
number and so on.

12:57.940 --> 13:00.980
You can use it in other scripts or in other parts of your program.

13:02.420 --> 13:06.860
If you do such an integrated symbolic calculation in the middle, for

13:06.860 --> 13:07.080
example.

13:11.470 --> 13:14.830
Then you can also make such substitutions, so that you put some

13:14.830 --> 13:17.450
expressions with a certain value.

13:19.470 --> 13:20.710
Here it is done.

13:20.810 --> 13:26.870
You simply take this expression and take the command subs and simply

13:26.870 --> 13:27.330
enter a 2 for the x.

13:29.910 --> 13:35.570
And what you can also do is simply solve equations.

13:36.070 --> 13:38.310
Here we have the x² plus 3x is 10.

13:39.370 --> 13:40.890
Of course, you can still do that on paper.

13:41.270 --> 13:43.090
These are all very, very simple examples.

13:43.770 --> 13:46.730
If it gets more complicated, it is of course worth starting the

13:46.730 --> 13:47.650
computer for it first.

13:48.690 --> 13:50.610
And you can do that with the spsolv.

13:51.790 --> 13:54.110
Here you have to pull everything to the left.

13:54.890 --> 13:59.190
That is, the 10 is equal to 10 becomes a minus 10.

13:59.790 --> 14:02.630
And then you get the solutions minus 5 and minus 2.

14:11.140 --> 14:14.240
Then of course there are also possibilities that you can solve

14:14.240 --> 14:14.540
equations.

14:16.340 --> 14:17.360
This is shown here.

14:17.840 --> 14:22.200
There is an equation e1 and an equation e2.

14:23.240 --> 14:25.400
There is left and right side again.

14:25.400 --> 14:31.060
The first argument that comes in here, this 3x plus 4y, is the left

14:31.060 --> 14:31.780
side of the equation.

14:32.100 --> 14:34.920
And then after the comma comes the right side.

14:35.460 --> 14:37.460
Here at equation 2 exactly the same.

14:38.040 --> 14:42.580
And then you can simply build this equation system, that you put both

14:42.580 --> 14:44.020
equations together in such a list.

14:45.340 --> 14:49.420
And then there are such solvers at SymPy that solve the equation

14:49.420 --> 14:49.980
system.

14:52.220 --> 14:57.500
And then you get the solution with the command linsolv.

15:05.550 --> 15:09.370
Of course, you can also work with ODEs and solve differential

15:09.370 --> 15:09.830
equations.

15:14.260 --> 15:17.980
You can do that with two lines.

15:18.720 --> 15:21.060
First you have to define a function.

15:23.140 --> 15:26.980
SymPy knows that y is a function of t.

15:27.600 --> 15:29.640
And then you can solve an equation.

15:30.060 --> 15:34.080
The command to solve it yourself is this dsolv differential equation.

15:34.760 --> 15:38.160
And then in the middle of the bracket there is an equation command.

15:39.880 --> 15:49.460
And then you can differentiate y of t twice to t minus y of t comma.

15:49.660 --> 15:53.940
Then comes the right side, the e function.

15:55.000 --> 15:57.980
And then this should be solved to y of t.

15:58.380 --> 16:01.520
And then you get it here with the integration constants.

16:03.260 --> 16:08.220
So you can solve such differential equations very quickly.

16:08.620 --> 16:13.120
And there is of course also the possibility to enter boundary

16:13.120 --> 16:18.100
conditions, so that the integration constants are also solved.

16:23.910 --> 16:26.290
Then you can also work with matrices in SymPy.

16:27.650 --> 16:30.210
There is also the matrix command.

16:32.090 --> 16:35.710
You type it in very similarly as in NumPy.

16:37.310 --> 16:39.930
That means you work with these right-angled brackets.

16:40.450 --> 16:42.950
There is a two-dimensional matrix.

16:43.210 --> 16:47.130
That means there is an outer right-angled bracket and in there you can

16:47.130 --> 16:47.970
find the lines.

16:48.790 --> 16:52.190
I wrote it with a comma and a line break so that you can see it right

16:52.190 --> 16:52.410
away.

16:52.410 --> 16:56.370
I think it's pretty clear how the syntax works.

16:56.450 --> 16:58.650
It's just like in NumPy.

16:59.310 --> 17:07.550
There are also special matrices like the unit matrix with SymPy i or

17:07.550 --> 17:08.670
such a zero matrix.

17:08.870 --> 17:12.310
You can specify the dimensions and so on.

17:21.150 --> 17:26.410
You can of course also do matrix decompositions and such inverts.

17:27.190 --> 17:31.390
You can either write that with the high minus 1 or so that it is

17:31.390 --> 17:33.630
perhaps a little more readable with the .inf.

17:35.170 --> 17:37.450
Here is the unit matrix built up.

17:37.690 --> 17:38.570
It is multiplied by 4.

17:39.210 --> 17:40.570
You have the values 4 on the main diagonal.

17:42.230 --> 17:43.790
The diagonal elements are 0.

17:44.670 --> 17:46.270
Then it is inverted.

17:48.550 --> 17:53.710
Then you can also overwrite the elements.

17:54.730 --> 17:58.150
Here is the second last entry.

18:00.250 --> 18:01.670
An X is simply written in there.

18:02.270 --> 18:04.670
Then we have such an X on the side diagonal element.

18:06.730 --> 18:11.310
You can of course also use other symbols in such a matrix and maybe

18:11.310 --> 18:17.610
define a state-space model and use certain physical units.

18:18.230 --> 18:25.590
Then later with substitution you can replace such symbols with

18:25.590 --> 18:26.730
numerical values in matrices.

18:31.520 --> 18:37.180
It is a very, very powerful algebra package and you can calculate

18:37.180 --> 18:38.120
symbolically with it.

18:38.680 --> 18:41.220
I think the most exciting thing is that you can simply install it as a

18:41.220 --> 18:45.100
package and install it in your Python environment.

18:46.780 --> 18:50.920
If you have a part where you want to calculate symbolically, then you

18:50.920 --> 18:56.860
don't have to switch to another tool or install another software and

18:56.860 --> 19:01.620
then export and import data and values from one tool to another.

19:02.660 --> 19:06.140
You can do it all integrated.

19:06.960 --> 19:10.260
That was the reason why I wanted to briefly introduce SymPy.

19:11.560 --> 19:13.920
Just a few features of it.

19:14.520 --> 19:17.900
You can find many more examples in the documentation.

19:18.280 --> 19:19.760
You can just have a look.

19:19.880 --> 19:22.260
You can try to solve a differential equation.

19:24.120 --> 19:30.280
Maybe you have some tasks in another course or solve a equation

19:30.280 --> 19:30.760
system.

19:33.280 --> 19:37.900
I think you will find interesting tasks where you can try out SymPy.

19:39.180 --> 19:44.940
If you already work with another tool and, for example, use Maple or

19:44.940 --> 19:51.060
something like that, then you can compare it and see what the commands

19:51.060 --> 19:55.520
are like and how it feels when you work directly in Python in a

19:55.520 --> 19:58.180
Jupyter notebook with SymPy.

20:00.360 --> 20:01.840
Thank you for listening.

20:01.980 --> 20:03.360
That was a bit of a shorter part.

20:03.480 --> 20:07.980
I just wanted you to see SymPy and know that it also works in Python.

20:07.980 --> 20:10.220
That's Symbolic Computing.

