How to use the mathjs.add 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 javascript-machine-learning / linear-algebra-matrix / index.js View on Github external
const matrixA = math.matrix([[0, 1], [2, 3], [4, 5]]);
const matrixB = math.matrix([[0], [1], [2]]);

console.log(`Matrix A Dimension: ${matrixA.size()[0]}x${matrixA.size()[1]}`);
console.log(`Is vector: ${matrixA.size()[1] === 1}`);

console.log(`Matrix B Dimension: ${matrixB.size()[0]}x${matrixB.size()[1]}`);
console.log(`Is vector: ${matrixB.size()[1] === 1}`);
console.log('\n');

// Matrix Addition

const matrixC = math.matrix([[0, 1], [2, 3], [4, -5]]);
const matrixD = math.matrix([[1, -1], [-2, 4], [-7, 4]]);

const matrixAdditionCD = math.add(matrixC, matrixD);

console.log('Matrix Addition:');
console.log(matrixAdditionCD.valueOf());
console.log('\n');

// Matrix Subtraction

const matrixE = math.matrix([[0, 1], [2, 3], [4, -5]]);
const matrixF = math.matrix([[1, -1], [-2, 4], [-7, 4]]);

const matrixAdditionEF = math.subtract(matrixE, matrixF);

console.log('Matrix Subtraction:');
console.log(matrixAdditionEF.valueOf());
console.log('\n');
github uccser / cs-field-guide / csfieldguide / static / interactives / matrix-simplifier / js / matrix-simplifier.js View on Github external
function addVectors(v) {
  var add = true;
  while (add) {
    if (v.length == 2) {
      add = false;
      return mathjs.add(v[0], v[1]);
    } else {
      result = mathjs.add(v[0], v[1]);
      // remove the first vector in array
      v.shift();
      // replace the new first element in array with result
      v[0] = result;
    }
  }
}
github quantastica / quantum-circuit / lib / quantum-circuit.js View on Github external
if(uval) {
					var row = rowmask;
					var col = colmask;

					var counter = unusedSpace;
					var countermask = getElMask(0);
					var incmask = getIncMask();
					var notmask = getNotMask();
					var toothless = countermask;
					while(counter--) {

						var state = this.state[col];
						if(state) {
							row = toothless | rowmask;
							if(uval == 1) {
								newState[row] = math.add(newState[row] || ZERO, state);
							} else {
								newState[row] = math.add(newState[row] || ZERO, math.multiply(uval, state));
							}
							newStateBits |= row;
						}

						toothless = (toothless + incmask) & notmask;
						col = toothless | colmask;
					}
				}
			}
		}
	}
	// replace current state with new state
	this.state = newState;
	this.stateBits = newStateBits;
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`;
github trungdq88 / smart-doge / src / pages / calc.js View on Github external
function calcResult(params) {
  let result = 0;
  const nextIndex = params.length + 1;
  for (let i = 0; i < params.length; i++) {
    result = math.add(
     math.fraction(result),
     math.fraction(
       math.multiply(
         math.fraction(params[i]),
         math.fraction(
           math.pow(nextIndex, params.length - i - 1)
         )
       )
     )
    );
  }

  return result;
}
github antoniodeluca / dn2a / assets / networks / alpha.js View on Github external
function(totalOutputError, layerNeuron) {
                        return add(
                            totalOutputError,
                            square(layerNeuron.outputError)
                        );
                    },
                    bignumber(0)
github antoniodeluca / dn2a / assets / networks / alpha.js View on Github external
function(
                                synapse,
                                synapseIndex,
                                synapses
                            ) {
                                synapse.previousWeightChange = synapse.weightChange;
                                synapse.weightChange = add(
                                    multiply(
                                        bignumber(learningRate),
                                        multiply(
                                            layerNeuron.delta,
                                            synapse.incomingConnection.output
                                        )
                                    ),
                                    multiply(
                                        bignumber(momentumRate),
                                        synapse.previousWeightChange
                                    )
                                );
                                synapse.previousWeight = synapse.weight;
                                synapse.weight = add(
                                    synapse.weight,
                                    synapse.weightChange
github soswow / Various-JS-and-Python / JS / evolution-strategies / nes.js View on Github external
const main = () => {
    let G = null;
    for (let i = 0; i < 3; i++) {
        if (G === null) {
            G = randomG();
        } else {
            G = math.add(G, randomG());
        }
    }
    for (let i = 0; i < 3; i++) {
        G = math.subtract(G, randomG());
    }

    const alpha = learningRate;
    const sigma = samplesArea;
    let w = [math.randomInt(W - sigma * 4) + sigma * 2, math.randomInt(H - sigma * 4) + sigma * 2];

    const points = [];
    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]));

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