-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy patharrays.jl
213 lines (186 loc) · 6.3 KB
/
arrays.jl
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""
second_order_or(tracers)
Compute the most conservative elementwise OR of tracer sparsity patterns,
including second-order interactions to update the `hessian` field of `HessianTracer`.
This is functionally equivalent to:
```julia
reduce(^, tracers)
```
"""
function second_order_or(ts::AbstractArray{T}) where {T<:AbstractTracer}
# TODO: improve performance
return reduce(second_order_or, ts; init=myempty(T))
end
function second_order_or(a::T, b::T) where {T<:GradientTracer}
return gradient_tracer_2_to_1(a, b, false, false)
end
function second_order_or(a::T, b::T) where {T<:HessianTracer}
return hessian_tracer_2_to_1(a, b, false, false, false, false, false)
end
"""
first_order_or(tracers)
Compute the most conservative elementwise OR of tracer sparsity patterns,
excluding second-order interactions of `HessianTracer`.
This is functionally equivalent to:
```julia
reduce(+, tracers)
```
"""
function first_order_or(ts::AbstractArray{T}) where {T<:AbstractTracer}
# TODO: improve performance
return reduce(first_order_or, ts; init=myempty(T))
end
function first_order_or(a::T, b::T) where {T<:GradientTracer}
return gradient_tracer_2_to_1(a, b, false, false)
end
function first_order_or(a::T, b::T) where {T<:HessianTracer}
return hessian_tracer_2_to_1(a, b, false, true, false, true, true)
end
#===========#
# Utilities #
#===========#
function split_dual_array(A::AbstractArray{D}) where {D<:Dual}
primals = getproperty.(A, :primal)
tracers = getproperty.(A, :tracer)
return primals, tracers
end
#==================#
# LinearAlgebra.jl #
#==================#
# TODO: replace `second_order_or` by less conservative sparsity patterns when possible
## Determinant
LinearAlgebra.det(A::AbstractMatrix{T}) where {T<:AbstractTracer} = second_order_or(A)
LinearAlgebra.logdet(A::AbstractMatrix{T}) where {T<:AbstractTracer} = second_order_or(A)
function LinearAlgebra.logabsdet(A::AbstractMatrix{T}) where {T<:AbstractTracer}
t1 = second_order_or(A)
t2 = sign(t1) # corresponds to sign of det(A): set first- and second-order derivatives to zero
return (t1, t2)
end
## Norm
function LinearAlgebra.norm(A::AbstractArray{T}, p::Real=2) where {T<:AbstractTracer}
if isone(p) || isinf(p)
return first_order_or(A)
else
return second_order_or(A)
end
end
function LinearAlgebra.opnorm(A::AbstractMatrix{T}, p::Real=2) where {T<:AbstractTracer}
if isone(p) || isinf(p)
return first_order_or(A)
else
return second_order_or(A)
end
end
## Eigenvalues
function LinearAlgebra.eigmax(
A::Union{T,AbstractMatrix{T}}; permute::Bool=true, scale::Bool=true
) where {T<:AbstractTracer}
return second_order_or(A)
end
function LinearAlgebra.eigmin(
A::Union{T,AbstractMatrix{T}}; permute::Bool=true, scale::Bool=true
) where {T<:AbstractTracer}
return second_order_or(A)
end
function LinearAlgebra.eigen(
A::AbstractMatrix{T};
permute::Bool=true,
scale::Bool=true,
sortby::Union{Function,Nothing}=nothing,
) where {T<:AbstractTracer}
LinearAlgebra.checksquare(A)
n = size(A, 1)
t = second_order_or(A)
values = Fill(t, n)
vectors = Fill(t, n, n)
return LinearAlgebra.Eigen(values, vectors)
end
## Inverse
function LinearAlgebra.inv(A::StridedMatrix{T}) where {T<:AbstractTracer}
LinearAlgebra.checksquare(A)
t = second_order_or(A)
return Fill(t, size(A)...)
end
function LinearAlgebra.inv(D::Diagonal{T}) where {T<:AbstractTracer}
ts_in = D.diag
ts_out = similar(ts_in)
for i in 1:length(ts_out)
ts_out[i] = inv(ts_in[i])
end
return Diagonal(ts_out)
end
function LinearAlgebra.pinv(
A::AbstractMatrix{T}; atol::Real=0.0, rtol::Real=0.0
) where {T<:AbstractTracer}
n, m = size(A)
t = second_order_or(A)
return Fill(t, m, n)
end
LinearAlgebra.pinv(D::Diagonal{T}) where {T<:AbstractTracer} = LinearAlgebra.inv(D)
## Division
function LinearAlgebra.:\(
A::AbstractMatrix{T}, B::AbstractVecOrMat
) where {T<:AbstractTracer}
Ainv = LinearAlgebra.pinv(A)
return Ainv * B
end
## Exponential
function LinearAlgebra.exp(A::AbstractMatrix{T}) where {T<:AbstractTracer}
LinearAlgebra.checksquare(A)
n = size(A, 1)
t = second_order_or(A)
return Fill(t, n, n)
end
## Matrix power
function LinearAlgebra.:^(A::AbstractMatrix{T}, p::Integer) where {T<:AbstractTracer}
LinearAlgebra.checksquare(A)
n = size(A, 1)
if iszero(p)
return Fill(myempty(T), n, n)
else
t = second_order_or(A)
return Fill(t, n, n)
end
end
function Base.literal_pow(::typeof(^), D::Diagonal{T}, ::Val{0}) where {T<:AbstractTracer}
ts = similar(D.diag)
ts .= myempty(T)
return Diagonal(ts)
end
#==========================#
# LinearAlgebra.jl on Dual #
#==========================#
# `Duals` should use LinearAlgebra's generic fallback implementations
# to compute the "least conservative" sparsity patterns possible on a scalar level.
# The following three methods are a temporary fix for issue #108.
# TODO: instead overload `lu` on AbstractMatrix of Duals.
function LinearAlgebra.det(A::AbstractMatrix{D}) where {D<:Dual}
primals, tracers = split_dual_array(A)
p = LinearAlgebra.logdet(primals)
t = LinearAlgebra.logdet(tracers)
return D(p, t)
end
function LinearAlgebra.logdet(A::AbstractMatrix{D}) where {D<:Dual}
primals, tracers = split_dual_array(A)
p = LinearAlgebra.logdet(primals)
t = LinearAlgebra.logdet(tracers)
return D(p, t)
end
function LinearAlgebra.logabsdet(A::AbstractMatrix{D}) where {D<:Dual}
primals, tracers = split_dual_array(A)
p1, p2 = LinearAlgebra.logabsdet(primals)
t1, t2 = LinearAlgebra.logabsdet(tracers)
return (D(p1, t1), D(p2, t2))
end
#==============#
# SparseArrays #
#==============#
# Helper function needed in SparseArrays's sparsematrix, sparsevector and higherorderfns.
# On Tracers, `iszero` and `!iszero` don't return a boolean,
# but we need a function that does to handle the structure of the array.
if VERSION >= v"1.9" # _iszero was added in JuliaSparse/SparseArrays.jl#177
SparseArrays._iszero(t::AbstractTracer) = isemptytracer(t)
SparseArrays._iszero(d::Dual) = isemptytracer(tracer(d)) && iszero(primal(d))
SparseArrays._isnotzero(t::AbstractTracer) = !isemptytracer(t)
SparseArrays._isnotzero(d::Dual) = !isemptytracer(tracer(d)) || !iszero(primal(d))
end