lapack.bend

bend-blas-lapack@0.0.0.1 · 76 lines · 12 definitions

1# bend-blas: LAPACK routines on Array<F32>, one def per routine, the
2# Fortran names without the trailing underscore. Generated by
3# gen/effs.py: edit the table there, not this file.
4#
5# The same conventions as blas.bend: sizes U32, matrices row-major and
6# consumed, answers fresh; a general operand is copied to column major
7# for LAPACK and back, a symmetric one as it is. A failure is
8# Fail{(code, message)} with the routine's info as the code (the pivot
9# or minor at fault, from 1); the .try twin dies on it. A routine with
10# several answers hands back a pair (Sigma), opened by matching in a
11# def of its own.
12#
13# The symbols (sgesv_ and so on) come from the same library as the BLAS
14# (Accelerate, OpenBLAS), or from liblapack beside a plain system BLAS.
15# On the interpreter the JS twins run: LU with pivoting, Cholesky, and
16# Jacobi for the SVD and the symmetric eigenproblem, correct and slow.
17import Base
18
19# X = A^-1 B: A is n x n, B is n x nrhs, both row-major, both consumed;
20# answers X (n x nrhs). Fails with code i when U(i, i) of the LU
21# factorization is exactly zero (A singular).
22def Lapack.sgesv(n: U32, nrhs: U32, a: Array<F32>, b: Array<F32>) -> IO(Result<&1, &1, U32 & String, Array<F32>>):
23  import "./effs/lapack_sgesv.c"
24  import "./effs/lapack_sgesv.js"
25
26def Lapack.sgesv.try(n: U32, nrhs: U32, a: Array<F32>, b: Array<F32>) -> IO(Array<F32>):
27  IO.try(Array<F32>, Lapack.sgesv(n, nrhs, a, b))
28
29# The Cholesky factor of a symmetric positive definite n x n matrix:
30# answers L, row-major, lower triangular with zeros above, A = L L^T.
31# Fails with code i when the leading minor of order i is not positive.
32def Lapack.spotrf(n: U32, a: Array<F32>) -> IO(Result<&1, &1, U32 & String, Array<F32>>):
33  import "./effs/lapack_spotrf.c"
34  import "./effs/lapack_spotrf.js"
35
36def Lapack.spotrf.try(n: U32, a: Array<F32>) -> IO(Array<F32>):
37  IO.try(Array<F32>, Lapack.spotrf(n, a))
38
39# X = A^-1 B given the Cholesky factor L of A from spotrf (n x n, lower,
40# row-major) and B (n x nrhs, row-major); both consumed, answers X.
41def Lapack.spotrs(n: U32, nrhs: U32, l: Array<F32>, b: Array<F32>) -> IO(Result<&1, &1, U32 & String, Array<F32>>):
42  import "./effs/lapack_spotrs.c"
43  import "./effs/lapack_spotrs.js"
44
45def Lapack.spotrs.try(n: U32, nrhs: U32, l: Array<F32>, b: Array<F32>) -> IO(Array<F32>):
46  IO.try(Array<F32>, Lapack.spotrs(n, nrhs, l, b))
47
48# The singular values of an m x n row-major matrix (consumed), largest
49# first, min(m, n) of them.
50def Lapack.sgesvd_s(m: U32, n: U32, a: Array<F32>) -> IO(Result<&1, &1, U32 & String, Array<F32>>):
51  import "./effs/lapack_sgesvd_s.c"
52  import "./effs/lapack_sgesvd_s.js"
53
54def Lapack.sgesvd_s.try(m: U32, n: U32, a: Array<F32>) -> IO(Array<F32>):
55  IO.try(Array<F32>, Lapack.sgesvd_s(m, n, a))
56
57# The thin SVD of an m x n row-major matrix (consumed): answers
58# (s, (u, vt)) with s the min(m, n) singular values largest first, u
59# m x k and vt k x n row-major, A = u diag(s) vt.
60def Lapack.sgesvd(m: U32, n: U32, a: Array<F32>) -> IO(Result<&1, &1, U32 & String, Array<F32> & (Array<F32> & Array<F32>)>):
61  import "./effs/lapack_sgesvd.c"
62  import "./effs/lapack_sgesvd.js"
63
64def Lapack.sgesvd.try(m: U32, n: U32, a: Array<F32>) -> IO(Array<F32> & (Array<F32> & Array<F32>)):
65  IO.try(Array<F32> & (Array<F32> & Array<F32>), Lapack.sgesvd(m, n, a))
66
67# The eigenvalues and eigenvectors of a symmetric n x n row-major matrix
68# (consumed, its lower triangle read): answers (w, v) with w the n
69# eigenvalues ascending and v n x n row-major, row i the unit eigenvector
70# of w[i].
71def Lapack.ssyev(n: U32, a: Array<F32>) -> IO(Result<&1, &1, U32 & String, Array<F32> & Array<F32>>):
72  import "./effs/lapack_ssyev.c"
73  import "./effs/lapack_ssyev.js"
74
75def Lapack.ssyev.try(n: U32, a: Array<F32>) -> IO(Array<F32> & Array<F32>):
76  IO.try(Array<F32> & Array<F32>, Lapack.ssyev(n, a))