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 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package com.feemers.android.fftdrawer.SignalProcessing;
  2. /******************************************************************************
  3. * Compilation: javac Complex.java
  4. * Execution: java Complex
  5. *
  6. * Data type for complex numbers.
  7. *
  8. * The data type is "immutable" so once you create and initialize
  9. * a Complex object, you cannot change it. The "final" keyword
  10. * when declaring re and im enforces this rule, making it a
  11. * compile-time error to change the .re or .im instance variables after
  12. * they've been initialized.
  13. *
  14. * % java Complex
  15. * a = 5.0 + 6.0i
  16. * b = -3.0 + 4.0i
  17. * Re(a) = 5.0
  18. * Im(a) = 6.0
  19. * b + a = 2.0 + 10.0i
  20. * a - b = 8.0 + 2.0i
  21. * a * b = -39.0 + 2.0i
  22. * b * a = -39.0 + 2.0i
  23. * a / b = 0.36 - 1.52i
  24. * (a / b) * b = 5.0 + 6.0i
  25. * conj(a) = 5.0 - 6.0i
  26. * |a| = 7.810249675906654
  27. * tan(a) = -6.685231390246571E-6 + 1.0000103108981198i
  28. *
  29. ******************************************************************************/
  30. public class Complex {
  31. private final double re; // the real part
  32. private final double im; // the imaginary part
  33. // create a new object with the given real and imaginary parts
  34. public Complex(double real, double imag) {
  35. re = real;
  36. im = imag;
  37. }
  38. // return a string representation of the invoking Complex object
  39. public String toString() {
  40. if (im == 0) return re + "";
  41. if (re == 0) return im + "i";
  42. if (im < 0) return re + " - " + (-im) + "i";
  43. return re + " + " + im + "i";
  44. }
  45. // return abs/modulus/magnitude
  46. public double abs() {
  47. return Math.hypot(re, im);
  48. }
  49. // return angle/phase/argument, normalized to be between -pi and pi
  50. public double phase() {
  51. return Math.atan2(im, re);
  52. }
  53. // return a new Complex object whose value is (this + b)
  54. public Complex plus(Complex b) {
  55. Complex a = this; // invoking object
  56. double real = a.re + b.re;
  57. double imag = a.im + b.im;
  58. return new Complex(real, imag);
  59. }
  60. // return a new Complex object whose value is (this - b)
  61. public Complex minus(Complex b) {
  62. Complex a = this;
  63. double real = a.re - b.re;
  64. double imag = a.im - b.im;
  65. return new Complex(real, imag);
  66. }
  67. // return a new Complex object whose value is (this * b)
  68. public Complex times(Complex b) {
  69. Complex a = this;
  70. double real = a.re * b.re - a.im * b.im;
  71. double imag = a.re * b.im + a.im * b.re;
  72. return new Complex(real, imag);
  73. }
  74. // return a new object whose value is (this * alpha)
  75. public Complex scale(double alpha) {
  76. return new Complex(alpha * re, alpha * im);
  77. }
  78. // return a new Complex object whose value is the conjugate of this
  79. public Complex conjugate() {
  80. return new Complex(re, -im);
  81. }
  82. // return a new Complex object whose value is the reciprocal of this
  83. public Complex reciprocal() {
  84. double scale = re*re + im*im;
  85. return new Complex(re / scale, -im / scale);
  86. }
  87. // return the real or imaginary part
  88. public double re() { return re; }
  89. public double im() { return im; }
  90. // return a / b
  91. public Complex divides(Complex b) {
  92. Complex a = this;
  93. return a.times(b.reciprocal());
  94. }
  95. // return a new Complex object whose value is the complex exponential of this
  96. public Complex exp() {
  97. return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
  98. }
  99. // return a new Complex object whose value is the complex sine of this
  100. public Complex sin() {
  101. return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
  102. }
  103. // return a new Complex object whose value is the complex cosine of this
  104. public Complex cos() {
  105. return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
  106. }
  107. // return a new Complex object whose value is the complex tangent of this
  108. public Complex tan() {
  109. return sin().divides(cos());
  110. }
  111. // a static version of plus
  112. public static Complex plus(Complex a, Complex b) {
  113. double real = a.re + b.re;
  114. double imag = a.im + b.im;
  115. Complex sum = new Complex(real, imag);
  116. return sum;
  117. }
  118. // See Section 3.3.
  119. public boolean equals(Object x) {
  120. if (x == null) return false;
  121. if (this.getClass() != x.getClass()) return false;
  122. Complex that = (Complex) x;
  123. return (this.re == that.re) && (this.im == that.im);
  124. }
  125. }