How to use the inv function from mathjs

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

mathjs.inv calculates the inverse of a matrix.

197
198
199
200
201
202
203
204
205
206
        }
    }
}

if (cell && !(this._cell.includes(null)))
    this._inv_cell = mjs.inv(this._cell);

// Check that the positions are the right size
var check_pos = (positions.length == this._N);
for (var i = 0; i < positions.length; ++i) {
fork icon2
star icon6
watch icon3

22
23
24
25
26
27
28
29
30
  if ((math.size(givenMatrix))[0] !== (math.size(givenMatrix))[1]) {
    return false;
  }

  const conjugateTranspose = math.transpose(math.conj(givenMatrix));
  const inverse = math.inv(givenMatrix);

  return math.deepEqual(inverse, conjugateTranspose);
};
fork icon1
star icon6
watch icon3

+ 5 other calls in file

How does mathjs.inv work?

The mathjs.inv function computes the inverse of a square matrix using Gaussian elimination with partial pivoting. It performs a sequence of operations on the input matrix in order to reduce it to the identity matrix (i.e., a matrix with ones on the diagonal and zeros elsewhere). The same sequence of operations is applied to the identity matrix in parallel to obtain the inverse. If the input matrix is not invertible, the function returns null.

24
25
26
27
28
29
30
31
32
  if (math.size(givenMatrix)[0] !== math.size(givenMatrix)[1]) {
    return false;
  }

  var conjugateTranspose = math.transpose(math.conj(givenMatrix));
  var inverse = math.inv(givenMatrix);

  return math.deepEqual(inverse, conjugateTranspose);
};
fork icon1
star icon6
watch icon3

52
53
54
55
56
57
58
59
60
61
                     [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
this.convert = function(lat, lng){
fork icon1
star icon1
watch icon4

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const math = require("mathjs");

const matrix = [
  [1, 2],
  [3, 4],
];

const inverseMatrix = math.inv(matrix);

console.log(inverseMatrix);
// Output: [
//           [-2, 1],
//           [1.5, -0.5]
//         ]

In this example, we're using mathjs to calculate the inverse of a 2x2 matrix. We first define our matrix as a 2D array of numbers. We then use the mathjs.inv function to calculate the inverse of the matrix, and store the result in the inverseMatrix variable. Finally, we log the result to the console.

617
618
619
620
621
622
623
624
625
626
627
628
}


// Invert the matrix
// Time inversion using mathjs
let start = new Date().getTime();
let inverseMathjs = mathjs.inv(matrix);
let end = new Date().getTime();
console.log(`Mathjs inversion took ${end - start} ms for a ${N}x${N} matrix`);


// Time inversion using my simple code (seems to have same performance as mathjs)
fork icon0
star icon0
watch icon1

+ 444 other calls in file

622
623
624
625
626
627
628
629
630
631
632
633
634




// // Invert the matrix
// // Time inversion using mathjs
// let start = new Date().getTime();
// let inverseMathjs = mathjs.inv(matrix);
// let end = new Date().getTime();
// console.log(`Mathjs inversion took ${end - start} ms for a ${N}x${N} matrix`);


// // Time inversion using my simple code (seems to have same performance as mathjs)
fork icon0
star icon0
watch icon1

+ 53 other calls in file

64
65
66
67
68
69
70
71
72
73
]);

this.Y = math.matrix([this.pre_matrix[1]]);

this.B = math.multiply(
  math.inv(math.multiply(this.X, math.transpose(this.X))),
  math.multiply(this.X, math.transpose(this.Y))
);

this.slope = this.B.get([0, 0]);
fork icon0
star icon0
watch icon0

+ 3 other calls in file

53
54
55
56
57
58
59
60
61
62
const matrix_XtY = math.matrix(matrix2);

const det = math.det(matrix_XtX);

const inverse = (m) => {
  return math.inv(m);
};
const matrix_XtX_inverse = inverse(matrix_XtX);

const betaMu = math.multiply(matrix_XtX_inverse, matrix_XtY);
fork icon0
star icon0
watch icon0

53
54
55
56
57
58
59
60
61
A = math.mod(A, modn)
let det = validateMatrix(A, modn)
if (det == null) return null

//compute adjoint matrix 
let inv = math.inv(A)
let adj = math.multiply(inv, det)
//round values to get ints
adj = math.map(adj, (value) => { return math.round(value) })
fork icon0
star icon0
watch icon0

+ 3 other calls in file

81
82
83
84
85
86
87
88
89
90
    
    return scores;
}

function opr(data) {
    return math.inv(makePairingMatrix(data)) * makeTeamScores(data);
}

data = [[92, 3595, 8336, 7023, 295, 189],
[577, 1369, 5220, 8337, 213, 93],
fork icon0
star icon0
watch icon3