Normand Briere
2019-10-04 57c5b6cd8d12ffdaa3e0b099451e3c031012750a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
 
import java.lang.Math.*;
 
/**
 * Class representing a complex number.
 *
 * @author Simon Horne.
 */
public class ComplexNumber{
  /**
   * The real part of the complex number.
   */
  public double real;
  /** 
   * The imaginary part of the complex number.
   */
  public double imaginary;
 
  /**
   * Default no-arg constructor.
   */
  public ComplexNumber(){
  }
 
  /**
   * Constrctor taking two doubles, the real and the imaginary values.
   *
   * @param r A double representing the real value.
   * @param i A double representing the imaginary value.
   */
  public ComplexNumber(double r, double i){
    real = r;
    imaginary = i;
  }
 
  /**
   * Constructor taking a ComplexNumber to make an identical copy.
   *
   * @param c ComplexNumber to be copied.
   */
  public ComplexNumber(ComplexNumber c){
    real = c.real;
    imaginary = c.imaginary;
  }
 
  /**
   * Method to obtain the magnitude of the complex number.
   *
   * @return double representing the magnitude.
   */ 
  public double magnitude(){
    return Math.sqrt(this.cNorm());
  }
 
  /**
   * Method to obtain the phase angle (in radians) of the complex number.
   *
   * @return double representing the phase angle in radians.
   */ 
  public double phaseAngle(){
    if(real==0 && imaginary == 0) return 0;
    else return Math.atan(imaginary/real);
  }
 
  double cNorm (){
    return real*real + imaginary*imaginary;
  }
 
  /**
   * Method to obtain the exp of a complex number.
   *
   * @param ComplexNumber input.
   * @return ComplexNumber output.
   */
  public static ComplexNumber cExp (ComplexNumber z){
    ComplexNumber x,y;
    x = new ComplexNumber(Math.exp(z.real),0.0);
    y = new ComplexNumber(Math.cos(z.imaginary),Math.sin(z.imaginary));
    return cMult (x,y);
  }
 
  /**
   * Method to multiply two complex numbers together.
   *
   * @param z1 ComplexNumber.
   * @param z2 ComlexNumber.
   * @return z3 ComplexNumber z3 = z1 * z2.
   */
  public static ComplexNumber cMult (ComplexNumber z1, ComplexNumber z2){
    ComplexNumber z3 = new ComplexNumber();
    z3.real = (z1.real)*(z2.real) - (z1.imaginary)*(z2.imaginary);
    z3.imaginary = (z1.real)*(z2.imaginary) + (z1.imaginary)*(z2.real);
    return z3;
  }
 
  /** 
   * Method to add two complex numbers together.
   *
   * @param z1 ComplexNumber.
   * @param z2 ComlexNumber.
   * @return z3 ComplexNumber z3 = z1 + z2.
   */
  public static ComplexNumber cSum (ComplexNumber z1, ComplexNumber z2){
    ComplexNumber z3 = new ComplexNumber();
    z3.real = z1.real + z2.real;
    z3.imaginary = z1.imaginary + z2.imaginary;
    return z3;
  }
 
  /** 
   * Method to divide a complex number by another.
   *
   * @param z1 ComplexNumber.
   * @param z2 ComlexNumber.
   * @return z3 ComplexNumber z3 = z1 / z2.
   */
  public static ComplexNumber cDiv (ComplexNumber z1, ComplexNumber z2){
    ComplexNumber z3 = new ComplexNumber();
    double n = z2.cNorm();
    
    z3.real = ((z1.real*z2.real) + (z1.imaginary*z2.imaginary))/n;
    z3.imaginary = ((z2.real*z1.imaginary) - (z1.real*z2.imaginary))/n;
    return z3;
  }
 
  /** 
   * Method to subtract a complex number from another.
   *
   * @param z1 ComplexNumber.
   * @param z2 ComlexNumber.
   * @return z3 ComplexNumber z3 = z1 - z2.
   */
  public static ComplexNumber cDif (ComplexNumber z1, ComplexNumber z2){
    ComplexNumber z3 = new ComplexNumber();
 
    z3.real = z1.real - z2.real;
    z3.imaginary = z1.imaginary - z2.imaginary;
    return z3;
  }
 
}