Geometric algebra: Difference between revisions
Content added Content deleted
(julia example) |
|||
Line 1,329: | Line 1,329: | ||
<pre>prompt$ jsish -u geometricAlgebra.jsi |
<pre>prompt$ jsish -u geometricAlgebra.jsi |
||
[PASS] geometricAlgebra.jsi</pre> |
[PASS] geometricAlgebra.jsi</pre> |
||
=={{header|Julia}}== |
|||
<lang julia>using LinearAlgebra, GeometryTypes |
|||
import Base.* |
|||
CliffordVector = Point{32, Float64} |
|||
e(n) = (v = zeros(32); v[(1 << n) + 1] = 1.0; CliffordVector(v)) |
|||
randommultivector() = CliffordVector(rand(32)) |
|||
randomvector() = sum(i -> rand() * e(i), 0:4) |
|||
bitcount(n) = (count = 0; while n != 0 n &= n - 1; count += 1 end; count) |
|||
function reorderingsign(i, j) |
|||
ssum, i = 0, i >> 1 |
|||
while i != 0 |
|||
ssum += bitcount(i & j) |
|||
i >>= 1 |
|||
end |
|||
return iseven(ssum) ? 1.0 : -1.0 |
|||
end |
|||
function Base.:*(v1::CliffordVector, v2::CliffordVector) |
|||
result = zeros(32) |
|||
for (i, x1) in enumerate(v1) |
|||
if x1 != 0.0 |
|||
for (j, x2) in enumerate(v2) |
|||
if x2 != 0.0 |
|||
s = reorderingsign(i - 1, j - 1) * x1 * x2 |
|||
k = (i - 1) ⊻ (j - 1) |
|||
result[k + 1] += s |
|||
end |
|||
end |
|||
end |
|||
end |
|||
return CliffordVector(result) |
|||
end |
|||
function testcliffordvector() |
|||
for i in 0:4, j in 0:4 |
|||
if i < j |
|||
if dot(e(i), e(j))[1] != 0.0 |
|||
println("Unexpected nonzero scalar product") |
|||
return |
|||
end |
|||
elseif i == j |
|||
if dot(e(i), e(j))[1] == 0.0 |
|||
println("Unexpected zero scalar product") |
|||
end |
|||
end |
|||
end |
|||
a = randommultivector() |
|||
b = randommultivector() |
|||
c = randommultivector() |
|||
x = randomvector() |
|||
# (ab)c ≈ a(bc) |
|||
@show (a * b) * c ≈ a * (b * c) |
|||
# a(b+c) ≈ ab + ac |
|||
@show a * (b + c) ≈ a * b + a * c |
|||
# (a+b)c ≈ ac + bc |
|||
@show (a + b) * c ≈ a * c + b * c |
|||
# x^2 is real |
|||
isreal(x) = x[1] isa Real && all(y -> y == 0, x[2:end]) |
|||
@show isreal(x * x) |
|||
end |
|||
testcliffordvector() |
|||
</lang>{{out}} |
|||
<pre> |
|||
(a * b) * c ≈ a * (b * c) = true |
|||
a * (b + c) ≈ a * b + a * c = true |
|||
(a + b) * c ≈ a * c + b * c = true |
|||
isreal(x * x) = true |
|||
</pre> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |