Python: Sympy Matrices (Linear Algebra SOLVED!)

preview_player
Показать описание

^Downloadable code & more! Python Sympy Matrices

In this video we will use python on matrices to row reduce to echelon form Multiply matrices Solve the linear system Ax = b and finally get eigen vectors and values This is done in python using sympy, which will print out like latex or wolfram alpha That’s enough for this intro, lets get started.

Now let’s go ahead and create a basic two by two matrix. Take a look at that print out. I love how it can do this symbolically just like you would see in a text book.

To row reduce this matrix to echelon form, we write M.rref(). The output shows the reduced matrix and the indices of the pivot columns

This row reduces to the identity so it is invertible. Which by the way if we wanted to invert the matrix we would do M.inv().

Moving right along let’s create a vector x. We will do a matrix multiplication by using the @ symbol. This will only work if the column dimension of the first matrix matches the row dimension of the second.

Lets check the output.

Now let’s create a 3x3 matrix A. We can put fractions inside symbolically by using the sy.Rational() function of sympy.

And lets quickly get the row echelon form. Again this is invertible.

Real quick let’s create a vector b with a 1,2,3.

And now we are going to solve the famous linear system Ax = b.
To do this put A.LUsolve(b). And when we see x, we see how ugly this would have been to do by hand…

Let’s quickly verify that Ax does equal b.

No lets say we need to find the eigen values of A. We write A.eigenvals(). This will give us the value and its algebraic multiplicity.

Similarly we can get the eigenvectors by doing A.eigenvects(). This will give us the eigen values, multiplicities and corresponding eigen vectors.

That would have been so UGLYT to do by hand. Thank you python sympy.

So there you have it, that is how you symbolically can solve basic linear algebra problems using python. I hope this helped and I hope you’ll check out some of my other videos about Python. Thanks for watching!

This is a Python anaconda tutorial for help with coding, programming, or computer science. These are short python videos dedicated to troubleshooting python problems and learning Python syntax. For more videos see Python Help playlist by Rylan Fowers.





#PythonMarathon #LearnPython #PythonTutorial
Рекомендации по теме
Комментарии
Автор

Dubstep intros! You're now a legit youtuber

benjaminburt
Автор

Holy cow! I need to get that. Do you do all your programming on Linux?

fbDJLL