// 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_spotrs(n, nrhs, l, b) { if (bend_short(l, n * n)) return io_fail(2); if (bend_short(b, n * nrhs)) return io_fail(2); const X = bend_copy(b, n * nrhs); for (let j = 0; j < nrhs; j++) { for (let i = 0; i < n; i++) { let s = X[i * nrhs + j]; for (let k = 0; k < i; k++) s -= l[i * n + k] * X[k * nrhs + j]; X[i * nrhs + j] = s / l[i * n + i]; } for (let i = n - 1; i >= 0; i--) { let s = X[i * nrhs + j]; for (let k = i + 1; k < n; k++) s -= l[k * n + i] * X[k * nrhs + j]; X[i * nrhs + j] = s / l[i * n + i]; } } return io_done(X); } io_eff(CID(Lapack.spotrs), lapack_spotrs);