Skip to content

Instantly share code, notes, and snippets.

@Announcement
Created February 11, 2020 17:41
Show Gist options
  • Save Announcement/8e868004beb7cab4fc010f77733a9974 to your computer and use it in GitHub Desktop.
Save Announcement/8e868004beb7cab4fc010f77733a9974 to your computer and use it in GitHub Desktop.
why is `M` not defined on line 59?
function * count() {
const maximum =
(arguments.length === 1 && typeof arguments[0] === "number") ? arguments[0] :
(arguments.length === 1 && typeof arguments[0] === "object" && arguments[0].hasOwnProperty("maximum")) ? arguments[0].maximum :
(arguments.length === 1 && typeof arguments[0] === "object" && arguments[0].hasOwnProperty("max")) ? arguments[0].max :
(arguments.length === 2 || arguments.length === 3) ? arguments[1] : 0;
const minimum =
(arguments.length === 1 && typeof arguments[0] === "number") ? 0 :
(arguments.length === 1 && typeof arguments[0] === "object" && arguments[0].hasOwnProperty("minimum")) ? arguments[0].minimum :
(arguments.length === 1 && typeof arguments[0] === "object" && arguments[0].hasOwnProperty("min")) ? arguments[0].min :
(arguments.length === 2 || arguments.length === 3) ? arguments[0] : 0;
const increment =
(arguments.length === 1 && typeof arguments[0] === "object" && arguments[0].hasOwnProperty("increment")) ? arguments[0].increment :
(arguments.length === 1 && typeof arguments[0] === "object" && arguments[0].hasOwnProperty("inc")) ? arguments[0].inc :
(arguments.length === 3) ? arguments[2] : (maximum > minimum ? 1 : -1);
for (let i = 0, I = (Math.max(minimum, maximum) - Math.min(minimum, maximum)) / Math.abs(increment); i <= I; i++) {
yield i * increment + minimum;
}
}
function swap (A, h, k) {
for (let i = 0, I = M; i < I; i++)
{
A[h][i] = A[h][i] ^ A[k][i];
A[k][i] = A[k][i] ^ A[h][i];
A[h][i] = A[h][i] ^ A[k][i];
}
}
{
const A =
[ [ 2, -1, 0 ],
[ -1, 2, -1 ],
[ 0, -1, 2 ] ];
const N = A.length;
const M = A[0].length;
/* */
let h = 0;
let k = 0;
for (let h = 0, H = M; h < H; h++) {
for (let k = 0, K = N; k < K; k++) {
const i_max = [...count(h, M - 1)]
.map((i, n) => [ Math.abs(A[i][k]), n ])
.reduce(($0, $1) => $0[0] > $1[0] ? $0 : $1)[1];
if (A[i_max, k] === 0) {
k = k + 1;
}
else
{
swap(A, h, i_max);
}
for (let i = h + 1, I = M; i < I; i++) {
f = A[i][k] / A[h][k];
A[i][k] = 0;
for (let j = k + 1, J = N; j < J; j++) {
A[i][j] = A[i][j] - A[h][j] * f;
}
}
h = h + 1;
k = k + 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment