Skip to content

Commit 7475680

Browse files
committed
submodule
1 parent 465d230 commit 7475680

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

src/stdlib_linalg.fypp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
#:include "common.fypp"
2-
#:set RCI_KINDS_TYPES = REAL_KINDS_TYPES + CMPLX_KINDS_TYPES + INT_KINDS_TYPES
2+
#:set RC_KINDS_TYPES = REAL_KINDS_TYPES + CMPLX_KINDS_TYPES
3+
#:set RCI_KINDS_TYPES = RC_KINDS_TYPES + INT_KINDS_TYPES
4+
#:set RHS_SUFFIX = ["one","many"]
5+
#:set RHS_SYMBOL = [ranksuffix(r) for r in [1,2]]
6+
#:set RHS_EMPTY = [emptyranksuffix(r) for r in [1,2]]
7+
#:set ALL_RHS = list(zip(RHS_SYMBOL,RHS_SUFFIX,RHS_EMPTY))
38
module stdlib_linalg
49
!!Provides a support for various linear algebra procedures
510
!! ([Specification](../page/specs/stdlib_linalg.html))
6-
use stdlib_kinds, only: sp, dp, xdp, qp, &
7-
int8, int16, int32, int64
11+
use stdlib_kinds, only: xdp, int8, int16, int32, int64
12+
use stdlib_linalg_constants, only: sp, dp, qp, lk, ilp
813
use stdlib_error, only: error_stop
914
use stdlib_optval, only: optval
15+
use stdlib_linalg_state, only: linalg_state_type, linalg_error_handling
1016
implicit none
1117
private
1218

1319
public :: diag
1420
public :: eye
21+
public :: lstsq
1522
public :: trace
1623
public :: outer_product
1724
public :: kronecker_product
@@ -214,6 +221,30 @@ module stdlib_linalg
214221
#:endfor
215222
end interface is_hessenberg
216223

224+
! Least squares solution to system Ax=b, i.e. such that the 2-norm abs(b-Ax) is minimized.
225+
interface lstsq
226+
#:for nd,ndsuf,nde in ALL_RHS
227+
#:for rk,rt,ri in RC_KINDS_TYPES
228+
module function stdlib_linalg_${ri}$_lstsq_${ndsuf}$(a,b,cond,overwrite_a,rank,err) result(x)
229+
!> Input matrix a[n,n]
230+
${rt}$, intent(inout), target :: a(:,:)
231+
!> Right hand side vector or array, b[n] or b[n,nrhs]
232+
${rt}$, intent(in) :: b${nd}$
233+
!> [optional] cutoff for rank evaluation: singular values s(i)<=cond*maxval(s) are considered 0.
234+
real(${rk}$), optional, intent(in) :: cond
235+
!> [optional] Can A,b data be overwritten and destroyed?
236+
logical(lk), optional, intent(in) :: overwrite_a
237+
!> [optional] Return rank of A
238+
integer(ilp), optional, intent(out) :: rank
239+
!> [optional] state return flag. On error if not requested, the code will stop
240+
type(linalg_state_type), optional, intent(out) :: err
241+
!> Result array/matrix x[n] or x[n,nrhs]
242+
${rt}$, allocatable, target :: x${nd}$
243+
end function stdlib_linalg_${ri}$_lstsq_${ndsuf}$
244+
#:endfor
245+
#:endfor
246+
end interface lstsq
247+
217248
contains
218249

219250

src/stdlib_linalg_least_squares.fypp

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,21 @@
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

Comments
 (0)