Saturday, June 4, 2016

Maxima - Linear Algebra

Let's see more in details the matrices and operations with matrices. As we saw previously, the matrices are in fact a list of lists of values, each list is a row. The amount of list is the amount of rows, and the amount of elements in a list is the amount of columns (each list must have the same amount of elements, otherwise you will get an error). Let's try to change the size of a matrix:
Matrix modifications
First, each statement describe quite well the action he do, so try, when we create our own functions, to describe the best as possible the function by the name. If there is a Maxima command which we don't know what it does, or what it expects as input, just write describe(xxx); and Maxima will show you the documentation of the desired function.
Second, when adding rows or columns, be aware to adding the same amount of elements as the matrix accepts. We can see that when adding the row, it has 2 elements, but when adding the column it has 3 elements.
Third, the functions addrow() and addcol() don't modify the original matrix, that's why we need to save the output of those function to m. There are some function which modify the input but isn't a good practice, as you could need to save the result to another variable, maintaining the first matrix unchanged.
Let's continue with some other matrix operations.
 The transpose and invert operation of a matrix can be very tedious, but as we can see, with Maxima is easy. There is two ways to obtain the inverted matrix: using the command invert() or elevating the matrix to -1. Be aware with the second method, because if  want to obtain the inverse of the matrix, you must write it as m^^-1, but if you want to obtain the inverse of each element of the matrix, you must write it as m^-1.
We can continue with some other basics operations.
 First, we've created another matrix to play with. In the first operation (input 10), we've added the value 1 to each element of the matrix. At the input 11, we've made the sum of two matrices, having the result as the sum of each elements of each matrices. At the input 12, we've multiplied two matrices, having the result at the first row/first column as m1[1][1] * m2[1][1] + m1[1][2] * m[2][1] (the classic matrix multiplication). And at 13, we've the multiplication of each element of both matrices placed at the same position. You remember the difference between those two multiplication signs.
And now the eigenvalues and eigenvectors.
Advanced operations with matrix in Maxima
As we saw before, the functions determinant(), eigenvalues() and eigenvectors() accept a single input and return an output without modifying our input. For the eigenvalues and eigenvectors we have a list as a result (a list of vectors for the eigenvectors).
Let's solve a simple problem:
Write a function which rotate tridimensional objects. 
inputs:
  • A list of points which define the object to rotate
  • The three Euler angles
  • The center position from where the rotation must be made
output:
  • A list of points which define the rotated object.
 As this article is mainly based on the Maxima application, I will not explain in details the mathematical part of this problem. To obtain the result, we must first create the Euler matrix with the given angles. Then apply the rotation matrix to the given points. We also must take into account the center of rotation. So the main equation remains as center + rotation . (points - center). I've used the "." multiplication sign as it is the one used by Maxima for the matrices multiplication. Let's do this in a block, this way we can use it later easly.
Rotation function in Maxima
Let's analyze this piece of code. At the first line, I've declared the function with all the inputs and the inner variables we will use inside the block. When you're developing an application like this, it's hard to know at first time the inner variables you will need. Those are added as you're writing the code inside the block (with some practice, you will understand). In the third and fifth lines, I've created two temporary matrices to build later the Euler rotation matrix. At seventh line is the Euler rotation matrix. At ninth, I've created an intermediary matrix to avoid using parenthesis in the last line (to maintain the code more readable). And finally, at the last line, the equation we wanted.


No comments:

Post a Comment