Show only the necessary decimals

This commit is contained in:
Alexander Hess 2020-10-28 16:57:48 +01:00
parent 85b7fd274c
commit ca9df9e440
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
6 changed files with 152 additions and 150 deletions

View file

@ -1083,13 +1083,13 @@
" # ...\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(f\"{x:.3f}\" for x in self._entries)\n",
" args = \", \".join(repr(x) for x in self._entries)\n",
" return f\"Vector(({args}))\"\n",
"\n",
" def __str__(self):\n",
" first, last = self._entries[0], self._entries[-1]\n",
" n_entries = len(self._entries)\n",
" return f\"Vector({first:.1f}, ..., {last:.1f})[{n_entries:d}]\""
" return f\"Vector({first!r}, ..., {last!r})[{n_entries:d}]\""
]
},
{
@ -1132,7 +1132,7 @@
{
"data": {
"text/plain": [
"Vector((1.000, 2.000, 3.000))"
"Vector((1.0, 2.0, 3.0))"
]
},
"execution_count": 31,
@ -1167,7 +1167,7 @@
{
"data": {
"text/plain": [
"Vector((1.000, 2.000, 3.000))"
"Vector((1.0, 2.0, 3.0))"
]
},
"execution_count": 32,
@ -1176,7 +1176,7 @@
}
],
"source": [
"Vector((1.000, 2.000, 3.000))"
"Vector((1.0, 2.0, 3.0))"
]
},
{
@ -1202,7 +1202,7 @@
{
"data": {
"text/plain": [
"'Vector((1.000, 2.000, 3.000))'"
"'Vector((1.0, 2.0, 3.0))'"
]
},
"execution_count": 33,
@ -1334,13 +1334,13 @@
" raise ValueError(\"a matrix must have at least one entry\")\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(\"(\" + \", \".join(f\"{c:.3f}\" for c in r) + \",)\" for r in self._entries)\n",
" args = \", \".join(\"(\" + \", \".join(repr(c) for c in r) + \",)\" for r in self._entries)\n",
" return f\"Matrix(({args}))\"\n",
"\n",
" def __str__(self):\n",
" first, last = self._entries[0][0], self._entries[-1][-1]\n",
" m, n = len(self._entries), len(self._entries[0])\n",
" return f\"Matrix(({first:.1f}, ...), ..., (..., {last:.1f}))[{m:d}x{n:d}]\""
" return f\"Matrix(({first!r}, ...), ..., (..., {last!r}))[{m:d}x{n:d}]\""
]
},
{
@ -1521,7 +1521,7 @@
{
"data": {
"text/plain": [
"Matrix(((1.000, 2.000, 3.000,), (4.000, 5.000, 6.000,), (7.000, 8.000, 9.000,)))"
"Matrix(((1.0, 2.0, 3.0,), (4.0, 5.0, 6.0,), (7.0, 8.0, 9.0,)))"
]
},
"execution_count": 43,
@ -1660,7 +1660,7 @@
" # ...\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(\"(\" + \", \".join(f\"{c:.3f}\" for c in r) + \",)\" for r in self._entries)\n",
" args = \", \".join(\"(\" + \", \".join(repr(c) for c in r) + \",)\" for r in self._entries)\n",
" return f\"Matrix(({args}))\"\n",
"\n",
" def transpose(self):\n",
@ -1703,7 +1703,7 @@
{
"data": {
"text/plain": [
"Matrix(((1.000, 2.000, 3.000,), (4.000, 5.000, 6.000,), (7.000, 8.000, 9.000,)))"
"Matrix(((1.0, 2.0, 3.0,), (4.0, 5.0, 6.0,), (7.0, 8.0, 9.0,)))"
]
},
"execution_count": 49,
@ -1727,7 +1727,7 @@
{
"data": {
"text/plain": [
"Matrix(((1.000, 4.000, 7.000,), (2.000, 5.000, 8.000,), (3.000, 6.000, 9.000,)))"
"Matrix(((1.0, 4.0, 7.0,), (2.0, 5.0, 8.0,), (3.0, 6.0, 9.0,)))"
]
},
"execution_count": 50,
@ -1775,7 +1775,7 @@
{
"data": {
"text/plain": [
"Matrix(((1.000, 2.000, 3.000,), (4.000, 5.000, 6.000,), (7.000, 8.000, 9.000,)))"
"Matrix(((1.0, 2.0, 3.0,), (4.0, 5.0, 6.0,), (7.0, 8.0, 9.0,)))"
]
},
"execution_count": 52,
@ -1876,7 +1876,7 @@
" # ...\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(\"(\" + \", \".join(f\"{c:.3f}\" for c in r) + \",)\" for r in self._entries)\n",
" args = \", \".join(\"(\" + \", \".join(repr(c) for c in r) + \",)\" for r in self._entries)\n",
" return f\"Matrix(({args}))\"\n",
"\n",
" def transpose(self):\n",
@ -1923,7 +1923,7 @@
{
"data": {
"text/plain": [
"Matrix(((1.000, 2.000, 3.000,), (4.000, 5.000, 6.000,), (7.000, 8.000, 9.000,)))"
"Matrix(((1.0, 2.0, 3.0,), (4.0, 5.0, 6.0,), (7.0, 8.0, 9.0,)))"
]
},
"execution_count": 57,
@ -1993,7 +1993,7 @@
" raise ValueError(\"a matrix must have at least one entry\")\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(\"(\" + \", \".join(f\"{c:.3f}\" for c in r) + \",)\" for r in self._entries)\n",
" args = \", \".join(\"(\" + \", \".join(repr(c) for c in r) + \",)\" for r in self._entries)\n",
" return f\"Matrix(({args}))\"\n",
"\n",
" @property\n",

View file

@ -77,7 +77,7 @@
" raise ValueError(\"a vector must have at least one entry\")\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(f\"{x:.3f}\" for x in self._entries)\n",
" args = \", \".join(repr(x) for x in self._entries)\n",
" return f\"Vector(({args}))\"\n",
"\n",
" def __len__(self):\n",
@ -762,7 +762,7 @@
" # ...\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(\"(\" + \", \".join(f\"{c:.3f}\" for c in r) + \",)\" for r in self._entries)\n",
" args = \", \".join(\"(\" + \", \".join(repr(c) for c in r) + \",)\" for r in self._entries)\n",
" return f\"Matrix(({args}))\"\n",
"\n",
" @property\n",
@ -825,7 +825,7 @@
" # ...\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(f\"{x:.3f}\" for x in self)\n",
" args = \", \".join(repr(x) for x in self)\n",
" return f\"Vector(({args}))\"\n",
"\n",
" def __iter__(self):\n",
@ -860,7 +860,7 @@
{
"data": {
"text/plain": [
"Matrix(((1.000, 2.000, 3.000,), (4.000, 5.000, 6.000,), (7.000, 8.000, 9.000,)))"
"Matrix(((1.0, 2.0, 3.0,), (4.0, 5.0, 6.0,), (7.0, 8.0, 9.0,)))"
]
},
"execution_count": 25,
@ -951,7 +951,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Vector((1.000, 2.000, 3.000)) Vector((4.000, 5.000, 6.000)) Vector((7.000, 8.000, 9.000)) "
"Vector((1.0, 2.0, 3.0)) Vector((4.0, 5.0, 6.0)) Vector((7.0, 8.0, 9.0)) "
]
}
],
@ -973,7 +973,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Vector((1.000, 4.000, 7.000)) Vector((2.000, 5.000, 8.000)) Vector((3.000, 6.000, 9.000)) "
"Vector((1.0, 4.0, 7.0)) Vector((2.0, 5.0, 8.0)) Vector((3.0, 6.0, 9.0)) "
]
}
],
@ -1069,7 +1069,7 @@
" # ...\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(\"(\" + \", \".join(f\"{c:.3f}\" for c in r) + \",)\" for r in self._entries)\n",
" args = \", \".join(\"(\" + \", \".join(repr(c) for c in r) + \",)\" for r in self._entries)\n",
" return f\"Matrix(({args}))\"\n",
"\n",
" def transpose(self):\n",
@ -1102,7 +1102,7 @@
{
"data": {
"text/plain": [
"Matrix(((1.000, 2.000, 3.000,), (4.000, 5.000, 6.000,), (7.000, 8.000, 9.000,)))"
"Matrix(((1.0, 2.0, 3.0,), (4.0, 5.0, 6.0,), (7.0, 8.0, 9.0,)))"
]
},
"execution_count": 34,
@ -1161,7 +1161,7 @@
{
"data": {
"text/plain": [
"Matrix(((1.000, 4.000, 7.000,), (2.000, 5.000, 8.000,), (3.000, 6.000, 9.000,)))"
"Matrix(((1.0, 4.0, 7.0,), (2.0, 5.0, 8.0,), (3.0, 6.0, 9.0,)))"
]
},
"execution_count": 36,
@ -1248,7 +1248,7 @@
" # ...\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(\"(\" + \", \".join(f\"{c:.3f}\" for c in r) + \",)\" for r in self._entries)\n",
" args = \", \".join(\"(\" + \", \".join(repr(c) for c in r) + \",)\" for r in self._entries)\n",
" return f\"Matrix(({args}))\"\n",
"\n",
" def __iter__(self): # adapted for brevity; uses parts of entries()\n",
@ -1285,7 +1285,7 @@
{
"data": {
"text/plain": [
"Matrix(((1.000, 2.000, 3.000,), (4.000, 5.000, 6.000,), (7.000, 8.000, 9.000,)))"
"Matrix(((1.0, 2.0, 3.0,), (4.0, 5.0, 6.0,), (7.0, 8.0, 9.0,)))"
]
},
"execution_count": 40,
@ -1333,7 +1333,7 @@
{
"data": {
"text/plain": [
"Matrix(((1.000, 4.000, 7.000,), (2.000, 5.000, 8.000,), (3.000, 6.000, 9.000,)))"
"Matrix(((1.0, 4.0, 7.0,), (2.0, 5.0, 8.0,), (3.0, 6.0, 9.0,)))"
]
},
"execution_count": 42,
@ -1357,7 +1357,7 @@
{
"data": {
"text/plain": [
"Matrix(((1.000, 4.000, 7.000,), (2.000, 5.000, 8.000,), (3.000, 6.000, 9.000,)))"
"Matrix(((1.0, 4.0, 7.0,), (2.0, 5.0, 8.0,), (3.0, 6.0, 9.0,)))"
]
},
"execution_count": 43,
@ -1434,7 +1434,7 @@
" # ...\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(f\"{x:.3f}\" for x in self)\n",
" args = \", \".join(repr(x) for x in self)\n",
" return f\"Vector(({args}))\"\n",
"\n",
" def __getitem__(self, index):\n",
@ -1472,7 +1472,7 @@
{
"data": {
"text/plain": [
"Vector((1.000, 2.000, 3.000))"
"Vector((1.0, 2.0, 3.0))"
]
},
"execution_count": 47,
@ -1520,7 +1520,7 @@
{
"data": {
"text/plain": [
"Vector((nan, 2.000, 3.000))"
"Vector((nan, 2.0, 3.0))"
]
},
"execution_count": 49,
@ -1557,7 +1557,7 @@
{
"data": {
"text/plain": [
"Vector((99.000, 2.000, 3.000))"
"Vector((99.0, 2.0, 3.0))"
]
},
"execution_count": 51,

View file

@ -86,7 +86,7 @@
" # ...\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(f\"{x:.3f}\" for x in self)\n",
" args = \", \".join(repr(x) for x in self)\n",
" return f\"Vector(({args}))\"\n",
"\n",
" def __iter__(self):\n",
@ -115,7 +115,7 @@
" # ...\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(\"(\" + \", \".join(f\"{c:.3f}\" for c in r) + \",)\" for r in self._entries)\n",
" args = \", \".join(\"(\" + \", \".join(repr(c) for c in r) + \",)\" for r in self._entries)\n",
" return f\"Matrix(({args}))\"\n",
"\n",
" @property\n",
@ -160,7 +160,7 @@
{
"data": {
"text/plain": [
"Vector((1.000, 2.000, 3.000))"
"Vector((1.0, 2.0, 3.0))"
]
},
"execution_count": 4,
@ -208,7 +208,7 @@
{
"data": {
"text/plain": [
"Matrix(((1.000,), (2.000,), (3.000,)))"
"Matrix(((1.0,), (2.0,), (3.0,)))"
]
},
"execution_count": 6,
@ -267,7 +267,7 @@
{
"data": {
"text/plain": [
"Vector((1.000, 2.000, 3.000))"
"Vector((1.0, 2.0, 3.0))"
]
},
"execution_count": 8,
@ -315,7 +315,7 @@
{
"data": {
"text/plain": [
"Matrix(((1.000, 2.000, 3.000,)))"
"Matrix(((1.0, 2.0, 3.0,)))"
]
},
"execution_count": 10,
@ -363,7 +363,7 @@
{
"data": {
"text/plain": [
"Vector((1.000, 2.000, 3.000))"
"Vector((1.0, 2.0, 3.0))"
]
},
"execution_count": 12,
@ -682,7 +682,7 @@
" # ...\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(f\"{x:.3f}\" for x in self)\n",
" args = \", \".join(repr(x) for x in self)\n",
" return f\"Vector(({args}))\"\n",
"\n",
" def __len__(self):\n",
@ -790,7 +790,7 @@
{
"data": {
"text/plain": [
"Vector((2.000, 4.000, 6.000))"
"Vector((2.0, 4.0, 6.0))"
]
},
"execution_count": 24,
@ -814,7 +814,7 @@
{
"data": {
"text/plain": [
"Vector((3.000, 6.000, 9.000))"
"Vector((3.0, 6.0, 9.0))"
]
},
"execution_count": 25,
@ -910,7 +910,7 @@
{
"data": {
"text/plain": [
"Vector((0.333, 0.667, 1.000))"
"Vector((0.3333333333333333, 0.6666666666666666, 1.0))"
]
},
"execution_count": 28,
@ -945,7 +945,7 @@
{
"data": {
"text/plain": [
"Vector((2.000, 4.000, 6.000))"
"Vector((2.0, 4.0, 6.0))"
]
},
"execution_count": 29,
@ -969,7 +969,7 @@
{
"data": {
"text/plain": [
"Vector((0.000, 0.000, 0.000))"
"Vector((0.0, 0.0, 0.0))"
]
},
"execution_count": 30,
@ -1019,7 +1019,7 @@
{
"data": {
"text/plain": [
"Vector((101.000, 102.000, 103.000))"
"Vector((101.0, 102.0, 103.0))"
]
},
"execution_count": 32,
@ -1043,7 +1043,7 @@
{
"data": {
"text/plain": [
"Vector((99.000, 98.000, 97.000))"
"Vector((99.0, 98.0, 97.0))"
]
},
"execution_count": 33,
@ -1146,7 +1146,7 @@
" # ...\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(\"(\" + \", \".join(f\"{c:.3f}\" for c in r) + \",)\" for r in self._entries)\n",
" args = \", \".join(\"(\" + \", \".join(repr(c) for c in r) + \",)\" for r in self._entries)\n",
" return f\"Matrix(({args}))\"\n",
"\n",
" @property\n",
@ -1270,7 +1270,7 @@
{
"data": {
"text/plain": [
"Matrix(((10.000, 20.000, 30.000,), (40.000, 50.000, 60.000,), (70.000, 80.000, 90.000,)))"
"Matrix(((10.0, 20.0, 30.0,), (40.0, 50.0, 60.0,), (70.0, 80.0, 90.0,)))"
]
},
"execution_count": 37,
@ -1294,7 +1294,7 @@
{
"data": {
"text/plain": [
"Matrix(((1.000, 2.000, 3.000,), (4.000, 5.000, 6.000,), (7.000, 8.000, 9.000,)))"
"Matrix(((1.0, 2.0, 3.0,), (4.0, 5.0, 6.0,), (7.0, 8.0, 9.0,)))"
]
},
"execution_count": 38,
@ -1318,7 +1318,7 @@
{
"data": {
"text/plain": [
"Matrix(((0.000, 0.000, 0.000,), (0.000, 0.000, 0.000,), (0.000, 0.000, 0.000,)))"
"Matrix(((0.0, 0.0, 0.0,), (0.0, 0.0, 0.0,), (0.0, 0.0, 0.0,)))"
]
},
"execution_count": 39,
@ -1353,7 +1353,7 @@
{
"data": {
"text/plain": [
"Matrix(((138.000, 171.000, 204.000,), (174.000, 216.000, 258.000,)))"
"Matrix(((138.0, 171.0, 204.0,), (174.0, 216.0, 258.0,)))"
]
},
"execution_count": 40,
@ -1426,7 +1426,7 @@
{
"data": {
"text/plain": [
"Vector((14.000, 32.000, 50.000))"
"Vector((14.0, 32.0, 50.0))"
]
},
"execution_count": 42,
@ -1450,7 +1450,7 @@
{
"data": {
"text/plain": [
"Vector((30.000, 36.000, 42.000))"
"Vector((30.0, 36.0, 42.0))"
]
},
"execution_count": 43,
@ -1474,7 +1474,7 @@
{
"data": {
"text/plain": [
"Vector((68.000, 86.000))"
"Vector((68.0, 86.0))"
]
},
"execution_count": 44,
@ -1536,7 +1536,7 @@
{
"data": {
"text/plain": [
"Matrix(((101.000, 102.000, 103.000,), (104.000, 105.000, 106.000,), (107.000, 108.000, 109.000,)))"
"Matrix(((101.0, 102.0, 103.0,), (104.0, 105.0, 106.0,), (107.0, 108.0, 109.0,)))"
]
},
"execution_count": 46,
@ -1560,7 +1560,7 @@
{
"data": {
"text/plain": [
"Matrix(((99.000, 98.000, 97.000,), (96.000, 95.000, 94.000,), (93.000, 92.000, 91.000,)))"
"Matrix(((99.0, 98.0, 97.0,), (96.0, 95.0, 94.0,), (93.0, 92.0, 91.0,)))"
]
},
"execution_count": 47,
@ -1724,7 +1724,7 @@
" # ...\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(f\"{x:.3f}\" for x in self)\n",
" args = \", \".join(repr(x) for x in self)\n",
" return f\"Vector(({args}))\"\n",
"\n",
" def __len__(self):\n",
@ -1881,7 +1881,7 @@
{
"data": {
"text/plain": [
"Vector((1.000, 2.000, 3.000))"
"Vector((1.0, 2.0, 3.0))"
]
},
"execution_count": 58,
@ -1994,7 +1994,7 @@
" # ...\n",
"\n",
" def __repr__(self):\n",
" args = \", \".join(f\"{x:.3f}\" for x in self)\n",
" args = \", \".join(repr(x) for x in self)\n",
" return f\"Vector(({args}))\"\n",
"\n",
" def __iter__(self):\n",
@ -2058,7 +2058,7 @@
{
"data": {
"text/plain": [
"Vector((1.000, 2.000, 3.000))"
"Vector((1.0, 2.0, 3.0))"
]
},
"execution_count": 65,
@ -2106,7 +2106,7 @@
{
"data": {
"text/plain": [
"Vector((-3.000, -4.000))"
"Vector((-3.0, -4.0))"
]
},
"execution_count": 67,

File diff suppressed because one or more lines are too long

View file

@ -43,7 +43,7 @@ class Matrix:
Example Usage:
>>> Matrix([(1, 2), (3, 4)])
Matrix(((1.000, 2.000,), (3.000, 4.000,)))
Matrix(((1.0, 2.0,), (3.0, 4.0,)))
"""
self._entries = self.storage(
self.storage(self.typing(x) for x in r) for r in data
@ -73,7 +73,7 @@ class Matrix:
Example Usage:
>>> Matrix.from_columns([(1, 2), (3, 4)])
Matrix(((1.000, 3.000,), (2.000, 4.000,)))
Matrix(((1.0, 3.0,), (2.0, 4.0,)))
"""
return cls(data).transpose()
@ -89,7 +89,7 @@ class Matrix:
"""Text representation of a Matrix."""
name = self.__class__.__name__
args = ", ".join(
"(" + ", ".join(f"{c:.3f}" for c in r) + ",)" for r in self._entries
"(" + ", ".join(repr(c) for c in r) + ",)" for r in self._entries
)
return f"{name}(({args}))"
@ -97,7 +97,7 @@ class Matrix:
"""Human-readable text representation of a Matrix."""
name = self.__class__.__name__
first, last, m, n = self[0], self[-1], self.n_rows, self.n_cols
return f"{name}(({first:.1f}, ...), ..., (..., {last:.1f}))[{m:d}x{n:d}]"
return f"{name}(({first!r}, ...), ..., (..., {last!r}))[{m:d}x{n:d}]"
@property
def n_rows(self):
@ -211,13 +211,13 @@ class Matrix:
Example Usage:
>>> Matrix([(1, 2), (3, 4)]) + Matrix([(2, 3), (4, 5)])
Matrix(((3.000, 5.000,), (7.000, 9.000,)))
Matrix(((3.0, 5.0,), (7.0, 9.0,)))
>>> Matrix([(1, 2), (3, 4)]) + 5
Matrix(((6.000, 7.000,), (8.000, 9.000,)))
Matrix(((6.0, 7.0,), (8.0, 9.0,)))
>>> 10 + Matrix([(1, 2), (3, 4)])
Matrix(((11.000, 12.000,), (13.000, 14.000,)))
Matrix(((11.0, 12.0,), (13.0, 14.0,)))
"""
# Matrix addition
if isinstance(other, self.__class__):
@ -247,13 +247,13 @@ class Matrix:
Example Usage:
>>> Matrix([(2, 3), (4, 5)]) - Matrix([(1, 2), (3, 4)])
Matrix(((1.000, 1.000,), (1.000, 1.000,)))
Matrix(((1.0, 1.0,), (1.0, 1.0,)))
>>> Matrix([(1, 2), (3, 4)]) - 1
Matrix(((0.000, 1.000,), (2.000, 3.000,)))
Matrix(((0.0, 1.0,), (2.0, 3.0,)))
>>> 10 - Matrix([(1, 2), (3, 4)])
Matrix(((9.000, 8.000,), (7.000, 6.000,)))
Matrix(((9.0, 8.0,), (7.0, 6.0,)))
"""
# As subtraction is the inverse of addition,
# we first dispatch to .__neg__() to invert the signs of
@ -286,23 +286,23 @@ class Matrix:
Example Usage:
>>> Matrix([(1, 2), (3, 4)]) * Matrix([(1, 2), (3, 4)])
Matrix(((7.000, 10.000,), (15.000, 22.000,)))
Matrix(((7.0, 10.0,), (15.0, 22.0,)))
>>> 2 * Matrix([(1, 2), (3, 4)])
Matrix(((2.000, 4.000,), (6.000, 8.000,)))
Matrix(((2.0, 4.0,), (6.0, 8.0,)))
>>> Matrix([(1, 2), (3, 4)]) * 3
Matrix(((3.000, 6.000,), (9.000, 12.000,)))
Matrix(((3.0, 6.0,), (9.0, 12.0,)))
Matrix-vector and vector-matrix multiplication are not commutative.
>>> from sample_package import Vector
>>> Matrix([(1, 2), (3, 4)]) * Vector([5, 6])
Vector((17.000, 39.000))
Vector((17.0, 39.0))
>>> Vector([5, 6]) * Matrix([(1, 2), (3, 4)])
Vector((23.000, 34.000))
Vector((23.0, 34.0))
"""
# Scalar multiplication
if isinstance(other, numbers.Number):
@ -334,7 +334,7 @@ class Matrix:
Example Usage:
>>> Matrix([(1, 2), (3, 4)]) / 4
Matrix(((0.250, 0.500,), (0.750, 1.000,)))
Matrix(((0.25, 0.5,), (0.75, 1.0,)))
"""
# As scalar division division is the same as multiplication
# with the inverse, we dispatch to .__mul__().
@ -409,7 +409,7 @@ class Matrix:
Example Usage:
>>> Matrix([(1, 2, 3)]).as_vector()
Vector((1.000, 2.000, 3.000))
Vector((1.0, 2.0, 3.0))
"""
if not (self.n_rows == 1 or self.n_cols == 1):
raise RuntimeError("one dimension (m or n) must be 1")
@ -424,9 +424,9 @@ class Matrix:
Example Usage:
>>> m = Matrix([(1, 2), (3, 4)])
>>> m
Matrix(((1.000, 2.000,), (3.000, 4.000,)))
Matrix(((1.0, 2.0,), (3.0, 4.0,)))
>>> m.transpose()
Matrix(((1.000, 3.000,), (2.000, 4.000,)))
Matrix(((1.0, 3.0,), (2.0, 4.0,)))
"""
return self.__class__(zip(*self._entries))

View file

@ -42,10 +42,10 @@ class Vector:
Example Usage:
>>> Vector([1, 2, 3])
Vector((1.000, 2.000, 3.000))
Vector((1.0, 2.0, 3.0))
>>> Vector(range(3))
Vector((0.000, 1.000, 2.000))
Vector((0.0, 1.0, 2.0))
"""
self._entries = self.storage(self.typing(x) for x in data)
if len(self) == 0:
@ -54,14 +54,14 @@ class Vector:
def __repr__(self):
"""Text representation of a Vector."""
name = self.__class__.__name__
args = ", ".join(f"{x:.3f}" for x in self)
args = ", ".join(repr(x) for x in self)
return f"{name}(({args}))"
def __str__(self):
"""Human-readable text representation of a Vector."""
name = self.__class__.__name__
first, last, n_entries = self[0], self[-1], len(self)
return f"{name}({first:.1f}, ..., {last:.1f})[{n_entries:d}]"
return f"{name}({first!r}, ..., {last!r})[{n_entries:d}]"
def __len__(self):
"""Number of entries in a Vector."""
@ -88,13 +88,13 @@ class Vector:
Example Usage:
>>> Vector([1, 2, 3]) + Vector([2, 3, 4])
Vector((3.000, 5.000, 7.000))
Vector((3.0, 5.0, 7.0))
>>> Vector([1, 2, 3]) + 4
Vector((5.000, 6.000, 7.000))
Vector((5.0, 6.0, 7.0))
>>> 10 + Vector([1, 2, 3])
Vector((11.000, 12.000, 13.000))
Vector((11.0, 12.0, 13.0))
"""
# Vector addition
if isinstance(other, self.__class__):
@ -119,13 +119,13 @@ class Vector:
Example Usage:
>>> Vector([7, 8, 9]) - Vector([1, 2, 3])
Vector((6.000, 6.000, 6.000))
Vector((6.0, 6.0, 6.0))
>>> Vector([1, 2, 3]) - 1
Vector((0.000, 1.000, 2.000))
Vector((0.0, 1.0, 2.0))
>>> 10 - Vector([1, 2, 3])
Vector((9.000, 8.000, 7.000))
Vector((9.0, 8.0, 7.0))
"""
# As subtraction is the inverse of addition,
# we first dispatch to .__neg__() to invert the signs of
@ -148,10 +148,10 @@ class Vector:
20.0
>>> 2 * Vector([1, 2, 3])
Vector((2.000, 4.000, 6.000))
Vector((2.0, 4.0, 6.0))
>>> Vector([1, 2, 3]) * 3
Vector((3.000, 6.000, 9.000))
Vector((3.0, 6.0, 9.0))
"""
# Dot product
if isinstance(other, self.__class__):
@ -176,7 +176,7 @@ class Vector:
Example Usage:
>>> Vector([9, 6, 12]) / 3
Vector((3.000, 2.000, 4.000))
Vector((3.0, 2.0, 4.0))
"""
# As scalar division division is the same as multiplication
# with the inverse, we dispatch to .__mul__().
@ -253,9 +253,9 @@ class Vector:
Example Usage:
>>> v = Vector([1, 2, 3])
>>> v.as_matrix()
Matrix(((1.000,), (2.000,), (3.000,)))
Matrix(((1.0,), (2.0,), (3.0,)))
>>> v.as_matrix(column=False)
Matrix(((1.000, 2.000, 3.000,)))
Matrix(((1.0, 2.0, 3.0,)))
"""
if column:
return self.matrix_cls([x] for x in self)