Geometric algebra: Difference between revisions

Line 1,219:
my $v = [+] @coeff Z* @e[^5];
ok ($v**2).narrow ~~ Real, 'contraction';</lang>
 
=={{header|Python}}==
{{trans|D}}
<lang python>import copy, random
 
def bitcount(n):
return bin(n).count("1")
 
def reoderingSign(i, j):
k = i >> 1
sum = 0
while k != 0:
sum += bitcount(k & j)
k = k >> 1
return 1.0 if ((sum & 1) == 0) else -1.0
 
class Vector:
def __init__(self, da):
self.dims = da
 
def dot(self, other):
return (self * other + other * self) * 0.5
 
def __getitem__(self, i):
return self.dims[i]
 
def __setitem__(self, i, v):
self.dims[i] = v
 
def __neg__(self):
return self * -1.0
 
def __add__(self, other):
result = copy.copy(other.dims)
for i in xrange(0, len(self.dims)):
result[i] += self.dims[i]
return Vector(result)
 
def __mul__(self, other):
if isinstance(other, Vector):
result = [0.0] * 32
for i in xrange(0, len(self.dims)):
if self.dims[i] != 0.0:
for j in xrange(0, len(self.dims)):
if other.dims[j] != 0.0:
s = reoderingSign(i, j) * self.dims[i] * other.dims[j]
k = i ^ j
result[k] += s
return Vector(result)
else:
result = copy.copy(self.dims)
for i in xrange(0, len(self.dims)):
self.dims[i] *= other
return Vector(result)
 
def __str__(self):
return str(self.dims)
 
def e(n):
assert n <= 4, "n must be less than 5"
result = Vector([0.0] * 32)
result[1 << n] = 1.0
return result
 
def randomVector():
result = Vector([0.0] * 32)
for i in xrange(0, 5):
result += Vector([random.uniform(0, 1)]) * e(i)
return result
 
def randomMultiVector():
result = Vector([0.0] * 32)
for i in xrange(0, 32):
result[i] = random.uniform(0, 1)
return result
 
def main():
for i in xrange(0, 5):
for j in xrange(0, 5):
if i < j:
if e(i).dot(e(j))[0] != 0.0:
print "Unexpected non-null scalar product"
return
elif i == j:
if e(i).dot(e(j))[0] == 0.0:
print "Unexpected non-null scalar product"
 
a = randomMultiVector()
b = randomMultiVector()
c = randomMultiVector()
x = randomVector()
 
# (ab)c == a(bc)
print (a * b) * c
print a * (b * c)
print
 
# a(b+c) == ab + ac
print a * (b + c)
print a * b + a * c
print
 
# (a+b)c == ac + bc
print (a + b) * c
print a * c + b * c
print
 
# x^2 is real
print x * x
 
main()</lang>
{{out}}
<pre>[2.646777769717816, -5.480686120935684, -8.183342078006843, -9.178717618666656, -0.21247781959240397, -3.1560121872423172, -14.210376795019405, -7.975576839132462, -2.963314079857538, -8.128489630952732, 8.84291288803876, 6.849688422048398, -3.948403894153645, -6.3295864734054295, 0.858339386946704, 0.04073276768257372, -0.8170168057484614, -5.987310468330181, -5.089567141509365, -6.5916164371098205, 1.066652018944462, -0.7553724661211869, -16.61957782752131, -10.74332838047719, -0.22326945346944393, -5.502857138805277, 11.833089760883906, 11.020055749901102, -3.7471254230186233, -3.5483496341413763, 7.788213699886802, 5.385261642366723]
[2.6467777697178145, -5.480686120935683, -8.183342078006845, -9.178717618666658, -0.2124778195924022, -3.156012187242318, -14.210376795019414, -7.975576839132467, -2.9633140798575406, -8.128489630952735, 8.842912888038763, 6.849688422048397, -3.9484038941536435, -6.329586473405431, 0.8583393869467044, 0.04073276768257594, -0.8170168057484637, -5.987310468330179, -5.089567141509367, -6.591616437109819, 1.0666520189444642, -0.7553724661211856, -16.61957782752132, -10.743328380477191, -0.22326945346944407, -5.502857138805281, 11.83308976088391, 11.0200557499011, -3.7471254230186246, -3.5483496341413807, 7.7882136998868035, 5.385261642366723]
 
[-4.45416862548425, -4.007728055936393, 2.2599167577088886, -0.5008511526260965, 6.404388731518947, 3.553799027487341, -1.4559344997025763, 1.200629741306491, 0.28084755190469957, 1.6881017142666677, 4.622735951152484, 2.042861306698215, -1.1859431529346458, -0.23268120656473645, 3.3088499800790308, 6.272881551311293, -6.283417063207868, -6.059129990387314, 0.9004412097752342, 0.540839567518711, 2.9772708785233157, 2.2875667777090625, 1.6049404915540564, 4.551625497411361, 3.4613544600410537, 4.601629280006443, 4.934029921827034, 3.0257810667323715, -1.280636387447562, -0.21306231994217983, 6.089428007073901, 8.128821330734313]
[-4.454168625484251, -4.007728055936392, 2.259916757708888, -0.5008511526260957, 6.404388731518948, 3.553799027487342, -1.455934499702577, 1.2006297413064915, 0.2808475519046991, 1.688101714266668, 4.622735951152485, 2.0428613066982155, -1.1859431529346458, -0.23268120656473568, 3.30884998007903, 6.272881551311293, -6.283417063207869, -6.059129990387315, 0.9004412097752316, 0.5408395675187101, 2.9772708785233157, 2.287566777709062, 1.6049404915540577, 4.551625497411363, 3.4613544600410533, 4.601629280006441, 4.934029921827035, 3.025781066732372, -1.2806363874475617, -0.21306231994217995, 6.0894280070738995, 8.128821330734311]
 
[-4.713407548689167, -5.470701972164317, 0.2834720300902142, -1.8843569665485043, 2.8883659013514302, 3.6276355158044815, -2.7614493177411843, 0.14095340884428206, -1.191066813989091, 1.1017933922740846, 2.995254519836379, 1.5249479602578073, 2.153333527417164, 1.999841187299821, 6.220565393668025, 8.730809912614522, -7.61200478654088, -9.557862449328784, 1.432009511995673, 0.14006605762543944, 1.450154388175902, 2.5115288790301835, -1.5609458922816675, 1.9148273860716452, 3.56683599400551, 3.9854109527025505, 3.6838872880534086, 3.059534508634617, 3.237048050921782, 2.674541802512928, 6.252577743980652, 7.309261452099341]
[-4.713407548689167, -5.470701972164316, 0.28347203009021416, -1.8843569665485043, 2.8883659013514302, 3.6276355158044815, -2.761449317741185, 0.14095340884428198, -1.1910668139890919, 1.101793392274085, 2.9952545198363785, 1.5249479602578067, 2.153333527417164, 1.9998411872998219, 6.220565393668025, 8.730809912614523, -7.612004786540879, -9.557862449328784, 1.4320095119956717, 0.14006605762543944, 1.4501543881759016, 2.511528879030183, -1.5609458922816672, 1.9148273860716456, 3.5668359940055105, 3.985410952702549, 3.683887288053409, 3.0595345086346164, 3.2370480509217803, 2.6745418025129273, 6.252577743980652, 7.30926145209934]
 
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]</pre>
1,452

edits