Geometric algebra: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: added syntax colouring, marked p2js compatible)
m (syntax highlighting fixup automation)
Line 55:
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang="csharp">using System;
using System.Text;
 
Line 237:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>[-2.7059639813936, -2.65443237395364, -1.03355975191747, 5.431067101183, 9.57183741787636, 7.41390675997241, -8.09043009371666, -7.30180304927878, -1.50642825479215, -4.14594595273162, -1.78857280373918, -0.484382016930444, -7.42401696794793, -4.78491995868705, -8.43252648860165, -4.47485336471182, -2.3458119817208, 3.6184495826099, -7.50802924695147, -2.10106278080463, 7.15782745037037, 7.60049996423655, -10.6945339837475, -6.9874232887485, -4.85603010723139, -9.8225346377117, 3.50534384939214, 3.08239272713895, -9.92019737517891, -10.1065306142574, -8.79795495448311, -4.01442257971653]
Line 252:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <random>
Line 419:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>[-5.63542, -6.59107, -10.2043, -5.21095, 8.68946, 0.579114, -4.67295, -6.72461, 1.55005, -2.63952, -1.83855, 2.4967, -4.3396, -9.9157, -4.6942, -3.23625, -2.3767, -4.55607, -14.3135, -14.2001, 9.84839, 3.69933, -3.38306, -7.60063, -0.236772, 0.988399, -0.549176, 6.61959, 4.69712, -5.34606, -12.2294, -12.6537]
Line 434:
=={{header|D}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="d">import std.exception;
import std.random;
import std.stdio;
Line 586:
// x^2 is real
writeln(x * x);
}</langsyntaxhighlight>
{{out}}
<pre>Vector([-4.60357, -1.66951, 0.230125, -4.36372, 11.0032, 7.21226, -1.5373, -6.44947, -5.07115, -1.63098, 2.90828, 7.1582, -15.5565, -1.31705, 1.3186, -1.07552, -4.04055, -2.16556, -4.41229, 0.323326, 5.03127, -1.36494, -0.915379, -6.86147, -5.87756, -4.31528, 12.4005, 15.6349, -9.54983, -1.08376, 3.60886, 4.17844])
Line 602:
We build a CGA based upon a generating quadratic form in R^n. The implementation is general enough, that is ei*ei = +/- 1 , and not restricted to 1. The 5 dimension limit comes from the use of 32 bits numbers to generate all permutations 101... , but this could be improved. The multi-vector multiplication is based on a multiplication table 2^n * 2^n , generated once for all.
 
<langsyntaxhighlight lang="scheme">
(define e-bits (build-vector 32 (lambda(i) (arithmetic-shift 1 i)))) ;; 1,2,4,..
(define (e-index i) ;; index of ei in native vector
Line 715:
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="scheme">
;; we use indices (1 ... n) in conformity with the Wikipedia reference
 
Line 767:
(• II J K) → -1*1
 
</syntaxhighlight>
</lang>
Multiplication table for A(3 0)
 
Line 823:
=={{header|Go}}==
{{trans|JavaScript}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 950:
// x² is real
fmt.Println(mul(x, x))
}</langsyntaxhighlight>
 
{{out}}
Line 970:
Implementation:
 
<langsyntaxhighlight Jlang="j">NB. indices are signed machine integers
vzero=: 1 $.2^31+IF64*32
odim=. 2^.#vzero
Line 1,003:
 
obasis=:1 (2^i.odim)ndx01 vzero
e=: {&obasis</langsyntaxhighlight>
 
Explanation:
Line 1,027:
Task examples:
 
<langsyntaxhighlight Jlang="j"> NB. test arbitrary vector being real (and having the specified result)
clean gmul~ +/ (e 0 1 2 3 4) gmul 1 _1 2 3 _2 (0 ndx01) vzero
0 │ 19
Line 1,070:
I gmul J+K
10 │ 1
12 │ _1</langsyntaxhighlight>
 
Note that sparse arrays display as <code>indices | value</code> for elements which are not the default element (0).
Line 1,078:
=={{header|Java}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="java">import java.util.Arrays;
import java.util.Random;
 
Line 1,235:
System.out.println(x.times(x));
}
}</langsyntaxhighlight>
{{out}}
<pre>(-14.826385115483191, -10.223187918212313, -15.02996653452487, -14.46670632198489, 9.367877413249497, 16.476783705852135, -15.946557036515442, -6.398255774639048, 1.5560496352407314, -7.135570742872677, -3.926278194944716, 4.7948511296793646, -8.132546330778185, -6.8139397609050025, -4.203632573891533, -0.5216302370612196, -11.240590807272154, -4.724378457579242, -16.477497153082254, -16.781194046381223, 3.715894934067395, 14.220125782905383, -12.846081825616357, -2.8313637859206557, 3.149468439469149, -5.163082715280577, -7.174869565605504, -3.34527279512389, -11.232806169860876, -9.975821980348016, -3.7795503524017735, -1.0610782061627755)
Line 1,249:
 
=={{header|javascript}}==
<langsyntaxhighlight lang="javascript">var GA = function () {
function e(n) {
var result = [];
Line 1,314:
mul : mul
};
}();</langsyntaxhighlight>
 
And then, from the console:
 
<langsyntaxhighlight lang="javascript">var e = GA.e, cdot = GA.cdot;
for (var i = 0; i < 5; i++) {
Line 1,357:
 
// x² is real
console.log(GA.mul(x, x));</langsyntaxhighlight>
{{out}}
<pre>[-7.834854130554672, -10.179405417124476, 5.696414143584243, -1.4014556169803851, 12.334288331422336, 11.690738709598888, -0.4279888274147221, 6.226618790084965, -10.904144874917206, -5.46919448234424, -5.647472225071031, -2.9801969751721744, -8.284532508545746, -3.3280413654836494, -2.2182526412098493, 0.4191036292473347, 3.0485450100607103, -0.20619687045226742, 2.1369938048939527, 3.730913391951158, 10.929856967963905, 8.301187183717643, -4.874133827873075, 0.7918650606624789, -8.520661635525103, -7.732342981599732, -6.494750491582618, -2.458749173402162, 3.573788336699224, 2.784339193089742, -1.6479372032388944, -0.35120747879544256]
Line 1,370:
From Javascript.
 
<langsyntaxhighlight lang="javascript">/* Geometric Algebra, in Jsih */
 
var GA = function () {
Line 1,496:
GA.mul(x, x) ==> [ 1.659737254471485, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 1,504:
=={{header|Julia}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="julia">using GeometryTypes
import Base.*
 
Line 1,563:
 
testcliffordvector()
</langsyntaxhighlight>{{out}}
<pre>
e(i) * e(j) are orthonormal for i, j ϵ [0, 4]: true
Line 1,574:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">fun bitCount(i: Int): Int {
var j = i
j -= ((j shr 1) and 0x55555555)
Line 1,723:
// x^2 is real
println(x * x)
}</langsyntaxhighlight>
{{out}}
<pre>(-6.38113123172589, -6.025395204580336, 0.5762054454373319, -2.224611121553874, -0.03467815839340305, -0.6665488550665257, -3.012105902847624, 0.7315782457554153, -4.183528079943369, -1.8391037440709876, -0.137654892093293, 0.10852885457965271, -6.021317788342983, -5.486453322362711, -3.524908677069778, -1.030729377561671, -6.858194536947578, -8.724962937014816, 0.4660400096706247, -1.6434599565678671, 4.212637141194194, 2.916899539720754, -1.365566480297562, 1.898991559248674, -2.5943503153384517, -0.7167616808942235, 1.2152416665362584, 2.9936787524618067, -5.394453145898911, -4.180356766796923, -6.622391097517418, -4.249450373116712)
Line 1,738:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import bitops, random, sequtils, strutils
 
type Vector = seq[float]
Line 1,815:
 
# x² is real.
echo x * x</langsyntaxhighlight>
 
{{out}}
Line 1,831:
=={{header|Phix}}==
{{trans|Go}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">bitCount</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
Line 1,934:
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"x^2 is real"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">xsq</span><span style="color: #0000FF;">,</span><span style="color: #000000;">xsq</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]&</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">31</span><span style="color: #0000FF;">))</span>
<!--</langsyntaxhighlight>-->
{{out}}
Note the comparison of string representations of floats, rather than the floats themselves. That effectively ensures they
Line 1,947:
=={{header|Python}}==
{{trans|D}}
<langsyntaxhighlight lang="python">import copy, random
 
def bitcount(n):
Line 2,054:
print x * x
 
main()</langsyntaxhighlight>
{{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]
Line 2,072:
Here we write a simplified version of the [https://github.com/grondilu/clifford Clifford] module. It is very general as it is of infinite dimension and also contains an anti-euclidean basis @ē in addition to the euclidean basis @e.
 
<syntaxhighlight lang="raku" perl6line>unit class MultiVector;
subset UIntHash of MixHash where .keys.all ~~ UInt;
has UIntHash $.blades;
Line 2,171:
my @coeff = (.5 - rand) xx 5;
my $v = [+] @coeff Z* @e[^5];
ok ($v**2).narrow ~~ Real, 'contraction';</langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Option Strict On
 
Imports System.Text
Line 2,366:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>[0.574263754349833, -1.04033308353824, -0.776121961159351, 2.00792496222078, 3.16921091358851, 6.73557610270275, -13.4327886840529, -7.74195172209513, -7.21674283588092, -5.86045645950882, 2.22312083899981, 1.32215440847646, -6.14796858424355, -6.31067579900569, -8.24016821785677, -7.13138966213629, 1.70037903072841, -1.61670055512076, -1.53920826677554, 0.0830404550813257, 3.93618132588852, 7.40029585163889, -6.36594388310886, 2.14605789872596, -12.3353817163416, -9.51720072572794, 4.57164353732716, 3.11601890765627, -3.45821025466289, -4.2333151576991, -5.88080545860622, -6.53549242641427]
Line 2,381:
=={{header|Wren}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
 
var bitCount = Fn.new { |i|
Line 2,510:
 
// x^2 is real
System.print(x * x)</langsyntaxhighlight>
 
{{out}}
10,333

edits