-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from JuliaDiff/aa/restructure
Minor code movement (NFC) and port some matrix types from Nabla
- Loading branch information
Showing
11 changed files
with
102 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Structured matrices | ||
|
||
##### | ||
##### `Diagonal` | ||
##### | ||
|
||
rrule(::Type{<:Diagonal}, d::AbstractVector) = Diagonal(d), Rule(diag) | ||
|
||
rrule(::typeof(diag), A::AbstractMatrix) = diag(A), Rule(Diagonal) | ||
|
||
##### | ||
##### `Symmetric` | ||
##### | ||
|
||
rrule(::Type{<:Symmetric}, A::AbstractMatrix) = Symmetric(A), Rule(_symmetric_back) | ||
|
||
_symmetric_back(ΔΩ) = UpperTriangular(ΔΩ) + LowerTriangular(ΔΩ)' - Diagonal(ΔΩ) | ||
_symmetric_back(ΔΩ::Union{Diagonal,UpperTriangular}) = ΔΩ | ||
|
||
##### | ||
##### `Adjoint` | ||
##### | ||
|
||
# TODO: Deal with complex-valued arrays as well | ||
rrule(::Type{<:Adjoint}, A::AbstractMatrix{<:Real}) = Adjoint(A), Rule(adjoint) | ||
rrule(::Type{<:Adjoint}, A::AbstractVector{<:Real}) = Adjoint(A), Rule(vec∘adjoint) | ||
|
||
rrule(::typeof(adjoint), A::AbstractMatrix{<:Real}) = adjoint(A), Rule(adjoint) | ||
rrule(::typeof(adjoint), A::AbstractVector{<:Real}) = adjoint(A), Rule(vec∘adjoint) | ||
|
||
##### | ||
##### `Transpose` | ||
##### | ||
|
||
rrule(::Type{<:Transpose}, A::AbstractMatrix) = Transpose(A), Rule(transpose) | ||
rrule(::Type{<:Transpose}, A::AbstractVector) = Transpose(A), Rule(vec∘transpose) | ||
|
||
rrule(::typeof(transpose), A::AbstractMatrix) = transpose(A), Rule(transpose) | ||
rrule(::typeof(transpose), A::AbstractVector) = transpose(A), Rule(vec∘transpose) | ||
|
||
##### | ||
##### Triangular matrices | ||
##### | ||
|
||
rrule(::Type{<:UpperTriangular}, A::AbstractMatrix) = UpperTriangular(A), Rule(Matrix) | ||
|
||
rrule(::Type{<:LowerTriangular}, A::AbstractMatrix) = LowerTriangular(A), Rule(Matrix) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Some utility functions for optimizing linear algebra operations that aren't specific | ||
# to any particular rule definition | ||
|
||
# F .* (X - X'), overwrites X | ||
function _mulsubtrans!(X::AbstractMatrix{T}, F::AbstractMatrix{T}) where T<:Real | ||
k = size(X, 1) | ||
@inbounds for j = 1:k, i = 1:j # Iterate the upper triangle | ||
if i == j | ||
X[i,i] = zero(T) | ||
else | ||
X[i,j], X[j,i] = F[i,j] * (X[i,j] - X[j,i]), F[j,i] * (X[j,i] - X[i,j]) | ||
end | ||
end | ||
X | ||
end | ||
|
||
# I - X, overwrites X | ||
function _eyesubx!(X::AbstractMatrix) | ||
n, m = size(X) | ||
@inbounds for j = 1:m, i = 1:n | ||
X[i,j] = (i == j) - X[i,j] | ||
end | ||
X | ||
end | ||
|
||
# X + Y, overwrites X | ||
function _add!(X::AbstractVecOrMat{T}, Y::AbstractVecOrMat{T}) where T<:Real | ||
@inbounds for i = eachindex(X, Y) | ||
X[i] += Y[i] | ||
end | ||
X | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters