Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add adjoint_coeffs #48

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/bases.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,28 @@ function coeffs!(res, cfs, source::AbstractBasis, target::AbstractBasis)
end
return res
end

"""
adjoint_coeffs(cfs, source, target)
Return `A' * cfs` where `A` is the linear map applied by
`coeffs`.
"""
function adjoint_coeffs(cfs, source::AbstractBasis, target::AbstractBasis)
source === target && return cfs
source == target && return cfs
res = zero_coeffs(valtype(cfs), source)
return adjoint_coeffs!(res, cfs, source, target)
end

function adjoint_coeffs!(res, cfs, source::AbstractBasis, target::AbstractBasis)
MA.operate!(zero, res)
for (k, v) in nonzero_pairs(cfs)
x = target[k]
# If `x` is not in `source` then the corresponding row in `A` is zero
# so the column in `A'` is zero hence we can ignore it.
if x in source
res[source[x]] += v
end
end
return res
end
3 changes: 3 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ coeffs(a::AlgebraElement) = a.coeffs
function coeffs(x::AlgebraElement, b::AbstractBasis)
return coeffs(coeffs(x), basis(x), b)
end
function adjoint_coeffs(a::AlgebraElement, target::AbstractBasis)
return adjoint_coeffs(coeffs(a), target, basis(a))
end
basis(a::AlgebraElement) = basis(parent(a))

function AlgebraElement(coeffs, A::AbstractStarAlgebra)
Expand Down
4 changes: 4 additions & 0 deletions test/abstract_coeffs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
y = AlgebraElement(ACoeffs(coeffs(b, basis(fRG))), fRG)
x, y
end
@test coeffs(α - β, SA.FixedBasis(basis(RG); n = 2)) == [1, -1]
@test_throws KeyError coeffs(α - β, SA.FixedBasis(basis(RG); n = 1))
@test SA.adjoint_coeffs(α - β, SA.FixedBasis(basis(RG); n = 2)) == [1, -1]
@test SA.adjoint_coeffs(α - β, SA.FixedBasis(basis(RG); n = 1)) == [1]
@test coeffs(2α) isa ACoeffs{Int}
@test coeffs(α - β) isa ACoeffs
@test coeffs(α - β // 3) isa ACoeffs{<:Rational}
Expand Down
Loading