File: example_constrained_lstsq1.f90

package info (click to toggle)
fortran-stdlib 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 34,008 kB
  • sloc: f90: 24,178; ansic: 1,244; cpp: 623; python: 119; makefile: 13
file content (40 lines) | stat: -rw-r--r-- 1,173 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
! Constrained least-squares solver: functional interface
program example_constrained_lstsq1
   use stdlib_linalg_constants, only: dp
   use stdlib_linalg, only: constrained_lstsq
   implicit none
   integer, parameter :: m = 5, n = 4, p = 3
   !> Least-squares cost.
   real(dp) :: A(m, n), b(m)
   !> Equality constraints.
   real(dp) :: C(p, n), d(p)
   !> Solution.
   real(dp) :: x(n), x_true(n)

   !> Least-squares cost.
   A(1, :) = [1.0_dp, 1.0_dp, 1.0_dp, 1.0_dp]
   A(2, :) = [1.0_dp, 3.0_dp, 1.0_dp, 1.0_dp]
   A(3, :) = [1.0_dp, -1.0_dp, 3.0_dp, 1.0_dp]
   A(4, :) = [1.0_dp, 1.0_dp, 1.0_dp, 3.0_dp]
   A(5, :) = [1.0_dp, 1.0_dp, 1.0_dp, -1.0_dp]

   b = [2.0_dp, 1.0_dp, 6.0_dp, 3.0_dp, 1.0_dp]

   !> Equality constraints.
   C(1, :) = [1.0_dp, 1.0_dp, 1.0_dp, -1.0_dp]
   C(2, :) = [1.0_dp, -1.0_dp, 1.0_dp, 1.0_dp]
   C(3, :) = [1.0_dp, 1.0_dp, -1.0_dp, 1.0_dp]

   d = [1.0_dp, 3.0_dp, -1.0_dp]

   ! Compute the solution.
   x = constrained_lstsq(A, b, C, d)
   x_true = [0.5_dp, -0.5_dp, 1.5_dp, 0.5_dp]

   print *, "Exact solution :"
   print *, x_true
   print *
   print *, "Computed solution :"
   print *, x

end program example_constrained_lstsq1