Gruppe 1
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Complex.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package com.example.ueberwachungssystem.Detection.Signalverarbeitung;
  2. import java.util.Objects;
  3. public class Complex {
  4. private final double re; // the real part
  5. private final double im; // the imaginary part
  6. // create a new object with the given real and imaginary parts
  7. public Complex(double real, double imag) {
  8. re = real;
  9. im = imag;
  10. }
  11. // return a string representation of the invoking com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object
  12. public String toString() {
  13. if (im == 0) return re + "";
  14. if (re == 0) return im + "i";
  15. if (im < 0) return re + " - " + (-im) + "i";
  16. return re + " + " + im + "i";
  17. }
  18. // return abs/modulus/magnitude
  19. public double abs() {
  20. return Math.hypot(re, im);
  21. }
  22. // return angle/phase/argument, normalized to be between -pi and pi
  23. public double phase() {
  24. return Math.atan2(im, re);
  25. }
  26. // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is (this + b)
  27. public Complex plus(Complex b) {
  28. Complex a = this; // invoking object
  29. double real = a.re + b.re;
  30. double imag = a.im + b.im;
  31. return new Complex(real, imag);
  32. }
  33. // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is (this - b)
  34. public Complex minus(Complex b) {
  35. Complex a = this;
  36. double real = a.re - b.re;
  37. double imag = a.im - b.im;
  38. return new Complex(real, imag);
  39. }
  40. // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is (this * b)
  41. public Complex times(Complex b) {
  42. Complex a = this;
  43. double real = a.re * b.re - a.im * b.im;
  44. double imag = a.re * b.im + a.im * b.re;
  45. return new Complex(real, imag);
  46. }
  47. // return a new object whose value is (this * alpha)
  48. public Complex scale(double alpha) {
  49. return new Complex(alpha * re, alpha * im);
  50. }
  51. // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is the conjugate of this
  52. public Complex conjugate() {
  53. return new Complex(re, -im);
  54. }
  55. // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is the reciprocal of this
  56. public Complex reciprocal() {
  57. double scale = re * re + im * im;
  58. return new Complex(re / scale, -im / scale);
  59. }
  60. // return the real or imaginary part
  61. public double re() {
  62. return re;
  63. }
  64. public double im() {
  65. return im;
  66. }
  67. // return a / b
  68. public Complex divides(Complex b) {
  69. Complex a = this;
  70. return a.times(b.reciprocal());
  71. }
  72. // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is the complex exponential of this
  73. public Complex exp() {
  74. return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
  75. }
  76. // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is the complex sine of this
  77. public Complex sin() {
  78. return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
  79. }
  80. // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is the complex cosine of this
  81. public Complex cos() {
  82. return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
  83. }
  84. // return a new com.example.ueberwachungssystem.Detection.Signalverarbeitung.Complex object whose value is the complex tangent of this
  85. public Complex tan() {
  86. return sin().divides(cos());
  87. }
  88. // a static version of plus
  89. public static Complex plus(Complex a, Complex b) {
  90. double real = a.re + b.re;
  91. double imag = a.im + b.im;
  92. Complex sum = new Complex(real, imag);
  93. return sum;
  94. }
  95. // See Section 3.3.
  96. public boolean equals(Object x) {
  97. if (x == null) return false;
  98. if (this.getClass() != x.getClass()) return false;
  99. Complex that = (Complex) x;
  100. return (this.re == that.re) && (this.im == that.im);
  101. }
  102. // See Section 3.3.
  103. public int hashCode() {
  104. return Objects.hash(re, im);
  105. }
  106. // sample client for testing
  107. public static void main(String[] args) {
  108. Complex a = new Complex(5.0, 6.0);
  109. Complex b = new Complex(-3.0, 4.0);
  110. System.out.println("a = " + a);
  111. System.out.println("b = " + b);
  112. System.out.println("Re(a) = " + a.re());
  113. System.out.println("Im(a) = " + a.im());
  114. System.out.println("b + a = " + b.plus(a));
  115. System.out.println("a - b = " + a.minus(b));
  116. System.out.println("a * b = " + a.times(b));
  117. System.out.println("b * a = " + b.times(a));
  118. System.out.println("a / b = " + a.divides(b));
  119. System.out.println("(a / b) * b = " + a.divides(b).times(b));
  120. System.out.println("conj(a) = " + a.conjugate());
  121. System.out.println("|a| = " + a.abs());
  122. System.out.println("tan(a) = " + a.tan());
  123. }
  124. }