* @param {mat2} a the matrix to calculate Frobenius norm of
* @returns {Number} Frobenius norm
*/
functionmat2frob(a){
returnMath.sqrt(
Math.pow(a[0],2)+
Math.pow(a[1],2)+
Math.pow(a[2],2)+
Math.pow(a[3],2)
)
}
/**
* Set a mat2 to the identity matrix
*
* @alias mat2.identity
* @param {mat2} out the receiving matrix
* @returns {mat2} out
*/
functionmat2identity(out){
out[0]=1
out[1]=0
out[2]=0
out[3]=1
returnout
}
/**
* Inverts a mat2
*
* @alias mat2.invert
* @param {mat2} out the receiving matrix
* @param {mat2} a the source matrix
* @returns {mat2} out
*/
functionmat2invert(out,a){
vara0=a[0]
vara1=a[1]
vara2=a[2]
vara3=a[3]
vardet=a0*a3-a2*a1
if(!det)returnnull
det=1.0/det
out[0]=a3*det
out[1]=-a1*det
out[2]=-a2*det
out[3]=a0*det
returnout
}
/**
* 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
*/
functionmat2ldu(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]
}
/**
* 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
*/
functionmat2multiply(out,a,b){
vara0=a[0],a1=a[1],a2=a[2],a3=a[3]
varb0=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
returnout
}
/**
* 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
*/
functionmat2rotate(out,a,rad){
vara0=a[0],a1=a[1],a2=a[2],a3=a[3]
vars=Math.sin(rad)
varc=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
returnout
}
/**
* 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
**/
functionmat2scale(out,a,v){
vara0=a[0],a1=a[1],a2=a[2],a3=a[3]
varv0=v[0],v1=v[1]
out[0]=a0*v0
out[1]=a1*v0
out[2]=a2*v1
out[3]=a3*v1
returnout
}
/**
* Transpose the values of a mat2
*
* @alias mat2.transpose
* @param {mat2} out the receiving matrix
* @param {mat2} a the source matrix
* @returns {mat2} out
*/
functionmat2transpose(out,a){
// If we are transposing ourselves we can skip a few steps but have to cache some values
if(out===a){
vara1=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]
}
returnout
}
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.