Add initial version of chapter 11, part 4

This commit is contained in:
Alexander Hess 2020-10-28 16:18:15 +01:00
commit 85b7fd274c
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
9 changed files with 2927 additions and 222 deletions

View file

@ -808,7 +808,7 @@
}
},
"source": [
"Every instance comes with a special `.__class__` attribute that also references the corresponding class object."
"`v`'s semantic \"value\" is not so clear yet. We fix this in the next section."
]
},
{
@ -819,41 +819,6 @@
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"__main__.Vector"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v.__class__"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"`v`'s semantic \"value\" is not so clear yet. We fix this in the next section."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
@ -861,7 +826,7 @@
"<__main__.Vector at 0x7f9b9416d760>"
]
},
"execution_count": 22,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
@ -885,7 +850,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 22,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -910,7 +875,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 23,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -946,7 +911,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 24,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -983,7 +948,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 25,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -996,7 +961,7 @@
"[1.0, 2.0, 3.0]"
]
},
"execution_count": 26,
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
@ -1031,7 +996,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 26,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -1042,6 +1007,30 @@
"x = 1, 2, 3"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(1, 2, 3)"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 28,
@ -1062,30 +1051,6 @@
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(1, 2, 3)"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(1, 2, 3)"
]
@ -1103,7 +1068,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 29,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -1144,7 +1109,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 30,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -1157,7 +1122,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 31,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -1170,7 +1135,7 @@
"Vector((1.000, 2.000, 3.000))"
]
},
"execution_count": 32,
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
@ -1192,7 +1157,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 32,
"metadata": {
"slideshow": {
"slide_type": "skip"
@ -1205,7 +1170,7 @@
"Vector((1.000, 2.000, 3.000))"
]
},
"execution_count": 33,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
@ -1227,7 +1192,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 33,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -1240,7 +1205,7 @@
"'Vector((1.000, 2.000, 3.000))'"
]
},
"execution_count": 34,
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
@ -1266,7 +1231,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 34,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -1279,7 +1244,7 @@
"'Vector(1.0, ..., 3.0)[3]'"
]
},
"execution_count": 35,
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
@ -1301,7 +1266,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 35,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -1342,14 +1307,14 @@
}
},
"source": [
"Below is a first implementation of the `Matrix` class that stores the entries internally as a `list` of `list`s.\n",
"Below is a first implementation of the `Matrix` class that stores the `._entries` internally as a `list` of `list`s.\n",
"\n",
"The `.__init__()` method ensures that all the rows come with the same number of columns. Again, we do not allow `Matrix` instances without any entries."
]
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 36,
"metadata": {
"code_folding": [],
"slideshow": {
@ -1391,7 +1356,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 37,
"metadata": {
"slideshow": {
"slide_type": "skip"
@ -1404,7 +1369,7 @@
"94113690738160"
]
},
"execution_count": 38,
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
@ -1413,6 +1378,30 @@
"id(Matrix)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"text/plain": [
"type"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(Matrix)"
]
},
{
"cell_type": "code",
"execution_count": 39,
@ -1421,30 +1410,6 @@
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
"text/plain": [
"type"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(Matrix)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"outputs": [
{
"data": {
@ -1452,7 +1417,7 @@
"__main__.Matrix"
]
},
"execution_count": 40,
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
@ -1474,7 +1439,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 40,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -1487,7 +1452,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 41,
"metadata": {
"slideshow": {
"slide_type": "skip"
@ -1500,7 +1465,7 @@
"140306180401856"
]
},
"execution_count": 42,
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
@ -1511,7 +1476,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 42,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -1524,7 +1489,7 @@
"__main__.Matrix"
]
},
"execution_count": 43,
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
@ -1546,7 +1511,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 43,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -1559,7 +1524,7 @@
"Matrix(((1.000, 2.000, 3.000,), (4.000, 5.000, 6.000,), (7.000, 8.000, 9.000,)))"
]
},
"execution_count": 44,
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
@ -1570,7 +1535,7 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": 44,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -1602,7 +1567,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 45,
"metadata": {
"slideshow": {
"slide_type": "skip"
@ -1628,7 +1593,7 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 46,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -1673,12 +1638,14 @@
"source": [
"The methods we have seen so far are all **instance methods**. The characteristic idea behind an instance method is that the behavior it provides either depends on the state of a concrete instance or mutates it. In other words, an instance method *always* works with attributes on the `self` argument. If a method does *not* need access to `self` to do its job, it is conceptually *not* an instance method and we should probably convert it into another kind of method as shown below.\n",
"\n",
"An example of an instance method from linear algebra is the `.transpose()` method below that switches the rows and columns of an *existing* `Matrix` instance and returns a *new* `Matrix` instance based off that. It is implemented by passing the *iterator* created with the [zip() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/functions.html#zip) built-in as the `data` argument to the `Matrix` constructor: The expression `zip(*self._entries)` may be a bit hard to understand because of the involved unpacking but simply flips a `Vector`'s rows and columns. The built-in [list() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/functions.html#func-list) constructor within the `.__init__()` method then materializes the iterator into the `._entries`. Without a concrete `Matrix`'s rows and columns, `.transpose()` does not make sense, conceptually speaking."
"An example of an instance method from linear algebra is the `.transpose()` method below that switches the rows and columns of an *existing* `Matrix` instance and returns a *new* `Matrix` instance based off that. It is implemented by passing the *iterator* created with the [zip() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/functions.html#zip) built-in as the `data` argument to the `Matrix` constructor: The expression `zip(*self._entries)` may be a bit hard to understand because of the involved unpacking but simply flips a `Matrix`'s rows and columns. The built-in [list() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/functions.html#func-list) constructor within the `.__init__()` method then materializes the iterator into the `._entries` attribute. Without a concrete `Matrix`'s rows and columns, `.transpose()` does not make sense, conceptually speaking.\n",
"\n",
"Also, we see that it is ok to reference a class from within one of its methods. While this seems trivial to some readers, others may find this confusing. The final versions of the `Vector` and `Matrix` classes in the [fourth part <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_nb.png\">](https://nbviewer.jupyter.org/github/webartifex/intro-to-python/blob/develop/11_classes/04_content.ipynb#The-final-Vector-and-Matrix-Classes) of this chapter show how this \"hard coded\" redundancy can be avoided."
]
},
{
"cell_type": "code",
"execution_count": 48,
"execution_count": 47,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -1713,7 +1680,7 @@
},
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 48,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -1724,6 +1691,30 @@
"m = Matrix([(1, 2, 3), (4, 5, 6), (7, 8, 9)])"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Matrix(((1.000, 2.000, 3.000,), (4.000, 5.000, 6.000,), (7.000, 8.000, 9.000,)))"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m"
]
},
{
"cell_type": "code",
"execution_count": 50,
@ -1732,30 +1723,6 @@
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Matrix(((1.000, 2.000, 3.000,), (4.000, 5.000, 6.000,), (7.000, 8.000, 9.000,)))"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
@ -1763,7 +1730,7 @@
"Matrix(((1.000, 4.000, 7.000,), (2.000, 5.000, 8.000,), (3.000, 6.000, 9.000,)))"
]
},
"execution_count": 51,
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
@ -1785,7 +1752,7 @@
},
{
"cell_type": "code",
"execution_count": 52,
"execution_count": 51,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -1796,6 +1763,30 @@
"n = m.transpose().transpose()"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Matrix(((1.000, 2.000, 3.000,), (4.000, 5.000, 6.000,), (7.000, 8.000, 9.000,)))"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n"
]
},
{
"cell_type": "code",
"execution_count": 53,
@ -1808,7 +1799,7 @@
{
"data": {
"text/plain": [
"Matrix(((1.000, 2.000, 3.000,), (4.000, 5.000, 6.000,), (7.000, 8.000, 9.000,)))"
"False"
]
},
"execution_count": 53,
@ -1817,7 +1808,18 @@
}
],
"source": [
"n"
"m is n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Unintuitively, the comparison operator `==` returns a wrong result as `m` and `n` have `_entries` attributes that compare equal. We fix this in the \"*Operator Overloading*\" section later in this chapter."
]
},
{
@ -1840,41 +1842,6 @@
"output_type": "execute_result"
}
],
"source": [
"m is n"
]
},
{
"cell_type": "markdown",
"metadata": {
"slideshow": {
"slide_type": "skip"
}
},
"source": [
"Unintuitively, the comparison operator `==` returns a wrong result as `m` and `n` have `_entries` attributes that compare equal. We fix this in the \"*Operator Overloading*\" section later in this chapter."
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {
"slideshow": {
"slide_type": "fragment"
}
},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"m == n"
]
@ -1894,7 +1861,7 @@
},
{
"cell_type": "code",
"execution_count": 56,
"execution_count": 55,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -1933,7 +1900,7 @@
},
{
"cell_type": "code",
"execution_count": 57,
"execution_count": 56,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -1946,7 +1913,7 @@
},
{
"cell_type": "code",
"execution_count": 58,
"execution_count": 57,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -1959,7 +1926,7 @@
"Matrix(((1.000, 2.000, 3.000,), (4.000, 5.000, 6.000,), (7.000, 8.000, 9.000,)))"
]
},
"execution_count": 58,
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
@ -2007,7 +1974,7 @@
},
{
"cell_type": "code",
"execution_count": 59,
"execution_count": 58,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -2051,7 +2018,7 @@
},
{
"cell_type": "code",
"execution_count": 60,
"execution_count": 59,
"metadata": {
"slideshow": {
"slide_type": "slide"
@ -2064,7 +2031,7 @@
},
{
"cell_type": "code",
"execution_count": 61,
"execution_count": 60,
"metadata": {
"slideshow": {
"slide_type": "fragment"
@ -2077,7 +2044,7 @@
"(2, 3)"
]
},
"execution_count": 61,
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
@ -2099,7 +2066,7 @@
},
{
"cell_type": "code",
"execution_count": 62,
"execution_count": 61,
"metadata": {
"slideshow": {
"slide_type": "fragment"