Okay, so doing linear algebra with NumPy, again, we can't just use the star
operation for matrix vector multiplication.
But, again, you can define an array you can use this dot notation to multiply a,
a matrix times a vector, if that's what you're doing.
often we want to solve linear systems. In MatLab if you're familiar with that
there's this backslash operator if you want to solve system ax=b where A is a
matrix and B is a vector of the appropriate shape, then you can set
x=a/b, and that gives you the solution of the linear system.
In Python, there's no backslash operator but it's, it's not much harder to solve
it. there's a module called linalg that's a
submodule of numpy. So from numpy dot linalg, you can import
functions there. Or we can import this whole module.
Here I've just imported the solve function from numpy dot linalg.
and solve gives us the solution of this system.
So you could say x equals solve of A comma b, and x would end up being this
array, 8 comma 9, in this case. And again, you should look at the NumPy
documentation to see more about what other functions are in that, module.
one of them is the eig function, for example, just as in Matlab, there's a
function that computes eigenvalues of a matrix.
So in this case A can be, again, a numpy array, so if this is the same A that we
hit on the previous page, this 2 by 2 matrix if you compute the eigen values of
that matrix. If you just call eig of a, what it
returns is actually a tuple, so there's a left paren, right paren, and the tuple
has two components the first is an array with two components and the second
element of the tuple is another array. that has well let's see.
What we could view as a matrix the first element of this tuple is the eigen
values. It's a two by two matrix, so there's two
eigen values. And the second thing that's returned is.
you could think of as a matrix whose columns are the eigen vectors of the
matrix. Here I just typed eig(A) so it actually
just prints out the results if instead, I had had given it some names I could say
evals, evecs for example = eig(A) then evals would be the first element of this
tuples the eigen values the array of eigen values.
And evecs would be the second element of this tuple, which is the matrix of
eigenvectors. So this is useful notation in Python more
generally when you have a function that has some return value.
If it returns more than one thing, if you have somewhere in your Python function
return, evals, evecs. Then the way it returns it, is as a
tuple, and so if you just type out the name of the function, it would give you
the tuple as the result. I could've said, you know, T equals eig
of A, and t would've been this tuple but if you say evals, evecs equals eig of A,
then it returns that tuple and it unpacks it.
So it puts the first element in the evals and the second element in the evecs.
So it's just a handy way to, to write the, the output of a function that
returns more than one thing in a tuple.