How to use the multiply function from mathjs

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

The mathjs.multiply function returns the product of two numbers or matrices.

133
134
135
136
137
138
139
140
141
142
143


const calculateMarketPriceByTotal = (orderSize = 0, orders = []) =>
    orders.reduce(
        ([accumulatedPrice, accumulatedSize], [price = 0, size = 0]) => {
            if (math.larger(orderSize, accumulatedPrice)) {
                let currentTotal = math.multiply(size, price);
                const remainingSize = math.subtract(orderSize, accumulatedPrice);
                if (math.largerEq(remainingSize, currentTotal)) {
                    return [
                        math.sum(accumulatedPrice, currentTotal),
fork icon211
star icon236
watch icon0

+ 3 other calls in file

13517
13518
13519
13520
13521
13522
13523
13524
13525
13526

var theta = math.multiply(2, math.acos(alpha));
var phi = 0;

if(!(math.round(beta.re, 8) == 0 && math.round(beta.im, 8) == 0)) {
	phi = math.multiply(math.complex(0, -1), math.log(math.multiply(beta, math.csc(math.divide(theta, 2))))).re;
	if(isNaN(phi)) {
		phi = 0;
	}
}
fork icon43
star icon207
watch icon14

+ 947 other calls in file

How does mathjs.multiply work?

mathjs.multiply is a function that multiplies two or more numbers, matrices, or complex numbers and returns the result. If the inputs are matrices or complex numbers, it performs matrix multiplication or complex multiplication, respectively.

19
20
21
22
23
24
25
26
27
    let k = math.dot(p0,n) / math.dot(v,n);
    
    let pt = v.map(x => x*k);
        //let pt = v;

    let coord = math.multiply(mtx, pt);

    return { x: coord[0]*scale, y: coord[1]*scale};
}
fork icon48
star icon115
watch icon18

+ 15 other calls in file

4
5
6
7
8
9
10
11
12
13

const cadd = math.sum;
const csub = math.subtract;
const cdiv = math.divide;
const cmul = math.multiply;
const cmul_i = z => math.multiply(z, I);
const creciprocal = z => math.divide(1, z);
const csin = math.sin;
const cexp = math.exp;
const clog = math.log;
fork icon8
star icon29
watch icon3

+ 7 other calls in file

Ai Example

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

const result = math.multiply(3, 4);
console.log(result); // Output: 12

In this example, we import the mathjs library and use the math.multiply function to multiply two numbers 3 and 4. The result is 12, which is logged to the console.

15
16
17
18
19
20
21
22
23
24
    'tau': 2 * Math.PI,
    'phi': (1 + Math.sqrt(5))/2,
}

function fract(z) {return math.complex(z.re - Math.floor(z.re), z.im - Math.floor(z.im));}
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 = {
fork icon8
star icon29
watch icon3

+ 13 other calls in file

206
207
208
209
210
211
212
213
214
215
  const pairs = this.getSingleQubitPairs(offset)
  for (let i = 0; i < pairs.length; i++) {
    const pair = pairs[i]
    const a = pair[0].amplitude,
          b = pair[1].amplitude
    const r = math.multiply(matrix, math.matrix([[a], [b]]))
    this.amplitudes[pair[0].i] = r.subset(math.index(0, 0))
    this.amplitudes[pair[1].i] = r.subset(math.index(1, 0))
  }
}
fork icon6
star icon18
watch icon4

+ 9 other calls in file

2
3
4
5
6
7
8
9
10

console.log(Gate.s.matrix);
const circuit = Circuit.createCircuit(1);
circuit.addGate(Gate.x, 0, 0).addGate(Gate.s, 1, 0).run();

console.log(math.pow(math.e, math.multiply(math.i, math.PI / 2)));
console.log(math.i);

console.log(circuit.stateToString());
fork icon6
star icon14
watch icon2

+ 11 other calls in file

103
104
105
106
107
108
109
110
111
let W2 = W / 2;
let H2 = H / 2;

let coords = math.matrix([[-W2, W2, W2, -W2], [H2, H2, -H2, -H2]])

let res = math.multiply(rot, coords);

let X = res.subset(math.index(0, math.range(0, 4)));
let Y = res.subset(math.index(1, math.range(0, 4)));
fork icon0
star icon3
watch icon1

+ 13 other calls in file

139
140
141
142
143
144
145
146
147
148
const dif = math.subtract(urCorner, ulCorner);
const dist = math.norm(dif);

const maxSize = 100; // max size of the bigest side of the image
let ratio = maxSize / dist;
let scaledXYZ = math.multiply(xyzCam, ratio);
// Insert the computed coordinates in the collada template file
// ------------------------------------------------------------
// create the coordinate string for collada
scaledXYZ = math.transpose(scaledXYZ)
fork icon3
star icon4
watch icon0

95
96
97
98
99
100
101
102
103
104
        0
    ];
    vc[2] = Math.sqrt(l[2] * l[2] - vc[0] * vc[0] - vc[1] * vc[1]);

    // Convert to the Cartesian x, y, z - system
    var cell = mjs.multiply([va, vb, vc], [X, Y, Z]);

    return cell
}
module.exports.cellparToCell = cellparToCell;
fork icon2
star icon6
watch icon3

+ 3 other calls in file

0
1
2
3
4
5
6
7
8
9
const exports = module.exports = {};
const math = require('mathjs');

exports.colSum = function colSum(A) {
  const ones = math.ones(A.size()[0]);
  return math.multiply(math.transpose(A), ones);
};

exports.repmat = function repmat(row, n) {
  // Should really do ones(n, 1)*row but mathjs has very
fork icon2
star icon6
watch icon2

18
19
20
21
22
23
24
25
26
27
  index = math.index(math.range(0, nCols), j);
  col = matrix.subset(index);
  scale = norm(col, p);

  if (scale > 0) {
    matrix.subset(index, math.multiply(col, 1 / scale));
  }
}

return matrix;
fork icon4
star icon5
watch icon3

+ 7 other calls in file

1044
1045
1046
1047
1048
1049
1050
1051
1052
    }

    this.#values = this.#applyPrecision(multipliedValues, this.#precision);

    if (signal.units.values != "-") {
        let multipliedUnit = mathjs.multiply(mathjs.unit(this.units.values), mathjs.unit(signal.units.values));
        this.#units.values = multipliedUnit.toString();
    }
}
fork icon0
star icon3
watch icon0

+ 3 other calls in file

58
59
60
61
62
63
64
65
66
67
            console.log("enlarge bias = " + bias)
        }
    }
    if (!found) {
        var move_amount_energy = math.reshape(
            math.multiply(learning_rate, G), [N, 2])
        config.points = math.subtract(config.points, move_amount_energy)
        console.log("follow G")
    }
} else {
fork icon1
star icon1
watch icon7

+ 5 other calls in file

51
52
53
54
55
56
57
58
59
60
this.mc = m2.matrix([[1,                    0,                      0],
                     [0,                    Math.cos(this.tc),      -Math.sin(this.tc)],
                     [0,                    Math.sin(this.tc),      Math.cos(this.tc)]]);

//Compute the aggregate transformation matrix
this.matrix = m2.multiply(this.mc, m2.multiply(this.mb, this.ma));
this.invMatrix = m2.inv(this.matrix);


//Convert a standard lat/lng pair to a transformed coordinate
fork icon1
star icon1
watch icon4

+ 5 other calls in file

205
206
207
208
209
210
211
212
213
  return functions
}

doMagic(inputVector){

  let postInputsWeightsVectorA = Mathjs.multiply(this.inputWeightsA, inputVector);
  let postInputsWeightsVectorB = Mathjs.multiply(this.inputWeightsB, inputVector);

  let postInputsWeightsVector = Mathjs.add(postInputsWeightsVectorA, postInputsWeightsVectorB);
fork icon0
star icon1
watch icon1

+ 23 other calls in file

159
160
161
162
163
164
165
166
167
168
    else if(temp.indexOf('translate') !== -1){
        translateX = parseFloat(temp.substring(temp.indexOf('(')+1, temp.indexOf(',')));
        translateY = parseFloat(temp.substring(temp.indexOf(',')+1, temp.indexOf(')')));
    }
    var tempmat = math.matrix([[scaleX, 0, translateX], [0, scaleY, translateY], [0, 0, 1]]);
    transmat = math.multiply(transmat, tempmat);
}

if (viewBox.data !== undefined){
    var viewBoxScale = Math.min((width * 90) / viewBox.sx, (height * 90) / viewBox.sy);  //convert dimensions from inches to pixels, and scale coordinates to satisfy viewBox
fork icon0
star icon1
watch icon0

+ 2 other calls in file

16
17
18
19
20
21
22
23
24
                    .re.toFixed(PRECISION);
    return sum == 1;
}

tensor(otherKet) {
    let tensored = this.amplitudes.map(a => otherKet.amplitudes.map(b => math.multiply(a, b)))
                                  .reduce((v1, v2) => v1.concat(v2));
    return new Ket(tensored);
} 
fork icon2
star icon4
watch icon3

39
40
41
42
43
44
45
46
47
math.divide(a, b); // divide
math.exp(8) // exponent (e ** x)
math.log(a) // ln
math.log(a, 2) // log base 2
math.mod(a, b) // modulus (remainder)
math.multiply(a, b) // multiply
math.pow(a, b) // a ** b
math.sqrt(a) // square root
math.subtract(a, b) // subtract
fork icon0
star icon3
watch icon0

+ 6 other calls in file

17
18
19
20
21
22
23
24
25
26
    setValueX(X);
    console.log(X);
}

function check(A,ValueX){
    var checkX = math.multiply(A,ValueX);
    setcheckValue(checkX);
}

const [ValueX, setValueX] = useState([]);
fork icon0
star icon1
watch icon0