[Edu-sig] Quadrays again... cross-training in two langauges
Kirby Urner
kurner at oreillyschool.com
Thu Aug 6 19:35:36 CEST 2015
I've posted about Quadrays on edu-sig before.... going back and forth
between two languages may be a good way to learn both, or to learn one
using one's knowledge of the other. In this case, I'm developing the same
Quadray class in Python and Clojure.
I'll followup with a link to the Clojure version. I'm just beginning with
that language and I'm hoping the experts pick it apart some, as I'm sure
it's not as Pythonic as it could be. :-D
... actually three languages if you find the links to the C++ version.
Kirby
"""
(cl) K. Urner, MIT License 2015
Python -> Java -> Clojure curriculum
2D + 3D Graphics: Martian Math
Topic: Quadrays
http://www.grunch.net/synergetics/quadintro.html
https://en.wikipedia.org/wiki/Quadray_coordinates
Asynchronous Learning Engine (Open Source project)
http://controlroom.blogspot.com/2015/08/asynchronous-learning-engine-ale.html
"""
from math import sqrt
class Quadray:
def __init__(self, OA, OB, OC, OD):
self.OA = OA
self.OB = OB
self.OC = OC
self.OD = OD
self._norm()
def _norm(self):
"""
Quadrays have at least one element = 0 as one direction is inactive
per quadrant
"""
the_min = min(self.OA, self.OB, self.OC, self.OD)
self.OA = self.OA - the_min
self.OB = self.OB - the_min
self.OC = self.OC - the_min
self.OD = self.OD - the_min
def __neg__(self):
return Quadray(-self.OA, -self.OB, -self.OC, -self.OD)
def __add__(self, other):
return Quadray(self.OA + other.OA, self.OB + other.OB,
self.OC + other.OC, self.OD + other.OD)
def __sub__(self, other):
return self + (-other)
def len(self):
"""
uses Tom Ace normalization such that the sum of the elements is 0.
Simplifies the distance formula, which is set at Edge of Unit Tetra
= 2
i.e. D(1,0,0,0) == math.sqrt(6)/2
Tom Ace: http://www.minortriad.com/index.html
"""
k = sum([self.OA, self.OB, self.OC, self.OD]) / 4.0
t0, t1, t2, t3 = self.OA - k, self.OB - k, self.OC - k, self.OD - k
return sqrt(2) * sqrt(sum([t0**2, t1**2, t2**2, t3**2]))
def __repr__(self):
return 'Quadray(%s, %s, %s, %s)' % (self.OA, self.OB, self.OC,
self.OD)
v0 = Quadray(1,0,0,0)
v1 = Quadray(0,1,0,0)
print(v0 - v1)
print(v0 + v1)
print(-v1)
print(v1.len()) # math.sqrt(6)/2
print(-v1.len() + v1.len())
print((v1-v1).len())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20150806/48252e19/attachment.html>
More information about the Edu-sig
mailing list