Skip to content

Commit b306f91

Browse files
committed
Added examples.
1 parent ddc0f0d commit b306f91

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

example/linalg/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ ADD_EXAMPLE(blas_gemv)
3535
ADD_EXAMPLE(lapack_getrf)
3636
ADD_EXAMPLE(lstsq1)
3737
ADD_EXAMPLE(lstsq2)
38+
ADD_EXAMPLE(constrained_lstsq1)
39+
ADD_EXAMPLE(constrained_lstsq2)
3840
ADD_EXAMPLE(norm)
3941
ADD_EXAMPLE(mnorm)
4042
ADD_EXAMPLE(get_norm)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
! Constrained least-squares solver: functional interface
2+
program example_constrained_lstsq1
3+
use stdlib_linalg_constants, only: dp
4+
use stdlib_linalg, only: constrained_lstsq
5+
implicit none
6+
integer, parameter :: m = 5, n = 4, p = 3
7+
!> Least-squares cost.
8+
real(dp) :: A(m, n), b(m)
9+
!> Equality constraints.
10+
real(dp) :: C(p, n), d(p)
11+
!> Solution.
12+
real(dp) :: x(n), x_true(n)
13+
14+
!> Least-squares cost.
15+
A(1, :) = [1.0_dp, 1.0_dp, 1.0_dp, 1.0_dp]
16+
A(2, :) = [1.0_dp, 3.0_dp, 1.0_dp, 1.0_dp]
17+
A(3, :) = [1.0_dp, -1.0_dp, 3.0_dp, 1.0_dp]
18+
A(4, :) = [1.0_dp, 1.0_dp, 1.0_dp, 3.0_dp]
19+
A(5, :) = [1.0_dp, 1.0_dp, 1.0_dp, -1.0_dp]
20+
21+
b = [2.0_dp, 1.0_dp, 6.0_dp, 3.0_dp, 1.0_dp]
22+
23+
!> Equality constraints.
24+
C(1, :) = [1.0_dp, 1.0_dp, 1.0_dp, -1.0_dp]
25+
C(2, :) = [1.0_dp, -1.0_dp, 1.0_dp, 1.0_dp]
26+
C(3, :) = [1.0_dp, 1.0_dp, -1.0_dp, 1.0_dp]
27+
28+
d = [1.0_dp, 3.0_dp, -1.0_dp]
29+
30+
! Compute the solution.
31+
x = constrained_lstsq(A, b, C, d)
32+
x_true = [0.5_dp, -0.5_dp, 1.5_dp, 0.5_dp]
33+
34+
print *, "Exact solution :"
35+
print *, x_true
36+
print *
37+
print *, "Computed solution :"
38+
print *, x
39+
40+
end program example_constrained_lstsq1
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
! Demonstrate expert subroutine interface with pre-allocated arrays
2+
program example_constrained_lstsq2
3+
use stdlib_linalg_constants, only: dp
4+
use stdlib_linalg, only: solve_constrained_lstsq, constrained_lstsq_space
5+
implicit none
6+
integer, parameter :: m = 5, n = 4, p = 3
7+
!> Least-squares cost.
8+
real(dp) :: A(m, n), b(m)
9+
!> Equality constraints.
10+
real(dp) :: C(p, n), d(p)
11+
!> Solution.
12+
real(dp) :: x(n), x_true(n)
13+
!> Workspace array.
14+
integer :: lwork
15+
real(dp), allocatable :: work(:)
16+
17+
!> Least-squares cost.
18+
A(1, :) = [1.0_dp, 1.0_dp, 1.0_dp, 1.0_dp]
19+
A(2, :) = [1.0_dp, 3.0_dp, 1.0_dp, 1.0_dp]
20+
A(3, :) = [1.0_dp, -1.0_dp, 3.0_dp, 1.0_dp]
21+
A(4, :) = [1.0_dp, 1.0_dp, 1.0_dp, 3.0_dp]
22+
A(5, :) = [1.0_dp, 1.0_dp, 1.0_dp, -1.0_dp]
23+
24+
b = [2.0_dp, 1.0_dp, 6.0_dp, 3.0_dp, 1.0_dp]
25+
26+
!> Equality constraints.
27+
C(1, :) = [1.0_dp, 1.0_dp, 1.0_dp, -1.0_dp]
28+
C(2, :) = [1.0_dp, -1.0_dp, 1.0_dp, 1.0_dp]
29+
C(3, :) = [1.0_dp, 1.0_dp, -1.0_dp, 1.0_dp]
30+
31+
d = [1.0_dp, 3.0_dp, -1.0_dp]
32+
33+
!> Optimal workspace size.
34+
call constrained_lstsq_space(A, b, C, d, lwork)
35+
allocate (work(lwork))
36+
37+
! Compute the solution.
38+
call solve_constrained_lstsq(A, b, C, d, x, &
39+
storage=work, &
40+
overwrite_matrices=.true.)
41+
x_true = [0.5_dp, -0.5_dp, 1.5_dp, 0.5_dp]
42+
43+
print *, "Exact solution :"
44+
print *, x_true
45+
print *
46+
print *, "Computed solution :"
47+
print *, x
48+
end program example_constrained_lstsq2

0 commit comments

Comments
 (0)