fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Quaternion {
  6. private double a;
  7. private double b;
  8. private double c;
  9. private double d;
  10.  
  11. public Quaternion() {
  12. a = 0;
  13. b = 0;
  14. c = 0;
  15. d = 0;
  16. }
  17. public Quaternion(double a) {
  18. this.a = a;
  19. b = 0;
  20. c = 0;
  21. d = 0;
  22. }
  23. public Quaternion(double a, double b, double c, double d) {
  24. this.a = a;
  25. this.b = b;
  26. this.c = c;
  27. this.d = d;
  28. }
  29.  
  30. public Quaternion conjug() {
  31. return new Quaternion(a, -b, -c, -d);
  32. }
  33. public double abs() {
  34. return Math.sqrt(a * a + b * b + c * c + d * d);
  35. }
  36. public Quaternion inverse() throws ArithmeticException {
  37. return Quaternion.divide(this.conjug(), this.abs() * this.abs());
  38. }
  39. public boolean isZero() {
  40. return (a == 0 && b == 0 && c == 0 && d == 0);
  41. }
  42.  
  43. public static Quaternion sum(Quaternion a, Quaternion b) {
  44. return new Quaternion(a.a + b.a, a.b + b.b, a.c + b.c, a.d + b.d);
  45. }
  46. public static Quaternion sub(Quaternion a, Quaternion b) {
  47. return new Quaternion(a.a - b.a, a.b - b.b, a.c - b.c, a.d - b.d);
  48. }
  49. public static Quaternion mul(Quaternion a, double mult) {
  50. return new Quaternion(a.a * mult, a.b * mult, a.c * mult, a.d * mult);
  51. }
  52. public static Quaternion mul(Quaternion a, Quaternion b) {
  53. return new Quaternion(a.a * b.a - a.b * b.b - a.c * b.c - a.d * b.d,
  54. a.a * b.b + a.b * b.a + a.c * b.d - a.d * b.c,
  55. a.a * b.c - a.b * b.d + a.c * b.a + a.d * b.b,
  56. a.a * b.d + a.b * b.c - a.c * b.b + a.d * b.a);
  57. }
  58. public static Quaternion divide(Quaternion a, double divider) throws ArithmeticException {
  59. if (divider == 0) throw new ArithmeticException("Zero divider");
  60. return new Quaternion(a.a / divider, a.b / divider, a.c / divider, a.d / divider);
  61. }
  62. public static Quaternion divide(Quaternion a, Quaternion b) throws ArithmeticException {
  63. if (b.isZero()) throw new ArithmeticException("Zero divider");
  64. return Quaternion.mul(a, b.inverse());
  65. }
  66.  
  67. public String toString() {
  68. return new String(a +
  69. ((b < 0)? " - " : " + ") + Math.abs(b) + "*i" +
  70. ((c < 0)? " - " : " + ") + Math.abs(c) + "*j" +
  71. ((d < 0)? " - " : " + ") + Math.abs(d) + "*k\n");
  72. }
  73. }
  74. class Main {
  75. public static void main (String[] args) {
  76. Quaternion qt1 = new Quaternion();
  77. Quaternion qt2 = new Quaternion(5);
  78. Quaternion qt3 = new Quaternion(1, 2, 3, 4);
  79. Quaternion qt4 = new Quaternion(0.1, 1.4, -2.5, -1.7);
  80. if (!qt1.isZero()) {
  81. System.out.print(Quaternion.sum(qt3, qt4));
  82. } else {
  83. System.out.print(Quaternion.mul(qt3, qt4));
  84. }
  85. do {
  86. qt2 = Quaternion.sub(qt2, qt3);
  87. System.out.print(qt2);
  88. System.out.print(qt2.conjug());
  89. System.out.print(qt2.inverse());
  90. } while (qt2.isZero());
  91. try {
  92. System.out.print(Quaternion.divide(qt3, qt4));
  93. } catch (ArithmeticException e) {
  94. System.out.print(e.getMessage());
  95. }
  96. System.out.print(Quaternion.mul(qt3, 17.2));
  97. }
  98. }
Success #stdin #stdout 0.11s 320576KB
stdin
Standard input is empty
stdout
11.600000000000001 + 6.5*i + 6.8*j - 10.499999999999998*k
4.0 - 2.0*i - 3.0*j - 4.0*k
4.0 + 2.0*i + 3.0*j + 4.0*k
0.08888888888888888 + 0.04444444444444444*i + 0.06666666666666665*j + 0.08888888888888888*k
-1.026102610261026 - 0.5490549054905489*i - 0.5580558055805579*j + 1.017101710171017*k
17.2 + 34.4*i + 51.599999999999994*j + 68.8*k