Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (t === i) continue;
// transform correlation into distance
const distance = (1 - forest.correlationMatrix[i][t]) * rmax + rmin;
// produce voting image
const r = math.sqrt(
math.add(
math.dotPow(math.subtract(X, position.valueOf()[0][i]), 2),
math.dotPow(math.subtract(Y, position.valueOf()[1][i]), 2)
)
);
const votingImage = math.number(math.smaller(math.abs(math.subtract(r, distance)), width));
// Pad voting image with zeros to 120x120, so the convolution will return a 100x100 result
const paddedVotingImage = math.subset(math.zeros(N + 20, N + 20), math.index(math.range(10, N + 10), math.range(10, N + 10)), votingImage);
const tmp = math.matrix(nj.array(paddedVotingImage.valueOf()).convolve(GAUSSIAN_2D).tolist());
// vote
const oldVotingImage = math.subset(votingMatrices, math.index(math.range(0, N), math.range(0, N), t));
const newVotingImage = math.add(oldVotingImage, math.reshape(tmp, [N, N, 1]));
votingMatrices = math.subset(votingMatrices, math.index(math.range(0, N), math.range(0, N), t), newVotingImage)
}
// "delete" current tree from list
treeStrengths[i] = 0;
}
console.timeEnd('Computing forest map');
return forest.trees.map((tree, i) => {
const coordinates = math.subset(position, math.index([0, 1], i)).valueOf();
return {
function computeForestMap({
forest,
rmax = 50,
rmin = 10,
width = 5,
N = 100
}) {
// some pre-calculations
const X = math.matrix(Array(N).fill(null).map(() => math.range(1, N + 1)));
const Y = math.transpose(X);
const treeStrengths = forest.trees.map(tree => tree.strength);
// Init empty output matrix (x and y coordinates for each tree)
let position = math.zeros(2, treeStrengths.length);
// Init empty NxN voting spaces for each tree
let votingMatrices = math.zeros(N, N, treeStrengths.length);
console.time('Computing forest map');
for (let t1 = 0; t1 < treeStrengths.length; t1++) {
// find tree with current maximal strength
const i = argmax(treeStrengths);
// if its the first tree, define position as center
if (t1 === 0) {
position = math.subset(position, math.index([0, 1], i), [[N / 2], [N / 2]])
} else {
// else get current voting space of this tree
const tmp = math.subset(votingMatrices, math.index(math.range(0, N), math.range(0, N), i));
// Set the coordinates of the maximum index of the voting space
const pos = argmax(math.flatten(tmp).valueOf());
fetchDataTiles(parsedIds, date).then((tiles) => {
// TODO: when year and week aren't specified, we should also
// skip the step of trying to fetch data tiles
if (date.year && date.week) {
let totalPercentDiffArray = mathjs.zeros(7, 24)
let totalSpeedArray = mathjs.zeros(7, 24)
let totalRefSpeedArray = mathjs.zeros(7, 24)
let totalCountArray = mathjs.zeros(7, 24)
parsedIds.forEach((id) => {
// Will add either meaured or reference speed
addSpeedToMapGeometry(tiles, date, id, id)
let dataFromThisSegment = prepareDataForBarChart(tiles, date, id)
totalPercentDiffArray = mathjs.add(totalPercentDiffArray, dataFromThisSegment.percentDiff)
totalSpeedArray = mathjs.add(totalSpeedArray, dataFromThisSegment.speeds)
totalRefSpeedArray = mathjs.add(totalRefSpeedArray, dataFromThisSegment.refSpeeds)
totalCountArray = mathjs.add(totalCountArray, dataFromThisSegment.counts)
})
store.dispatch(setBarchartData(totalSpeedArray, totalPercentDiffArray, totalCountArray, totalRefSpeedArray))
const routeTime = getRouteTime(response)
store.dispatch(setTrafficRouteTime(routeTime))
}
// Now let's draw this
module.exports = (gate, qubit0, qubit1, numQbits, state) => {
// eslint-disable-next-line no-bitwise
const enlargeOpt = math.zeros(1 << numQbits, 1 << numQbits);
dbg('Starting "addUnitaryTwo" with opions', {
gate,
qubit0,
qubit1,
numQbits,
});
dbg('\n\nState before\n\n');
dbg(state.toString());
dbg('\n\nEnlarge before\n\n');
dbg(enlargeOpt.toString());
dbg('Position 0,0 before', enlargeOpt.get([0, 0]));
export function prepareDataForBarChart (tiles, date, segment) {
// not all levels and tiles are available yet, so try()
// skips it if it doesn't work
try {
const subtiles = tiles.historic[date.year][date.week][segment.level][segment.tileIdx]
const subtile = getSubtileForSegmentIdx(segment.segmentIdx, subtiles)
if (subtile) {
var percentDiffsByDayAndHourArray = mathjs.zeros(7, 24)
var speedsByDayAndHourArray = mathjs.zeros(7, 24)
var refSpeedsByDayAndHourArray = mathjs.zeros(7, 24)
var nonZeroSpeedCountByDayAndHourArray = mathjs.zeros(7, 24)
var speeds = getValuesFromSubtile(segment.segmentIdx, subtile, [0, 7], [0, 24], 'speeds')
const refSpeeds = getValuesFromSubtile(segment.segmentIdx, tiles.reference[segment.level][segment.tileIdx], [0, 7], [0, 24], 'speeds')
chunk(speeds, 24).forEach((speedsForThisDay, dayIndex) => {
speedsForThisDay.forEach((speedForThisHour, hourIndex) => {
if (speedForThisHour > 0) {
const refSpeed = refSpeeds[dayIndex * 24 + hourIndex]
const percentDiffForThisHour = (speedForThisHour - refSpeed) / refSpeed * 100
percentDiffsByDayAndHourArray.set([dayIndex, hourIndex], Number(percentDiffForThisHour.toFixed(2)))
speedsByDayAndHourArray.set([dayIndex, hourIndex], speedForThisHour)
refSpeedsByDayAndHourArray.set([dayIndex, hourIndex], refSpeed)
nonZeroSpeedCountByDayAndHourArray.set([dayIndex, hourIndex], 1)
}
})
})
self.fill = function(criterion) {
// Return the existing matrix
if (self.matrices[criterion]) {
return self.matrices[criterion];
}
let m = math.zeros(self.selectors.segments.length + 1, Object.keys(self.terms).length + 1),
size = m.size(),
segSize = size[0],
termSize = size[1];
// Fill the matrix with values of indexation
self.indexations.map((el, i) => {
m.set([self.segments[el.segment], self.terms[el.term]], (el[criterion]) ? el[criterion] : m.get([self.segments[el.segment], self.terms[el.term]]));
m.set([self.segments[el.segment], termSize - 1], m.get([self.segments[el.segment], termSize - 1]) + m.get([self.segments[el.segment], self.terms[el.term]]));
m.set([segSize - 1, self.terms[el.term]], m.get([segSize - 1, self.terms[el.term]]) + m.get([self.segments[el.segment], self.terms[el.term]]));
});
// Save the result
self.matrices[criterion] = m;
return m;
};
export function prepareDataForBarChart (tiles, date, segment) {
// not all levels and tiles are available yet, so try()
// skips it if it doesn't work
try {
const subtiles = tiles.historic[date.year][date.week][segment.level][segment.tileIdx]
const subtile = getSubtileForSegmentIdx(segment.segmentIdx, subtiles)
if (subtile) {
var percentDiffsByDayAndHourArray = mathjs.zeros(7, 24)
var speedsByDayAndHourArray = mathjs.zeros(7, 24)
var refSpeedsByDayAndHourArray = mathjs.zeros(7, 24)
var nonZeroSpeedCountByDayAndHourArray = mathjs.zeros(7, 24)
var speeds = getValuesFromSubtile(segment.segmentIdx, subtile, [0, 7], [0, 24], 'speeds')
const refSpeeds = getValuesFromSubtile(segment.segmentIdx, tiles.reference[segment.level][segment.tileIdx], [0, 7], [0, 24], 'speeds')
chunk(speeds, 24).forEach((speedsForThisDay, dayIndex) => {
speedsForThisDay.forEach((speedForThisHour, hourIndex) => {
if (speedForThisHour > 0) {
const refSpeed = refSpeeds[dayIndex * 24 + hourIndex]
const percentDiffForThisHour = (speedForThisHour - refSpeed) / refSpeed * 100
percentDiffsByDayAndHourArray.set([dayIndex, hourIndex], Number(percentDiffForThisHour.toFixed(2)))
speedsByDayAndHourArray.set([dayIndex, hourIndex], speedForThisHour)
refSpeedsByDayAndHourArray.set([dayIndex, hourIndex], refSpeed)
nonZeroSpeedCountByDayAndHourArray.set([dayIndex, hourIndex], 1)
}
})
})
return {
transpose(expandedX).forEach((point, idx) => {
const fit = weightedLeastSquare(this.expandedX, this.y, weights[idx])
if (fit.error) {
const mle = math.multiply(this.y, weights[idx]) / math.sum(weights[idx])
fit.beta = math.zeros(this.expandedX.length).set([0], mle)
fit.residual = math.subtract(this.y, mle)
}
fitted.push(math.squeeze(math.multiply(point, fit.beta)))
residuals.push(fit.residual)
betas.push(fit.beta.toArray())
const median = math.median(math.abs(fit.residual))
wt[idx] = fit.residual.map(r => weightFunc(r, 6 * median, 2))
})
}
module.exports = function(len_) {
return math.zeros(len_, 1, 'sparse');
};
const initRandomMatrix = (h, w) =>
math.map(math.zeros(h, w), () => math.random());