How to use the matrix function from mathjs

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

4
5
6
7
8
9
10
11
12
13

console.log(math.multiply([[2, 3], [2, 1]], [4, 7]));

console.log(math.det([[5, 4], [3, 2]]));

var m1 = math.matrix([[1, 2, 0, 0], [0, 0, 3, 4], [0, 5, 0, 6]], 'sparse');

console.log(m1);

console.log(math.trace([[5, 4], [3, 6]]));
fork icon22
star icon35
watch icon8

14
15
16
17
18
19
20
21
22
23

  for (var i = 0; i < n; i++) {
    repMatrix.push(row._data);
  }

  return math.matrix(repMatrix);
};

_exports.rowMatrixToArray = function rowMatrixToArray(M) {
  var nbrCols = M.size()[1];
fork icon2
star icon6
watch icon2

45
46
47
48
49
50
51
52
53
54

```js
const mc = require('markov-clustering');
const math = require('mathjs');

const A = math.matrix([[0, 1, 1, 0, 0, 0],
                       [1, 0, 1, 0, 0, 0],
                       [1, 1, 0, 1, 0, 0],
                       [0, 0, 1, 0, 1, 1],
                       [0, 0, 0, 1, 0, 1],
fork icon2
star icon6
watch icon2

14
15
16
17
18
19
20
21
22
23
// Checks to if a matrix is unitary. Accepts a mathjs matrix or
// array, and returns true or false accordingly
const isUnitary = (matrix) => {
  // A Square matrix is unitary if its conjugate transpose is
  // equal to its inverse
  const givenMatrix = math.matrix(matrix, 'sparse');

  // Check if its Square
  if ((math.size(givenMatrix))[0] !== (math.size(givenMatrix))[1]) {
    return false;
fork icon1
star icon6
watch icon3

+ 5 other calls in file

16
17
18
19
20
21
22
23
24
25
// Checks to if a matrix is unitary. Accepts a mathjs matrix or
// array, and returns true or false accordingly
var isUnitary = function isUnitary(matrix) {
  // A Square matrix is unitary if its conjugate transpose is
  // equal to its inverse
  var givenMatrix = math.matrix(matrix, 'sparse');

  // Check if its Square
  if (math.size(givenMatrix)[0] !== math.size(givenMatrix)[1]) {
    return false;
fork icon1
star icon6
watch icon3

39
40
41
42
43
44
45
46
47
48
for (let i = 0; i < ncol; ++i) {
  replace_index.push(i);
}


let mat_JS = math.matrix();
let start: boolean = true;
let count: number = 0;
let mat_JS_row: number[] = [];
let row_count: number = 0;
fork icon0
star icon5
watch icon2

+ 3 other calls in file

96
97
98
99
100
101
102
103
104
sourceDimensions (W, H, rotation) {
    let r = (Math.PI / 180) * rotation;
    let c = Math.cos(r);
    let s = Math.sin(r);

    let rot = math.matrix([[c, -s], [s, c]]);

    let W2 = W / 2;
    let H2 = H / 2;
fork icon0
star icon3
watch icon1

+ 27 other calls in file

46
47
48
49
50
51
52
53
54
55

this.mb = m2.matrix([[Math.cos(this.ta),    0,                      -Math.sin(this.ta)],
                     [0,                    1,                      0],
                     [Math.sin(this.ta),    0,                      Math.cos(this.ta)]]);

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
fork icon1
star icon1
watch icon4

+ 5 other calls in file

23
24
25
26
27
28
29
30
31

if (Array.isArray(mean)) {
    mean = math.matrix(mean)
}
if (Array.isArray(std)) {
    std = math.matrix(std)
}

var gamma = math.dotDivide(math.subtract(mean, bestObjective), std);
fork icon0
star icon2
watch icon0

+ 7 other calls in file

40
41
42
43
44
45
46
47
48
49
// import all replacements into math.js, override existing trigonometric functions
mathjs.import(replacements, {override: true});
/////////////////////////////// End of adapted file ///////////////////////////////

// matrices and vector that will appear onload
m1 = mathjs.matrix([
  [mathjs.cos(45),0,mathjs.sin(45)],
  [0,1,0],
  [-mathjs.sin(45),0,mathjs.cos(45)]
]);
fork icon75
star icon0
watch icon0

+ 19 other calls in file

130
131
132
133
134
135
136
137
138
139

const urCorner = [imageCoordinates.ur[0], imageCoordinates.ur[1], imageCoordinates.ur[2]];
const ulCorner = [imageCoordinates.ul[0], imageCoordinates.ul[1], imageCoordinates.ul[2]];//imageCoordinates.ul;
const lrCorner = [imageCoordinates.lr[0], imageCoordinates.lr[1], imageCoordinates.lr[2]];//imageCoordinates.lr;
const llCorner = [imageCoordinates.ll[0], imageCoordinates.ll[1], imageCoordinates.ll[2]];//imageCoordinates.ll;
const xyzCam = math.matrix([urCorner, ulCorner, lrCorner, llCorner]);

// Scale the model
// ---------------
const dif = math.subtract(urCorner, ulCorner);
fork icon3
star icon4
watch icon0

12
13
14
15
16
17
18
19
20
21
}

math.config(config);

const bignumber = math.bignumber;
const matrix = math.matrix;

//const TRIANGLE_UNIT_HEIGHT = math.divide(math.sqrt(bignumber("3")), bignumber("2"));

const GRADIENTS = {
fork icon1
star icon0
watch icon1

3627
3628
3629
3630
3631
3632
3633
3634
3635
3636

try {
    const seed = [];
    let f = new h5wasm.File(file, "r");
    seed[0] = math.matrix(f.get('/ai/inputWeights', 'r').to_array());
    seed[1] = math.matrix(f.get('/ai/layersWeights', 'r').to_array());
    seed[2] = math.matrix(f.get('/ai/layersBias', 'r').to_array());
    seed[3] = math.matrix(f.get('/ai/outputWeights', 'r').to_array());
    seed[4] = math.matrix(f.get('/ai/outputBias', 'r').to_array());
    latestAI = new AI(seed, 0);
fork icon1
star icon2
watch icon2

+ 14 other calls in file

516
517
518
519
520
521
522
523
524
525
   // 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}
   // console.log("fX", fX);
   var y = target(X);
fork icon1
star icon0
watch icon0

+ 2 other calls in file

59
60
61
62
63
64
65
66
67
68
},
(grpc_req, grpc_res) => {
    // RPC callback for processing node - get result then callback to the block-mult handler
    console.log("Multiply Response recieved, block: " + res_id);

    let mat_res = math.matrix(grpc_res.matrix.values);
    mat_res = math.reshape(mat_res, [grpc_res.matrix.size, -1]);

    //* Debug: Log the incoming result from processing node
    //console.log(mat_res + "\n");
fork icon0
star icon1
watch icon0

+ 3 other calls in file

7
8
9
10
11
12
13
14
15
16
17
18
19
20
const math = require('mathjs');


const app = new Koa();
var router = new Router();


//const matrix = math.matrix([[0, 1], [2, 3], [4, 5]]);


const permutations = R.compose(R.sequence(R.of), R.flip(R.repeat));


let example =  [{"Sun":{"longitude":129.7486188}},{"Moon":{"longitude":321.2147636}},{"Mercury":{"longitude":119.051831}}];
fork icon0
star icon1
watch icon0

131
132
133
134
135
136
137
138
139
        }
        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);
    }
}
fork icon0
star icon1
watch icon0

+ 3 other calls in file

180
181
182
183
184
185
186
187
188
189
  }); // runs asynchronously since the file contents are only required after ~1 second (this.WINDOW_SIZE samples)
}

// helper functions
jStatToMathJS(o) {
  return math.matrix(o.slice());
}

lte(feature, max) {
  // checks if all channels are less than or equal to max
fork icon1
star icon0
watch icon0

441
442
443
444
445
446
447
448
449
450
    A.push(a1,a2);
    setcheckmatrix(A)
    var B = b.map(v=>[v]);
    console.log(A);
    console.log(B);
    var x0 = math.matrix([[0], [0]]);
    Calconjugate(math.matrix(A),math.matrix(B),x0);
}else if(Sizematrix == 3){
    a1.push(parseFloat(A11),parseFloat(A12),parseFloat(A13));
    a2.push(parseFloat(A21),parseFloat(A22),parseFloat(A23));
fork icon0
star icon1
watch icon0

312
313
314
315
316
317
318
319
320
321
if (!zRotio) zRotio += 0.0001;

const vector1Norm = [xRotio, yRotio, zRotio];

// 矩阵转换
let a = mathjs.matrix([
  [xRotio, yRotio, zRotio],
  [1, 0, 0],
  [0, 1, 0],
]);
fork icon0
star icon0
watch icon1

+ 19 other calls in file