Dual & Quaternion classes done

This commit is contained in:
F. Y. H. 2023-05-19 02:11:21 +02:00
parent 7c934dc49e
commit 4dcfa47e31
Signed by: Alnotz
GPG key ID: B808CA1113935C00
5 changed files with 70 additions and 48 deletions

View file

@ -3,6 +3,9 @@ package fr.alnotz;
public class Application { public class Application {
public static void main(String[] args){ public static void main(String[] args){
System.out.println("Hi."); 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);
} }
} }

View file

@ -44,18 +44,18 @@ public class Dual implements IsDual{
} }
@Override @Override
public void plus(IsDual dual) { public Dual plus(IsDual dual) {
this.r += dual.getR(); return new Dual(this.r + dual.getR(),
this.d += dual.getD(); this.d + dual.getD());
} }
@Override @Override
public void times(IsDual dual) { public Dual times(IsDual dual) {
this.r *= dual.getR(); return new Dual(this.r * dual.getR(),
this.d = this.r * dual.getD() + this.d * dual.getR(); this.r * dual.getD() + this.d * dual.getR());
} }
@Override @Override
public void conjugate(){ public Dual conjugate(){
this.d *= -1; return new Dual(this.r, this.d * -1);
} }
@Override @Override
public boolean equals(IsDual dual) { public boolean equals(IsDual dual) {

View file

@ -5,9 +5,9 @@ public interface IsDual {
double getD(); double getD();
void setR(double r); void setR(double r);
void setD(double d); void setD(double d);
void plus(IsDual dual); IsDual plus(IsDual dual);
void times(IsDual dual); IsDual times(IsDual dual);
void conjugate(); IsDual conjugate();
boolean equals(IsDual dual); boolean equals(IsDual dual);
String toString(); String toString();
} }

View file

@ -5,13 +5,13 @@ public interface IsQuaternion {
double getI(); double getI();
double getJ(); double getJ();
double getK(); double getK();
void setS(); void setS(double s);
void setI(); void setI(double i);
void setJ(); void setJ(double j);
void setK(); void setK(double k);
void plus(IsQuaternion quaternion); IsQuaternion plus(IsQuaternion quaternion);
void times(IsQuaternion quaternion); IsQuaternion times(IsQuaternion quaternion);
void conjugate(); IsQuaternion conjugate();
boolean equals(IsQuaternion quaternion); boolean equals(IsQuaternion quaternion);
String toString(); String toString();
} }

View file

@ -19,61 +19,80 @@ public class Quaternion implements IsQuaternion{
} }
@Override @Override
public double getS() { public double getS() {
return 0; return this.s;
} }
@Override @Override
public double getI() { public double getI() {
return 0; return this.i;
} }
@Override @Override
public double getJ() { public double getJ() {
return 0; return this.j;
} }
@Override @Override
public double getK() { public double getK() {
return 0; return this.k;
} }
@Override @Override
public void setS() { public void setS(double s) {
this.s = s;
} }
@Override @Override
public void setI() { public void setI(double i) {
this.i = i;
} }
@Override @Override
public void setJ() { public void setJ(double j) {
this.j = j;
} }
@Override @Override
public void setK() { public void setK(double k) {
this.k = k;
} }
@Override @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 @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 @Override
public void conjugate() { public Quaternion conjugate() {
return new Quaternion(this.s, -1 * this.i, -1 * this.j, -1 * this.k);
} }
@Override @Override
public boolean equals(IsQuaternion quaternion) { public boolean equals(IsQuaternion quaternion) {
return false; return false;
} }
@Override
public String toString(){
return String.format("%+f %+fi %+fj %+fk\n", this.s, this.i, this.j, this.k);
}
} }