forked from mgckind/somsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
som.f90
141 lines (117 loc) · 3.11 KB
/
som.f90
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
subroutine map(XD,ndim,niter,distlib,np,w,importance,npix,aps,ape)
implicit none
integer :: i1,i,it,niter,np,npix,j,k,ndim,best
integer :: ind_rn(0:np-1)
double precision :: distlib(npix,npix),XD(0:np-1,ndim),inputs(ndim)
double precision :: w(ndim,npix),importance(ndim)
double precision :: sigma0,sigma_single,aps,ape
double precision :: par,Hn,bigN,tt,alpha,sig,Htemp
!f2py intent(in) niter,np,npix,distlib,w,XD,importance,ndim,aps,ape
!f2py intent(in,out,copy) w
!f2py depend(npix) distlib,w
!f2py depend(ndim) XD,w,importance
!f2py depend(np) XD
bigN=1.0d0*niter*np
do i=0,np-1
ind_rn(i)=i
enddo
tt=0.0d0
sigma_single=minval(distlib,mask=distlib .gt. 0)
sigma0=maxval(distlib)
do it=1,niter
call shuffle(ind_rn,np)
alpha=par(tt,aps,ape,bigN)
sig=par(tt,sigma0,sigma_single,bigN)
do j=0,np-1
tt=tt+1.0d0
k=ind_rn(j)
inputs=XD(k,:)
call som_best_cell(inputs,w,npix,ndim,importance,best)
do i1=1,npix
Htemp=Hn(best,i1,distlib,sig,npix)
w(:,i1)=w(:,i1)+alpha*Htemp*(inputs-w(:,i1))
enddo
enddo
enddo
end
!!!!!!!________________________________
subroutine map_b(XD,ndim,niter,distlib,np,w,importance,npix)
implicit none
integer :: i1,i,it,niter,np,npix,j,k,ndim,best
double precision :: distlib(npix,npix),XD(0:np-1,ndim),inputs(ndim),importance(ndim)
double precision :: w(ndim,npix),accum_w(ndim,npix),accum_n(npix)
double precision :: sigma0,sigma_single
double precision :: par,Hn,bigN,tt,sig,Htemp
!f2py intent(in) niter,np,npix,distlib,w,XD,ndim
!f2py intent(in,out,copy) w
!f2py depend(npix) distlib,w
!f2py depend(ndim) XD,w,importance
!f2py depend(np) XD
bigN=1.0d0*niter*np
tt=0.0d0
sigma_single=minval(distlib,mask=distlib .gt. 0)
sigma0=maxval(distlib)
do it=1,niter
sig=par(tt,sigma0,sigma_single,bigN)
accum_w=0.0d0
accum_n=0.0d0
do j=0,np-1
tt=tt+1.0d0
inputs=XD(j,:)
call som_best_cell(inputs,w,npix,ndim,importance,best)
do i1=1,npix
Htemp=Hn(best,i1,distlib,sig,npix)
accum_n(i1)=accum_n(i1)+Htemp
do k=1,ndim
accum_w(k,i1)=accum_w(k,i1)+Htemp*inputs(k)
enddo
enddo
enddo
do k=1,ndim
w(k,:)=accum_w(k,:)/accum_n(:)
enddo
enddo
end
!----------------------------------------------------
function par(t,alphas,alphae,bigN)
implicit none
double precision par,alphas,alphae,t,bigN
par=alphas*((alphae/alphas)**(t/bigN))
end
function Hn(bmu,bindex,mapD,sigma,npix)
implicit none
integer :: bmu,bindex,npix
double precision :: Hn,sigma,mapD(npix,npix)
Hn=exp(-1.0d0*(mapD(bmu,bindex)**2)/(sigma**2))
end
subroutine shuffle(ind_rn,np)
implicit none
integer :: i,j,trn,tmp
integer, intent(in):: np
integer, intent(inout)::ind_rn(0:np-1)
real :: rn
do i=0,np-1
call random_number(rn)
trn=int(rn*np)
j=i+mod(trn,(np-i))
tmp=ind_rn(i)
ind_rn(i)=ind_rn(j)
ind_rn(j)=tmp
enddo
end subroutine
subroutine som_best_cell(inp,wei,npix,ndim,importance,best)
implicit none
integer :: i
integer, intent(in) :: npix,ndim
double precision, intent(in):: inp(ndim), wei(ndim,npix),importance(ndim)
integer, intent(out):: best
double precision :: act,act2
act=10000000.0d0
do i=1,npix
act2=sum(importance(:)*(inp(:)-wei(:,i))**2)
if (act2 .lt. act) then
act=act2
best=i
endif
enddo
end subroutine