Re-run Chapter 11 files with Python 3.12
This commit is contained in:
parent
1902eb2a50
commit
c206a75f3c
7 changed files with 256 additions and 248 deletions
|
|
@ -74,7 +74,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/home/webartifex/repos/intro-to-python/11_classes\n"
|
||||
"/home/alexander/Repositories/intro-to-python/11_classes\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
@ -106,8 +106,8 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"00_content.ipynb 02_content.ipynb 04_content.ipynb\n",
|
||||
"01_exercises.ipynb 03_content.ipynb sample_package\n"
|
||||
"00_content.ipynb 02_content.ipynb 04_content.ipynb\t06_review.ipynb\n",
|
||||
"01_exercises.ipynb 03_content.ipynb 05_summary.ipynb\tsample_package\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
@ -202,7 +202,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"<module 'sample_package' from '/home/webartifex/repos/intro-to-python/11_classes/sample_package/__init__.py'>"
|
||||
"<module 'sample_package' from '/home/alexander/Repositories/intro-to-python/11_classes/sample_package/__init__.py'>"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
|
|
@ -324,9 +324,9 @@
|
|||
" - vector: defines the Vector class\n",
|
||||
" - utils: defines the norm() function that is shared by Matrix and Vector\n",
|
||||
" and package-wide constants\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" The classes implement arithmetic operations involving vectors and matrices.\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" See the docstrings in the modules and classes for further info.\n",
|
||||
"\n",
|
||||
"PACKAGE CONTENTS\n",
|
||||
|
|
@ -338,14 +338,14 @@
|
|||
" builtins.object\n",
|
||||
" sample_package.matrix.Matrix\n",
|
||||
" sample_package.vector.Vector\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" class Matrix(builtins.object)\n",
|
||||
" | Matrix(data)\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | An m-by-n-dimensional matrix from linear algebra.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | All entries are converted to floats, or whatever is set in the typing attribute.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Attributes:\n",
|
||||
" | storage (callable): data type used to store the entries internally;\n",
|
||||
" | defaults to tuple\n",
|
||||
|
|
@ -354,63 +354,63 @@
|
|||
" | vector_cls (vector.Vector): a reference to the Vector class to work with\n",
|
||||
" | zero_threshold (float): max. tolerance when comparing an entry to zero;\n",
|
||||
" | defaults to 1e-12\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Methods defined here:\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __abs__(self)\n",
|
||||
" | The Frobenius norm of a Matrix.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __add__(self, other)\n",
|
||||
" | Handle `self + other` and `other + self`.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | This may be either matrix addition or broadcasting addition.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> Matrix([(1, 2), (3, 4)]) + Matrix([(2, 3), (4, 5)])\n",
|
||||
" | Matrix(((3.0, 5.0,), (7.0, 9.0,)))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> Matrix([(1, 2), (3, 4)]) + 5\n",
|
||||
" | Matrix(((6.0, 7.0,), (8.0, 9.0,)))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> 10 + Matrix([(1, 2), (3, 4)])\n",
|
||||
" | Matrix(((11.0, 12.0,), (13.0, 14.0,)))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __bool__(self)\n",
|
||||
" | A Matrix is truthy if its Frobenius norm is strictly positive.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __eq__(self, other)\n",
|
||||
" | Handle `self == other`.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Compare two Matrix instances for equality.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> Matrix([(1, 2), (3, 4)]) == Matrix([(1, 2), (3, 4)])\n",
|
||||
" | True\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> Matrix([(1, 2), (3, 4)]) == Matrix([(5, 6), (7, 8)])\n",
|
||||
" | False\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __float__(self)\n",
|
||||
" | Cast a Matrix as a scalar.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Returns:\n",
|
||||
" | scalar (float)\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Raises:\n",
|
||||
" | RuntimeError: if the Matrix has more than one entry\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __getitem__(self, index)\n",
|
||||
" | Obtain an individual entry of a Matrix.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Args:\n",
|
||||
" | index (int / tuple of int's): if index is an integer,\n",
|
||||
" | the Matrix is viewed as a sequence in row-major order;\n",
|
||||
" | if index is a tuple of integers, the first one refers to\n",
|
||||
" | the row and the second one to the column of the entry\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Returns:\n",
|
||||
" | entry (Matrix.typing)\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> m = Matrix([(1, 2), (3, 4)])\n",
|
||||
" | >>> m[0]\n",
|
||||
|
|
@ -419,226 +419,228 @@
|
|||
" | 4.0\n",
|
||||
" | >>> m[0, 1]\n",
|
||||
" | 2.0\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __init__(self, data)\n",
|
||||
" | Create a new matrix.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Args:\n",
|
||||
" | data (sequence of sequences): the matrix's entries;\n",
|
||||
" | viewed as a sequence of the matrix's rows (i.e., row-major order);\n",
|
||||
" | use the .from_columns() class method if the data come as a sequence\n",
|
||||
" | of the matrix's columns (i.e., column-major order)\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Raises:\n",
|
||||
" | ValueError:\n",
|
||||
" | - if no entries are provided\n",
|
||||
" | - if the number of columns is inconsistent across the rows\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> Matrix([(1, 2), (3, 4)])\n",
|
||||
" | Matrix(((1.0, 2.0,), (3.0, 4.0,)))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __iter__(self)\n",
|
||||
" | Loop over a Matrix's entries.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | See .entries() for more customization options.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __len__(self)\n",
|
||||
" | Number of entries in a Matrix.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __mul__(self, other)\n",
|
||||
" | Handle `self * other` and `other * self`.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | This may be either scalar multiplication, matrix-vector multiplication,\n",
|
||||
" | vector-matrix multiplication, or matrix-matrix multiplication.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> Matrix([(1, 2), (3, 4)]) * Matrix([(1, 2), (3, 4)])\n",
|
||||
" | Matrix(((7.0, 10.0,), (15.0, 22.0,)))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> 2 * Matrix([(1, 2), (3, 4)])\n",
|
||||
" | Matrix(((2.0, 4.0,), (6.0, 8.0,)))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> Matrix([(1, 2), (3, 4)]) * 3\n",
|
||||
" | Matrix(((3.0, 6.0,), (9.0, 12.0,)))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Matrix-vector and vector-matrix multiplication are not commutative.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> from sample_package import Vector\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> Matrix([(1, 2), (3, 4)]) * Vector([5, 6])\n",
|
||||
" | Vector((17.0, 39.0))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> Vector([5, 6]) * Matrix([(1, 2), (3, 4)])\n",
|
||||
" | Vector((23.0, 34.0))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __neg__(self)\n",
|
||||
" | Handle `-self`.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Negate all entries of a Matrix.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __pos__(self)\n",
|
||||
" | Handle `+self`.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | This is simply an identity operator returning the Matrix itself.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __radd__(self, other)\n",
|
||||
" | See docstring for .__add__().\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __repr__(self)\n",
|
||||
" | Text representation of a Matrix.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __reversed__(self)\n",
|
||||
" | Loop over a Matrix's entries in reverse order.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | See .entries() for more customization options.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __rmul__(self, other)\n",
|
||||
" | See docstring for .__mul__().\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __rsub__(self, other)\n",
|
||||
" | See docstring for .__sub__().\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __str__(self)\n",
|
||||
" | Human-readable text representation of a Matrix.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __sub__(self, other)\n",
|
||||
" | Handle `self - other` and `other - self`.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | This may be either matrix subtraction or broadcasting subtraction.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> Matrix([(2, 3), (4, 5)]) - Matrix([(1, 2), (3, 4)])\n",
|
||||
" | Matrix(((1.0, 1.0,), (1.0, 1.0,)))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> Matrix([(1, 2), (3, 4)]) - 1\n",
|
||||
" | Matrix(((0.0, 1.0,), (2.0, 3.0,)))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> 10 - Matrix([(1, 2), (3, 4)])\n",
|
||||
" | Matrix(((9.0, 8.0,), (7.0, 6.0,)))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __truediv__(self, other)\n",
|
||||
" | Handle `self / other`.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Divide a Matrix by a scalar.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> Matrix([(1, 2), (3, 4)]) / 4\n",
|
||||
" | Matrix(((0.25, 0.5,), (0.75, 1.0,)))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | as_vector(self)\n",
|
||||
" | Get a Vector representation of a Matrix.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Returns:\n",
|
||||
" | vector (vector.Vector)\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Raises:\n",
|
||||
" | RuntimeError: if one of the two dimensions, .n_rows or .n_cols, is not 1\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> Matrix([(1, 2, 3)]).as_vector()\n",
|
||||
" | Vector((1.0, 2.0, 3.0))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | cols(self)\n",
|
||||
" | Loop over a Matrix's columns.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Returns:\n",
|
||||
" | columns (generator): produces a Matrix's columns as Vectors\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | entries(self, *, reverse=False, row_major=True)\n",
|
||||
" | Loop over a Matrix's entries.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Args:\n",
|
||||
" | reverse (bool): flag to loop backwards; defaults to False\n",
|
||||
" | row_major (bool): flag to loop in row-major order; defaults to True\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Returns:\n",
|
||||
" | entries (generator): produces a Matrix's entries\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | rows(self)\n",
|
||||
" | Loop over a Matrix's rows.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Returns:\n",
|
||||
" | rows (generator): produces a Matrix's rows as Vectors\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | transpose(self)\n",
|
||||
" | Switch the rows and columns of a Matrix.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Returns:\n",
|
||||
" | matrix (Matrix)\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> m = Matrix([(1, 2), (3, 4)])\n",
|
||||
" | >>> m\n",
|
||||
" | Matrix(((1.0, 2.0,), (3.0, 4.0,)))\n",
|
||||
" | >>> m.transpose()\n",
|
||||
" | Matrix(((1.0, 3.0,), (2.0, 4.0,)))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | ----------------------------------------------------------------------\n",
|
||||
" | Class methods defined here:\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | from_columns(data) from builtins.type\n",
|
||||
" | Create a new matrix.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | This is an alternative constructor for data provided in column-major order.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Args:\n",
|
||||
" | data (sequence of sequences): the matrix's entries;\n",
|
||||
" | viewed as a sequence of the matrix's columns (i.e., column-major order);\n",
|
||||
" | use the normal constructor method if the data come as a sequence\n",
|
||||
" | of the matrix's rows (i.e., row-major order)\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Raises:\n",
|
||||
" | ValueError:\n",
|
||||
" | - if no entries are provided\n",
|
||||
" | - if the number of rows is inconsistent across the columns\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> Matrix.from_columns([(1, 2), (3, 4)])\n",
|
||||
" | Matrix(((1.0, 3.0,), (2.0, 4.0,)))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | from_rows(data) from builtins.type\n",
|
||||
" | See docstring for .__init__().\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | ----------------------------------------------------------------------\n",
|
||||
" | Readonly properties defined here:\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | n_cols\n",
|
||||
" | Number of columns in a Matrix.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | n_rows\n",
|
||||
" | Number of rows in a Matrix.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | ----------------------------------------------------------------------\n",
|
||||
" | Data descriptors defined here:\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __dict__\n",
|
||||
" | dictionary for instance variables (if defined)\n",
|
||||
" | \n",
|
||||
" | dictionary for instance variables\n",
|
||||
" |\n",
|
||||
" | __weakref__\n",
|
||||
" | list of weak references to the object (if defined)\n",
|
||||
" | \n",
|
||||
" | list of weak references to the object\n",
|
||||
" |\n",
|
||||
" | ----------------------------------------------------------------------\n",
|
||||
" | Data and other attributes defined here:\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __hash__ = None\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | storage = <class 'tuple'>\n",
|
||||
" | Built-in immutable sequence.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | If no argument is given, the constructor returns an empty tuple.\n",
|
||||
" | If iterable is specified the tuple is initialized from iterable's items.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | If the argument is a tuple, the return value is the same object.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" |\n",
|
||||
" | typing = <class 'float'>\n",
|
||||
" | Convert a string or number to a floating point number, if possible.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" |\n",
|
||||
" | vector_cls = <class 'sample_package.vector.Vector'>\n",
|
||||
" | A one-dimensional vector from linear algebra.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | All entries are converted to floats, or whatever is set in the typing attribute.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Attributes:\n",
|
||||
" | matrix_cls (matrix.Matrix): a reference to the Matrix class to work with\n",
|
||||
" | storage (callable): data type used to store the entries internally;\n",
|
||||
|
|
@ -647,16 +649,17 @@
|
|||
" | defaults to float\n",
|
||||
" | zero_threshold (float): max. tolerance when comparing an entry to zero;\n",
|
||||
" | defaults to 1e-12\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" |\n",
|
||||
" | zero_threshold = 1e-12\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" class Vector(builtins.object)\n",
|
||||
" | Vector(data)\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | A one-dimensional vector from linear algebra.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | All entries are converted to floats, or whatever is set in the typing attribute.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Attributes:\n",
|
||||
" | matrix_cls (matrix.Matrix): a reference to the Matrix class to work with\n",
|
||||
" | storage (callable): data type used to store the entries internally;\n",
|
||||
|
|
@ -665,179 +668,179 @@
|
|||
" | defaults to float\n",
|
||||
" | zero_threshold (float): max. tolerance when comparing an entry to zero;\n",
|
||||
" | defaults to 1e-12\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Methods defined here:\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __abs__(self)\n",
|
||||
" | The Euclidean norm of a vector.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __add__(self, other)\n",
|
||||
" | Handle `self + other` and `other + self`.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | This may be either vector addition or broadcasting addition.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> Vector([1, 2, 3]) + Vector([2, 3, 4])\n",
|
||||
" | Vector((3.0, 5.0, 7.0))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> Vector([1, 2, 3]) + 4\n",
|
||||
" | Vector((5.0, 6.0, 7.0))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> 10 + Vector([1, 2, 3])\n",
|
||||
" | Vector((11.0, 12.0, 13.0))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __bool__(self)\n",
|
||||
" | A Vector is truthy if its Euclidean norm is strictly positive.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __eq__(self, other)\n",
|
||||
" | Handle `self == other`.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Compare two Vectors for equality.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> Vector([1, 2, 3]) == Vector([1, 2, 3])\n",
|
||||
" | True\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> Vector([1, 2, 3]) == Vector([4, 5, 6])\n",
|
||||
" | False\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __float__(self)\n",
|
||||
" | Cast a Vector as a scalar.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Returns:\n",
|
||||
" | scalar (float)\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Raises:\n",
|
||||
" | RuntimeError: if the Vector has more than one entry\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __getitem__(self, index)\n",
|
||||
" | Obtain an individual entry of a Vector.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __init__(self, data)\n",
|
||||
" | Create a new vector.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Args:\n",
|
||||
" | data (sequence): the vector's entries\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Raises:\n",
|
||||
" | ValueError: if no entries are provided\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> Vector([1, 2, 3])\n",
|
||||
" | Vector((1.0, 2.0, 3.0))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> Vector(range(3))\n",
|
||||
" | Vector((0.0, 1.0, 2.0))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __iter__(self)\n",
|
||||
" | Loop over a Vector's entries.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __len__(self)\n",
|
||||
" | Number of entries in a Vector.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __mul__(self, other)\n",
|
||||
" | Handle `self * other` and `other * self`.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | This may be either the dot product of two vectors or scalar multiplication.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> Vector([1, 2, 3]) * Vector([2, 3, 4])\n",
|
||||
" | 20.0\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> 2 * Vector([1, 2, 3])\n",
|
||||
" | Vector((2.0, 4.0, 6.0))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> Vector([1, 2, 3]) * 3\n",
|
||||
" | Vector((3.0, 6.0, 9.0))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __neg__(self)\n",
|
||||
" | Handle `-self`.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Negate all entries of a Vector.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __pos__(self)\n",
|
||||
" | Handle `+self`.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | This is simply an identity operator returning the Vector itself.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __radd__(self, other)\n",
|
||||
" | See docstring for .__add__().\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __repr__(self)\n",
|
||||
" | Text representation of a Vector.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __reversed__(self)\n",
|
||||
" | Loop over a Vector's entries in reverse order.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __rmul__(self, other)\n",
|
||||
" | See docstring for .__mul__().\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __rsub__(self, other)\n",
|
||||
" | See docstring for .__sub__().\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __str__(self)\n",
|
||||
" | Human-readable text representation of a Vector.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __sub__(self, other)\n",
|
||||
" | Handle `self - other` and `other - self`.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | This may be either vector subtraction or broadcasting subtraction.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> Vector([7, 8, 9]) - Vector([1, 2, 3])\n",
|
||||
" | Vector((6.0, 6.0, 6.0))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> Vector([1, 2, 3]) - 1\n",
|
||||
" | Vector((0.0, 1.0, 2.0))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | >>> 10 - Vector([1, 2, 3])\n",
|
||||
" | Vector((9.0, 8.0, 7.0))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __truediv__(self, other)\n",
|
||||
" | Handle `self / other`.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Divide a Vector by a scalar.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> Vector([9, 6, 12]) / 3\n",
|
||||
" | Vector((3.0, 2.0, 4.0))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | as_matrix(self, *, column=True)\n",
|
||||
" | Get a Matrix representation of a Vector.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Args:\n",
|
||||
" | column (bool): if the vector is interpreted as a\n",
|
||||
" | column vector or a row vector; defaults to True\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Returns:\n",
|
||||
" | matrix (matrix.Matrix)\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Example Usage:\n",
|
||||
" | >>> v = Vector([1, 2, 3])\n",
|
||||
" | >>> v.as_matrix()\n",
|
||||
" | Matrix(((1.0,), (2.0,), (3.0,)))\n",
|
||||
" | >>> v.as_matrix(column=False)\n",
|
||||
" | Matrix(((1.0, 2.0, 3.0,)))\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | ----------------------------------------------------------------------\n",
|
||||
" | Data descriptors defined here:\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __dict__\n",
|
||||
" | dictionary for instance variables (if defined)\n",
|
||||
" | \n",
|
||||
" | dictionary for instance variables\n",
|
||||
" |\n",
|
||||
" | __weakref__\n",
|
||||
" | list of weak references to the object (if defined)\n",
|
||||
" | \n",
|
||||
" | list of weak references to the object\n",
|
||||
" |\n",
|
||||
" | ----------------------------------------------------------------------\n",
|
||||
" | Data and other attributes defined here:\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | __hash__ = None\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | matrix_cls = <class 'sample_package.matrix.Matrix'>\n",
|
||||
" | An m-by-n-dimensional matrix from linear algebra.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | All entries are converted to floats, or whatever is set in the typing attribute.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | Attributes:\n",
|
||||
" | storage (callable): data type used to store the entries internally;\n",
|
||||
" | defaults to tuple\n",
|
||||
|
|
@ -846,18 +849,21 @@
|
|||
" | vector_cls (vector.Vector): a reference to the Vector class to work with\n",
|
||||
" | zero_threshold (float): max. tolerance when comparing an entry to zero;\n",
|
||||
" | defaults to 1e-12\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" |\n",
|
||||
" | storage = <class 'tuple'>\n",
|
||||
" | Built-in immutable sequence.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | If no argument is given, the constructor returns an empty tuple.\n",
|
||||
" | If iterable is specified the tuple is initialized from iterable's items.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" | If the argument is a tuple, the return value is the same object.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" |\n",
|
||||
" | typing = <class 'float'>\n",
|
||||
" | Convert a string or number to a floating point number, if possible.\n",
|
||||
" | \n",
|
||||
" |\n",
|
||||
" |\n",
|
||||
" | zero_threshold = 1e-12\n",
|
||||
"\n",
|
||||
"DATA\n",
|
||||
|
|
@ -870,7 +876,7 @@
|
|||
" Alexander Hess\n",
|
||||
"\n",
|
||||
"FILE\n",
|
||||
" /home/webartifex/repos/intro-to-python/11_classes/sample_package/__init__.py\n",
|
||||
" /home/alexander/Repositories/intro-to-python/11_classes/sample_package/__init__.py\n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
|
|
@ -1102,15 +1108,15 @@
|
|||
"\n",
|
||||
"norm(vec_or_mat)\n",
|
||||
" Calculate the Frobenius or Euclidean norm of a matrix or vector.\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" Find more infos here: https://en.wikipedia.org/wiki/Matrix_norm#Frobenius_norm\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" Args:\n",
|
||||
" vec_or_mat (Vector / Matrix): object whose entries are squared and summed up\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" Returns:\n",
|
||||
" norm (float)\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" Example Usage:\n",
|
||||
" As Vector and Matrix objects are by design non-empty sequences,\n",
|
||||
" norm() may be called, for example, with `[3, 4]` as the argument:\n",
|
||||
|
|
@ -1453,8 +1459,8 @@
|
|||
"\u001b[0;34m\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_entries\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstorage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtyping\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n",
|
||||
"\u001b[0;34m\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n",
|
||||
"\u001b[0;34m\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"a vector must have at least one entry\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mFile:\u001b[0m ~/repos/intro-to-python/11_classes/sample_package/vector.py\n",
|
||||
"\u001b[0;31mType:\u001b[0m function\n"
|
||||
"\u001b[0;31mFile:\u001b[0m ~/Repositories/intro-to-python/11_classes/sample_package/vector.py\n",
|
||||
"\u001b[0;31mType:\u001b[0m function"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
|
|
@ -1635,8 +1641,8 @@
|
|||
"\u001b[0;34m Matrix(((1.0, 3.0,), (2.0, 4.0,)))\u001b[0m\n",
|
||||
"\u001b[0;34m \"\"\"\u001b[0m\u001b[0;34m\u001b[0m\n",
|
||||
"\u001b[0;34m\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__class__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_entries\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mFile:\u001b[0m ~/repos/intro-to-python/11_classes/sample_package/matrix.py\n",
|
||||
"\u001b[0;31mType:\u001b[0m function\n"
|
||||
"\u001b[0;31mFile:\u001b[0m ~/Repositories/intro-to-python/11_classes/sample_package/matrix.py\n",
|
||||
"\u001b[0;31mType:\u001b[0m function"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
|
|
@ -1748,9 +1754,9 @@
|
|||
"\u001b[0;34m\u001b[0m \u001b[0margs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\", \"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n",
|
||||
"\u001b[0;34m\u001b[0m \u001b[0;34m\"(\"\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m\", \"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrepr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mc\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mr\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m\",)\"\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mr\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_entries\u001b[0m\u001b[0;34m\u001b[0m\n",
|
||||
"\u001b[0;34m\u001b[0m \u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n",
|
||||
"\u001b[0;34m\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34mf\"{name}(({args}))\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mFile:\u001b[0m ~/repos/intro-to-python/11_classes/sample_package/matrix.py\n",
|
||||
"\u001b[0;31mType:\u001b[0m function\n"
|
||||
"\u001b[0;34m\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0;34mf\"\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m((\u001b[0m\u001b[0;34m{\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m))\u001b[0m\u001b[0;34m\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mFile:\u001b[0m ~/Repositories/intro-to-python/11_classes/sample_package/matrix.py\n",
|
||||
"\u001b[0;31mType:\u001b[0m function"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
|
|
@ -1865,8 +1871,8 @@
|
|||
"\u001b[0;34m\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mcolumn\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\n",
|
||||
"\u001b[0;34m\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatrix_cls\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\n",
|
||||
"\u001b[0;34m\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmatrix_cls\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mx\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[0;31mFile:\u001b[0m ~/repos/intro-to-python/11_classes/sample_package/vector.py\n",
|
||||
"\u001b[0;31mType:\u001b[0m function\n"
|
||||
"\u001b[0;31mFile:\u001b[0m ~/Repositories/intro-to-python/11_classes/sample_package/vector.py\n",
|
||||
"\u001b[0;31mType:\u001b[0m function"
|
||||
]
|
||||
},
|
||||
"metadata": {},
|
||||
|
|
@ -2122,7 +2128,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Vector(11378937.3105893, ..., 13593029.305789862)[100]\n"
|
||||
"Vector(11378937.310589302, ..., 13593029.305789862)[100]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
@ -2167,7 +2173,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Matrix((14370711.265265431, ...), ..., (..., 16545418.239505699))[100x100]\n"
|
||||
"Matrix((14370711.26526542, ...), ..., (..., 16545418.239505697))[100x100]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
@ -2201,7 +2207,7 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Matrix((32618511.50703142, ...), ..., (..., 32339164.77803234))[50x50]\n"
|
||||
"Matrix((32618511.507031415, ...), ..., (..., 32339164.778032355))[50x50]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
|
@ -2265,7 +2271,7 @@
|
|||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m<ipython-input-55-fd81d962f516>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mA\u001b[0m \u001b[0;34m*\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||||
"Cell \u001b[0;32mIn[55], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mA\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mx\u001b[49m\n",
|
||||
"\u001b[0;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'tuple'"
|
||||
]
|
||||
}
|
||||
|
|
@ -2916,7 +2922,7 @@
|
|||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[0;32m<ipython-input-79-032c970f8cfc>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mA_arr\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mA_mat\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
|
||||
"Cell \u001b[0;32mIn[79], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mA_arr\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mA_mat\u001b[49m\n",
|
||||
"\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (3,3) (9,) "
|
||||
]
|
||||
}
|
||||
|
|
@ -2953,7 +2959,7 @@
|
|||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.6"
|
||||
"version": "3.12.2"
|
||||
},
|
||||
"livereveal": {
|
||||
"auto_select": "code",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue