def test_normalized(x, y): """We can normalize a vector.""" vec = v(x, y) if vec.is_zero(): return r, theta = vec.to_polar() assert vec.normalized().to_polar() == approx((1.0, theta))
def timeit(name, stmt): defs = dict( v=v, a=v(1.0, 2.0), b=v(-1.0, -1.0), ) loops, time = Timer( setup="c = v(0, 0)", stmt=stmt, globals={ **globals(), **defs }, ).autorange() dur = time / loops print(f"{name}: {dur * 1e6:0.2f}us per op ({loops} samples)")
def test_length(x, y): """We can get the length of a vector.""" vec = v(x, y) assert vec.length() == approx(hypot(x, y))
def test_iter(): """We can unpack a vector.""" x, y = v(1, 2) assert x, y == (1, 2)
def test_len(): """The length of a vector is 2.""" assert len(v(0, 0)) == 2
def test_add(ax, ay, bx, by): """We can add two vectors.""" if ax + bx in (inf, -inf) or ay + by in (inf, -inf): return assert v(ax, ay) + v(bx, by) == v(ax + bx, ay + by)
def test_repr(x, y): """Construct a vec and display its repr.""" a = v(x, y) assert eval(repr(a)) == a
def test_getattr(x, y): """We can get the x and y attributes.""" a = v(x, y) assert a.x == x assert a.y == y