Sat, 04 Dec 2021 08:57:12 +0200
Update metadata to Julia 1.7 format
27
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
1 | ######################################################## |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
2 | # Abstract class for differentiable functions that have |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
3 | # abstract LinOps as differentials. Also included are |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
4 | # sum composition and squaring. |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
5 | ######################################################## |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
6 | |
21 | 7 | module DifferentiableFN |
8 | ||
9 | using ..LinOps | |
27
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
10 | using ..Util |
21 | 11 | |
25
90f92ee9cb81
Add basic Func to DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
12 | export Func, |
90f92ee9cb81
Add basic Func to DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
13 | DiffF, |
21 | 14 | value, |
15 | differential, | |
27
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
16 | adjoint_differential, |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
17 | Squared, |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
18 | Summed, |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
19 | SummedVOnly |
21 | 20 | |
25
90f92ee9cb81
Add basic Func to DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
21 | abstract type Func{X,Y} end |
90f92ee9cb81
Add basic Func to DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
22 | abstract type DiffF{X,Y,T} <: Func{X, Y} end |
21 | 23 | |
25
90f92ee9cb81
Add basic Func to DifferentiableFN
Tuomo Valkonen <tuomov@iki.fi>
parents:
21
diff
changeset
|
24 | function value(f :: F, x :: X) :: Y where {X, Y, F <: Func{X,Y}} |
21 | 25 | @error "`value` unimplemented" |
26 | end | |
27 | ||
28 | # function (f :: D)(x::X) where {X, Y, T <: LinOp{X, Y}, D <: DiffF{X, Y, T}} | |
29 | # return value(x) | |
30 | # end | |
31 | ||
32 | function differential(f :: DiffF{X,Y,T}, x :: X) :: T where {X, Y, T <: LinOp{X, Y}} | |
33 | @error "`differential` unimplemented" | |
34 | end | |
35 | ||
36 | function adjoint_differential(f :: DiffF{X,Y,T}, x :: X) :: T where {X, Y, T <: LinOp{X, Y}} | |
37 | @error "`adjoint_differential` unimplemented" | |
38 | end | |
39 | ||
40 | function adjoint_differential(f :: DiffF{X,Y, T}, x :: X) :: T where {X, Y, T <: AdjointableOp{X, Y}} | |
41 | return AdjointOp(differential(f, x)) | |
42 | end | |
43 | ||
27
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
44 | # Sum of differentiable functions |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
45 | |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
46 | struct Summed{X, Y, T, D₁ <: DiffF{X, Y, T}, D₂ <: DiffF{X, Y, T}} <: DiffF{X, Y, T} |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
47 | a :: D₁ |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
48 | b :: D₂ |
21 | 49 | end |
27
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
50 | |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
51 | function value(summed :: Summed{X, Y, T, D₁, D₂}, x :: X) where {X, Y, T, D₁ <: DiffF{X, Y, T}, D₂ <: DiffF{X, Y, T}} |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
52 | return value(summed.a, x)+value(summed.b, x) |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
53 | end |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
54 | |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
55 | function differential(summed :: Summed{X, Y, T, D₁, D₂}, x :: X) where {X, Y, T, D₁ <: DiffF{X, Y, T}, D₂ <: DiffF{X, Y, T}} |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
56 | return differential(summed.a, x) + differential(summed.b, x) |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
57 | end |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
58 | |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
59 | # Sum of (non-differentiable) fucntions |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
60 | |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
61 | struct SummedVOnly{X, Y, D₁ <: Func{X, Y}, D₂ <: Func{X, Y}} <: Func{X, Y} |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
62 | a :: D₁ |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
63 | b :: D₂ |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
64 | end |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
65 | |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
66 | function value(summed :: SummedVOnly{X, Y, D₁, D₂}, x :: X) where {X, Y, D₁ <: Func{X, Y}, D₂ <: Func{X, Y}} |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
67 | return value(summed.a, x)+value(summed.b, x) |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
68 | end |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
69 | |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
70 | # Squared norm of a differentiable function as a differentiable function |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
71 | |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
72 | ℝ = Float64 |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
73 | ℝⁿ = Vector{Float64} |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
74 | |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
75 | struct Squared <: DiffF{ℝⁿ, ℝ, MatrixOp{ℝ}} |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
76 | r :: DiffF{ℝⁿ, ℝⁿ, MatrixOp{ℝ}} |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
77 | end |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
78 | |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
79 | function value(sq :: Squared, x :: ℝⁿ) |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
80 | return norm₂²(value(sq.r, x))/2 |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
81 | end |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
82 | |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
83 | function differential(sq :: Squared, x :: ℝⁿ) |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
84 | v = value(sq.r, x) |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
85 | d = differential(sq.r, x) |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
86 | return MatrixOp(v'*d.m) |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
87 | end |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
88 | |
62c62f451a41
DifferentiableFN sums and squares
Tuomo Valkonen <tuomov@iki.fi>
parents:
25
diff
changeset
|
89 | end |