Skip to content

Commit

Permalink
bring back a 3-arg MA.opearate_to!(res, ::UnsafeAddMul, ...)
Browse files Browse the repository at this point in the history
  • Loading branch information
kalmarek committed Dec 10, 2024
1 parent 5ca08fb commit ab37477
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
24 changes: 8 additions & 16 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,28 +135,20 @@ end
function MA.operate_to!(
res::AlgebraElement,
::typeof(*),
A::AlgebraElement,
B::AlgebraElement,
α = true,
)
@assert parent(res) == parent(A)
@assert parent(res) == parent(B)
ABC::Vararg{AlgebraElement,N},
) where {N}
@assert allequal(parent, (res, ABC...))
mstr = mstructure(basis(res))
MA.operate_to!(coeffs(res), mstr, coeffs(A), coeffs(B), α)
MA.operate_to!(coeffs(res), mstr, map(coeffs, ABC)..., true)
return res
end

function MA.operate_to!(
res::AlgebraElement,
mul::UnsafeAddMul{typeof(*)},
A::AlgebraElement,
B::AlgebraElement,
α = true,
)
@assert parent(res) == parent(A)
@assert parent(res) == parent(B)
mstr = mstructure(basis(res))
MA.operate_to!(coeffs(res), mul, coeffs(A), coeffs(B), α)
mul::UnsafeAddMul,
ABC::Vararg{AlgebraElement,N},
) where {N}
MA.operate_to!(coeffs(res), mul, map(coeffs, ABC)..., true)
return res
end

Expand Down
20 changes: 17 additions & 3 deletions src/mstructures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,20 @@ be treated as a scalar.
Canonicalization of the result happens only once at the end of the operation.
"""
function MA.operate_to!(res, ms::MultiplicativeStructure, A, B, α = true)
if res === A || res === B
function MA.operate_to!(
res,
ms::MultiplicativeStructure,
args::Vararg{Any,N},
) where {N}
if any(Base.Fix1(===, res), args)
throw(
ArgumentError(
"Aliasing arguments in multiplication is not supported",
),
)
end
MA.operate!(zero, res)
res = MA.operate_to!(res, UnsafeAddMul(ms), A, B, α)
res = MA.operate_to!(res, UnsafeAddMul(ms), args...)
MA.operate!(canonical, res)
return res
end
Expand Down Expand Up @@ -85,6 +89,16 @@ function MA.operate_to!(res, op::UnsafeAddMul, A, B, α = true)
return res
end

function MA.operate_to!(res, op::UnsafeAddMul, A, B, C, α)
for (kA, vA) in nonzero_pairs(A)
for (kB, vB) in nonzero_pairs(B)
cfs = MA.@rewrite α * vA * vB
MA.operate_to!(res, op, op.structure(kA, kB), C, cfs)
end
end
return res
end

struct DiracMStructure{Op} <: MultiplicativeStructure
op::Op
end
Expand Down
6 changes: 4 additions & 2 deletions test/monoid_algebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@
@test @allocated(MA.operate_to!(d, *, 2, d)) == 0
@test d == 4a

MA.operate_to!(d, *, a, b, 2)
@test d == 2 * a * b
MA.operate_to!(d, *, a, b)
@test d == a * b
MA.operate_to!(d, *, a, b, b)
@test d == a * b * b
end
end
end

0 comments on commit ab37477

Please sign in to comment.