Geometric algebra: Difference between revisions

Line 1,075:
 
So, for example in the part where we check for orthogonality, we are forming a 5 by 5 by 9223372036854775807 array. The first two dimensions correspond to arguments to <code>e</code> and the final dimension is the multivector dimension. A <code>0</code> in the multivector dimension means that that's the "real" or "scalar" part of the multivector. And, since the pair of dimensions for the <code>e</code> arguments whose "dot" product are <code>1</code> are identical, we know we have an [[wp:Identity_matrix|identity matrix]].
 
=={{header|Java}}==
{{trans|Kotlin}}
<lang java>import java.util.Arrays;
import java.util.Random;
 
public class GeometricAlgebra {
private static int bitCount(int i) {
i -= ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
i = (i + (i >> 4)) & 0x0F0F0F0F;
i += (i >> 8);
i += (i >> 16);
return i & 0x0000003F;
}
 
private static double reorderingSign(int i, int j) {
int k = i >> 1;
int sum = 0;
while (k != 0) {
sum += bitCount(k & j);
k = k >> 1;
}
return ((sum & 1) == 0) ? 1.0 : -1.0;
}
 
static class Vector {
private double[] dims;
 
public Vector(double[] dims) {
this.dims = dims;
}
 
public Vector dot(Vector rhs) {
return times(rhs).plus(rhs.times(this)).times(0.5);
}
 
public Vector unaryMinus() {
return times(-1.0);
}
 
public Vector plus(Vector rhs) {
double[] result = Arrays.copyOf(dims, 32);
for (int i = 0; i < rhs.dims.length; ++i) {
result[i] += rhs.get(i);
}
return new Vector(result);
}
 
public Vector times(Vector rhs) {
double[] result = new double[32];
for (int i = 0; i < dims.length; ++i) {
if (dims[i] != 0.0) {
for (int j = 0; j < rhs.dims.length; ++j) {
if (rhs.get(j) != 0.0) {
double s = reorderingSign(i, j) * dims[i] * rhs.dims[j];
int k = i ^ j;
result[k] += s;
}
}
}
}
return new Vector(result);
}
 
public Vector times(double scale) {
double[] result = dims.clone();
for (int i = 0; i < 5; ++i) {
dims[i] *= scale;
}
return new Vector(result);
}
 
double get(int index) {
return dims[index];
}
 
void set(int index, double value) {
dims[index] = value;
}
 
@Override
public String toString() {
StringBuilder sb = new StringBuilder("(");
boolean first = true;
for (double value : dims) {
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append(value);
}
return sb.append(")").toString();
}
}
 
private static Vector e(int n) {
if (n > 4) {
throw new IllegalArgumentException("n must be less than 5");
}
Vector result = new Vector(new double[32]);
result.set(1 << n, 1.0);
return result;
}
 
private static final Random rand = new Random();
 
private static Vector randomVector() {
Vector result = new Vector(new double[32]);
for (int i = 0; i < 5; ++i) {
Vector temp = new Vector(new double[]{rand.nextDouble()});
result = result.plus(temp.times(e(i)));
}
return result;
}
 
private static Vector randomMultiVector() {
Vector result = new Vector(new double[32]);
for (int i = 0; i < 32; ++i) {
result.set(i, rand.nextDouble());
}
return result;
}
 
public static void main(String[] args) {
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
if (i < j) {
if (e(i).dot(e(j)).get(0) != 0.0) {
System.out.println("Unexpected non-null scalar product.");
return;
}
}
}
}
 
Vector a = randomMultiVector();
Vector b = randomMultiVector();
Vector c = randomMultiVector();
Vector x = randomVector();
 
// (ab)c == a(bc)
System.out.println(a.times(b).times(c));
System.out.println(a.times(b.times(c)));
System.out.println();
 
// a(b+c) == ab + ac
System.out.println(a.times(b.plus(c)));
System.out.println(a.times(b).plus(a.times(c)));
System.out.println();
 
// (a+b)c == ac + bc
System.out.println(a.plus(b).times(c));
System.out.println(a.times(c).plus(b.times(c)));
System.out.println();
 
// x^2 is real
System.out.println(x.times(x));
}
}</lang>
{{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)
(-14.826385115483196, -10.223187918212307, -15.029966534524872, -14.46670632198489, 9.367877413249495, 16.476783705852135, -15.946557036515443, -6.398255774639046, 1.5560496352407327, -7.135570742872677, -3.92627819494472, 4.794851129679368, -8.132546330778192, -6.813939760904995, -4.203632573891537, -0.5216302370612151, -11.240590807272161, -4.724378457579236, -16.477497153082247, -16.781194046381223, 3.7158949340673937, 14.220125782905386, -12.846081825616348, -2.831363785920656, 3.1494684394691537, -5.163082715280574, -7.174869565605505, -3.3452727951238828, -11.232806169860877, -9.975821980348009, -3.7795503524017757, -1.0610782061627715)
 
(-6.53148126676475, -3.686197874035778, -0.7257324996849555, 4.278415831115293, 6.812402569345597, 1.3919745454991994, -2.3737314163466, -2.534117136854738, -0.02773680252709232, -2.959894343894277, 9.079951306324414, 3.5445539013571175, 4.855317638943012, 2.7949884209613094, 3.4702307051832215, 5.9614310877021195, -6.273887650229915, -3.3318300067614355, 2.5996846757588705, 5.753030636602611, 8.66307309332676, 4.103168227184037, -2.0758275707527396, -0.7429391125916148, -1.7949737037366886, -2.709591610956401, 10.9623759848287, 5.951981557918796, 5.367767313008209, 1.288037481749198, 4.141675073009981, 7.643531800306178)
(-6.5314812667647475, -3.686197874035778, -0.7257324996849563, 4.278415831115293, 6.812402569345599, 1.3919745454991996, -2.3737314163465992, -2.5341171368547366, -0.02773680252709232, -2.9598943438942764, 9.079951306324414, 3.54455390135712, 4.8553176389430135, 2.7949884209613103, 3.470230705183222, 5.96143108770212, -6.273887650229915, -3.3318300067614346, 2.5996846757588723, 5.753030636602611, 8.66307309332676, 4.103168227184039, -2.075827570752741, -0.742939112591615, -1.7949737037366884, -2.7095916109564, 10.9623759848287, 5.951981557918795, 5.367767313008202, 1.2880374817491977, 4.141675073009981, 7.64353180030618)
 
(-7.736834353560962, -2.502599400656683, -1.3572044140931654, 0.08823728154205668, 6.035469456565145, -0.12760675585521175, 1.6863493427502443, 0.3572366658217211, -0.5993428822081044, -1.9667973240788488, 9.136744013591118, 5.590676320343252, 5.026876608331699, 2.930873432370117, 3.3588635932299327, 7.0096288327820115, -9.169529600001841, -2.648288994190627, 1.0293696456484516, 1.403189013821808, 6.487492648530549, 2.70613886829337, -0.1101834481621051, 0.9130516717900701, -1.5135624840660704, -0.03875540986063708, 7.7402678101288025, 3.7385632770106736, 2.9147287828638784, -1.2493549934763781, 2.9769191225149667, 7.2051875611073894)
(-7.736834353560959, -2.502599400656682, -1.357204414093165, 0.08823728154205956, 6.035469456565146, -0.12760675585521244, 1.686349342750246, 0.35723666582172275, -0.5993428822081048, -1.9667973240788483, 9.136744013591116, 5.590676320343253, 5.026876608331699, 2.9308734323701158, 3.358863593229933, 7.009628832782012, -9.169529600001841, -2.648288994190626, 1.0293696456484527, 1.4031890138218066, 6.487492648530545, 2.7061388682933707, -0.11018344816210468, 0.9130516717900696, -1.5135624840660693, -0.038755409860637524, 7.7402678101288025, 3.738563277010674, 2.914728782863878, -1.249354993476379, 2.976919122514966, 7.205187561107389)
 
(1.9374102817883092, 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|javascript}}==
1,452

edits