-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex.java
More file actions
63 lines (52 loc) · 1.3 KB
/
Copy pathComplex.java
File metadata and controls
63 lines (52 loc) · 1.3 KB
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
public class Complex
{
private final double re;
private final double im;
public Complex(double real, double imag)
{re = real; im = imag; }
public Complex plus(Complex b)
{ //return the sum of this number and b
double real = re + b.re;
double imag = im + b.im;
return new Complex(real, imag);
}
public Complex minus(Complex b)
{ //return the sum of this number and b
double real = re - b.re;
double imag = im - b.im;
return new Complex(real, imag);
}
public Complex times(Complex b)
{ //return the product of this number and b
double real = re * b.re - im * b.im;
double imag = re * b.im + im * b.re;
return new Complex(real, imag);
}
public Complex reciprocal()
{
double scale = re*re + im*im;
return new Complex(re / scale, -im / scale);
}
public Complex division(Complex b)
{
return this.times(b.reciprocal());
}
public double abs()
{ return Math.sqrt(re*re +im*im);}
public double re()
{return re; }
public double im()
{return im; }
public String toString()
{
return re + " + " + im + "i";
}
public static void main(String[] args)
{
Complex z0 = new Complex(1.0, 1.0);
Complex z = z0;
z = z.times(z).plus(z0);
z = z.times(z).plus(z0);
System.out.println(z);
}
}