Wed, 15 Dec 2021 01:09:09 +0200
Implement ZipArrays
33
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
1 | ########################### |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
2 | # Norms, projections, etc. |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
3 | ########################### |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
4 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
5 | __precompile__() |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
6 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
7 | module VectorMath |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
8 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
9 | ############## |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
10 | # Our exports |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
11 | ############## |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
12 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
13 | export norm₁, |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
14 | norm₂, |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
15 | γnorm₂, |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
16 | norm₂w, |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
17 | norm₂², |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
18 | norm₂w², |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
19 | norm₂₁, |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
20 | γnorm₂₁, |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
21 | dot, |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
22 | mean, |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
23 | proj_norm₂₁ball!, |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
24 | proj_nonneg! |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
25 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
26 | ########################### |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
27 | # Norms and inner products |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
28 | ########################### |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
29 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
30 | @inline function dot(x, y) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
31 | @assert(length(x)==length(y)) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
32 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
33 | accum=0 |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
34 | for i=1:length(y) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
35 | @inbounds accum += x[i]*y[i] |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
36 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
37 | return accum |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
38 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
39 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
40 | @inline function norm₂w²(y, w) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
41 | #Insane memory allocs |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
42 | #return @inbounds sum(i -> y[i]*y[i]*w[i], 1:length(y)) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
43 | accum=0 |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
44 | for i=1:length(y) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
45 | @inbounds accum=accum+y[i]*y[i]*w[i] |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
46 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
47 | return accum |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
48 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
49 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
50 | @inline function norm₂w(y, w) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
51 | return √(norm₂w²(y, w)) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
52 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
53 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
54 | @inline function norm₂²(y) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
55 | #Insane memory allocs |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
56 | #return @inbounds sum(i -> y[i]*y[i], 1:length(y)) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
57 | accum=0 |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
58 | for i=1:length(y) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
59 | @inbounds accum=accum+y[i]*y[i] |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
60 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
61 | return accum |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
62 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
63 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
64 | @inline function norm₂(y) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
65 | return √(norm₂²(y)) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
66 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
67 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
68 | @inline function norm₁(y) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
69 | accum=0 |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
70 | for i=1:length(y) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
71 | @inbounds accum=accum+abs(y[i]) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
72 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
73 | return accum |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
74 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
75 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
76 | @inline function γnorm₂(y, γ) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
77 | hubersq = xsq -> begin |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
78 | x=√xsq |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
79 | return if x > γ |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
80 | x-γ/2 |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
81 | elseif x<-γ |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
82 | -x-γ/2 |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
83 | else |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
84 | xsq/(2γ) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
85 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
86 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
87 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
88 | if γ==0 |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
89 | return norm₂(y) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
90 | else |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
91 | return hubersq(norm₂²(y)) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
92 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
93 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
94 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
95 | function norm₂₁(y) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
96 | return reduce_first_slice((s, x) -> s+norm₂(x), y) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
97 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
98 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
99 | function γnorm₂₁(y,γ) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
100 | return reduce_first_slice((s, x) -> s+γnorm₂(x, γ), y) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
101 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
102 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
103 | function mean(v) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
104 | return sum(v)/prod(size(v)) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
105 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
106 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
107 | ############## |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
108 | # Projections |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
109 | ############## |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
110 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
111 | @inline function proj_norm₂₁ball!(y, α) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
112 | α²=α*α |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
113 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
114 | if ndims(y)==3 && size(y, 1)==2 |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
115 | @inbounds for i=1:size(y, 2) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
116 | @simd for j=1:size(y, 3) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
117 | n² = y[1,i,j]*y[1,i,j]+y[2,i,j]*y[2,i,j] |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
118 | if n²>α² |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
119 | v = α/√n² |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
120 | y[1, i, j] *= v |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
121 | y[2, i, j] *= v |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
122 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
123 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
124 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
125 | else |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
126 | y′=reshape(y, (size(y, 1), prod(size(y)[2:end]))) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
127 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
128 | @inbounds @simd for i=1:size(y′, 2)# in CartesianIndices(size(y)[2:end]) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
129 | n² = norm₂²(@view(y′[:, i])) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
130 | if n²>α² |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
131 | @views y′[:, i] .*= (α/√n²) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
132 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
133 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
134 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
135 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
136 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
137 | @inline function proj_nonneg!(y) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
138 | @inbounds @simd for i=1:length(y) |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
139 | if y[i] < 0 |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
140 | y[i] = 0 |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
141 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
142 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
143 | return y |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
144 | end |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
145 | |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
146 | end # Module |
a60d2f12ef93
Split FunctionalProgramming.jl, VectorMath.jl, and ThreadUtil.jl out of Util.jl.
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff
changeset
|
147 |