Created
October 29, 2018 13:41
-
-
Save jul/e4b6f91a943e963faa4f5b692dd8765a to your computer and use it in GitHub Desktop.
Why Point2D is useless when dict supports LinearAlgebrae
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from archery import mdict, vdict | |
from math import pi, cos, sin, acos | |
class Matrix(mdict): | |
def __call__(self, other): | |
res= mdict() | |
for (src, dst), functor in self.items(): | |
res += mdict({ dst: functor(other[src])}) | |
return res | |
theta = pi/6 | |
u = mdict(x=1, y=2) | |
v = vdict(x=1, y=0) | |
def rotation_maker(theta): | |
""""Matrix takes as key (SRC, DST) (which is the opposite of "actual notation") | |
""" | |
return Matrix({ | |
("x", "x") : lambda x:1.0 * x * cos(theta), | |
("y", "x") : lambda y:1.0 * -y * sin(theta), | |
("x", "y") : lambda x:1.0 * x * sin(theta), | |
("y", "y") : lambda y:1.0 * y * cos(theta) | |
}) | |
rotation = rotation_maker(pi/6) | |
print(u) | |
# OUT:{'x': 1, 'y': 2} | |
print(rotation(u)) | |
# OUT:{'x': -0.13397459621556118, 'y': 2.232050807568877} | |
print("*" * 80) | |
# OUT:******************************************************************************** | |
print(v) | |
# OUT:{'x': 1, 'y': 0} | |
print(rotation(v)) | |
# OUT:{'x': 0.8660254037844387, 'y': 0.49999999999999994} | |
print(acos(v.cos(vdict(rotation(v))))/2 / pi * 360) | |
# OUT:29.999999999999993 | |
print(acos(v.cos(vdict(rotation_maker(pi/3)(v))))/2 / pi * 360) | |
# OUT:60.0 | |
print(acos(v.cos(vdict(rotation_maker(pi/5)(v))))/2 / pi * 360) | |
# OUT:36.0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment