|
| 1 | +const SYMMETRIC = false |
| 2 | +const HERMITIAN = false |
| 3 | + |
| 4 | +struct JVP!{F,P,B,X,C} |
| 5 | + f::F |
| 6 | + prep::P |
| 7 | + backend::B |
| 8 | + x::X |
| 9 | + contexts::C |
| 10 | +end |
| 11 | + |
| 12 | +struct VJP!{F,P,B,X,C} |
| 13 | + f::F |
| 14 | + prep::P |
| 15 | + backend::B |
| 16 | + x::X |
| 17 | + contexts::C |
| 18 | +end |
| 19 | + |
| 20 | +function (po::JVP!)(res::AbstractVector, v::AbstractVector) |
| 21 | + (; f, backend, x, contexts, prep) = po |
| 22 | + pushforward!(f, (res,), prep, backend, x, (v,), contexts...) |
| 23 | + return res |
| 24 | +end |
| 25 | + |
| 26 | +function (po::VJP!)(res::AbstractVector, v::AbstractVector) |
| 27 | + (; f, backend, x, contexts, prep) = po |
| 28 | + pullback!(f, (res,), prep, backend, x, (v,), contexts...) |
| 29 | + return res |
| 30 | +end |
| 31 | + |
| 32 | +## A |
| 33 | + |
| 34 | +function build_A( |
| 35 | + implicit::ImplicitFunction, |
| 36 | + x::AbstractVector, |
| 37 | + y::AbstractVector, |
| 38 | + z, |
| 39 | + args...; |
| 40 | + suggested_backend::AbstractADType, |
| 41 | +) |
| 42 | + return build_A_aux( |
| 43 | + implicit.representation, implicit, x, y, z, args...; suggested_backend |
| 44 | + ) |
| 45 | +end |
| 46 | + |
| 47 | +function build_A_aux(::MatrixRepresentation, implicit, x, y, z, args...; suggested_backend) |
| 48 | + (; conditions, backend, prep_A) = implicit |
| 49 | + actual_backend = isnothing(backend) ? suggested_backend : backend |
| 50 | + contexts = (Constant(x), Constant(z), map(Constant, args)...) |
| 51 | + A = jacobian(Switch12(conditions), prep_A..., actual_backend, y, contexts...) |
| 52 | + return factorize(A) |
| 53 | +end |
| 54 | + |
| 55 | +function build_A_aux( |
| 56 | + ::OperatorRepresentation, implicit, x, y, z, args...; suggested_backend |
| 57 | +) |
| 58 | + (; conditions, backend, prep_A) = implicit |
| 59 | + actual_backend = isnothing(backend) ? suggested_backend : backend |
| 60 | + contexts = (Constant(x), Constant(z), map(Constant, args)...) |
| 61 | + prep_A_same = prepare_pushforward_same_point( |
| 62 | + Switch12(conditions), prep_A..., actual_backend, y, (zero(y),), contexts... |
| 63 | + ) |
| 64 | + prod! = JVP!(Switch12(conditions), prep_A_same, actual_backend, y, contexts) |
| 65 | + return LinearOperator( |
| 66 | + eltype(y), length(y), length(y), SYMMETRIC, HERMITIAN, prod!, typeof(y) |
| 67 | + ) |
| 68 | +end |
| 69 | + |
| 70 | +## Aᵀ |
| 71 | + |
| 72 | +function build_Aᵀ( |
| 73 | + implicit::ImplicitFunction, |
| 74 | + x::AbstractVector, |
| 75 | + y::AbstractVector, |
| 76 | + z, |
| 77 | + args...; |
| 78 | + suggested_backend::AbstractADType, |
| 79 | +) |
| 80 | + return build_Aᵀ_aux( |
| 81 | + implicit.representation, implicit, x, y, z, args...; suggested_backend |
| 82 | + ) |
| 83 | +end |
| 84 | + |
| 85 | +function build_Aᵀ_aux(::MatrixRepresentation, implicit, x, y, z, args...; suggested_backend) |
| 86 | + (; conditions, backend, prep_Aᵀ) = implicit |
| 87 | + actual_backend = isnothing(backend) ? suggested_backend : backend |
| 88 | + contexts = (Constant(x), Constant(z), map(Constant, args)...) |
| 89 | + Aᵀ = transpose( |
| 90 | + jacobian(Switch12(conditions), prep_Aᵀ..., actual_backend, y, contexts...) |
| 91 | + ) |
| 92 | + return factorize(Aᵀ) |
| 93 | +end |
| 94 | + |
| 95 | +function build_Aᵀ_aux( |
| 96 | + ::OperatorRepresentation, implicit, x, y, z, args...; suggested_backend |
| 97 | +) |
| 98 | + (; conditions, backend, prep_Aᵀ) = implicit |
| 99 | + actual_backend = isnothing(backend) ? suggested_backend : backend |
| 100 | + contexts = (Constant(x), Constant(z), map(Constant, args)...) |
| 101 | + prep_Aᵀ_same = prepare_pullback_same_point( |
| 102 | + Switch12(conditions), prep_Aᵀ..., actual_backend, y, (zero(y),), contexts... |
| 103 | + ) |
| 104 | + prod! = VJP!(Switch12(conditions), prep_Aᵀ_same, actual_backend, y, contexts) |
| 105 | + return LinearOperator( |
| 106 | + eltype(y), length(y), length(y), SYMMETRIC, HERMITIAN, prod!, typeof(y) |
| 107 | + ) |
| 108 | +end |
| 109 | + |
| 110 | +## B |
| 111 | + |
| 112 | +function build_B( |
| 113 | + implicit::ImplicitFunction, |
| 114 | + x::AbstractVector, |
| 115 | + y::AbstractVector, |
| 116 | + z, |
| 117 | + args...; |
| 118 | + suggested_backend::AbstractADType, |
| 119 | +) |
| 120 | + return build_B_aux( |
| 121 | + implicit.representation, implicit, x, y, z, args...; suggested_backend |
| 122 | + ) |
| 123 | +end |
| 124 | + |
| 125 | +function build_B_aux(::MatrixRepresentation, implicit, x, y, z, args...; suggested_backend) |
| 126 | + (; conditions, backend, prep_B) = implicit |
| 127 | + actual_backend = isnothing(backend) ? suggested_backend : backend |
| 128 | + contexts = (Constant(y), Constant(z), map(Constant, args)...) |
| 129 | + return jacobian(conditions, prep_B..., actual_backend, x, contexts...) |
| 130 | +end |
| 131 | + |
| 132 | +function build_B_aux( |
| 133 | + ::OperatorRepresentation, implicit, x, y, z, args...; suggested_backend |
| 134 | +) |
| 135 | + (; conditions, backend, prep_B) = implicit |
| 136 | + actual_backend = isnothing(backend) ? suggested_backend : backend |
| 137 | + contexts = (Constant(y), Constant(z), map(Constant, args)...) |
| 138 | + prep_B_same = prepare_pushforward_same_point( |
| 139 | + conditions, prep_B..., actual_backend, x, (zero(x),), contexts... |
| 140 | + ) |
| 141 | + prod! = JVP!(conditions, prep_B_same, actual_backend, x, contexts) |
| 142 | + return LinearOperator( |
| 143 | + eltype(y), length(y), length(x), SYMMETRIC, HERMITIAN, prod!, typeof(x) |
| 144 | + ) |
| 145 | +end |
| 146 | + |
| 147 | +## Bᵀ |
| 148 | + |
| 149 | +function build_Bᵀ( |
| 150 | + implicit::ImplicitFunction, |
| 151 | + x::AbstractVector, |
| 152 | + y::AbstractVector, |
| 153 | + z, |
| 154 | + args...; |
| 155 | + suggested_backend::AbstractADType, |
| 156 | +) |
| 157 | + return build_Bᵀ_aux( |
| 158 | + implicit.representation, implicit, x, y, z, args...; suggested_backend |
| 159 | + ) |
| 160 | +end |
| 161 | + |
| 162 | +function build_Bᵀ_aux(::MatrixRepresentation, implicit, x, y, z, args...; suggested_backend) |
| 163 | + (; conditions, backend, prep_Bᵀ) = implicit |
| 164 | + actual_backend = isnothing(backend) ? suggested_backend : backend |
| 165 | + contexts = (Constant(y), Constant(z), map(Constant, args)...) |
| 166 | + return transpose(jacobian(conditions, prep_Bᵀ..., actual_backend, x, contexts...)) |
| 167 | +end |
| 168 | + |
| 169 | +function build_Bᵀ_aux( |
| 170 | + ::OperatorRepresentation, implicit, x, y, z, args...; suggested_backend |
| 171 | +) |
| 172 | + (; conditions, backend, prep_Bᵀ) = implicit |
| 173 | + actual_backend = isnothing(backend) ? suggested_backend : backend |
| 174 | + contexts = (Constant(y), Constant(z), map(Constant, args)...) |
| 175 | + prep_Bᵀ_same = prepare_pullback_same_point( |
| 176 | + conditions, prep_Bᵀ..., actual_backend, x, (zero(y),), contexts... |
| 177 | + ) |
| 178 | + prod! = VJP!(conditions, prep_Bᵀ_same, actual_backend, x, contexts) |
| 179 | + return LinearOperator( |
| 180 | + eltype(y), length(x), length(y), SYMMETRIC, HERMITIAN, prod!, typeof(x) |
| 181 | + ) |
| 182 | +end |
0 commit comments