This guide is obsolete, it was written during the early stages when I was learning python. A more up to date guide is available here:
Other python guides including installation of python are available in:
Let's look at basic numeric operations of Scalars, Vectors and Matrices using Python. In order to do this we need to use the Numeric Python NumPy:
In Python scalars can be created using numbers:
They can also be made using square brackets:
Addition of numbers can be carried out using:
However if square brackets are enclosed around the scalars, instead of adding the sum of the two scalars, they are instead concatenated together into a single array:
This may be useful in some cases. However supposing we want to add two 1 row by 4 columns vectors together:
[1, 2, 3, 4, 5, 6, 7, 8]
We find that once again we concatenate them instead of adding them together. For numeric operations, it is hence recommended to use a NumPy array opposed to a Python list:
array([ 6, 8, 10, 12])
These show up in the Variable Explorer, note the types are list and int32 respectively:
When a is examine din the variable explorer, each element in the list has its own Type (in this case all integers), each has a size, in this case each individual element has a size of 1 and these can be customised:
In contrast the NumPy array is totally numerical, and each cell can only contain one value.
So for instance, the following elements in the list can be updated:
Trying the same for the NumPy array gives an error:
Traceback (most recent call last):
File "<ipython-input-5-f1938797665c>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'g'
Traceback (most recent call last):
File "<ipython-input-6-316b8a7bcf3b>", line 1, in <module>
ValueError: setting an array element with a sequence.
In essence we are only allowed a scalar numeric value for each cell in the NumPy Array whereas a list can be made more complicated.
Instead of a vector, we can create a matrix. It is again possible to do both as a list and as NumPy array:
Note the matrix is input using the form:
The inner square brackets surrounding Row0 corresponds to the single dimension of a Row vector, i.e. Row0 by itself is a Row Vector. The outside square brackets however correspond to the fact that, Row0, alongside Row1, together make up a Matrix.
In other words if on the outside there is 1 square bracket we have a 1D object and if there are two square brackets on the outside we have a 2D object.
Once again we can see the difference between the list and the NumPy Array.
To make a 3D array or stack of matrices we can use:
Note this 3D Stack is of the form:
Once again we can see the difference between the List and the NumPy Array:
The NumPy Array is listed as a Stack and has dimensions of 2 pages, 2 rows and 4 columns. By default axis 0 is selected an one may go through each slice (showing all elements of axis 0):
It is worth looking at the shape of the NumPy Arrays b, d and f using:
Out: (2, 4)
Out: (2, 2, 4)
Here we see the Scalar gives no dimensions. This is because there is only 1 Row and 1 Column and thus there is no need to index any values in it as the entire scalar is just a single value. The Row Vector has 4 Columns. It also has 1 Row but because it is a single Row there is once again no need to index it. The Matrix has 2 Rows by 4 Columns and the Stack has 2 Pages, 2 Rows and 4 Columns.
This is perhaps seen more clearly with g, which has a different size in all the dimensions:
Typically we can think of a 3D array as a book, where every page is the same size and we flip through the book to get to the next page. In this case each page is the matrix [p,:,:] this is along axes 0.
However it is also possible to change the axis, in this analogy it is better to think of the 3D array as a 3D object. We can look at axis 1 where we view the matrix [:,r,:] or axis 2 where we view the matrix [:,:,c].
This Stack has 2 Pages, 3 Rows and 4 Columns. The value 8 is on the 0th Page, 1st row and 3rd column. It can be indexed using:
Check your understanding: Index into g to yield the numbers 19, 17 and 2 respectively.