Wed, 18 Dec 2019 21:32:27 +0200
put_unless_closed!
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, | |
26 | ⬿ | |
27 | ||
28 | ######################## | |
29 | # Functional programming | |
30 | ######################### | |
31 | ||
32 | curry = (f::Function,y...)->(z...)->f(y...,z...) | |
33 | ||
34 | ############################### | |
35 | # For working with NamedTuples | |
36 | ############################### | |
37 | ||
38 | ⬿ = merge | |
39 | ||
40 | ###### | |
41 | # map | |
42 | ###### | |
43 | ||
44 | @inline function map_first_slice!(f!, y) | |
45 | for i in CartesianIndices(size(y)[2:end]) | |
46 | @inbounds f!(@view(y[:, i])) | |
47 | end | |
48 | end | |
49 | ||
50 | @inline function map_first_slice!(x, f!, y) | |
51 | for i in CartesianIndices(size(y)[2:end]) | |
52 | @inbounds f!(@view(x[:, i]), @view(y[:, i])) | |
53 | end | |
54 | end | |
55 | ||
56 | @inline function reduce_first_slice(f, y; init=0.0) | |
57 | accum=init | |
58 | for i in CartesianIndices(size(y)[2:end]) | |
59 | @inbounds accum=f(accum, @view(y[:, i])) | |
60 | end | |
61 | return accum | |
62 | end | |
63 | ||
64 | ########################### | |
65 | # Norms and inner products | |
66 | ########################### | |
67 | ||
68 | @inline function dot(x, y) | |
69 | @assert(length(x)==length(y)) | |
70 | ||
71 | accum=0 | |
72 | for i=1:length(y) | |
73 | @inbounds accum += x[i]*y[i] | |
74 | end | |
75 | return accum | |
76 | end | |
77 | ||
78 | @inline function norm₂w²(y, w) | |
79 | #Insane memory allocs | |
80 | #return @inbounds sum(i -> y[i]*y[i]*w[i], 1:length(y)) | |
81 | accum=0 | |
82 | for i=1:length(y) | |
83 | @inbounds accum=accum+y[i]*y[i]*w[i] | |
84 | end | |
85 | return accum | |
86 | end | |
87 | ||
88 | @inline function norm₂w(y, w) | |
89 | return √(norm₂w²(y, w)) | |
90 | end | |
91 | ||
92 | @inline function norm₂²(y) | |
93 | #Insane memory allocs | |
94 | #return @inbounds sum(i -> y[i]*y[i], 1:length(y)) | |
95 | accum=0 | |
96 | for i=1:length(y) | |
97 | @inbounds accum=accum+y[i]*y[i] | |
98 | end | |
99 | return accum | |
100 | end | |
101 | ||
102 | @inline function norm₂(y) | |
103 | return √(norm₂²(y)) | |
104 | end | |
105 | ||
106 | @inline function γnorm₂(y, γ) | |
107 | hubersq = xsq -> begin | |
108 | x=√xsq | |
109 | return if x > γ | |
110 | x-γ/2 | |
111 | elseif x<-γ | |
112 | -x-γ/2 | |
113 | else | |
114 | xsq/(2γ) | |
115 | end | |
116 | end | |
117 | ||
118 | if γ==0 | |
119 | return norm₂(y) | |
120 | else | |
121 | return hubersq(norm₂²(y)) | |
122 | end | |
123 | end | |
124 | ||
125 | function norm₂₁(y) | |
126 | return reduce_first_slice((s, x) -> s+norm₂(x), y) | |
127 | end | |
128 | ||
129 | function γnorm₂₁(y,γ) | |
130 | return reduce_first_slice((s, x) -> s+γnorm₂(x, γ), y) | |
131 | end | |
132 | ||
133 | function mean(v) | |
134 | return sum(v)/prod(size(v)) | |
135 | end | |
136 | ||
137 | @inline function proj_norm₂₁ball!(y, α) | |
138 | α²=α*α | |
139 | y′=reshape(y, (size(y, 1), prod(size(y)[2:end]))) | |
140 | ||
141 | @inbounds @simd for i=1:size(y′, 2)# in CartesianIndices(size(y)[2:end]) | |
142 | n² = norm₂²(@view(y′[:, i])) | |
143 | if n²>α² | |
144 | y′[:, i] .*= (α/√n²) | |
145 | end | |
146 | end | |
147 | end | |
148 | ||
149 | end # Module | |
150 |