Show only the necessary decimals

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

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)