Skip to content

Instantly share code, notes, and snippets.

@SoulFireMage
Created May 27, 2023 07:50
Show Gist options
  • Save SoulFireMage/b8bf7dbbb1f59a63cde2db3630aa718b to your computer and use it in GitHub Desktop.
Save SoulFireMage/b8bf7dbbb1f59a63cde2db3630aa718b to your computer and use it in GitHub Desktop.
Octonions - A GPT 4 built implmentation
///In an effort to get some kind of intuition around what an octonion even is - I'm not a mathematician! - I asked GPT4 for help.
///During this conversation it gave me the outline of an octonion in C#. Running with this, I prompted for the main operations and built
///this up, sparked by the fact there wasn't an implementation out there (according to GPT4 - which probably is not true).
///Still, posting this up for my future reference as I'm interested in the notion of using such in a neural network
///Though I'm more likely to review abstracts of others who have actually code such CNNs already! :)
public struct Octonion
{
public double w, x, y, z, u, v, t, s; // the eight components of the octonion
public Octonion(double w, double x, double y, double z, double u, double v, double t, double s)
{
this.w = w;
this.x = x;
this.y = y;
this.z = z;
this.u = u;
this.v = v;
this.t = t;
this.s = s;
}
public static Octonion operator *(Octonion a, Octonion b)
{
return new Octonion(
a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z - a.u * b.u - a.v * b.v - a.t * b.t - a.s * b.s,
a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y + a.u * b.s - a.v * b.t + a.t * b.v + a.s * b.u,
a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x - a.u * b.t - a.v * b.s + a.t * b.u - a.s * b.v,
a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w + a.u * b.v + a.v * b.u - a.t * b.s - a.s * b.t,
a.w * b.u - a.x * b.s + a.y * b.t + a.z * b.v - a.u * b.w - a.v * b.x + a.t * b.y + a.s * b.z,
a.w * b.v + a.x * b.t - a.y * b.s - a.z * b.u + a.u * b.x - a.v * b.w - a.t * b.z + a.s * b.y,
a.w * b.t - a.x * b.v + a.y * b.u + a.z * b.s - a.u * b.y + a.v * b.z - a.t * b.w - a.s * b.x,
a.w * b.s + a.x * b.u - a.y * b.v + a.z * b.t + a.u * b.z - a.v * b.y + a.t * b.x - a.s * b.w
);
}
public static Octonion operator +(Octonion a, Octonion b)
{
return new Octonion(
a.w + b.w,
a.x + b.x,
a.y + b.y,
a.z + b.z,
a.u + b.u,
a.v + b.v,
a.t + b.t,
a.s + b.s
);
}
public static Octonion operator -(Octonion a, Octonion b)
{
return new Octonion(
a.w - b.w,
a.x - b.x,
a.y - b.y,
a.z - b.z,
a.u - b.u,
a.v - b.v,
a.t - b.t,
a.s - b.s
);
}
public Octonion Conjugate()
{
return new Octonion(
this.w,
-this.x,
-this.y,
-this.z,
-this.u,
-this.v,
-this.t,
-this.s
);
}
public double Norm()
{
return Math.Sqrt(w * w + x * x + y * y + z * z + u * u + v * v + t * t + s * s);
}
public Octonion Normalize()
{
double norm = this.Norm();
if (norm == 0)
throw new InvalidOperationException("Cannot normalize a zero octonion.");
return new Octonion(
this.w / norm,
this.x / norm,
this.y / norm,
this.z / norm,
this.u / norm,
this.v / norm,
this.t / norm,
this.s / norm
);
}
// Real Part
public double RealPart()
{
return this.w;
}
// Imaginary Part
public (double, double, double, double, double, double, double) ImaginaryPart()
{
return (this.x, this.y, this.z, this.u, this.v, this.t, this.s);
}
// Inverse
public Octonion Inverse()
{
double normSquared = this.Norm() * this.Norm();
if (normSquared == 0)
throw new InvalidOperationException("Cannot invert a zero octonion.");
Octonion conjugate = this.Conjugate();
return new Octonion(
conjugate.w / normSquared,
conjugate.x / normSquared,
conjugate.y / normSquared,
conjugate.z / normSquared,
conjugate.u / normSquared,
conjugate.v / normSquared,
conjugate.t / normSquared,
conjugate.s / normSquared
);
}
// Division
public static Octonion operator /(Octonion a, Octonion b)
{
return a * b.Inverse();
}
}
public static class OctonionExtensions
{
public static Octonion ToOctonion(this (double, double, double, double, double, double, double, double) tuple)
{
return new Octonion(tuple.Item1, tuple.Item2, tuple.Item3, tuple.Item4, tuple.Item5, tuple.Item6, tuple.Item7, tuple.Item8);
}
}
@alphanumericaracters
Copy link

  • [Español] Sinceramente estoy inspeccionando la matemática del código, para que se corresponda con el álgebra de los cuaterniones y octoniones (conmutatividad y asociatividad), para que funcione como deba.

  • [English] I am honestly inspecting the mathematics of the code, so that it corresponds to the algebra of quaternions and octonions (commutativity and associativity), so that it works as it should.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment