Refactor / optimize for loop for interpolations
This commit is contained in:
@@ -14,21 +14,26 @@ import {placeNearToNearestNeighbour} from "./interpCommon";
|
||||
* @param {number} endingIts - for phase 3, how many iterations to refine the
|
||||
* 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;
|
||||
|
||||
for (let node of remainderSet) {
|
||||
let nearestSample, minDist,
|
||||
sampleSubsetDistanceCache = [];
|
||||
// For each datapoint "node" to be interpolated
|
||||
for (let i = remainderSet.length-1; i>=0; i--) {
|
||||
let
|
||||
node = remainderSet[i],
|
||||
sampleSubsetDistanceCache = [],
|
||||
nearestSample, minDist, sample, dist, index;
|
||||
|
||||
for (let sample of sampleSet) {
|
||||
let dist = distanceFn(node, sample);
|
||||
// For each datapoint "sample" in the sample set
|
||||
for (let j = sampleSet.length-1; j>=0; j--) {
|
||||
sample = sampleSet[j];
|
||||
dist = distanceFn(node, sample);
|
||||
if (nearestSample === undefined || dist < minDist) {
|
||||
minDist = dist;
|
||||
nearestSample = sample;
|
||||
}
|
||||
|
||||
let index = sampleSubset.indexOf(sample);
|
||||
index = sampleSubset.indexOf(sample);
|
||||
if (index !== -1)
|
||||
sampleSubsetDistanceCache[index] = dist;
|
||||
}
|
||||
|
||||
@@ -59,11 +59,11 @@ export function placeNearToNearestNeighbour(node, nearNeighbour, radius, sampleS
|
||||
|
||||
function sumForcesToSample(node, samples, sampleCache) {
|
||||
let nodeVx = 0,
|
||||
nodeVy = 0;
|
||||
nodeVy = 0,
|
||||
x, y, l;
|
||||
|
||||
for (let i = 0; i < samples.length; i++) {
|
||||
var sample = samples[i];
|
||||
if(sample === node) continue;
|
||||
for (let i = samples.length-1; i >=0 ; i--) {
|
||||
let sample = samples[i];
|
||||
|
||||
// jiggle so l won't be zero and divide by zero error after this
|
||||
x = node.x - sample.x || jiggle();
|
||||
@@ -78,6 +78,14 @@ function sumForcesToSample(node, samples, sampleCache) {
|
||||
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) {
|
||||
while (lb <= hb) {
|
||||
if(lb === hb) return lb;
|
||||
@@ -87,9 +95,10 @@ function binarySearchMin(lb, hb, fn) {
|
||||
else return lb;
|
||||
}
|
||||
|
||||
let range = hb-lb;
|
||||
let valLowerHalf = fn(lb + range/4);
|
||||
let valHigherHalf = fn(lb + range*3/4);
|
||||
let
|
||||
range = hb-lb,
|
||||
valLowerHalf = fn(lb + range/4),
|
||||
valHigherHalf = fn(lb + range*3/4);
|
||||
|
||||
if (valLowerHalf > valHigherHalf)
|
||||
lb = Math.floor((lb + hb) / 2);
|
||||
|
||||
Reference in New Issue
Block a user