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 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);
}
}

View file

@ -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) {

View file

@ -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();
}

View file

@ -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();
}

View file

@ -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);
}
}