A non-negative least squares (NNLS) solver in Fortran. Modern examples exist for Python and Julia at least (I'm sure there are others) but the original Fortran code they're based on is from the 80s and fixed format hurts me to read so I wrote this one.
compile with make all
as usual; requires BLAS
and LAPACK
. Otherwise all strictly fortran 2018. Only tested on linux and with gfortran.
set SHARED = 1
in the makefile to build as a shared library; it uses iso_c_binding
and so should be straightforwardly callable from python using ctypes.
usage: solve(A, b, x, m, n, mode, res, maxiter, tol)
. when the subroutine returns x
will be overwritten with the result, mode
will be 0 on success and -1 on failure and res
will contain the norm
set SHARED = 0
to compile standalone with main.f
and then run ./test
to check it works.
this generates a set of 1000 matrices solve
as above with
note that there might be multiple solutions of the equations it generates; for each iteration it prints a.) res
any set of equations for which the maximum number of iterations is reached (currently set to