Skip to content

Commit

Permalink
fix norm/dot and provide coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kalmarek committed May 15, 2024
1 parent 7f17590 commit 653d90d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
13 changes: 9 additions & 4 deletions src/algebra_elts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ end
function LinearAlgebra.norm(a::AlgebraElement, p::Real)
return LinearAlgebra.norm(coeffs(a), p)
end
function LinearAlgebra.dot(a::AlgebraElement, v::AbstractVector)
return LinearAlgebra.dot(coeffs(a), v)

function LinearAlgebra.dot(a::AlgebraElement, b::AlgebraElement)
return LinearAlgebra.dot(coeffs(a), coeffs(b))
end

function LinearAlgebra.dot(w::AbstractVector, b::AlgebraElement)
return LinearAlgebra.dot(w, coeffs(b))
end
function LinearAlgebra.dot(v::AbstractVector, a::AlgebraElement)
return LinearAlgebra.dot(a, v)
function LinearAlgebra.dot(a::AlgebraElement, w::AbstractVector)
return LinearAlgebra.dot(coeffs(a), w)
end
28 changes: 24 additions & 4 deletions src/coefficients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,34 @@ end
end

function LinearAlgebra.norm(sc::AbstractCoefficients, p::Real)
isempty(keys(sc)) && return (0^p)^1 / p
return sum(v^p for v in values(sc))^1 / p
isempty(values(sc)) && return (0^p)^(1 / p)
return sum(abs(v)^p for v in values(sc))^(1 / p)
end

function LinearAlgebra.dot(ac::AbstractCoefficients, bc::AbstractCoefficients)
if isempty(values(ac)) || isempty(values(bc))
return zero(Base._return_type(*, Tuple{valtype(ac),valtype(bc)}))

Check warning on line 101 in src/coefficients.jl

View check run for this annotation

Codecov / codecov/patch

src/coefficients.jl#L101

Added line #L101 was not covered by tests
else
return sum(c * star(bc[i]) for (i, c) in nonzero_pairs(ac))
end
end

function LinearAlgebra.dot(w::AbstractVector, ac::AbstractCoefficients)
@assert key_type(ac) <: Integer
if isempty(values(ac))
return zero(Base._return_type(*, eltype(w), valtype(ac)))

Check warning on line 110 in src/coefficients.jl

View check run for this annotation

Codecov / codecov/patch

src/coefficients.jl#L107-L110

Added lines #L107 - L110 were not covered by tests
else
return sum(w[i] * star(v) for (i, v) in nonzero_pairs(ac))

Check warning on line 112 in src/coefficients.jl

View check run for this annotation

Codecov / codecov/patch

src/coefficients.jl#L112

Added line #L112 was not covered by tests
end
end

function LinearAlgebra.dot(ac::AbstractCoefficients, w::AbstractVector)
@assert key_type(ac) <: Integer
isempty(keys(ac)) && return zero(eltype(v))
return sum(v * w[i] for (i, v) in nonzero_pairs(ac))
if isempty(values(ac))
return zero(Base._return_type(*, eltype(w), valtype(ac)))

Check warning on line 119 in src/coefficients.jl

View check run for this annotation

Codecov / codecov/patch

src/coefficients.jl#L116-L119

Added lines #L116 - L119 were not covered by tests
else
return sum(v * star(w[i]) for (i, v) in nonzero_pairs(ac))

Check warning on line 121 in src/coefficients.jl

View check run for this annotation

Codecov / codecov/patch

src/coefficients.jl#L121

Added line #L121 was not covered by tests
end
end

# general mutable API
Expand Down
8 changes: 5 additions & 3 deletions test/monoid_algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@
fX = AlgebraElement(coeffs(X, basis(fRG)), fRG)
fY = AlgebraElement(coeffs(Y, basis(fRG)), fRG)

@test LinearAlgebra.dot(fX, coeffs(fX))
norm(fX)^2
LinearAlgebra.dot(coeffs(fX), fX)
@test dot(X, Y) == dot(fX, fY) == dot(coeffs(X), coeffs(Y))
@test dot(fX, coeffs(fY)) == dot(coeffs(fX), fY)

@test dot(X, X) norm(X)^2 dot(coeffs(X), coeffs(X))
@test dot(fX, fX) norm(fX)^2 dot(coeffs(fX), coeffs(fX))

@test coeffs(fX) ==
coeffs(coeffs(fX, basis(RG)), basis(RG), basis(fRG))
Expand Down

0 comments on commit 653d90d

Please sign in to comment.