Complex.java 4.6 KB

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