Skip to content

Commit

Permalink
Revert "Fix format"
Browse files Browse the repository at this point in the history
This reverts commit d21d562.
  • Loading branch information
blegat committed Jul 3, 2024
1 parent 7ea7a08 commit 8159b7a
Show file tree
Hide file tree
Showing 16 changed files with 171 additions and 108 deletions.
26 changes: 14 additions & 12 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ Base.:(//)(X::AlgebraElement, a::Number) = 1 // a * X
function Base.:-(X::AlgebraElement)
return MA.operate_to!(_preallocate_output(*, X, -1), -, X)
end
function MA.promote_operation(
::typeof(*),
::Type{T},
::Type{A},
) where {T<:Number,A<:AlgebraElement}
function MA.promote_operation(::typeof(*), ::Type{T}, ::Type{A}) where {T<:Number, A<:AlgebraElement}
return algebra_promote_operation(*, A, T)
end
function Base.:*(a::Number, X::AlgebraElement)
Expand All @@ -43,11 +39,7 @@ end

for op in [:+, :-, :*]
@eval begin
function MA.promote_operation(
::typeof($op),
::Type{X},
::Type{Y},
) where {X<:AlgebraElement,Y<:AlgebraElement}
function MA.promote_operation(::typeof($op), ::Type{X}, ::Type{Y}) where {X<:AlgebraElement,Y<:AlgebraElement}
return algebra_promote_operation($op, X, Y)
end
function Base.$op(X::AlgebraElement, Y::AlgebraElement)
Expand All @@ -69,13 +61,23 @@ function MA.operate!(::typeof(zero), a::AlgebraElement)
return a
end

function MA.operate_to!(res::AlgebraElement, ::typeof(*), X::AlgebraElement, a::Number)
function MA.operate_to!(
res::AlgebraElement,
::typeof(*),
X::AlgebraElement,
a::Number,
)
@assert parent(res) === parent(X)
MA.operate_to!(coeffs(res), *, coeffs(X), a)
return res
end

function MA.operate_to!(res::AlgebraElement, ::typeof(div), X::AlgebraElement, a::Number)
function MA.operate_to!(
res::AlgebraElement,
::typeof(div),
X::AlgebraElement,
a::Number,
)
@assert parent(res) === parent(X)
MA.operate_to!(coeffs(res), div, coeffs(X), a)
return res
Expand Down
9 changes: 6 additions & 3 deletions src/bases_fixed.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mutable struct FixedBasis{T,I,V<:AbstractVector{T},M<:MTable{T,I}} <: ExplicitBasis{T,I}
mutable struct FixedBasis{T,I,V<:AbstractVector{T},M<:MTable{T,I}} <:
ExplicitBasis{T,I}
elts::V
table::M
end
Expand Down Expand Up @@ -34,5 +35,7 @@ Base.iterate(b::FixedBasis, state) = iterate(b.elts, state)
Base.IndexStyle(::Type{<:FixedBasis{T,I,V}}) where {T,I,V} = Base.IndexStyle(V)

# To break ambiguity
Base.@propagate_inbounds Base.getindex(b::FixedBasis{T,I}, i::I) where {T,I<:Integer} =
b.elts[i]
Base.@propagate_inbounds Base.getindex(
b::FixedBasis{T,I},
i::I,
) where {T,I<:Integer} = b.elts[i]
12 changes: 8 additions & 4 deletions src/coefficients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ Base.iszero(ac::AbstractCoefficients) = isempty(keys(ac))
Base.similar(ac::AbstractCoefficients) = similar(ac, valtype(ac))

similar_type(::Type{<:Vector}, ::Type{T}) where {T} = Vector{T}
similar_type(::Type{<:SparseArrays.SparseVector{C,I}}, ::Type{T}) where {C,I,T} =
SparseArrays.SparseVector{T,I}
similar_type(::Type{<:SparseArrays.SparseVector{C,I}}, ::Type{T}) where {C,I,T} = SparseArrays.SparseVector{T,I}

"""
canonical(ac::AbstractCoefficients)
Expand Down Expand Up @@ -84,7 +83,8 @@ The iterator contains all pairs with `v` potentially non-zero.
return (k => v for (k, v) in zip(keys(ac), values(ac)))
end

@inline nonzero_pairs(v::AbstractVector) = (p for p in pairs(v) if !iszero(last(p)))
@inline nonzero_pairs(v::AbstractVector) =
(p for p in pairs(v) if !iszero(last(p)))
@inline function nonzero_pairs(v::AbstractSparseVector)
return zip(SparseArrays.nonzeroinds(v), SparseArrays.nonzeros(v))
end
Expand Down Expand Up @@ -149,7 +149,11 @@ function MA.operate!(::typeof(zero), X::AbstractCoefficients)
end

# unary -
function MA.operate_to!(res::AbstractCoefficients, ::typeof(-), X::AbstractCoefficients)
function MA.operate_to!(
res::AbstractCoefficients,
::typeof(-),
X::AbstractCoefficients,
)
if res !== X
MA.operate!(zero, res)
end
Expand Down
13 changes: 10 additions & 3 deletions src/diracs_augmented.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ Base.isless(ad1::Augmented, ad2::Augmented) = isless(ad1.elt, ad2.elt)

aug(::Augmented) = 0

struct AugmentedBasis{T,I,A<:Augmented{T},B<:AbstractBasis{T,I}} <: ImplicitBasis{A,I}
struct AugmentedBasis{T,I,A<:Augmented{T},B<:AbstractBasis{T,I}} <:
ImplicitBasis{A,I}
basis::B
end

Expand Down Expand Up @@ -114,12 +115,18 @@ function coeffs!(
)
s = aug(cfs)
if !iszero(s)
throw("Conversion to $target not possible due to non-zero augmentation: $s")
throw(
"Conversion to $target not possible due to non-zero augmentation: $s",
)
end
for (k, v) in nonzero_pairs(cfs)
isone(k) && continue
x = source[k]
MA.operate!(UnsafeAddMul(*), res, SparseCoefficients((target[Augmented(x)],), (v,)))
MA.operate!(
UnsafeAddMul(*),
res,
SparseCoefficients((target[Augmented(x)],), (v,)),
)
end
MA.operate!(canonical, res)
return res
Expand Down
7 changes: 5 additions & 2 deletions src/mstructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ struct ProductNotWellDefined <: Exception
end

function Base.showerror(io::IO, ex::ProductNotWellDefined)
print(io, "Product of elements $(ex.i) and $(ex.j) is not defined on the basis")
print(
io,
"Product of elements $(ex.i) and $(ex.j) is not defined on the basis",
)
print(io, " or the multiplicative structure could not be completed")
if isdefined(ex, :msg)
print(io, ": $(ex.msg)")
Expand Down Expand Up @@ -58,7 +61,7 @@ function MA.operate!(::UnsafeAddMul, res, c)
return res
end

function MA.operate!(op::UnsafeAddMul, res, b, c, args::Vararg{Any,N}) where {N}
function MA.operate!(op::UnsafeAddMul, res, b, c, args::Vararg{Any, N}) where {N}
for (kb, vb) in nonzero_pairs(b)
for (kc, vc) in nonzero_pairs(c)
for (k, v) in nonzero_pairs(op.structure(kb, kc))
Expand Down
3 changes: 2 additions & 1 deletion src/mtables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Multiplicative table, stored explicitly as an AbstractMatrix{I}.
mt[-i, j] == b[star(b[i])*b[j]]
```
"""
struct MTable{T,I,V<:AbstractVector,M<:AbstractMatrix,Ms} <: MultiplicativeStructure
struct MTable{T,I,V<:AbstractVector,M<:AbstractMatrix,Ms} <:
MultiplicativeStructure
elts::V
relts::Dict{T,I}
starof::Vector{I}
Expand Down
15 changes: 6 additions & 9 deletions src/sparse_coeffs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,8 @@ function Base.BroadcastStyle(::BroadcastStyle, ::Base.BroadcastStyle)
end

# Allow broadcasting over scalars.
<<<<<<< Updated upstream
function Base.BroadcastStyle(style::BroadcastStyle, ::Base.Broadcast.DefaultArrayStyle{0})
=======
function Base.BroadcastStyle(
style::BroadcastStyle,
::Base.Broadcast.DefaultArrayStyle{0},
)
@show @__LINE__
>>>>>>> Stashed changes
return style
end

Expand Down Expand Up @@ -174,7 +167,7 @@ function MA.operate!(::typeof(canonical), res::SparseCoefficients, cmp::C) where
end

todelete = BitSet()
for i = firstindex(res.basis_elements):lastindex(res.basis_elements)-1
for i in firstindex(res.basis_elements):lastindex(res.basis_elements)-1
if iszero(res.values[i])
push!(todelete, i)
elseif res.basis_elements[i] == res.basis_elements[i+1]
Expand All @@ -198,7 +191,11 @@ function MA.operate!(::typeof(zero), s::SparseCoefficients)
return s
end

function MA.operate_to!(res::SparseCoefficients, ::typeof(-), X::SparseCoefficients)
function MA.operate_to!(
res::SparseCoefficients,
::typeof(-),
X::SparseCoefficients,
)
return MA.operate_to!(res, *, X, -1)
end

Expand Down
5 changes: 4 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ function Base.one(T::Type, A::AbstractStarAlgebra)
@assert i in basis(A)
return AlgebraElement(sc, A)
else
return AlgebraElement(coeffs(sc, DiracBasis(object(A)), basis(A)), A)
return AlgebraElement(
coeffs(sc, DiracBasis(object(A)), basis(A)),
A,
)
end
end

Expand Down
12 changes: 9 additions & 3 deletions test/caching_allocations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ end
y = spzeros(length(basis(fRG)))
y[1:k] .= 1
Y = AlgebraElement(y, fRG)
@test Y == sum(fRG(basis(fRG)[i]) for i = 1:k)
@test Y == sum(fRG(basis(fRG)[i]) for i in 1:k)

@test Y isa AlgebraElement

Expand Down Expand Up @@ -83,10 +83,16 @@ end
fB = SA.FixedBasis(B; n = nwords(A★, 2), mt = UInt32(nwords(A★, 2)))
fRG = StarAlgebra(A★, fB)

y = SA.SparseCoefficients([first(fB)], [1])
y = SA.SparseCoefficients(
[first(fB)],
[1],
)
Y = AlgebraElement(y, fRG)

z = SA.SparseCoefficients((first(fB),), (1,))
z = SA.SparseCoefficients(
(first(fB),),
(1,),
)
Z = AlgebraElement(z, fRG)
@test _test_op(+, Z, Z) == _test_op(*, 2, Z)
@test _test_op(+, Z, Z) == _test_op(+, Y, Y)
Expand Down
25 changes: 11 additions & 14 deletions test/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ end
@test RG(0) == zero(RG(1))

x = let A = alph, n = 7
elts = [Word(A, rand(1:length(A), rand(0:7))) for _ = 1:n]
elts = [Word(A, rand(1:length(A), rand(0:7))) for _ in 1:n]
vals = rand(-5:5, n)
coeffs = SA.SparseCoefficients(elts, vals)
MA.operate!(SA.canonical, coeffs)
Expand Down Expand Up @@ -93,14 +93,11 @@ end
@test Z == a
@test sprint(show, Z) == "2.0·(id) + 1.0·b·c"
@test sprint(show, 2one(RG) - RG(p)) == "2·(id) - 1·b·c"
@test sprint(show, (2 + im) * one(RG) - (3im) * RG(p)) ==
"(2 + 1im)·(id) + (0 - 3im)·b·c"

@test sprint(print, (2 + im) * one(RG) - (3im) * RG(p)) ==
"(2 + 1im)·(id) + (0 - 3im)·b·c"
@test sprint(show, (2 + im) * one(RG) - (3im) * RG(p)) == "(2 + 1im)·(id) + (0 - 3im)·b·c"

@test sprint(print, (2 + im) * one(RG) - (3im) * RG(p)) == "(2 + 1im)·(id) + (0 - 3im)·b·c"
@test sprint(show, 1e-9 * one(RG)) == "1.0e-9·(id)"
@test sprint((io, x) -> show(io, "text/latex", x), 1e-9 * one(RG)) ==
"\$\$ 1.0 \\cdot 10^{-9} \\cdot (id) \$\$"
@test sprint((io, x) -> show(io, "text/latex", x), 1e-9 * one(RG)) == "\$\$ 1.0 \\cdot 10^{-9} \\cdot (id) \$\$"

@test LinearAlgebra.norm(a, 1) == 3

Expand All @@ -118,12 +115,12 @@ end
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 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 \$\$"

@test sprint((io, x) -> show(io, "text/latex", x),
SA.AlgebraElement(SA.SparseCoefficients([p], [latex]), RG)) ==
"\$\$ (\\(β∀) \\cdot b·c \$\$"

end
20 changes: 15 additions & 5 deletions test/group_algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
@test contains(sprint(show, RG), "*-algebra of")

@testset "Module structure" begin
sc = SA.SparseCoefficients(collect(SA.object(RG)), ones(Int, length(basis(RG))))
sc = SA.SparseCoefficients(
collect(SA.object(RG)),
ones(Int, length(basis(RG))),
)
a = AlgebraElement(sc, RG)

@test -a isa AlgebraElement
Expand Down Expand Up @@ -43,14 +46,19 @@
end

@testset "Additive structure" begin
sc = SA.SparseCoefficients(collect(SA.object(RG)), ones(Int, length(basis(RG))))
sc = SA.SparseCoefficients(
collect(SA.object(RG)),
ones(Int, length(basis(RG))),
)
a = AlgebraElement(sc, RG)
b = sum(sign(g) * RG(g) for g in G)

@test a == sum(RG(g) for g in G)

@test 1 / 2 * (coeffs(a + b)) ==
SA.SparseCoefficients(collect(SA.object(RG)), [1.0, 0.0, 1.0, 0.0, 1.0, 0.0])
@test 1 / 2 * (coeffs(a + b)) == SA.SparseCoefficients(
collect(SA.object(RG)),
[1.0, 0.0, 1.0, 0.0, 1.0, 0.0],
)

g, h = PermutationGroups.gens(G)
k = g * h
Expand All @@ -69,7 +77,9 @@
@test coeffs(a + b - 2a) == coeffs(a) + coeffs(b) - 2coeffs(a)
@test coeffs(2a // 2) == 2coeffs(a) // 2 == coeffs(a)
@test coeffs(3a) == 3.0coeffs(a)
@test coeffs(2a) - coeffs(a // 1) == 2coeffs(a) - coeffs(a) // 1 == coeffs(a)
@test coeffs(2a) - coeffs(a // 1) ==
2coeffs(a) - coeffs(a) // 1 ==
coeffs(a)
@test div(3coeffs(a), 2) == coeffs(a)
end

Expand Down
26 changes: 17 additions & 9 deletions test/monoid_algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@
let g = basis(fRG)[rand(1:121)]
X = RG(g)
Y = -RG(star(g))
for i = 1:3
for i in 1:3
coeffs(X)[basis(fRG)[rand(1:121)]] += rand(-3:3)
coeffs(Y)[basis(fRG)[rand(1:121)]] -= rand(3:3)
end

@test coeffs(X) == coeffs(coeffs(X, basis(fRG)), basis(fRG), basis(RG))
@test coeffs(Y) == coeffs(coeffs(Y, basis(fRG)), basis(fRG), basis(RG))
@test coeffs(X) ==
coeffs(coeffs(X, basis(fRG)), basis(fRG), basis(RG))
@test coeffs(Y) ==
coeffs(coeffs(Y, basis(fRG)), basis(fRG), basis(RG))

fX = AlgebraElement(coeffs(X, basis(fRG)), fRG)
fY = AlgebraElement(coeffs(Y, basis(fRG)), fRG)
Expand All @@ -51,13 +53,19 @@
@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))
@test coeffs(fY) == coeffs(coeffs(fY, basis(RG)), basis(RG), basis(fRG))
@test coeffs(fX) ==
coeffs(coeffs(fX, basis(RG)), basis(RG), basis(fRG))
@test coeffs(fY) ==
coeffs(coeffs(fY, basis(RG)), basis(RG), basis(fRG))

@test coeffs(X * Y, basis(fRG)) == coeffs(fX * fY)
@test coeffs(X * X, basis(fRG)) == coeffs(fX^2) == coeffs(X^2, basis(fRG))
@test coeffs(X * X, basis(fRG)) ==
coeffs(fX^2) ==
coeffs(X^2, basis(fRG))

@test coeffs(Y * Y, basis(fRG)) == coeffs(fY^2) == coeffs(Y^2, basis(fRG))
@test coeffs(Y * Y, basis(fRG)) ==
coeffs(fY^2) ==
coeffs(Y^2, basis(fRG))

MA.operate_to!(Z, *, X, Y)
MA.operate_to!(fZ, *, fX, fY)
Expand All @@ -71,14 +79,14 @@
end
@testset "mutable arithmetic" begin
a = let l = 12, R = 7
support = [Word(alph, rand(1:length(alph), rand(0:R))) for _ = 1:l]
support = [Word(alph, rand(1:length(alph), rand(0:R))) for _ in 1:l]
vals = rand(-3:3, l)
c = SA.SparseCoefficients(support, vals)
MA.operate!(SA.canonical, c)
AlgebraElement(c, RG)
end
b = let l = 7, R = 3
support = [Word(alph, rand(1:length(alph), rand(0:R))) for _ = 1:l]
support = [Word(alph, rand(1:length(alph), rand(0:R))) for _ in 1:l]
vals = rand(-3:3, l)
c = SA.SparseCoefficients(support, vals)
MA.operate!(SA.canonical, c)
Expand Down
Loading

0 comments on commit 8159b7a

Please sign in to comment.