How to use the mathjs.range 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.

github josdejong / mathjs / examples / matrices.js View on Github external
// Matrix indexes are zero-based.
e.subset(math.index(1, [0, 1]), [[7, 8]])
print(e) // [[5, 6], [7, 8]]
const f = math.multiply(d, e)
print(f) // [[19, 22], [43, 50]]
const g = f.subset(math.index(1, 0))
print(g) // 43
console.log()

// get a sub matrix
// Matrix indexes are zero-based.
console.log('get a sub matrix')
const h = math.diag(math.range(1, 4))
print(h) // [[1, 0, 0], [0, 2, 0], [0, 0, 3]]
print(h.subset(math.index([1, 2], [1, 2]))) // [[2, 0], [0, 3]]
const i = math.range(1, 6)
print(i) // [1, 2, 3, 4, 5]
print(i.subset(math.index(math.range(1, 4)))) // [2, 3, 4]
console.log()

// resize a multi dimensional matrix
console.log('resizing a matrix')
const j = math.matrix()
let defaultValue = 0
j.resize([2, 2, 2], defaultValue)
print(j) // [[[0, 0], [0, 0]], [[0, 0], [0, 0]]]
print(j.size()) // [2, 2, 2]
j.resize([2, 2])
print(j) // [[0, 0], [0, 0]]
print(j.size()) // [2, 2]
console.log()
github josdejong / mathjs / examples / matrices.js View on Github external
k.subset(math.index(2), 6)
print(k) // [0, 0, 6]
console.log()

console.log('set a value outside a matrices range, setting other new entries to null')
const m = math.matrix()
defaultValue = null
m.subset(math.index(2), 6, defaultValue)
print(m) // [null, null, 6]
console.log()

// create ranges
console.log('create ranges')
print(math.range(1, 6)) // [1, 2, 3, 4, 5]
print(math.range(0, 18, 3)) // [0, 3, 6, 9, 12, 15]
print(math.range('2:-1:-3')) // [2, 1, 0, -1, -2]
print(math.factorial(math.range('1:6'))) // [1, 2, 6, 24, 120]
console.log()

/**
 * Helper function to output a value in the console. Value will be formatted.
 * @param {*} value
 */
function print (value) {
  const precision = 14
  console.log(math.format(value, precision))
}
github birnbaum / rfvis / src / _compute_coordinates.js View on Github external
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 {
            strength: tree.strength,
            x: coordinates[0][0],
            y: coordinates[1][0]
        };
    });
}
github babsey / nest-desktop / src / app / forms / param-random / param-random.component.ts View on Github external
line: {
          smoothing: 0.85
        },
        // colorscale: 'Gray',
        // reversescale: true,
        autocontour: false,
        contours: {
          coloring: 'heatmap',
        },
        colorbar: {
          title: 'PDF'
        }
      }];
      this.plot.layout['title'] = this.selectedParameterType.label + ' distribution';
    } else {
      x = math.range(xmin, xmax, dx);
      y = this._distributionService.pdf[value.parametertype](x._data, value.specs);
      if (this.functionType == 'cdf') { // || ['uniform', 'normal', 'lognormal'].includes(value.parametertype)) {
        var ysum = math.sum(y);
        y = y.map(yi => yi / ysum);
        y = this.cumsum(y);
      }
      this.plot.data = [{
        fill: 'tozeroy',
        type: 'scatter',
        x: x._data,
        y: y,
      }];
      this.plot.layout['title'] = this.functionType + ' of ' + this.selectedParameterType.label + ' distribution';
    }
    this.plot.layout['xaxis'] = {
      title: ['uniform', 'normal', 'lognormal'].includes(value.parametertype) ? 'value' : 'distance',
github babsey / nest-desktop / src / app / forms / param-random / param-random.component.ts View on Github external
var value = this.value;
    if (!this._distributionService.pdf.hasOwnProperty(value.parametertype)) return

    var dx = 0.001;
    var xmin: any = 0;
    var xmax: any = 1.;
    if (['uniform', 'normal', 'lognormal'].includes(value.parametertype)) {
      xmin = value.specs.min;
      xmax = value.specs.max;
    }
    var x: any;
    var y: any;
    var z: any;

    if (value.parametertype == 'gaussian2D') {
      x = math.range(-.5, .5, .01);
      y = math.range(-.5, .5, .01);
      z = this._distributionService.pdf[value.parametertype](x._data, y._data, value.specs);
      this.plot.data = [{
        type: 'contour', //'heatmap',
        x: x._data,
        y: y._data,
        z: z,
        line: {
          smoothing: 0.85
        },
        // colorscale: 'Gray',
        // reversescale: true,
        autocontour: false,
        contours: {
          coloring: 'heatmap',
        },
github babsey / nest-desktop / src / lib / controller / node / spike_dtime.js View on Github external
nodeController.spike_dtime = (node) => {
    if (node.model != 'spike_generator') return
    var nodeDefaults = app.config.nest('node');
    var nodeElem = $('#nodes').find('.node[data-id=' + node.id + '] .content');

    var spike_dtime = node.spike_dtime || nodeDefaults.spike_dtime.value;
    var spike_weight = node.spike_weight || nodeDefaults.spike_weight.value;
    node.params.spike_times = math.range(spike_dtime, app.data.sim_time, spike_dtime)._data;
    node.params.spike_weights = numeric.rep([node.params.spike_times.length], spike_weight);

    var options = nodeDefaults.spike_dtime;
    options.value = node.spike_dtime;
    app.slider.create_dataSlider('#nodes .node[data-id=' + node.id + '] .nodeSlider', options.id, options)
        .on('slideStop', (d) => {
            node.spike_dtime = d.value;
            node.params.spike_times = math.range(node.spike_dtime, app.data.sim_time, node.spike_dtime)._data;
            node.params.spike_weights = numeric.rep([node.params.spike_times.length], node.spike_weight || nodeDefaults.spike_weight.value);
            app.simulation.simulate.init()
        })
    nodeElem.find('#spike_dtimeVal').on('change', function() {
        var value = $(this).val();
        var schema = $(this).data('schema');
        var valid = app.validation.validate(value, schema);
        $(this).parents('.form-group').toggleClass('has-success', valid.error == null)
github babsey / nest-desktop / src / app / network / services / position.service.ts View on Github external
range(min: number, max: number, size: number): number[] {
    var step = (max - min) / size / 2;
    return math.range(min, max, step)['_data'].filter((v, i) => i % 2 == 1)
  }
github babsey / nest-desktop / src / lib / controller / node / amplitude.js View on Github external
amplitude.update = (node) => {
    var dtime = node.amplitude_dtime || 1;
    var amplitude = node.amplitude_dvalue || 1;
    var endTime = node.params.stop || app.data.sim_time;
    node.params.amplitude_times = math.range(dtime, endTime, dtime)._data;
    node.params.amplitude_values = app.math.cumsum(Array(node.params.amplitude_times.length).fill(amplitude));
    app.slider.update_nodeSlider(node)
}

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
Latest version published 8 days ago

Package Health Score

92 / 100
Full package analysis