4
4
#:set RHS_SYMBOL = [ranksuffix(r) for r in [1,2]]
5
5
#:set RHS_EMPTY = [emptyranksuffix(r) for r in [1,2]]
6
6
#: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
8
9
use stdlib_linalg_constants
9
10
use stdlib_linalg_lapack, only: gelsd, stdlib_ilaenv
10
11
use stdlib_linalg_state, only: linalg_state_type, linalg_error_handling, LINALG_ERROR, &
11
12
LINALG_INTERNAL_ERROR, LINALG_VALUE_ERROR
12
13
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'
30
16
31
17
contains
32
18
33
19
#:for rk,rt,ri in RC_KINDS_TYPES
34
20
! 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)
36
22
integer(ilp), intent(in) :: m,n,nrhs
37
23
integer(ilp), intent(out) :: lrwork,liwork,lcwork
38
24
@@ -73,11 +59,11 @@ module stdlib_linalg_least_squares
73
59
#:for rk,rt,ri in RC_KINDS_TYPES
74
60
75
61
! 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)
77
63
!> Input matrix a[n,n]
78
- ${rt}$, intent(inout), target :: a(:,:)
64
+ ${rt}$, intent(inout), target :: a(:,:)
79
65
!> Right hand side vector or array, b[n] or b[n,nrhs]
80
- ${rt}$, intent(in) :: b${nd}$
66
+ ${rt}$, intent(in) :: b${nd}$
81
67
!> [optional] cutoff for rank evaluation: singular values s(i)<=cond*maxval(s) are considered 0.
82
68
real(${rk}$), optional, intent(in) :: cond
83
69
!> [optional] Can A,b data be overwritten and destroyed?
@@ -88,8 +74,8 @@ module stdlib_linalg_least_squares
88
74
type(linalg_state_type), optional, intent(out) :: err
89
75
!> Result array/matrix x[n] or x[n,nrhs]
90
76
${rt}$, allocatable, target :: x${nd}$
91
-
92
- !> Local variables
77
+
78
+ !! Local variables
93
79
type(linalg_state_type) :: err0
94
80
integer(ilp) :: m,n,lda,ldb,nrhs,info,mnmin,mnmax,arank,lrwork,liwork,lcwork
95
81
integer(ilp), allocatable :: iwork(:)
@@ -98,9 +84,8 @@ module stdlib_linalg_least_squares
98
84
real(${rk}$), allocatable :: singular(:),rwork(:)
99
85
${rt}$, pointer :: xmat(:,:),amat(:,:)
100
86
${rt}$, allocatable :: cwork(:)
101
- character(*), parameter :: this = 'lstsq'
102
87
103
- !> Problem sizes
88
+ ! Problem sizes
104
89
m = size(a,1,kind=ilp)
105
90
lda = size(a,1,kind=ilp)
106
91
n = size(a,2,kind=ilp)
@@ -207,4 +192,4 @@ module stdlib_linalg_least_squares
207
192
endif
208
193
end function ilog2
209
194
210
- end module stdlib_linalg_least_squares
195
+ end submodule stdlib_linalg_least_squares
0 commit comments