Skip to content

Commit c5da958

Browse files
committed
added poly helpers
1 parent 61b98e3 commit c5da958

File tree

7 files changed

+328
-567
lines changed

7 files changed

+328
-567
lines changed

.idea/workspace.xml

Lines changed: 216 additions & 491 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/tournament_tree/DistanceFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public P findWinner(P first, P second) {
2525
double firstDistance = p.getDistance(first);
2626
double secondDistance = p.getDistance(second);
2727
if (Math.abs(firstDistance - secondDistance) < 1e-10) System.out.println("FUCK");
28-
return firstDistance < secondDistance ? first : second;
28+
return firstDistance <= secondDistance ? first : second;
2929
}
3030

3131
@Override

src/main/java/tournament_tree/TournamentEvent.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
import kds.solvers.EigenSolver;
44
import org.ejml.data.Complex64F;
55
import utils.Primitive;
6-
import utils.RootFinder;
7-
8-
import java.util.ArrayList;
9-
import java.util.Collections;
10-
import java.util.NoSuchElementException;
6+
import utils.Polynomial;
117

128
/**
139
* Created by clausvium on 22/12/16.
1410
*/
15-
public class TournamentEvent<P extends Primitive> extends kds.Event<P>{
11+
public class TournamentEvent<P extends Primitive> extends kds.Event<P> {
1612

1713
private TournamentNode<P> node;
1814
private TournamentTreeWinner<P> winnerFunction;
@@ -60,33 +56,28 @@ public void computeFailureTime(EigenSolver solver, double t) {
6056
double[] p2_coeffs = getDistCoeffs(winnerFunction.getP().getCoeffsX(), winnerFunction.getP().getCoeffsY(),
6157
bCoeffsX, bCoeffsY);
6258

63-
double[] coeffs = new double[p1_coeffs.length];
64-
65-
for (int i = 0; i < p1_coeffs.length; ++i) {
66-
coeffs[i] = p1_coeffs[i] - p2_coeffs[i];
67-
}
59+
double[] coeffs = Polynomial.subtract(p1_coeffs, p2_coeffs);
6860

6961
try {
70-
double failureTime = RootFinder.findFirstRoot(coeffs, t, this.inFailedEvent);
62+
double failureTime = Polynomial.findFirstRoot(coeffs, t, this.inFailedEvent);
7163
super.setFailureTime(failureTime);
72-
} catch (RootFinder.NoRootException ex) {
64+
} catch (Polynomial.NoRootException ex) {
7365
// event is invalid
7466
this.setValid(false);
7567
super.setFailureTime(-1);
7668
}
7769
}
7870

7971
double[] getDistCoeffs(double[] p1_coeffsX, double[] p1_coeffsY, double[] p2_coeffsX, double[] p2_coeffsY) {
80-
double[] x = expand(p1_coeffsX, p2_coeffsX);
81-
double[] y = expand(p1_coeffsY, p2_coeffsY);
8272

83-
double[] res = new double[x.length];
73+
// dist^2 = (p2_x - p1_x)^2 + (p2_y - p1_y)^2
74+
double[] x = Polynomial.subtract(p1_coeffsX, p2_coeffsX);
75+
double[] y = Polynomial.subtract(p1_coeffsY, p2_coeffsY);
8476

85-
for (int i = 0; i < x.length; ++i) {
86-
res[i] = x[i] + y[i];
87-
}
77+
double[] x_squared = Polynomial.multiplication(x, x);
78+
double[] y_squared = Polynomial.multiplication(y, y);
8879

89-
return res;
80+
return Polynomial.add(x_squared, y_squared);
9081
}
9182

9283
double[] expand(double[] p1_coeffs, double[] p2_coeffs) {

src/main/java/tournament_tree/TournamentTree.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,10 @@ private void rebalance(double t, TournamentNode<P> tmp_node) {
301301
rotate_right(t, tmp_node.getRightChild());
302302
rotate_left(t, tmp_node.getLeftChild());
303303
}
304+
} else {
305+
// even if we don't rotate, we have to update the winner certificate
306+
updateWinner(t, tmp_node);
304307
}
305-
// even if we don't rotate, we have to update the winner certificate
306-
updateWinner(t, tmp_node);
307308

308309
tmp_node = tmp_node.getParent();
309310
}
@@ -374,7 +375,7 @@ public void rotate_left(double t, TournamentNode<P> node) {
374375

375376
// update the winners, we don't let it percolate though as there may be more rotations
376377
updateWinner(t, node);
377-
//updateWinner(t, newRoot);
378+
updateWinner(t, newRoot);
378379
}
379380

380381
public void rotate_right(double t, TournamentNode<P> node) {

src/main/java/tournament_tree/run.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static void main(String[] args) throws Exception {
2020
ArrayList<KDSPoint> leaves = new ArrayList<>();
2121
Random rand = new Random();
2222

23-
for (int i = 0; i < 10; ++i) {
23+
for (int i = 0; i < 2; ++i) {
2424
double[] coeffsX = new double[2];
2525
double[] coeffsY = new double[2];
2626
for (int j = 0; j < 2; ++j) {

src/main/java/utils/Polynomial.java

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package utils;
2+
3+
import kds.solvers.EigenSolver;
4+
import org.ejml.data.Complex64F;
5+
6+
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.NoSuchElementException;
9+
10+
/**
11+
* Created by clausvium on 02/01/17.
12+
*/
13+
public class Polynomial {
14+
15+
public static class NoRootException extends Exception {
16+
17+
}
18+
19+
public static double findFirstRoot(double[] coeffs, double t, boolean inFailedEvent) throws NoRootException {
20+
if (coeffs.length == 0) throw new NoRootException();
21+
22+
EigenSolver solver = new EigenSolver();
23+
Complex64F[] rootsX = solver.findRoots(coeffs, t);
24+
25+
ArrayList<Double> rootsXR = new ArrayList<>();
26+
27+
for (Complex64F r : rootsX) {
28+
if (Math.abs(r.getImaginary()) < 1e-10 && r.getReal() >= t) {
29+
rootsXR.add(Math.abs(r.getReal()));
30+
}
31+
}
32+
33+
if (inFailedEvent) {
34+
Collections.sort(rootsXR);
35+
36+
for (double r : rootsXR) {
37+
if (r-t > 1e-10) {
38+
return r;
39+
}
40+
}
41+
} else {
42+
try {
43+
return Collections.min(rootsXR);
44+
} catch (NoSuchElementException e) {
45+
// do nothing
46+
}
47+
}
48+
49+
throw new NoRootException();
50+
}
51+
52+
public static double[] multiplication(double[] p1, double[] p2) {
53+
int totalLength = p1.length + p2.length - 1;
54+
double[] result = new double[totalLength];
55+
for (int i = 0; i < p1.length; i++) {
56+
for (int j = 0; j < p2.length; j++) {
57+
result[i + j] += p1[i] * p2[j];
58+
}
59+
}
60+
61+
return result;
62+
}
63+
64+
public static double[] subtract(double[] p1, double[] p2) {
65+
int max_length = p1.length < p2.length ? p2.length : p1.length;
66+
double[] result = new double[max_length];
67+
68+
for (int i = 0; i < max_length; ++i) {
69+
if (i >= p1.length)
70+
result[i] = p2[i];
71+
else if (i >= p2.length)
72+
result[i] = -p1[i];
73+
else
74+
result[i] = p2[i] - p1[i];
75+
}
76+
77+
return result;
78+
}
79+
80+
public static double[] add(double[] p1, double[] p2) {
81+
int max_length = p1.length < p2.length ? p2.length : p1.length;
82+
double[] result = new double[max_length];
83+
84+
for (int i = 0; i < max_length; ++i) {
85+
if (i >= p1.length)
86+
result[i] = p2[i];
87+
else if (i >= p2.length)
88+
result[i] = p1[i];
89+
else
90+
result[i] = p2[i] + p1[i];
91+
}
92+
93+
return result;
94+
}
95+
}

src/main/java/utils/RootFinder.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)