Add sample_package with linear algebra tools
- add sample_package: + __init__.py => high-level description and imports + matrix.py => define a Matrix class + vector.py => define a Vector class + utils.py => package-wide utilities - streamline the code snippets in the chapter 11 notebooks to align with the sample_package
This commit is contained in:
parent
2c83a883c5
commit
6bcfff481c
7 changed files with 770 additions and 40 deletions
35
11_classes/sample_package/utils.py
Normal file
35
11_classes/sample_package/utils.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
"""This module provides utilities for the whole package.
|
||||
|
||||
The defined constants are used as defaults in the Vector and Matrix classes.
|
||||
|
||||
The norm() function is shared by Vector.__abs__() and Matrix.__abs__().
|
||||
"""
|
||||
|
||||
import math
|
||||
|
||||
|
||||
# Define constants (i.e., normal variables that are, by convention, named in UPPERCASE)
|
||||
# that are used as the defaults for class attributes within Vector and Matrix.
|
||||
DEFAULT_ENTRIES_STORAGE = tuple
|
||||
DEFAULT_ENTRY_TYPE = float
|
||||
ZERO_THRESHOLD = 1e-12
|
||||
|
||||
|
||||
def norm(vec_or_mat):
|
||||
"""Calculate the Frobenius or Euclidean norm of a matrix or vector.
|
||||
|
||||
Find more infos here: https://en.wikipedia.org/wiki/Matrix_norm#Frobenius_norm
|
||||
|
||||
Args:
|
||||
vec_or_mat (Vector / Matrix): object whose entries are squared and summed up
|
||||
|
||||
Returns:
|
||||
norm (float)
|
||||
|
||||
Example Usage:
|
||||
As Vector and Matrix objects are by design non-empty sequences,
|
||||
norm() may be called, for example, with `[3, 4]` as the argument:
|
||||
>>> norm([3, 4])
|
||||
5.0
|
||||
"""
|
||||
return math.sqrt(sum(x ** 2 for x in vec_or_mat))
|
||||
Loading…
Add table
Add a link
Reference in a new issue