Add initial version of chapter 11, part 4
This commit is contained in:
parent
8dc67c5bdd
commit
85b7fd274c
9 changed files with 2927 additions and 222 deletions
|
|
@ -82,7 +82,7 @@
|
|||
"class Vector:\n",
|
||||
"\n",
|
||||
" def __init__(self, data):\n",
|
||||
" self._entries = list(float(x) for x in data)\n",
|
||||
" self._entries = tuple(float(x) for x in data)\n",
|
||||
" # ...\n",
|
||||
"\n",
|
||||
" def __repr__(self):\n",
|
||||
|
|
@ -111,7 +111,7 @@
|
|||
"class Matrix:\n",
|
||||
"\n",
|
||||
" def __init__(self, data):\n",
|
||||
" self._entries = list(list(float(x) for x in r) for r in data)\n",
|
||||
" self._entries = tuple(tuple(float(x) for x in r) for r in data)\n",
|
||||
" # ...\n",
|
||||
"\n",
|
||||
" def __repr__(self):\n",
|
||||
|
|
@ -678,7 +678,7 @@
|
|||
"class Vector:\n",
|
||||
"\n",
|
||||
" def __init__(self, data):\n",
|
||||
" self._entries = list(float(x) for x in data)\n",
|
||||
" self._entries = tuple(float(x) for x in data)\n",
|
||||
" # ...\n",
|
||||
"\n",
|
||||
" def __repr__(self):\n",
|
||||
|
|
@ -692,7 +692,7 @@
|
|||
" return iter(self._entries)\n",
|
||||
"\n",
|
||||
" def __add__(self, other):\n",
|
||||
" if isinstance(other, self.__class__): # vector addition\n",
|
||||
" if isinstance(other, Vector): # vector addition\n",
|
||||
" if len(self) != len(other):\n",
|
||||
" raise ValueError(\"vectors must be of the same length\")\n",
|
||||
" return Vector(x + y for (x, y) in zip(self, other))\n",
|
||||
|
|
@ -704,7 +704,7 @@
|
|||
" return self + other\n",
|
||||
"\n",
|
||||
" def __sub__(self, other):\n",
|
||||
" if isinstance(other, self.__class__): # vector subtraction\n",
|
||||
" if isinstance(other, Vector): # vector subtraction\n",
|
||||
" if len(self) != len(other):\n",
|
||||
" raise ValueError(\"vectors must be of the same length\")\n",
|
||||
" return Vector(x - y for (x, y) in zip(self, other))\n",
|
||||
|
|
@ -719,7 +719,7 @@
|
|||
" return NotImplemented\n",
|
||||
"\n",
|
||||
" def __mul__(self, other):\n",
|
||||
" if isinstance(other, self.__class__): # dot product\n",
|
||||
" if isinstance(other, Vector): # dot product\n",
|
||||
" if len(self) != len(other):\n",
|
||||
" raise ValueError(\"vectors must be of the same length\")\n",
|
||||
" return sum(x * y for (x, y) in zip(self, other))\n",
|
||||
|
|
@ -1142,7 +1142,7 @@
|
|||
"class Matrix:\n",
|
||||
"\n",
|
||||
" def __init__(self, data):\n",
|
||||
" self._entries = list(list(float(x) for x in r) for r in data)\n",
|
||||
" self._entries = tuple(tuple(float(x) for x in r) for r in data)\n",
|
||||
" # ...\n",
|
||||
"\n",
|
||||
" def __repr__(self):\n",
|
||||
|
|
@ -1169,7 +1169,7 @@
|
|||
" return (self._entries[r][c] for r in range(self.n_rows) for c in range(self.n_cols))\n",
|
||||
"\n",
|
||||
" def __add__(self, other):\n",
|
||||
" if isinstance(other, self.__class__): # matrix addition\n",
|
||||
" if isinstance(other, Matrix): # matrix addition\n",
|
||||
" if (self.n_rows != other.n_rows) or (self.n_cols != other.n_cols):\n",
|
||||
" raise ValueError(\"matrices must have the same dimensions\")\n",
|
||||
" return Matrix((s_col + o_col for (s_col, o_col) in zip(s_row, o_row))\n",
|
||||
|
|
@ -1184,7 +1184,7 @@
|
|||
" return self + other\n",
|
||||
"\n",
|
||||
" def __sub__(self, other):\n",
|
||||
" if isinstance(other, self.__class__): # matrix subtraction\n",
|
||||
" if isinstance(other, Matrix): # matrix subtraction\n",
|
||||
" if (self.n_rows != other.n_rows) or (self.n_cols != other.n_cols):\n",
|
||||
" raise ValueError(\"matrices must have the same dimensions\")\n",
|
||||
" return Matrix((s_col - o_col for (s_col, o_col) in zip(s_row, o_row))\n",
|
||||
|
|
@ -1211,7 +1211,7 @@
|
|||
" return Matrix((x * other for x in r) for r in self._entries)\n",
|
||||
" elif isinstance(other, Vector):\n",
|
||||
" return self._matrix_multiply(other.as_matrix()).as_vector()\n",
|
||||
" elif isinstance(other, self.__class__):\n",
|
||||
" elif isinstance(other, Matrix):\n",
|
||||
" return self._matrix_multiply(other)\n",
|
||||
" return NotImplemented\n",
|
||||
"\n",
|
||||
|
|
@ -1720,7 +1720,7 @@
|
|||
" zero_threshold = 1e-12\n",
|
||||
"\n",
|
||||
" def __init__(self, data):\n",
|
||||
" self._entries = list(float(x) for x in data)\n",
|
||||
" self._entries = tuple(float(x) for x in data)\n",
|
||||
" # ...\n",
|
||||
"\n",
|
||||
" def __repr__(self):\n",
|
||||
|
|
@ -1734,7 +1734,7 @@
|
|||
" return iter(self._entries)\n",
|
||||
"\n",
|
||||
" def __eq__(self, other):\n",
|
||||
" if isinstance(other, self.__class__):\n",
|
||||
" if isinstance(other, Vector):\n",
|
||||
" if len(self) != len(other):\n",
|
||||
" raise ValueError(\"vectors must be of the same length\")\n",
|
||||
" for x, y in zip(self, other):\n",
|
||||
|
|
@ -1990,7 +1990,7 @@
|
|||
"class Vector:\n",
|
||||
"\n",
|
||||
" def __init__(self, data):\n",
|
||||
" self._entries = list(float(x) for x in data)\n",
|
||||
" self._entries = tuple(float(x) for x in data)\n",
|
||||
" # ...\n",
|
||||
"\n",
|
||||
" def __repr__(self):\n",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue