|
32 | 32 | x = randn(T, 3, 3)
|
33 | 33 | ΔΩ = Diagonal(randn(T, 3, 3))
|
34 | 34 | test_rrule(
|
35 |
| - SymHerm, Diagonal(ΔΩ), (x, Diagonal(∂x)), (uplo, nothing); |
| 35 | + SymHerm, x ⊢ Diagonal(randn(T, 3)), uplo ⊢ nothing; |
36 | 36 | check_inferred=false,
|
| 37 | + output_tangent = ΔΩ, |
37 | 38 | )
|
38 | 39 | if check_inferred
|
39 | 40 | @inferred (function (SymHerm, x, ΔΩ, ::Val)
|
40 | 41 | return rrule(SymHerm, x, uplo)[2](ΔΩ)
|
41 |
| - end)(SymHerm, x, Diagonal(ΔΩ), Val(uplo)) |
| 42 | + end)(SymHerm, x, ΔΩ, Val(uplo)) |
42 | 43 | end
|
43 | 44 | end
|
44 | 45 | end
|
45 | 46 | end
|
| 47 | + # constructing a `Matrix`/`Array` from `SymHerm` |
46 | 48 | @testset "$(f)(::$(SymHerm){$T}) with uplo=:$uplo" for f in (Matrix, Array),
|
47 | 49 | SymHerm in (Symmetric, Hermitian),
|
48 | 50 | T in (Float64, ComplexF64),
|
49 | 51 | uplo in (:U, :L)
|
| 52 | + |
50 | 53 | x = SymHerm(randn(T, 3, 3), uplo)
|
51 |
| - Δx = randn(T, 3, 3) |
52 |
| - ∂x = SymHerm(randn(T, 3, 3), uplo) |
53 |
| - ΔΩ = f(SymHerm(randn(T, 3, 3), uplo)) |
54 |
| - test_frule(f, (x, Δx)) |
55 |
| - test_frule(f, (x, SymHerm(Δx, uplo))) |
56 |
| - test_rrule(f, ΔΩ, (x, ∂x)) |
| 54 | + test_rrule(f, x) |
| 55 | + |
| 56 | + # intentionally specifying tangents here to test both Matrix and SymHerm tangents |
| 57 | + test_frule(f, x ⊢ randn(T, 3, 3)) |
| 58 | + test_frule(f, x ⊢ SymHerm(randn(T, 3, 3), uplo)) |
57 | 59 | end
|
58 | 60 |
|
59 | 61 | # symmetric/hermitian eigendecomposition follows the sign convention
|
60 | 62 | # v = v * sign(real(vₖ)) * sign(vₖ)', where vₖ is the first or last coordinate
|
61 | 63 | # in the eigenvector. This is unstable for finite differences, but using the convention
|
62 | 64 | # v = v * sign(vₖ)' seems to be more stable, the (co)tangents are related as
|
63 | 65 | # ∂v_ad = sign(real(vₖ)) * ∂v_fd
|
64 |
| - |
65 | 66 | function _eigvecs_stabilize_mat(vectors, uplo)
|
66 | 67 | Ui = Symbol(uplo) === :U ? @view(vectors[end, :]) : @view(vectors[1, :])
|
67 | 68 | return Diagonal(conj.(sign.(Ui)))
|
|
0 commit comments