44#:set RHS_SYMBOL = [ranksuffix(r) for r in [1,2]]
55#:set RHS_EMPTY = [emptyranksuffix(r) for r in [1,2]]
66#:set ALL_RHS = list(zip(RHS_SYMBOL,RHS_SUFFIX,RHS_EMPTY))
7- module stdlib_linalg_least_squares
7+ submodule (stdlib_linalg) stdlib_linalg_least_squares
8+ !! Least-squares solution to Ax=b
89 use stdlib_linalg_constants
910 use stdlib_linalg_lapack, only: gelsd, stdlib_ilaenv
1011 use stdlib_linalg_state, only: linalg_state_type, linalg_error_handling, LINALG_ERROR, &
1112 LINALG_INTERNAL_ERROR, LINALG_VALUE_ERROR
1213 implicit none(type,external)
13- private
14-
15- !> Compute a least squares solution to system Ax=b, i.e. such that the 2-norm abs(b-Ax) is minimized.
16- public :: lstsq
17-
18- ! NumPy: lstsq(a, b, rcond='warn')
19- ! Scipy: lstsq(a, b, cond=None, overwrite_a=False, overwrite_b=False, check_finite=True, lapack_driver=None)
20- ! IMSL: Result = IMSL_QRSOL(B, [A] [, AUXQR] [, BASIS] [, /DOUBLE] [, QR] [, PIVOT] [, RESIDUAL] [, TOLERANCE])
21-
22- interface lstsq
23- #:for nd,ndsuf,nde in ALL_RHS
24- #:for rk,rt,ri in RC_KINDS_TYPES
25- module procedure stdlib_linalg_${ri}$_lstsq_${ndsuf}$
26- #:endfor
27- #:endfor
28- end interface lstsq
29-
14+
15+ character(*), parameter :: this = 'lstsq'
3016
3117 contains
3218
3319 #:for rk,rt,ri in RC_KINDS_TYPES
3420 ! Workspace needed by gesv
35- subroutine ${ri}$gesv_space(m,n,nrhs,lrwork,liwork,lcwork)
21+ elemental subroutine ${ri}$gesv_space(m,n,nrhs,lrwork,liwork,lcwork)
3622 integer(ilp), intent(in) :: m,n,nrhs
3723 integer(ilp), intent(out) :: lrwork,liwork,lcwork
3824
@@ -73,11 +59,11 @@ module stdlib_linalg_least_squares
7359 #:for rk,rt,ri in RC_KINDS_TYPES
7460
7561 ! Compute the least-squares solution to a real system of linear equations Ax = B
76- function stdlib_linalg_${ri}$_lstsq_${ndsuf}$(a,b,cond,overwrite_a,rank,err) result(x)
62+ module function stdlib_linalg_${ri}$_lstsq_${ndsuf}$(a,b,cond,overwrite_a,rank,err) result(x)
7763 !> Input matrix a[n,n]
78- ${rt}$, intent(inout), target :: a(:,:)
64+ ${rt}$, intent(inout), target :: a(:,:)
7965 !> Right hand side vector or array, b[n] or b[n,nrhs]
80- ${rt}$, intent(in) :: b${nd}$
66+ ${rt}$, intent(in) :: b${nd}$
8167 !> [optional] cutoff for rank evaluation: singular values s(i)<=cond*maxval(s) are considered 0.
8268 real(${rk}$), optional, intent(in) :: cond
8369 !> [optional] Can A,b data be overwritten and destroyed?
@@ -88,8 +74,8 @@ module stdlib_linalg_least_squares
8874 type(linalg_state_type), optional, intent(out) :: err
8975 !> Result array/matrix x[n] or x[n,nrhs]
9076 ${rt}$, allocatable, target :: x${nd}$
91-
92- !> Local variables
77+
78+ !! Local variables
9379 type(linalg_state_type) :: err0
9480 integer(ilp) :: m,n,lda,ldb,nrhs,info,mnmin,mnmax,arank,lrwork,liwork,lcwork
9581 integer(ilp), allocatable :: iwork(:)
@@ -98,9 +84,8 @@ module stdlib_linalg_least_squares
9884 real(${rk}$), allocatable :: singular(:),rwork(:)
9985 ${rt}$, pointer :: xmat(:,:),amat(:,:)
10086 ${rt}$, allocatable :: cwork(:)
101- character(*), parameter :: this = 'lstsq'
10287
103- !> Problem sizes
88+ ! Problem sizes
10489 m = size(a,1,kind=ilp)
10590 lda = size(a,1,kind=ilp)
10691 n = size(a,2,kind=ilp)
@@ -207,4 +192,4 @@ module stdlib_linalg_least_squares
207192 endif
208193 end function ilog2
209194
210- end module stdlib_linalg_least_squares
195+ end submodule stdlib_linalg_least_squares
0 commit comments