Now that you have learned or refreshed the difference between vectors, dense matrices and sparse matrices, it’s time to take a closer look at vectors and what kind of mathematical operations you can do with them. The tutorial focuses here explicitly on mathematical operations so that you’ll come to see the similarities and differences with matrices, and because a huge part of linear algebra is, ultimately, working with matrices.

You have already seen that you can easily create a vector with ** np.array()**. But now that you have vectors at your disposal, you might also want to know of some basic operations that can be performed on them. vector1 and vector2 are already loaded for you in the following code chunk:

Now that you have successfully seen some vector operations, it’s time to get started on to the real matrix work!

**Matrici: operazioni e funzioni**

Similarly to where you left it off at the start of the previous section, you know how to create matrices, but you don’t know yet how you can use them to your advantage. This section will provide you with an overview of some matrix functions and basic matrix routines that you can use to work efficiently.

Firstly, let’s go over some functions. These will come quite easily if you have worked with NumPy before, but even if you don’t have any experience with it yet, you’ll see that these functions are easy to get going.

Let’s look at some examples of functions.

There’s ** np.add()** and

**to add and subtract arrays or matrices, and also**

`np.subtract()`

**and**

`np.divide()`

**for division and multiplication. This really doesn’t seem like a big msytery, does it? Also the**

`np.multiply`

**function that you have seen in the previous section where it was used to calculate the dot product, can also be used with matrices. But don’t forget to pass in two matrices instead of vectors.**

`np.dot()`

These are basic, right? Let’s go a bit less basic. When it comes to multiplications, there are also some other functions that you can consider, such as ** np.vdot()** for the dot product of vectors,

**or**

`np.inner()`

**for the inner or outer products of arrays,**

`np.outer()`

**and**

`np.tensordot()`

**for the Kronecker product of two arrays:**

`np.kron()`

Besides these, it might also be useful to consider some functions of the ** linalg** module: the matrix exponential functions

**,**

`linalg.expm()`

**and**

`linalg.expm2()`

**. The difference between these three lies in the ways that the exponential is calculated. Stick to the first one for a general matrix exponential, but definitely try the three of them out to see the difference in results!**

`linalg.expm3()`

Also trigonometric functions such as ** linalg.cosm()**,

**and**

`linalg.sinm()`

**, hyperbolic trigonometric functions such as**

`linalg.tanm()`

**,**

`linalg.coshm()`

**and**

`linalg.sinhm()`

**, the sign function**

`linalg.tanhm()`

**, the matrix logarithm**

`linalg.signm()`

**, and the matrix square root**

`linalg.logm()`

**.**

`linalg.sqrtm()`

Additionally, you can also evaluate a matrix function with the help of the ** linalg.funm()** function. OOPS 😯 non riuscito a farlo,

**sconosciuta 😡**

`funm()`

Let’s now take a look at some basic matrix routines. The first thing that you probably want to check out are the matrix attributes: ** T** for transposition,

**for conjugate transposition,**

`H`

**for inverse, and**

`I`

**to cast as an array.**

`A`

Qui di nuovo panico, devo approfondire, per adesso così:

When you tranpose a matrix, you make a new matrix whose rows are the columns of the original. A conjugate transposition, on the other hand, interchanges the row and column index for each matrix element. The inverse of a matrix is a matrix that, if multiplied with the original matrix, results in an identity matrix.

But besides those attributes, there are also real functions that you can use to perform some basic matrix routines, such as ** np.transpose()** and

**for transposition and matrix inverse, respectively. Ah! ‘desso me lo dici! restano comunque da scoprite i nomi corti.**

`linalg.inv()`

Besides these, you can also retrieve the trace or sum of the elements on the main matrix diagonal with ** np.trace()**. Similarly, you can also retrieve the matrix rank or the number of Singular Value Decomposition singular values of an array that are greater than a certain treshold with

**_rank from NumPy. Don’t worry if the matrix rank doesn’t make sense for now; You’ll see more on that later on in this tutorial.**

`linalg.matrix`

For now, let’s focus on two more routines that you can use:

- The norm of a matrix can be computed with
: a matrix norm is a number defined in terms of the entries of the matrix. The norm is a useful quantity which can give important information about a matrix because it tells you how large the elements are.`linalg.norm`

- On top of that, you can also calculate the determinant, which is a useful value that can be computed from the elements of a square matrix, with
. The determinant boils down a square matrix to a a single number, which determines whether the square matrix is invertible or not.`linalg.det()`

Lastly, solving large systems of linear equations are one of the most basic applications of matrices. If you have a system of ** Ax = b**, where

**is a square matrix and**

`A`

**a general matrix, you have two methods that you can use to find**

`b`

**, depending of course on which type of matrix you’re working with.**

`x`

Non riesco a fare la prima parte (cioè sì ma mi da altri risultati), questa è la seconda:

To solve sparse matrices, you can use ** linalg.spsolve()**. When you can not solve the equation, it might still be possible to obtain an approximate x with the help of the

**command.**

`linalg.lstsq()`

Now that you have gotten a clue on how you can create matrices and how you can use them for mathematical operations, it’s time to tackle some more advanced topics that you’ll need to really get into machine learning.

## Trackback

[…] da qui, copio qui. Davvero, quella del titolo è la traduzione di eigenvalues and eigenvectors; o almeno […]