Skip to content

Instantly share code, notes, and snippets.

@idiotWu

idiotWu/perf.mjs Secret

Last active June 16, 2021 01:12
Show Gist options
  • Save idiotWu/80de6019ee80a3e6c4d6ddb19f2eb7b5 to your computer and use it in GitHub Desktop.
Save idiotWu/80de6019ee80a3e6c4d6ddb19f2eb7b5 to your computer and use it in GitHub Desktop.
import * as mathjs from 'mathjs';
import mat from './mat.mjs';
const m = [
[3, -1, 2],
[4, 1, 0],
[-3, 2, 1],
[1, 1, 5],
[-2, 0, 3],
];
const b = [[10], [10], [-5], [15], [0]];
const m2 = [
[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]
];
const b2 = [[-1], [-1], [-1], [-1]];
function qrsolve(A, b) {
const { Q, R } = mathjs.qr(A);
const dim1 = A[0].length;
return mathjs.multiply(
mathjs.inv(R.slice(0, dim1)),
mathjs.transpose(Q).slice(0, dim1),
b,
);
}
function run(label, fn) {
console.time(label);
for (let i = 0; i < 1e6; i++) {
fn();
}
console.timeEnd(label);
}
run('qrsolve()', () => qrsolve(m, b));
run('mat.QRDecomposition()', () => mat.QRDecomposition(m, b))
run('mathjs.lusolve()', () => mathjs.lusolve(m2, b2));
run('mat.LUDecomposition()', () => mat.LUDecomposition(m2, b2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment