@@ -59,6 +59,84 @@ test in-place Hermitian eigendecomposition rules via Mooncake's non-primitive AD
5959"""
6060eigh!_wrapper (f!, A, alg) = (F = f! (project_hermitian! (A), alg); MatrixAlgebraKit. zero! (A); F)
6161
62+ """
63+ qr_gauge_invariant_wrapper(f, A, alg, r)
64+
65+ Wrapper that calls `Q, R = f(A, alg)` and returns only the parts of the decomposition that
66+ are differentiable functions of `A` if `A` has rank `r`: the first `r` columns of `Q`, the
67+ first `r` rows of `R`, and the projector onto the extra columns of `Q` (for `qr_full`).
68+ Used to test forward-mode QR rules with finite differences, which cannot be restricted to
69+ the gauge-invariant subspace through an `output_tangent`.
70+ """
71+ qr_gauge_invariant_wrapper (f, A, alg, r) = qr_gauge_invariant_part (f (A, alg), r)
72+
73+ """
74+ qr!_gauge_invariant_wrapper(f!, A, alg, r)
75+
76+ In-place variant of [`qr_gauge_invariant_wrapper`](@ref), which zeros `A` after calling `f!`.
77+ """
78+ qr!_gauge_invariant_wrapper (f!, A, alg, r) = qr_gauge_invariant_part (call_and_zero! (f!, A, alg), r)
79+
80+ function qr_gauge_invariant_part ((Q, R), r)
81+ minmn = min (size (Q, 1 ), size (R, 2 ))
82+ Q₃ = Q[:, (minmn + 1 ): end ]
83+ return Q[:, 1 : r], R[1 : r, :], Q₃ * Q₃'
84+ end
85+
86+ """
87+ qr_null_gauge_invariant_wrapper(f, A, alg)
88+
89+ Wrapper that calls `N = f(A, alg)` and returns the projector `N * N'` onto the nullspace,
90+ which, unlike `N` itself, is a differentiable function of `A`.
91+ """
92+ qr_null_gauge_invariant_wrapper (f, A, alg) = (N = f (A, alg); N * N' )
93+
94+ """
95+ qr_null!_gauge_invariant_wrapper(f!, A, alg)
96+
97+ In-place variant of [`qr_null_gauge_invariant_wrapper`](@ref), which zeros `A` after calling `f!`.
98+ """
99+ qr_null!_gauge_invariant_wrapper (f!, A, alg) = (N = call_and_zero! (f!, A, alg); N * N' )
100+
101+ """
102+ lq_gauge_invariant_wrapper(f, A, alg, r)
103+
104+ Wrapper that calls `L, Q = f(A, alg)` and returns only the parts of the decomposition that
105+ are differentiable functions of `A` if `A` has rank `r`: the first `r` columns of `L`, the
106+ first `r` rows of `Q`, and the projector onto the extra rows of `Q` (for `lq_full`).
107+ Used to test forward-mode LQ rules with finite differences, which cannot be restricted to
108+ the gauge-invariant subspace through an `output_tangent`.
109+ """
110+ lq_gauge_invariant_wrapper (f, A, alg, r) = lq_gauge_invariant_part (f (A, alg), r)
111+
112+ """
113+ lq!_gauge_invariant_wrapper(f!, A, alg, r)
114+
115+ In-place variant of [`lq_gauge_invariant_wrapper`](@ref), which zeros `A` after calling `f!`.
116+ """
117+ lq!_gauge_invariant_wrapper (f!, A, alg, r) = lq_gauge_invariant_part (call_and_zero! (f!, A, alg), r)
118+
119+ function lq_gauge_invariant_part ((L, Q), r)
120+ minmn = min (size (L, 1 ), size (Q, 2 ))
121+ Q₃ = Q[(minmn + 1 ): end , :]
122+ return L[:, 1 : r], Q[1 : r, :], Q₃' * Q₃
123+ end
124+
125+ """
126+ lq_null_gauge_invariant_wrapper(f, A, alg)
127+
128+ Wrapper that calls `Nᴴ = f(A, alg)` and returns the projector `Nᴴ' * Nᴴ` onto the nullspace,
129+ which, unlike `Nᴴ` itself, is a differentiable function of `A`.
130+ """
131+ lq_null_gauge_invariant_wrapper (f, A, alg) = (Nᴴ = f (A, alg); Nᴴ' * Nᴴ)
132+
133+ """
134+ lq_null!_gauge_invariant_wrapper(f!, A, alg)
135+
136+ In-place variant of [`lq_null_gauge_invariant_wrapper`](@ref), which zeros `A` after calling `f!`.
137+ """
138+ lq_null!_gauge_invariant_wrapper (f!, A, alg) = (Nᴴ = call_and_zero! (f!, A, alg); Nᴴ' * Nᴴ)
139+
62140function stabilize_eigvals! (D:: AbstractVector )
63141 absD = collect (abs .(D))
64142 p = invperm (sortperm (collect (absD))) # rank of abs(D)
0 commit comments