Vector warmup¶
Topic: basic operations, functions, vectors, timing
- Task:
Create matrices as list tables and numpy arrays.
Test what happens, when you make changes to these.
You should run into some problems, try to fix them.
Test how vectorization affects numeric efficiency by increasing the size of your matrices.
Template: vectors.py
- Further reading:
vectors.py¶
- vectors.add_scalar(matrix, scalar)[source]¶
Adds a scalar to a matrix.
The function checks whether the given matrix is a true numpy array or just a table made of lists.
If the matrix is an array, the operation is executed as a single vector command. If the matrix is a table, the only option is to loop over all elements.
- Parameters:
matrix (list or array) – the matrix
scalar (float) – added value
- vectors.change_center_element(matrix, value)[source]¶
Change the center element of the matrix to the given value.
The given matrix is not affected. Instead, a new matrix is returned.
- Parameters:
matrix (list or array) – the matrix
value (float) – new value
- Returns:
new matrix
- Return type:
array or list
- vectors.change_center_element_in_place(matrix, value)[source]¶
Change the center element of the matrix to the given value.
The change is derectly applied on the given matrix.
- Parameters:
matrix (list or array) – the matrix
value (float) – new value
- vectors.main(nx, ny, printout=True)[source]¶
The main function.
Creates a matrix both as a list of lists and a numpy array and performs similar operations on both.
The first operation is changing the center element of the matrix using either
change_center_element()
orchange_center_element_in_place()
.The second operation is multiplying the matrix with a scalar with
multiply_by_scalar()
.The function times the multiplication for both matrices and returns the required time in seconds.
Note
This function is incomplete!
- Parameters:
nx (int) – number of columns (horizontal size)
ny (int) – number of rows (vertical size)
printout (bool) – if True, the matrices are printed after each step
- Returns:
multiplication time for a table, for a matrix
- Return type:
float, float
- vectors.matrix(nx, ny)[source]¶
Create a matrix of ones as a numpy array.
Note
This function is incomplete!
- Parameters:
nx (int) – number of columns (horizontal size)
ny (int) – number of rows (vertical size)
- Returns:
matrix of ones
- Return type:
list
- vectors.matrix_as_lists(nx, ny)[source]¶
Create a ‘matrix’ of ones as a list of lists.
- Parameters:
nx (int) – number of columns (horizontal size)
ny (int) – number of rows (vertical size)
- Returns:
table of ones
- Return type:
list
- vectors.multiply_by_scalar(matrix, scalar)[source]¶
Multiplies the matrix by a scalar.
The function checks whether the given matrix is a true numpy array or just a table made of lists.
If the matrix is an array, the operation is executed as a single vector command. If the matrix is a table, the only option is to loop over all elements.
- Parameters:
matrix (list or array) – the matrix
scalar (float) – multiplier
- vectors.print_matrix(matrix, name)[source]¶
Prints a matrix in a tabulated format.
For numpy arrays, printing automatically produces a nice matrix-like representation, but lists are printed on one line. This function will tabulate both.
- Parameters:
matrix (list or array) – the matrix
name (str) – the title printed before the actual matrix