Further information
Contents
Further information#
Why does this book not discuss commenting of code?#
In Python it is possible to write statements that are ignored using the #
symbol. This creates something called a “comment”. For example:
import sympy as sym # Importing the sympy library using an alias
Comments like these often do not add to the readability of the code. In fact they can make the code less readable or at worse confusing [Martin, 2009].
In this section of the book there is in fact no need for comments like this as you are mainly using tools that are well documented. Furthermore when using Jupyter notebooks you can add far more to the readability of the code by adding prose alongside our code instead of using small brief inline comments.
This does not mean that readability of code is not important.
Important
Being able to read and understand written code is important.
In Chapter Functions and data structures you will start to write functions and emphasis will be given there on readability and documenting (as opposed to commenting) the code written. A specific discussion about using a tool called a docstring as opposed to a comment will be covered.
In chapters Modularisation to Testing there is more information on how to ensure code is readable and understandable.
Why do we use @
for matrix multiplication and not *
?#
With sympy
it is in fact possible to use the *
operator for matrix
multiplication:
import sympy as sym
matrix = sym.Matrix([[sym.S(1) / 5, 1], [1, 1]])
other_matrix = sym.Matrix([[sym.S(4) / 5, 0], [0, 0]])
matrix * other_matrix
However there are other libraries that can be used for linear algebra and in
those libraries the *
does not do matrix multiplication, it does element wise
multiplication instead. So for clarity it is preferred to use @
throughout.
I have read that numpy
is a library for linear algebra?#
numpy
is one of the most popular and important libraries in the Python
ecosystem. It is in fact the best library to use when doing linear
algebra as it is computationally efficient, however it cannot handle
symbolic variables which is why you are seeing how to use Sympy
here.
An introduction to numpy
is covered in Numpy.