Published
Edited
Jul 16, 2019
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
a = mat2.create()
Insert cell
Insert cell
b = mat2create()
Insert cell
Insert cell
mat2 = ({
determinant: mat2determinant,
transpose: mat2transpose,
multiply: mat2multiply,
identity: mat2identity,
adjoint: mat2adjoint,
rotate: mat2rotate,
invert: mat2invert,
create: mat2create,
scale: mat2scale,
copy: mat2copy,
frob: mat2frob,
ldu: mat2ldu
})
Insert cell
/**
* Calculates the adjugate of a mat2
*
* @alias mat2.adjoint
* @param {mat2} out the receiving matrix
* @param {mat2} a the source matrix
* @returns {mat2} out
*/
function mat2adjoint(out, a) {
// Caching this value is nessecary if out == a
var a0 = a[0]
out[0] = a[3]
out[1] = -a[1]
out[2] = -a[2]
out[3] = a0

return out
}
Insert cell
/**
* Copy the values from one mat2 to another
*
* @alias mat2.copy
* @param {mat2} out the receiving matrix
* @param {mat2} a the source matrix
* @returns {mat2} out
*/
function mat2copy(out, a) {
out[0] = a[0]
out[1] = a[1]
out[2] = a[2]
out[3] = a[3]
return out
}
Insert cell
/**
* Creates a new identity mat2
*
* @alias mat2.create
* @returns {mat2} a new 2x2 matrix
*/
function mat2create() {
var out = new Float32Array(4)
out[0] = 1
out[1] = 0
out[2] = 0
out[3] = 1
return out
}
Insert cell
/**
* Calculates the determinant of a mat2
*
* @alias mat2.determinant
* @param {mat2} a the source matrix
* @returns {Number} determinant of a
*/
function mat2determinant(a) {
return a[0] * a[3] - a[2] * a[1]
}
Insert cell
/**
* Returns Frobenius norm of a mat2
*
* @alias mat2.frob
* @param {mat2} a the matrix to calculate Frobenius norm of
* @returns {Number} Frobenius norm
*/
function mat2frob(a) {
return Math.sqrt(
Math.pow(a[0], 2) +
Math.pow(a[1], 2) +
Math.pow(a[2], 2) +
Math.pow(a[3], 2)
)
}
Insert cell
/**
* Set a mat2 to the identity matrix
*
* @alias mat2.identity
* @param {mat2} out the receiving matrix
* @returns {mat2} out
*/
function mat2identity(out) {
out[0] = 1
out[1] = 0
out[2] = 0
out[3] = 1
return out
}
Insert cell
/**
* Inverts a mat2
*
* @alias mat2.invert
* @param {mat2} out the receiving matrix
* @param {mat2} a the source matrix
* @returns {mat2} out
*/
function mat2invert(out, a) {
var a0 = a[0]
var a1 = a[1]
var a2 = a[2]
var a3 = a[3]
var det = a0 * a3 - a2 * a1

if (!det) return null
det = 1.0 / det

out[0] = a3 * det
out[1] = -a1 * det
out[2] = -a2 * det
out[3] = a0 * det

return out
}
Insert cell
/**
* Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrix
*
* @alias mat2.ldu
* @param {mat2} L the lower triangular matrix
* @param {mat2} D the diagonal matrix
* @param {mat2} U the upper triangular matrix
* @param {mat2} a the input matrix to factorize
*/
function mat2ldu(L, D, U, a) {
L[2] = a[2]/a[0]
U[0] = a[0]
U[1] = a[1]
U[3] = a[3] - L[2] * U[1]
return [L, D, U]
}
Insert cell
/**
* Multiplies two mat2's
*
* @alias mat2.multiply
* @param {mat2} out the receiving matrix
* @param {mat2} a the first operand
* @param {mat2} b the second operand
* @returns {mat2} out
*/
function mat2multiply(out, a, b) {
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]
var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]
out[0] = a0 * b0 + a2 * b1
out[1] = a1 * b0 + a3 * b1
out[2] = a0 * b2 + a2 * b3
out[3] = a1 * b2 + a3 * b3
return out
}
Insert cell
/**
* Rotates a mat2 by the given angle
*
* @alias mat2.rotate
* @param {mat2} out the receiving matrix
* @param {mat2} a the matrix to rotate
* @param {Number} rad the angle to rotate the matrix by
* @returns {mat2} out
*/
function mat2rotate(out, a, rad) {
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]
var s = Math.sin(rad)
var c = Math.cos(rad)
out[0] = a0 * c + a2 * s
out[1] = a1 * c + a3 * s
out[2] = a0 * -s + a2 * c
out[3] = a1 * -s + a3 * c
return out
}
Insert cell
/**
* Scales the mat2 by the dimensions in the given vec2
*
* @alias mat2.scale
* @param {mat2} out the receiving matrix
* @param {mat2} a the matrix to rotate
* @param {vec2} v the vec2 to scale the matrix by
* @returns {mat2} out
**/
function mat2scale(out, a, v) {
var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]
var v0 = v[0], v1 = v[1]
out[0] = a0 * v0
out[1] = a1 * v0
out[2] = a2 * v1
out[3] = a3 * v1
return out
}
Insert cell
/**
* Transpose the values of a mat2
*
* @alias mat2.transpose
* @param {mat2} out the receiving matrix
* @param {mat2} a the source matrix
* @returns {mat2} out
*/
function mat2transpose(out, a) {
// If we are transposing ourselves we can skip a few steps but have to cache some values
if (out === a) {
var a1 = a[1]
out[1] = a[2]
out[2] = a1
} else {
out[0] = a[0]
out[1] = a[2]
out[2] = a[1]
out[3] = a[3]
}

return out
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more