All published worksheets from http://sagenb.org
Image: ubuntu2004
I stayed up a bit and wrote an LU decomposition function for Sage. You can see the actual code that is going into Sage soon at http://trac.sagemath.org/sage_trac/ticket/3048 or http://trac.sagemath.org/sage_trac/attachment/ticket/3048/lu_decomposition.patch. In particular, you might notice that the new LU decomposition code is used in calculating the determinant of a matrix, solving a system of inequalities, and finding the inverse of a matrix.
Let's try the example from the book.
If the following command doesn't work, evaluate the first cell. You can also look at the source code in that cell for the LU decomposition. It's slightly more general than the algorithm we talked about in class, but the underlying principles are exactly the same. The generalization came in when doing the pivoting and row swaps.
Let's check that it is right.
Remember that Matlab gave us a different answer? That was because Matlab used partial pivoting (it chose the largest pivot and did a row swap). Partial pivoting helps minimize numerical errors. In Sage, because there is exact arithmetic (and we are dealing with exact numbers), we don't need to use partial pivoting. However, we can still ask for it anyway.
Notice that from , we see that the first and last rows are swapped in order to get better pivots numerically. Let's still verify the results.
You might notice in other examples that the Matlab is still a bit different than the Sage . That's because the Matlab , , and satisfy , while the Sage results satisfy . As we'll find out later in the course, basically the Matlab is the transpose of the Sage . There are different conventions for where should go. Some other software does a decomposition. The Lapack software does a decomposition, like Sage.
is the set of complex numbers, so here, we construct a random complex matrix.
If we have a matrix that is not exact (like this floating point complex matrix), the function above knows automatically to do partial pivoting to minimize numerical error.
Check the results. We can see that the difference between the two matrices is really small.