Skip to content

Commit

Permalink
Allow coeffs not defining zero (#45)
Browse files Browse the repository at this point in the history
* Allow coeffs not defining zero

* Add test

* Add comment
  • Loading branch information
blegat authored Jun 13, 2024
1 parent 7550169 commit ba9429b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/sparse_coeffs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ function Base.getindex(sc::SparseCoefficients{K}, key::K) where {K}
k = searchsortedfirst(sc.basis_elements, key; lt = comparable(K))
if k in eachindex(sc.basis_elements)
v = sc.values[k]
return ifelse(sc.basis_elements[k] == key, v, zero(v))
if sc.basis_elements[k] == key
return v
else
return zero(v)
end
else
return zero(valtype(sc))
end
Expand Down
7 changes: 5 additions & 2 deletions test/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,15 @@ end
@test parent(deepcopy(a)) === parent(a)

latex = CustomLaTeXPrint(" \$\$ \\[\\(α_β∀ \\) \\]\t \$\$")
@test sprint((io, x) -> show(io, "text/latex", x),
SA.AlgebraElement(SA.SparseCoefficients([p], [latex]), RG)) ==
a = SA.AlgebraElement(SA.SparseCoefficients([p], [latex]), RG)
# Tests that `getindex` works even if `zero(typeof(latex))` is not defined
@test SA.coeffs(a)[p] == latex
@test sprint((io, x) -> show(io, "text/latex", x), a) ==
"\$\$ (α_β∀) \\cdot b·c \$\$"
# Test that the check for `\\)` handles unicode well
latex = CustomLaTeXPrint("\\(β∀")
@test sprint((io, x) -> show(io, "text/latex", x),
SA.AlgebraElement(SA.SparseCoefficients([p], [latex]), RG)) ==
"\$\$ (\\(β∀) \\cdot b·c \$\$"

end

0 comments on commit ba9429b

Please sign in to comment.