Refactor / optimize for loop for interpolations

This commit is contained in:
Pitchaya Boonsarngsuk
2018-01-31 16:05:36 +00:00
parent 28c27df551
commit f6c151a761
2 changed files with 28 additions and 14 deletions

View File

@@ -14,21 +14,26 @@ import {placeNearToNearestNeighbour} from "./interpCommon";
* @param {number} endingIts - for phase 3, how many iterations to refine the * @param {number} endingIts - for phase 3, how many iterations to refine the
* placement of each interpolated point * placement of each interpolated point
*/ */
export default function(sampleSet, remainderSet, distanceFn) { export default function(sampleSet, remainderSet, distanceFn, endingIts) {
let sampleSubset = takeSampleFrom(sampleSet, Math.sqrt(sampleSet.length)).sample; let sampleSubset = takeSampleFrom(sampleSet, Math.sqrt(sampleSet.length)).sample;
for (let node of remainderSet) { // For each datapoint "node" to be interpolated
let nearestSample, minDist, for (let i = remainderSet.length-1; i>=0; i--) {
sampleSubsetDistanceCache = []; let
node = remainderSet[i],
sampleSubsetDistanceCache = [],
nearestSample, minDist, sample, dist, index;
for (let sample of sampleSet) { // For each datapoint "sample" in the sample set
let dist = distanceFn(node, sample); for (let j = sampleSet.length-1; j>=0; j--) {
sample = sampleSet[j];
dist = distanceFn(node, sample);
if (nearestSample === undefined || dist < minDist) { if (nearestSample === undefined || dist < minDist) {
minDist = dist; minDist = dist;
nearestSample = sample; nearestSample = sample;
} }
let index = sampleSubset.indexOf(sample); index = sampleSubset.indexOf(sample);
if (index !== -1) if (index !== -1)
sampleSubsetDistanceCache[index] = dist; sampleSubsetDistanceCache[index] = dist;
} }

View File

@@ -59,11 +59,11 @@ export function placeNearToNearestNeighbour(node, nearNeighbour, radius, sampleS
function sumForcesToSample(node, samples, sampleCache) { function sumForcesToSample(node, samples, sampleCache) {
let nodeVx = 0, let nodeVx = 0,
nodeVy = 0; nodeVy = 0,
x, y, l;
for (let i = 0; i < samples.length; i++) { for (let i = samples.length-1; i >=0 ; i--) {
var sample = samples[i]; let sample = samples[i];
if(sample === node) continue;
// jiggle so l won't be zero and divide by zero error after this // jiggle so l won't be zero and divide by zero error after this
x = node.x - sample.x || jiggle(); x = node.x - sample.x || jiggle();
@@ -78,6 +78,14 @@ function sumForcesToSample(node, samples, sampleCache) {
return {x: nodeVx, y: nodeVy}; return {x: nodeVx, y: nodeVy};
} }
/**
* Perform binary search on an integer range to find the value x where f(x) is minimum.
* This implies that f(x) should be binary-searchable over the specified range.
* @param {integer} lb - lower bound of the integer range, must be <= to hb
* @param {integer} hb - higher bound of the integer range
* @param {function(x)} fn - function that takes in a number x and returns a number
* @return {integer} - an integer x where f(x) is minimum
*/
function binarySearchMin(lb, hb, fn) { function binarySearchMin(lb, hb, fn) {
while (lb <= hb) { while (lb <= hb) {
if(lb === hb) return lb; if(lb === hb) return lb;
@@ -87,9 +95,10 @@ function binarySearchMin(lb, hb, fn) {
else return lb; else return lb;
} }
let range = hb-lb; let
let valLowerHalf = fn(lb + range/4); range = hb-lb,
let valHigherHalf = fn(lb + range*3/4); valLowerHalf = fn(lb + range/4),
valHigherHalf = fn(lb + range*3/4);
if (valLowerHalf > valHigherHalf) if (valLowerHalf > valHigherHalf)
lb = Math.floor((lb + hb) / 2); lb = Math.floor((lb + hb) / 2);