Skip to content

Instantly share code, notes, and snippets.

@jwerle
Created September 16, 2015 18:17
Show Gist options
  • Save jwerle/13cd6a94724c7534f195 to your computer and use it in GitHub Desktop.
Save jwerle/13cd6a94724c7534f195 to your computer and use it in GitHub Desktop.
Identity matrix construction from the KroneckerDelta function
'use strict';
// kronecker delta
let kd = (i, j) => i == j ? 1 : 0;
// (In)ij identity matrix constructor
let I = n => {
let a = [], k = 0;
for (let i = 0; i < n; ++i)
for (let j = 0; j < n; ++j)
a[k++] = kd(i, j);
return a;
};
// constructs
let mat2 = _ => I(2);
let mat3 = _ => I(3);
let mat4 = _ => I(4);
// proof
console.log(4 == mat2().length);
console.log(9 == mat3().length);
console.log(16 == mat4().length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment