# How to use the mathjs.zeros function in mathjs

## To help you get started, we’ve selected a few mathjs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

birnbaum / rfvis / src / _compute_coordinates.js View on Github
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.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);

// 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 {
birnbaum / rfvis / src / _compute_coordinates.js View on Github
function computeForestMap({
forest,
rmax = 50,
rmin = 10,
width = 5,
N = 100
}) {
// some pre-calculations
const X = math.matrix(Array(N).fill(null).map(() =&gt; math.range(1, N + 1)));
const Y = math.transpose(X);

const treeStrengths = forest.trees.map(tree =&gt; 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 &lt; 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());
opentraffic / analyst-ui / src / app / route.js View on Github
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
let dataFromThisSegment = prepareDataForBarChart(tiles, date, id)
})
store.dispatch(setBarchartData(totalSpeedArray, totalPercentDiffArray, totalCountArray, totalRefSpeedArray))
const routeTime = getRouteTime(response)
store.dispatch(setTrafficRouteTime(routeTime))
}

// Now let's draw this
Qiskit / qiskit-js / packages / qiskit-sim / lib / gateTwo.js View on Github
module.exports = (gate, qubit0, qubit1, numQbits, state) =&gt; {
// eslint-disable-next-line no-bitwise
const enlargeOpt = math.zeros(1 &lt;&lt; numQbits, 1 &lt;&lt; numQbits);

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]));
opentraffic / analyst-ui / src / app / processing.js View on Github
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)
}
})
})
jupitex / sisyphe / src / worker / skeeft / lib / matrix.js View on Github
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;
};
opentraffic / analyst-ui / src / app / processing.js View on Github
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 {
yongjun21 / loess / src / index.js View on Github
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))
})
}
zjohn77 / retrieval / src / util / make_zeros.js View on Github
module.exports = function(len_) {
return math.zeros(len_, 1, 'sparse');
};
soswow / Various-JS-and-Python / JS / evolution-strategies / nes.js View on Github
const initRandomMatrix = (h, w) =>
math.map(math.zeros(h, w), () => math.random());

## mathjs

Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif

Apache-2.0

89 / 100