Mon, 30 Mar 2020 16:46:47 -0500
Added README.md
0 | 1 | ######################### |
2 | # Some utility functions | |
3 | ######################### | |
4 | ||
4
59fd17a3cea0
Add __precompile__() for what it is worth
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
5 | __precompile__() |
59fd17a3cea0
Add __precompile__() for what it is worth
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
6 | |
0 | 7 | module Util |
8 | ||
9 | ############## | |
10 | # Our exports | |
11 | ############## | |
12 | ||
13 | export map_first_slice!, | |
14 | reduce_first_slice, | |
15 | norm₂, | |
16 | γnorm₂, | |
17 | norm₂w, | |
18 | norm₂², | |
19 | norm₂w², | |
20 | norm₂₁, | |
21 | γnorm₂₁, | |
22 | dot, | |
23 | mean, | |
24 | proj_norm₂₁ball!, | |
25 | curry, | |
8 | 26 | ⬿, |
9 | 27 | @threadsif, |
10 | 28 | @background, |
29 | @backgroundif | |
8 | 30 | |
31 | ||
32 | ########## | |
33 | # Threads | |
34 | ########## | |
35 | ||
36 | macro threadsif(threads, loop) | |
37 | return esc(:(if $threads | |
38 | Threads.@threads $loop | |
10 | 39 | else |
8 | 40 | $loop |
10 | 41 | end)) |
8 | 42 | end |
0 | 43 | |
9 | 44 | macro background(bgtask, fgtask) |
45 | return :(t = Threads.@spawn $(esc(bgtask)); | |
46 | $(esc(fgtask)); | |
47 | wait(t)) | |
48 | end | |
49 | ||
10 | 50 | macro backgroundif(threads, bgtask, fgtask) |
51 | return :(if $(esc(threads)) | |
52 | @background $(esc(bgtask)) $(esc(fgtask)) | |
53 | else | |
54 | $(esc(bgtask)) | |
55 | $(esc(fgtask)) | |
56 | end) | |
57 | end | |
58 | ||
0 | 59 | ######################## |
60 | # Functional programming | |
61 | ######################### | |
62 | ||
63 | curry = (f::Function,y...)->(z...)->f(y...,z...) | |
64 | ||
65 | ############################### | |
66 | # For working with NamedTuples | |
67 | ############################### | |
68 | ||
69 | ⬿ = merge | |
70 | ||
71 | ###### | |
72 | # map | |
73 | ###### | |
74 | ||
75 | @inline function map_first_slice!(f!, y) | |
76 | for i in CartesianIndices(size(y)[2:end]) | |
77 | @inbounds f!(@view(y[:, i])) | |
78 | end | |
79 | end | |
80 | ||
81 | @inline function map_first_slice!(x, f!, y) | |
82 | for i in CartesianIndices(size(y)[2:end]) | |
83 | @inbounds f!(@view(x[:, i]), @view(y[:, i])) | |
84 | end | |
85 | end | |
86 | ||
87 | @inline function reduce_first_slice(f, y; init=0.0) | |
88 | accum=init | |
89 | for i in CartesianIndices(size(y)[2:end]) | |
90 | @inbounds accum=f(accum, @view(y[:, i])) | |
91 | end | |
92 | return accum | |
93 | end | |
94 | ||
95 | ########################### | |
96 | # Norms and inner products | |
97 | ########################### | |
98 | ||
99 | @inline function dot(x, y) | |
100 | @assert(length(x)==length(y)) | |
101 | ||
102 | accum=0 | |
103 | for i=1:length(y) | |
104 | @inbounds accum += x[i]*y[i] | |
105 | end | |
106 | return accum | |
107 | end | |
108 | ||
109 | @inline function norm₂w²(y, w) | |
110 | #Insane memory allocs | |
111 | #return @inbounds sum(i -> y[i]*y[i]*w[i], 1:length(y)) | |
112 | accum=0 | |
113 | for i=1:length(y) | |
114 | @inbounds accum=accum+y[i]*y[i]*w[i] | |
115 | end | |
116 | return accum | |
117 | end | |
118 | ||
119 | @inline function norm₂w(y, w) | |
120 | return √(norm₂w²(y, w)) | |
121 | end | |
122 | ||
123 | @inline function norm₂²(y) | |
124 | #Insane memory allocs | |
125 | #return @inbounds sum(i -> y[i]*y[i], 1:length(y)) | |
126 | accum=0 | |
127 | for i=1:length(y) | |
128 | @inbounds accum=accum+y[i]*y[i] | |
129 | end | |
130 | return accum | |
131 | end | |
132 | ||
133 | @inline function norm₂(y) | |
134 | return √(norm₂²(y)) | |
135 | end | |
136 | ||
137 | @inline function γnorm₂(y, γ) | |
138 | hubersq = xsq -> begin | |
139 | x=√xsq | |
140 | return if x > γ | |
141 | x-γ/2 | |
142 | elseif x<-γ | |
143 | -x-γ/2 | |
144 | else | |
145 | xsq/(2γ) | |
146 | end | |
147 | end | |
148 | ||
149 | if γ==0 | |
150 | return norm₂(y) | |
151 | else | |
152 | return hubersq(norm₂²(y)) | |
153 | end | |
154 | end | |
155 | ||
156 | function norm₂₁(y) | |
157 | return reduce_first_slice((s, x) -> s+norm₂(x), y) | |
158 | end | |
159 | ||
160 | function γnorm₂₁(y,γ) | |
161 | return reduce_first_slice((s, x) -> s+γnorm₂(x, γ), y) | |
162 | end | |
163 | ||
164 | function mean(v) | |
165 | return sum(v)/prod(size(v)) | |
166 | end | |
167 | ||
168 | @inline function proj_norm₂₁ball!(y, α) | |
169 | α²=α*α | |
170 | ||
7 | 171 | if ndims(y)==3 && size(y, 1)==2 |
172 | @inbounds for i=1:size(y, 2) | |
173 | @simd for j=1:size(y, 3) | |
174 | n² = y[1,i,j]*y[1,i,j]+y[2,i,j]*y[2,i,j] | |
175 | if n²>α² | |
176 | v = α/√n² | |
177 | y[1, i, j] *= v | |
178 | y[2, i, j] *= v | |
179 | end | |
180 | end | |
181 | end | |
182 | else | |
183 | y′=reshape(y, (size(y, 1), prod(size(y)[2:end]))) | |
184 | ||
185 | @inbounds @simd for i=1:size(y′, 2)# in CartesianIndices(size(y)[2:end]) | |
186 | n² = norm₂²(@view(y′[:, i])) | |
187 | if n²>α² | |
188 | y′[:, i] .*= (α/√n²) | |
189 | end | |
0 | 190 | end |
191 | end | |
192 | end | |
193 | ||
194 | end # Module | |
195 |