Linear Algebra With Python & Numpy

Hi there. Here is a page where I try out some linear algebra functions in Python with the Numpy package.




Python’s Numpy Package

The Numpy package in Python stands for numerical Python. This package contains a lot of functions for mathematics, statistics and for linear algebra.

Part One – Vectors

This first section deals with vectors while the next section deals with matrices.

We start with creating two vectors and performing vector addition and subtraction. The two vectors are \textbf{a} = (1, -1, 2) and \textbf{b} = (2, -1, 3).

When it comes to vector addition and vector subtraction the operation performs element wise.

Scalar multiplication is when a vector is multiplied by a number.

Dot Product

The dot product of two vectors consists of element wise multiplication and then taking the sum of the products.

Cross Product

Given two vectors, the cross product vector is a third vector that is perpendicular/orthogonal (or is 90 degrees) to both the two vectors.

To make sure that the vector (-1, 1, 1) is truly a cross product vector to (1, -1, 2) and (2, -1, 3), we take the dot products between each of the two vectors and (-1, 1, 1).

Vector Of Random Numbers

In Numpy, you can create a vector of random numbers. In this example, I create four normally distributed random numbers.

A Function Example

You can feed in values from a Numpy array into a function. In this example, I have a Numpy array of 0, 1, 2 and 3 and I feed those numbers into a simple exponential function.


Part Two – Matrices

This section starts with a few special matrices.

In Numpy, matrices are created by rows and not by columns in np.array(). The matrix A is created in this form:


Matrix Determinants

Diagonal Of A Matrix

Trace Of A Matrix

Transpose Of A Matrix (Switch Rows With Columns & Vice Versa)

Inverse Of A Square Matrix

Eigenvalues & Eigenvectors Of A Square Matrix

Matrix Multiplication

Solving A Linear System


Leave a Reply