Skip to content

Commit

Permalink
Rules for cross-product (#199)
Browse files Browse the repository at this point in the history
* Add cross product rules

* Add cross product tests

* Increment version number

* Relax equality check

* Increment version number

* Bump version number

* Remove chunked frule

* Remove unused method

* Add back complex frule tests
  • Loading branch information
sethaxen authored Jun 22, 2020
1 parent fc4201b commit c7aa2a0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRules"
uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2"
version = "0.6.4"
version = "0.6.5"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
17 changes: 17 additions & 0 deletions src/rulesets/LinearAlgebra/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ function rrule(::typeof(dot), x, y)
return dot(x, y), dot_pullback
end

#####
##### `cross`
#####

function frule((_, Δa, Δb), ::typeof(cross), a::AbstractVector, b::AbstractVector)
return cross(a, b), cross(Δa, b) .+ cross(a, Δb)
end

# TODO: support complex vectors
function rrule(::typeof(cross), a::AbstractVector{<:Real}, b::AbstractVector{<:Real})
Ω = cross(a, b)
function cross_pullback(ΔΩ)
return (NO_FIELDS, @thunk(cross(b, ΔΩ)), @thunk(cross(ΔΩ, a)))
end
return Ω, cross_pullback
end

#####
##### `inv`
#####
Expand Down
17 changes: 17 additions & 0 deletions test/rulesets/LinearAlgebra/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@
rrule_test(dot, randn(), (x, x̄), (y, ȳ))
end
end
@testset "cross" begin
@testset "frule" begin
@testset "$T" for T in (Float64, ComplexF64)
n = 3
x, y = randn(T, n), randn(T, n)
ẋ, ẏ = randn(T, n), randn(T, n)
frule_test(cross, (x, ẋ), (y, ẏ))
end
end
@testset "rrule" begin
n = 3
x, y = randn(n), randn(n)
x̄, ȳ = randn(n), randn(n)
ΔΩ = randn(n)
rrule_test(cross, ΔΩ, (x, x̄), (y, ȳ))
end
end
@testset "inv" begin
N = 3
B = generate_well_conditioned_matrix(N)
Expand Down

2 comments on commit c7aa2a0

@sethaxen
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/16755

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.5 -m "<description of version>" c7aa2a0cb2e212f558bef306f5b51f46f8e9ce23
git push origin v0.6.5

Please sign in to comment.