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

@ -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)