-
Notifications
You must be signed in to change notification settings - Fork 0
/
nnls.f
177 lines (155 loc) · 5.08 KB
/
nnls.f
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
! 13/02/2024
! @author: callum
! refs:
! 1. scipy.optimize.nnls
! 2. Lawson & Hanson
! doi.org/10.1137/1.9781611971217
! 3. Bro & de Jong
! doi.org/10.1002/(SICI)1099-128X(199709/10)11:5<393::AID-CEM483>3.0.CO;2-L
module nnls
use iso_fortran_env
use iso_c_binding
use, intrinsic :: ieee_arithmetic
implicit none
public :: update_s, solve
integer, parameter :: CF = c_double
integer, parameter :: CI = c_int
integer, parameter :: CB = c_bool
contains
pure function inf_pos() result(r)
real(kind=CF) :: r
r = ieee_value(r, ieee_positive_inf)
end function inf_pos
pure function inf_neg() result(r)
real(kind=CF) :: r
r = ieee_value(r, ieee_negative_inf)
end function inf_neg
subroutine update_s(s, ata, atb, pr, n, n_true, s_p_min)&
bind(C, name='update_s')
implicit none
integer(kind=CI) :: n, n_true, i, info, lwork
real(kind=CF), dimension(n, n) :: ata
real(kind=CF), dimension(n) :: atb, s
logical(kind=CB), dimension(n) :: pr
real(kind=CF) :: s_p_min
integer(kind=CI), dimension(n_true) :: p_i, ipiv
real(kind=CF), dimension(n_true, n_true) :: atap
real(kind=CF), dimension(n_true) :: atbp
real(kind=CF), dimension(:), allocatable :: work
if (size(ata, 1).ne.size(ata, 2)) then
write (*,*) "ata not square"
stop
end if
if (size(ata, 1).ne.size(pr)) then
write (*,*) "ata and p not same size"
stop
end if
! indices where p is true
p_i = pack([(i, i = 1_CI, size(pr))], pr)
atap = ata(p_i, p_i)
atbp = atb(p_i)
allocate(work(1))
call dsysv("U", n_true, 1, atap, n_true,&
ipiv, atbp, n_true, work, -1, info)
lwork = int(work(1))
deallocate(work)
allocate(work(lwork))
call dsysv("U", n_true, 1, atap, n_true,&
ipiv, atbp, n_true, work, lwork, info)
! atbp is now s[p]
s_p_min = inf_pos()
do i = 1, n_true
s(p_i(i)) = atbp(i)
if (s(p_i(i)).lt.s_p_min) then
s_p_min = s(p_i(i))
end if
end do
deallocate(work)
end subroutine update_s
!> NNLS solver (cf. refs above).
!! Solves the system of equations $ Ax = b $,
!! where $A$ is an $ m \times n $ matrix, $b$ is an $m$-vector
!! and $x$ is an $n$-vector.
!! If $x$ contains any data on input it will be overwritten.
!! res will contain the norm $|Ax - b|$.
!! mode will be 0 on success and -1 on failure.
subroutine solve(A, b, x, m, n, mode, res, maxiter, tol)&
bind(C, name='solve')
implicit none
integer(kind=CI) :: m, n, iter, i, mode, k, maxiter
real(kind=CF) :: tol, s_p_min, alpha, alpha_min, res
real(kind=CF), dimension(m, n) :: A !< equation matrix
real(kind=CF), dimension(m) :: b
real(kind=CF), dimension(n) :: x
real(kind=CF), dimension(:, :), allocatable :: ata
real(kind=CF), dimension(:), allocatable :: atb, resid, s
logical(kind=CB), dimension(:), allocatable :: pr
if (size(b).ne.m) then
write(*, *) size(A, 1), size(A, 2), size(b)
write(*, *) "A and b dimensions incompatible"
stop
end if
if (size(x).ne.n) then
write(*, *) size(A, 1), size(A, 2), size(x)
write(*, *) "A and x dimensions incompatible"
stop
end if
allocate(ata(n, n), source=0.0_CF)
ata = matmul(transpose(A), A)
allocate(atb(n), source=0.0_CF)
allocate(resid(n), source=0.0_CF)
atb = matmul(b, A)
resid = atb
x = 0.0_CF
allocate(pr(n))
pr = .false._CB
allocate(s(n), source=0.0_CF)
mode = 1
iter = 0
do while ((.not.all(pr)).and.&
(any(merge(resid, 0.0_CF,&
(pr.eqv..false._CB)).gt.tol)))
where (pr) resid = inf_neg()
k = maxloc(resid, 1, kind=CI)
pr(k) = .true._CB ! must specify dim to get scalar
s = 0.0_CF
call update_s(s, ata, atb, pr, n, count(pr), s_p_min)
do while ((iter.lt.maxiter).and.(s_p_min.le.tol))
alpha_min = inf_pos()
do i = 1, n
if ((pr(i)).and.s(i).le.tol) then
alpha = (x(i) / (x(i) - s(i)))
if (alpha.lt.alpha_min) then
alpha_min = alpha
end if
end if
end do
x = x * (1.0_CF - alpha_min)
x = x + (alpha_min * s)
where (x.lt.tol) pr = .false._CB
call update_s(s, ata, atb, pr, n, count(pr), s_p_min)
where (.not.pr) s = 0.0_CF
iter = iter + 1_CI
end do
x = s
resid = atb - matmul(ata, x)
if (iter.ge.maxiter) then
x = inf_pos()
res = inf_pos()
mode = -1_CI
deallocate(ata)
deallocate(atb)
deallocate(resid)
deallocate(pr)
deallocate(s)
return
end if
end do
res = norm2(matmul(A, x) - b)
deallocate(ata)
deallocate(atb)
deallocate(resid)
deallocate(pr)
deallocate(s)
end subroutine solve
end module nnls