Jump to content

Geometric algebra: Difference between revisions

m (→‎{{header|Perl 6}}: Unify class and test code into a single entry for easier offline testing)
Line 783:
<pre>prompt$ jsish -u geometricAlgebra.jsi
[PASS] geometricAlgebra.jsi</pre>
 
=={{header|Kotlin}}==
{{trans|Go}}
<lang scala>fun bitCount(i: Int): Int {
var j = i
j -= ((j shr 1) and 0x55555555)
j = (j and 0x33333333) + ((j shr 2) and 0x33333333)
j = (j + (j shr 4)) and 0x0F0F0F0F
j += (j shr 8)
j += (j shr 16)
return j and 0x0000003F
}
 
fun reorderingSign(i: Int, j: Int): Double {
var k = i shr 1
var sum = 0
while (k != 0) {
sum += bitCount(k and j)
k = k shr 1
}
return if (sum and 1 == 0) 1.0 else -1.0
}
 
class Vector(private val dims: DoubleArray) {
 
infix fun dot(rhs: Vector): Vector {
return (this * rhs + rhs * this) * 0.5
}
 
operator fun unaryMinus(): Vector {
return this * -1.0
}
 
operator fun plus(rhs: Vector): Vector {
val result = DoubleArray(32)
dims.copyInto(result)
for (i in 0 until rhs.dims.size) {
result[i] += rhs[i]
}
return Vector(result)
}
 
operator fun times(rhs: Vector): Vector {
val result = DoubleArray(32)
for (i in 0 until dims.size) {
if (dims[i] != 0.0) {
for (j in 0 until rhs.dims.size) {
if (rhs[j] != 0.0) {
val s = reorderingSign(i, j) * dims[i] * rhs[j]
val k = i xor j
result[k] += s
}
}
}
}
return Vector(result)
}
 
operator fun times(scale: Double): Vector {
val result = dims.clone()
for (i in 0 until 5) {
dims[i] = dims[i] * scale
}
return Vector(result)
}
 
operator fun get(index: Int): Double {
return dims[index]
}
 
operator fun set(index: Int, value: Double) {
dims[index] = value
}
 
override fun toString(): String {
val sb = StringBuilder("(")
val it = dims.iterator()
if (it.hasNext()) {
sb.append(it.next())
}
while (it.hasNext()) {
sb.append(", ").append(it.next())
}
return sb.append(")").toString()
}
}
 
fun e(n: Int): Vector {
if (n > 4) {
throw IllegalArgumentException("n must be less than 5")
}
val result = Vector(DoubleArray(32))
result[1 shl n] = 1.0
return result
}
 
val rand = java.util.Random()
 
fun randomVector(): Vector {
var result = Vector(DoubleArray(32))
for (i in 0 until 5) {
result += Vector(doubleArrayOf(rand.nextDouble())) * e(i)
}
return result
}
 
fun randomMultiVector(): Vector {
val result = Vector(DoubleArray(32))
for (i in 0 until 32) {
result[i] = rand.nextDouble()
}
return result
}
 
fun main() {
for (i in 0..4) {
for (j in 0..4) {
if (i < j) {
if ((e(i) dot e(j))[0] != 0.0) {
println("Unexpected non-null scalar product.")
return
} else if (i == j) {
if ((e(i) dot e(j))[0] == 0.0) {
println("Unexpected null scalar product.")
}
}
}
}
}
 
val a = randomMultiVector()
val b = randomMultiVector()
val c = randomMultiVector()
val x = randomVector()
 
// (ab)c == a(bc)
println((a * b) * c)
println(a * (b * c))
println()
 
// a(b+c) == ab + ac
println(a * (b + c))
println(a * b + a * c)
println()
 
// (a+b)c == ac + bc
println((a + b) * c)
println(a * c + b * c)
println()
 
// x^2 is real
println(x * x)
}</lang>
{{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)
(-6.381131231725885, -6.025395204580336, 0.5762054454373331, -2.2246111215538744, -0.034678158393403866, -0.6665488550665265, -3.012105902847625, 0.7315782457554131, -4.183528079943374, -1.83910374407099, -0.13765489209329423, 0.1085288545796525, -6.021317788342984, -5.486453322362712, -3.524908677069777, -1.0307293775616722, -6.858194536947579, -8.724962937014814, 0.46604000967062464, -1.6434599565678658, 4.212637141194197, 2.916899539720756, -1.3655664802975644, 1.8989915592486726, -2.5943503153384526, -0.716761680894223, 1.2152416665362615, 2.9936787524618076, -5.394453145898909, -4.1803567667969235, -6.622391097517417, -4.24945037311671)
 
(-5.001996874378833, -5.342863347345118, 0.5930301138931318, 1.8495242515856356, 5.226690714239807, 5.60029170092836, 1.9264816320512175, 2.9647400073622383, -1.231074336132209, -0.03574406666207321, 2.0490593688751666, 2.709392811857615, -0.534934380183112, 0.9566513059906786, 0.581531676517317, 2.6867651675849284, -6.072314695960932, -5.780622914352332, -2.264083859970755, -0.29904973719224437, 3.1693202958893196, 3.710947660874322, -0.3876119148699613, 0.28047758363117253, 0.4891346543855399, 1.1102262337923456, 1.9059090529704015, 2.2175302024987404, 3.6850877929174954, 4.798530127158409, 0.6243236143375706, 2.5910748978446576)
(-5.001996874378833, -5.3428633473451175, 0.5930301138931321, 1.8495242515856363, 5.22669071423981, 5.600291700928359, 1.9264816320512164, 2.9647400073622387, -1.231074336132209, -0.035744066662073595, 2.0490593688751657, 2.7093928118576143, -0.5349343801831126, 0.9566513059906783, 0.5815316765173175, 2.6867651675849284, -6.072314695960929, -5.78062291435233, -2.264083859970754, -0.2990497371922432, 3.169320295889319, 3.7109476608743237, -0.3876119148699616, 0.2804775836311723, 0.4891346543855406, 1.1102262337923452, 1.9059090529704013, 2.21753020249874, 3.6850877929174954, 4.798530127158408, 0.6243236143375701, 2.591074897844657)
 
(-3.583945607396393, -3.5239300960595137, 0.801943558725803, 2.9686354622847357, 4.064529783603304, 3.6456241887862015, 0.5954304494585552, 1.2819925842824296, 0.6768216109361052, 0.352207714198545, 2.843092732483635, 2.680239849424965, -0.04943045827884884, 0.19653339086911384, 2.875432393192088, 2.8657275256839823, -4.689497942711508, -3.42309859067624, -2.830665114554725, -0.017676578013020444, 3.635425127096899, 3.10264457980534, -1.2355052114199154, 0.02671272974814648, 2.5200281685651413, 2.2030677245192627, 2.727374336905742, 1.7812768180621927, 1.901781821035387, 2.8306551025240863, 3.9572153730448463, 2.9611369534401093)
(-3.5839456073963945, -3.5239300960595115, 0.8019435587258037, 2.968635462284734, 4.064529783603305, 3.6456241887862024, 0.5954304494585542, 1.2819925842824302, 0.6768216109361052, 0.3522077141985451, 2.843092732483634, 2.6802398494249644, -0.04943045827884848, 0.1965333908691129, 2.875432393192088, 2.8657275256839827, -4.689497942711507, -3.4230985906762403, -2.8306651145547246, -0.017676578013019806, 3.6354251270968985, 3.1026445798053395, -1.2355052114199154, 0.026712729748146535, 2.5200281685651413, 2.2030677245192627, 2.7273743369057417, 1.7812768180621923, 1.9017818210353874, 2.830655102524086, 3.957215373044847, 2.9611369534401097)
 
(2.6501903002573224, 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>
 
=={{header|Perl 6}}==
1,452

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.