From 4dcfa47e31029d54d2aca005452e82a5fe43bbc8 Mon Sep 17 00:00:00 2001 From: Alnotz Date: Fri, 19 May 2023 02:11:21 +0200 Subject: [PATCH] Dual & Quaternion classes done --- fr/alnotz/Application.java | 5 ++- fr/alnotz/Dual.java | 16 ++++---- fr/alnotz/IsDual.java | 6 +-- fr/alnotz/IsQuaternion.java | 14 +++---- fr/alnotz/Quaternion.java | 77 +++++++++++++++++++++++-------------- 5 files changed, 70 insertions(+), 48 deletions(-) diff --git a/fr/alnotz/Application.java b/fr/alnotz/Application.java index 3a2ff18..cac71be 100644 --- a/fr/alnotz/Application.java +++ b/fr/alnotz/Application.java @@ -3,6 +3,9 @@ package fr.alnotz; public class Application { public static void main(String[] args){ System.out.println("Hi."); - + Dual dual = new Dual(10, 35); + System.out.println(dual); + Quaternion quaternion = new Quaternion(21, 45, 7, -9); + System.out.println(quaternion); } } diff --git a/fr/alnotz/Dual.java b/fr/alnotz/Dual.java index defb25c..a991730 100644 --- a/fr/alnotz/Dual.java +++ b/fr/alnotz/Dual.java @@ -44,18 +44,18 @@ public class Dual implements IsDual{ } @Override - public void plus(IsDual dual) { - this.r += dual.getR(); - this.d += dual.getD(); + public Dual plus(IsDual dual) { + return new Dual(this.r + dual.getR(), + this.d + dual.getD()); } @Override - public void times(IsDual dual) { - this.r *= dual.getR(); - this.d = this.r * dual.getD() + this.d * dual.getR(); + public Dual times(IsDual dual) { + return new Dual(this.r * dual.getR(), + this.r * dual.getD() + this.d * dual.getR()); } @Override - public void conjugate(){ - this.d *= -1; + public Dual conjugate(){ + return new Dual(this.r, this.d * -1); } @Override public boolean equals(IsDual dual) { diff --git a/fr/alnotz/IsDual.java b/fr/alnotz/IsDual.java index e5ee819..c704f60 100644 --- a/fr/alnotz/IsDual.java +++ b/fr/alnotz/IsDual.java @@ -5,9 +5,9 @@ public interface IsDual { double getD(); void setR(double r); void setD(double d); - void plus(IsDual dual); - void times(IsDual dual); - void conjugate(); + IsDual plus(IsDual dual); + IsDual times(IsDual dual); + IsDual conjugate(); boolean equals(IsDual dual); String toString(); } diff --git a/fr/alnotz/IsQuaternion.java b/fr/alnotz/IsQuaternion.java index 4ff97f5..2981461 100644 --- a/fr/alnotz/IsQuaternion.java +++ b/fr/alnotz/IsQuaternion.java @@ -5,13 +5,13 @@ public interface IsQuaternion { double getI(); double getJ(); double getK(); - void setS(); - void setI(); - void setJ(); - void setK(); - void plus(IsQuaternion quaternion); - void times(IsQuaternion quaternion); - void conjugate(); + void setS(double s); + void setI(double i); + void setJ(double j); + void setK(double k); + IsQuaternion plus(IsQuaternion quaternion); + IsQuaternion times(IsQuaternion quaternion); + IsQuaternion conjugate(); boolean equals(IsQuaternion quaternion); String toString(); } diff --git a/fr/alnotz/Quaternion.java b/fr/alnotz/Quaternion.java index d68d338..bc4328b 100644 --- a/fr/alnotz/Quaternion.java +++ b/fr/alnotz/Quaternion.java @@ -19,61 +19,80 @@ public class Quaternion implements IsQuaternion{ } @Override public double getS() { - return 0; + return this.s; } - @Override public double getI() { - return 0; + return this.i; } - @Override public double getJ() { - return 0; + return this.j; } - @Override public double getK() { - return 0; + return this.k; } - @Override - public void setS() { - + public void setS(double s) { + this.s = s; } - @Override - public void setI() { - + public void setI(double i) { + this.i = i; } - @Override - public void setJ() { - + public void setJ(double j) { + this.j = j; } - @Override - public void setK() { - + public void setK(double k) { + this.k = k; } - @Override - public void plus(IsQuaternion quaternion) { - + public Quaternion plus(IsQuaternion quaternion) { + return new Quaternion(this.s + quaternion.getS(), + this.i + quaternion.getI(), + this.j + quaternion.getJ(), + this.k + quaternion.getK()); } - @Override - public void times(IsQuaternion quaternion) { - + public Quaternion times(IsQuaternion quaternion) { + /* + * (a0 + b0i + c0j + d0k)(a1 + b1i + c1j + d1k) + * (a0a1 - b0b1 - c0c1 - d0d1) + + * (a0b1 + b0a1 + c0d1 - d0c1)i + + * (a0c1 - b0d1 + c0a1 + d0b1)j + + * (a0d1 + b0c1 - c0b1 + d0a1)k + */ + double newS = this.s * quaternion.getS() - + this.i * quaternion.getI() - + this.j * quaternion.getJ() - + this.k * quaternion.getK(); + double newI = this.s * quaternion.getI() + + this.i * quaternion.getK() + + this.j * quaternion.getS() - + this.k * quaternion.getJ(); + double newJ = this.s * quaternion.getJ() - + this.i * quaternion.getK() + + this.j * quaternion.getS() + + this.k * quaternion.getI(); + double newK = this.s * quaternion.getK() + + this.i * quaternion.getJ() - + this.j * quaternion.getI() + + this.k * quaternion.getS(); + return new Quaternion(newS, newI, newJ, newK); } - @Override - public void conjugate() { - + public Quaternion conjugate() { + return new Quaternion(this.s, -1 * this.i, -1 * this.j, -1 * this.k); } - @Override public boolean equals(IsQuaternion quaternion) { return false; } + @Override + public String toString(){ + return String.format("%+f %+fi %+fj %+fk\n", this.s, this.i, this.j, this.k); + } }