// bend-blas, the JS twins: plain loops, correct and slow, for the // interpreter and the tests. An Array is a JS array of numbers. // A failure is io_fail(code): the runtime's message for a code is not // this library's, so the code is the contract on this lane. function bend_fresh(n) { let slots = 1; while (slots < n) slots *= 2; return new Array(slots).fill(0); } function bend_copy(src, n) { const c = bend_fresh(n); for (let i = 0; i < n; i++) c[i] = src[i]; return c; } function bend_short(a, n) { return n > 0x7fffffff || a.length < n; } function bend_gemm(ta, tb, m, n, k, alpha, a, b, beta, c) { const out = bend_copy(c, m * n); for (let i = 0; i < m; i++) for (let j = 0; j < n; j++) { let acc = 0; for (let l = 0; l < k; l++) acc += (ta ? a[l * m + i] : a[i * k + l]) * (tb ? b[j * k + l] : b[l * n + j]); out[i * n + j] = alpha * acc + beta * out[i * n + j]; } return out; } function lapack_spotrf(n, a) { if (bend_short(a, n * n)) return io_fail(2); const L = bend_fresh(n * n); for (let j = 0; j < n; j++) { let s = a[j * n + j]; for (let k = 0; k < j; k++) s -= L[j * n + k] * L[j * n + k]; if (!(s > 0)) return io_fail(j + 1); L[j * n + j] = Math.sqrt(s); for (let i = j + 1; i < n; i++) { let t = a[i * n + j]; for (let k = 0; k < j; k++) t -= L[i * n + k] * L[j * n + k]; L[i * n + j] = t / L[j * n + j]; } } return io_done(L); } io_eff(CID(Lapack.spotrf), lapack_spotrf);