Skip to content

Instantly share code, notes, and snippets.

@fversnel
Last active February 17, 2017 11:12
Show Gist options
  • Save fversnel/0679f10978da2b2eb67a7ab41bacd981 to your computer and use it in GitHub Desktop.
Save fversnel/0679f10978da2b2eb67a7ab41bacd981 to your computer and use it in GitHub Desktop.
My first attempt at implementing lateral (i.e. imaginary) numbers. Currently implemented +, - and *
namespace Lateralus {
public struct ComplexNumber {
public readonly double Direct; // Real part
public readonly Lateral Lateral; // Imaginary part
public ComplexNumber(double direct, Lateral lateral) {
Direct = direct;
Lateral = lateral;
}
public ComplexNumber(double direct, double lateral) : this(direct, new Lateral(lateral)) {}
public static ComplexNumber operator +(ComplexNumber c1, ComplexNumber c2) {
return new ComplexNumber(c1.Direct + c2.Direct, c1.Lateral + c2.Lateral);
}
public static ComplexNumber operator -(ComplexNumber c1, ComplexNumber c2) {
return new ComplexNumber(c1.Direct - c2.Direct, c1.Lateral - c2.Lateral);
}
/// <summary>
/// Multiplying two complex numbers means rotating the first number
/// by the second number (we actually sum the rotation of the first and second
/// number)
///
/// Secondly we multiply the length of both vectors to give the new complex
/// number its appropriate size
/// </summary>
/// <param name="c1"></param>
/// <param name="c2"></param>
/// <returns></returns>
public static ComplexNumber operator *(ComplexNumber c1, ComplexNumber c2) {
// i¹ = i = √-1
// i² = -1
// i³ = -i
// i⁴ = 1
var direct = c1.Direct * c2.Direct;
var lateral1 = c1.Direct * c2.Lateral;
var lateral2 = c1.Lateral * c2.Direct;
var squaredLateral = c1.Lateral * c2.Lateral;
return new ComplexNumber(direct + squaredLateral, lateral1 + lateral2);
}
public override string ToString() {
return Direct + " + " + Lateral;
}
}
public struct Lateral {
public readonly double Value;
public Lateral(double value) {
Value = value;
}
public static Lateral operator +(Lateral l1, Lateral l2) {
return new Lateral(l1.Value + l2.Value);
}
public static Lateral operator -(Lateral l1, Lateral l2) {
return new Lateral(l1.Value - l2.Value);
}
/// <summary>
/// Multiplying two lateral components implies
/// squaring them. Since i² = -1 we negate the result
/// of the multiplication and arrive at the correct number
/// </summary>
public static double operator *(Lateral l1, Lateral l2) {
return -(l1.Value * l2.Value);
}
public static Lateral operator *(double direct, Lateral lateral) {
return lateral * direct;
}
public static Lateral operator *(Lateral lateral, double direct) {
return new Lateral(lateral.Value * direct);
}
public override string ToString() {
return Value + "i";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment