How to use the mathjs.subtract 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 / basic_usage.js View on Github external
console.log()

// chained operations
console.log('chained operations')
const a = math.chain(3)
  .add(4)
  .multiply(2)
  .done()
print(a) // 14
console.log()

// mixed use of different data types in functions
console.log('mixed use of data types')
print(math.add(4, [5, 6])) // number + Array, [9, 10]
print(math.multiply(math.unit('5 mm'), 3)) // Unit * number,  15 mm
print(math.subtract([2, 3, 4], 5)) // Array - number, [-3, -2, -1]
print(math.add(math.matrix([2, 3]), [4, 5])) // Matrix + Array, [6, 8]
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 blocknetdx / block-dx / src / app / orderbook.component.ts View on Github external
updatePricingData() {
    const { pricingEnabled, pricingAvailable, pricing, symbols } = this;
    const [ section1, section2 ] = this.sections;
    const { rows: asks } = section1;
    const { rows: bids } = section2;

    if(asks.length > 0 && bids.length > 0 && pricingEnabled && pricingAvailable && pricing) {
      const askPrice = pricing.getPrice(asks[asks.length - 1][0], symbols[1]);
      const bidPrice = pricing.getPrice(bids[0][0], symbols[1]);
      this.pricingSpread = String(math.subtract(bignumber(askPrice), bignumber(bidPrice)).toNumber());
      // console.log('pricingSpread', this.pricingSpread);
    } else {
      this.pricingSpread = '';
    }

    // Update the pricing on bid/asks
    if (this.pricing) {
      this.sections[0].rows.forEach(ask => {
        ask[8] = this.pricing.getPrice(ask[0], this.symbols[1]);
      });
      this.sections[1].rows.forEach(bid => {
        bid[8] = this.pricing.getPrice(bid[0], this.symbols[1]);
      });
    }
  }
github SpatialTranscriptomicsResearch / st_spot_detector / client / src / app / viewer / graphics / line.js View on Github external
(x, y, line) => {
        /* eslint-disable no-multi-spaces, array-bracket-spacing, yoda */
        const v0 = math.subtract(
            [line.x1, line.y1],
            [line.x0, line.y0],
        );
        const v1 = math.subtract(
            [      x,       y],
            [line.x0, line.y0],
        );
        const c = math.dot(v0, v1) / math.dot(v0, v0);
        const v2 = math.subtract(v1, math.multiply(c, v0));
        return (0 <= c && c <= 1) &&
            math.dot(v2, v2) < (line.scale * line.lineWidth) ** 2;
    },
);
github hashgraph / hedera-mirror-node / hedera-mirror-rest / monitoring / monitor_apis / account_tests.js View on Github external
if (undefined === accounts) {
    var message = `accounts is undefined`;
    currentTestResult.failureMessages.push(message);
    acctestutils.addTestResult(classResults, currentTestResult, false);
    return;
  }

  if (accounts.length !== 1) {
    var message = `accounts.length of ${accounts.length} was expected to be 1`;
    currentTestResult.failureMessages.push(message);
    acctestutils.addTestResult(classResults, currentTestResult, false);
    return;
  }

  let plusOne = math.add(math.bignumber(accounts[0].balance.timestamp), math.bignumber(1));
  let minusOne = math.subtract(math.bignumber(accounts[0].balance.timestamp), math.bignumber(1));
  let paq = `${accountsPath}?timestamp=gt:${minusOne.toString()}` + `&timestamp=lt:${plusOne.toString()}&limit=1`;

  url = acctestutils.getUrl(server, paq);
  currentTestResult.url = url;
  accounts = await getAccounts(url, currentTestResult);

  if (undefined === accounts) {
    var message = `accounts is undefined`;
    currentTestResult.failureMessages.push(message);
    acctestutils.addTestResult(classResults, currentTestResult, false);
    return;
  }

  if (accounts.length !== 1) {
    var message = `accounts.length of ${accounts.length} was expected to be 1`;
    currentTestResult.failureMessages.push(message);
github soswow / Various-JS-and-Python / JS / evolution-strategies / nes.js View on Github external
const samplePoints = [];
    let minimumFound = false;
    while (!minimumFound) {
        const noise = math.add(math.multiply(initRandomMatrix(sampleSize, 2), 4), -2);
        const wp = math.add(math.dotMultiply(sigma, noise), math.multiply(math.ones(sampleSize, 1), [w]));

        samplePoints.push(wp.toArray());

        let R = wp.toArray().map(([x, y]) =>
            G.get(
                [
                    Math.round(clipValue(y, 0, H - 1)),
                    Math.round(clipValue(x, 0, W - 1))
                ])
        );
        R = math.subtract(R, math.mean(R));
        R = math.dotDivide(R, math.std(R));
        g = math.multiply([R], noise)

        const u = math.dotMultiply(g, alpha).toArray()[0];
        points.push(w);
        w = math.add(w, u);
        if (points.length > 5 && math.distance(points[points.length - 1], points[points.length - 6]) < 2) {
            minimumFound = true;
        }
    }

    let frameIndex = 0;
    renderMatrix(G);
    const frame = () => {
        clearContext();
        drawPoints(samplePoints[frameIndex]);
github devalpha-io / devalpha-node / lib / selectors / metrics.js View on Github external
(capitalHistory, positionsHistory) => {
    if (capitalHistory.size === 0 || positionsHistory.size === 0) {
      return 0
    }

    const totals = capitalHistory
      .zipWith((capital, positions) => [capital, positions], positionsHistory)
      .map(([capital, positions]) => calculateTotal(capital, positions))

    let maxDrawdown = 0
    let peak = -Infinity

    for (let i = 0; i < totals.size; i += 1) {
      const total = totals.get(i)
      if (total > peak) peak = total
      const drawdown = n(chain(subtract(b(peak), b(total))).divide(b(peak)).done())
      if (drawdown > maxDrawdown) {
        maxDrawdown = drawdown
      }
    }

    return maxDrawdown
  }
)
github antoniodeluca / dn2a / assets / networks / alpha.js View on Github external
layerNeurons.forEach(function(
                        layerNeuron,
                        layerNeuronIndex,
                        layerNeurons
                    ) {
                        layerNeuron.delta = chain(
                            layerNeuron.outputError
                        ).multiply(
                            layerNeuron.output
                        ).multiply(
                            subtract(
                                bignumber(1),
                                layerNeuron.output
                            )
                        ).done();
                    });
                } else {

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 1 day ago

Package Health Score

92 / 100
Full package analysis