How to use the add function from mathjs

Find comprehensive JavaScript mathjs.add code examples handpicked from public code repositorys.

mathjs.add is a function that adds two or more numbers or matrices together.

15
16
17
18
19
20
21
22
23
24
25
    orders.reduce((total, [, size]) => math.add(total, size), 0);


const sumOrderTotal = (orders) =>
    orders.reduce(
        (total, [price, size]) =>
            math.add(
                total,
                math.number(math.multiply(math.fraction(size), math.fraction(price)))
            ),
        0
fork icon211
star icon236
watch icon0

+ 3 other calls in file

13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
	while(base--) {
		var row = insertBit(base, qpos, el & 2 ? 1 : 0);
		if(this.state[row]) {
			var col = insertBit(base, qpos, el & 1 ? 1 : 0);
			if(this.state[col]) {
				trace[el] = math.add(trace[el], math.multiply(this.state[row], math.conj(this.state[col])));
			}
		}
	}
}
fork icon43
star icon207
watch icon14

+ 947 other calls in file

How does mathjs.add work?

mathjs.add is a function provided by the mathjs library that adds two or more numbers or arrays element-wise. When called with two or more arguments, it returns a new array with the sum of the corresponding elements of each input array. If called with a single argument that is an array, it returns the sum of all the elements in the array. If called with a single argument that is a number, it returns a function that takes another number as input and returns the sum of the two numbers.

81
82
83
84
85
86
87
88
89
90
if (!/(.*)%/.test(val)) {
  this.$message('格式不正确')
} else {
  let count = 0
  this.fans.forEach((element: any) => {
    count = math.add(math.multiply(Number(element.send.split('%')[0]), 0.01), count)
  })
  count = strip(count)
  if (count !== 1) {
    this.$message('百分比合计不为100%')
fork icon9
star icon78
watch icon2

+ 7 other calls in file

69
70
71
72
73
74
75
76
77
    ));
}

function eta_left(w) {
    const z = w.neg();
    const zp1 = math.add(z, 1);

    if (z.im < 0) {return math.conj(eta_left(math.conj(w)));}

fork icon8
star icon29
watch icon3

+ 3 other calls in file

Ai Example

1
2
3
4
5
const math = require("mathjs");

const result = math.add(2, 3);

console.log(result); // 5

In this example, mathjs.add is used to add the numbers 2 and 3, and the result is stored in the result variable. The console.log statement then outputs the result to the console.

20
21
22
23
24
25
26
27
28
29
const mod =  (z, w) => math.multiply(w, fract(math.divide(z, w)));
const add4 = (a, b, c, d) => math.add(math.add(a, b), math.add(c, d));

const I = math.complex(0, 1);
const fns = {
    add8: (a, b, c, d, e, f, g, h) => math.add(add4(a, b, c, d), add4(e, f, g, h)),
    add4,
    mul4: (a, b, c, d) => math.multiply(math.multiply(a, b), math.multiply(c, d)),
    rawpow: math.pow,
    sub: math.subtract,
fork icon8
star icon29
watch icon3

+ 13 other calls in file

42
43
44
45
46
47
48
49
50
51
  return math.pow(A, factor);
};

exports.addDiagonal = function addDiagonal(A, value) {
  const diag = math.eye(A.size());
  return math.add(A, math.multiply(value, diag));
};

/* A is a square matrix */
exports.getClusters = function getClusters(A) {
fork icon2
star icon6
watch icon2

60
61
62
63
64
65
66
67
68
69
var p1 = maths.sqrt(maths.chain(q)
              .dotMultiply(q)
              .add(maths.dotMultiply(j2,omega[i]*Math.PI*4.0e-7*this.mr1*this.con1))
              .done());

var qp1a = maths.add(maths.dotMultiply(q, this.mr1), p1);
var qp1s = maths.subtract(maths.dotMultiply(q, this.mr1), p1);

var p1p2a = maths.add(maths.dotMultiply(p1, this.mr1), maths.dotMultiply(p2, this.mr1));
var p1p2s = maths.subtract(maths.dotMultiply(p1, this.mr1), maths.dotMultiply(p2, this.mr1));
fork icon2
star icon3
watch icon0

+ 3 other calls in file

41
42
43
44
45
46
47
48
49


  getDiscountedReturn(rewards) {
    const discountedRewards = rewards.map(
      (reward, index) => Math.pow(this.environment.gamma, index) * reward
    )
    return math.add(0, ...discountedRewards) // mathjs gets mad if you only pass one value
  }
}
fork icon1
star icon3
watch icon0

174
175
176
177
178
179
180
181
182
183
                        const d = math.subtract(valueGE, valueLE);
                        const p = math.divide(math.subtract(targetValue, valueLE), d);
                        // console.log({d, p})
                        // console.log(math.multiply(math.subtract(1, p), volumeLE))
                        // console.log(math.multiply(p, volumeGE))
                        item.volume = math.add(math.multiply(math.subtract(1, p), volumeLE), math.multiply(p, volumeGE));
                }
                // console.log({spec, dataLE, dataGE, volume: item.volume})
        }
}
fork icon0
star icon9
watch icon3

31
32
33
34
35
36
37
38
39
40
const b = 4;

const numbers = [1, 2, 3, 4, 5]

// arithmetic
math.add(a, b); // add
math.cube(a); // cube
math.cbrt(8); // cube root
math.divide(a, b); // divide
math.exp(8) // exponent (e ** x)
fork icon0
star icon3
watch icon1

+ 2 other calls in file

3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
    //Iterate over each and every weight and bias and add mutate * Math.random() to each
    this.inputWeights  = math.add(this.inputWeights,  math.random([2000, 1000],     -mutate, mutate));
    this.layersWeights = math.add(this.layersWeights, math.random([20, 1000, 2000], -mutate, mutate));
    this.layersBias    = math.add(this.layersBias,    math.random([21, 1000],       -mutate, mutate));
    this.outputWeights = math.add(this.outputWeights, math.random([14, 1000],       -mutate, mutate));
    this.outputBias    = math.add(this.outputBias,    math.random([14],             -mutate, mutate));
}

this.evaluate = (inputs, output) => {
    let currentRow = math.add(math.multiply(inputs, this.inputWeights), this.layersBias[0]);
fork icon1
star icon2
watch icon2

+ 41 other calls in file

244
245
246
247
248
249
250
251
252
253
        x2o.push(x2);
        x3o.push(x3);
        x4o.push(x4);
    }
    console.log(obj);
    p = math.add(r, math.multiply(rsnew / rsold, p));
    rsold = rsnew;
}while(er > e);
console.log(iter,"Iteration");
console.log(math.flatten(x));
fork icon0
star icon1
watch icon0

42
43
44
45
46
47
48
49
50
51
console.log(re);
i = 0;
err = tol + 1; 
z = [i,x[0],x[1],x[2],err];
while(err > tol && i<iter){
	xi = mathjs.add(mathjs.multiply(T,x),C);
	console.log(xi);
	err = mathjs.norm(xi - x);
	x = xi;
}
fork icon1
star icon0
watch icon0

55
56
57
58
59
60
61
62
63
64
let i = 0;
let err = tol + 1; 
   iteration.push([i,x[0],x[1],x[2]]);

while(err > tol && i<iter){
	let xi = mathjs.add(mathjs.multiply(T,x),C);
	err = mathjs.norm(mathjs.subtract(xi,x));
	x = xi;
	i=i+1;
	iteration.push([i,x[0],x[1],x[2],err]);
fork icon1
star icon0
watch icon0

514
515
516
517
518
519
520
521
522
523
   // notice using math.js evaluate we can use +, -
   // for elementwise addition, subtraction, but .* and .^
   // for elementwise multiplication and power. (but the function
   // would be called dotMultiply)
   function target(x) {
return math.add(x, math.dotPow(x, 2), math.dotPow(x, 3), math.dotPow(x, 4));
   }
   var X = math.matrix([[0.0], [0.1], [0.2], [0.3], [0.4], [0.5]]);
   // console.log("s", s);
   var fX = math.evaluate(s, {"x0": X}); // could use  {"x0": X[0], etc if needed}
fork icon1
star icon0
watch icon0

+ 2 other calls in file

133
134
135
136
137
138
139
140
141
142
  
  // // Returns moving average of the electrical band
  // if (this.electricalBandMovingAverage == null) {
  //   this.electricalBandMovingAverage = electricalBand;
  // } else {
  //   this.electricalBandMovingAverage = math.add(math.multiply(this.ELECTRICAL_BAND_MOVING_AVERAGE_CONSTANT, this.electricalBandMovingAverage), 
  //     math.multiply(1-this.ELECTRICAL_BAND_MOVING_AVERAGE_CONSTANT, electricalBand));
  // }
  // return this.electricalBandMovingAverage
}
fork icon1
star icon0
watch icon0

42
43
44
45
46
47
48
49
50
51
        console.log("time series differs");
}
if (typeof yout === 'undefined'){
        yout = yvals;
} else {
        yout = math.add(yout, yvals);
}
}
catch (TypeError) {
        console.log(county, age, " not supported");
fork icon0
star icon1
watch icon4

+ 9 other calls in file

131
132
133
134
135
136
137
138
139
140
  let layerRow = layerColumns[j] = new Array(this.inputSize);
  for (var k = 0; k < layerRow.length; k++) {
    layerRow[k] = func(this.XBounds[j]) * funY(this.YBounds[k]);
  }
}
let funkyWeights = Mathjs.add(this.funkyWeightsA, this.funkyWeightsB);
let temp = Mathjs.multiply(Mathjs.matrix(layerColumns), funkyWeights);
temp = temp.map(function(value, index, matrix){
    let result = 1.0/(1.0 + Mathjs.exp(-1 + value));
    result-=0.5;
fork icon0
star icon1
watch icon1

+ 31 other calls in file

66
67
68
69
70
71
72
73
74
75
var h = math.divide(math.add(b, -a), math.multiply(2, M));
var sum1 = 0;
var sum2 = 0;
for(var k = 1; k <= M; ++k) {
        var x = math.add(a, math.multiply(h, 2 * k - 1));
        sum1 = math.add(sum1, f(x));
        if(k != M) {
                x = math.add(a, math.multiply(h, 2 * k));
                sum2 = math.add(sum2, f(x));
        }
fork icon0
star icon1
watch icon13

+ 103 other calls in file

87
88
89
90
91
92
93
94
95
96
let k2 = Object.keys(b);
let intersect = this.intersect(k1,k2);
let outdict = {};
for (let ob of intersect){
        if (!fixed.includes(ob)){
                outdict[ob] = math.add(a[ob], b[ob]);
        }
        else {
                outdict[ob] = a[ob];
        }
fork icon0
star icon1
watch icon4