blob: 4a81e8a8ea8510572dd68cf285f021a219e1aa6b (
plain)
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
|
package bjc.utils.math;
/**
* Represents a 'dual' number.
*
* Think imaginary numbers, where instead of i, we add a value d such that d^2 =
* 0.
*/
public class Dual {
/**
* The real part of the dual number.
*/
public double real;
/**
* The dual part of the dual number.
*/
public double dual;
/**
* Create a new dual with both parts zero.
*/
public Dual() {
real = 0;
dual = 0;
}
/**
* Create a new dual number with a zero dual part.
*
* @param real
* The real part of the number.
*/
public Dual(double real) {
this.real = real;
this.dual = 0;
}
/**
* Create a new dual number with a specified dual part.
*
* @param real
* The real part of the number.
* @param dual
* The dual part of the number.
*/
public Dual(double real, double dual) {
this.real = real;
this.dual = dual;
}
@Override
public String toString() {
return String.format("<%f, %f>", real, dual);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(dual);
result = prime * result + (int) (temp ^ (temp >>> 32));
temp = Double.doubleToLongBits(real);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Dual other = (Dual) obj;
if (Double.doubleToLongBits(dual) != Double.doubleToLongBits(other.dual))
return false;
if (Double.doubleToLongBits(real) != Double.doubleToLongBits(other.real))
return false;
return true;
}
}
|