From 66c12427134979d73af34e9f4a951f7da3665232 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20G=C3=B6ttgens?= Date: Tue, 8 Oct 2024 11:32:34 +0200 Subject: [PATCH] Add `add!`, `sub!` for FreeAssAlgElem --- src/generic/FreeAssociativeAlgebra.jl | 96 +++++++++++++++++++++++++++ src/generic/imports.jl | 1 + 2 files changed, 97 insertions(+) diff --git a/src/generic/FreeAssociativeAlgebra.jl b/src/generic/FreeAssociativeAlgebra.jl index 22d1fddccb..b82756a4a7 100644 --- a/src/generic/FreeAssociativeAlgebra.jl +++ b/src/generic/FreeAssociativeAlgebra.jl @@ -690,6 +690,102 @@ function neg!(z::FreeAssociativeAlgebraElem{T}, a::FreeAssociativeAlgebraElem{T} return z end +function add!(a::FreeAssociativeAlgebraElem{T}, b::FreeAssociativeAlgebraElem{T}) where T <: RingElement + iszero(b) && return a + return add!(zero(a), a, b) +end + +function add!(z::FreeAssociativeAlgebraElem{T}, a::FreeAssociativeAlgebraElem{T}, b::FreeAssociativeAlgebraElem{T}) where T <: RingElement + if z === a + return add!(z, b) + elseif z === b + return add!(z, a) + end + z.coeffs = empty!(z.coeffs) + z.exps = empty!(z.exps) + i = j = 1 + while i <= a.length && j <= b.length + c = word_cmp(a.exps[i], b.exps[j]) + if c < 0 + push!(z.coeffs, b.coeffs[j]) + push!(z.exps, b.exps[j]) + j += 1 + elseif c > 0 + push!(z.coeffs, a.coeffs[i]) + push!(z.exps, a.exps[i]) + i += 1 + else + s = a.coeffs[i] + b.coeffs[j] + if !iszero(s) + push!(z.coeffs, s) + push!(z.exps, a.exps[i]) + end + i += 1 + j += 1 + end + end + while i <= a.length + push!(z.coeffs, a.coeffs[i]) + push!(z.exps, a.exps[i]) + i += 1 + end + while j <= b.length + push!(z.coeffs, b.coeffs[j]) + push!(z.exps, b.exps[j]) + j += 1 + end + z.length = length(z.coeffs) + return z +end + +function sub!(a::FreeAssociativeAlgebraElem{T}, b::FreeAssociativeAlgebraElem{T}) where T <: RingElement + iszero(b) && return a + return sub!(zero(a), a, b) +end + +function sub!(z::FreeAssociativeAlgebraElem{T}, a::FreeAssociativeAlgebraElem{T}, b::FreeAssociativeAlgebraElem{T}) where T <: RingElement + if z === a + return sub!(z, b) + elseif z === b + return sub!(zero(a), a, b) + end + z.coeffs = empty!(z.coeffs) + z.exps = empty!(z.exps) + i = j = 1 + while i <= a.length && j <= b.length + c = word_cmp(a.exps[i], b.exps[j]) + if c < 0 + push!(z.coeffs, -b.coeffs[j]) + push!(z.exps, b.exps[j]) + j += 1 + elseif c > 0 + push!(z.coeffs, a.coeffs[i]) + push!(z.exps, a.exps[i]) + i += 1 + else + s = a.coeffs[i] - b.coeffs[j] + if !iszero(s) + push!(z.coeffs, s) + push!(z.exps, a.exps[i]) + end + i += 1 + j += 1 + end + end + while i <= a.length + push!(z.coeffs, a.coeffs[i]) + push!(z.exps, a.exps[i]) + i += 1 + end + while j <= b.length + push!(z.coeffs, -b.coeffs[j]) + push!(z.exps, b.exps[j]) + j += 1 + end + z.length = length(z.coeffs) + return z +end + ################################################################################ # diff --git a/src/generic/imports.jl b/src/generic/imports.jl index a31d016351..93a7f79b1e 100644 --- a/src/generic/imports.jl +++ b/src/generic/imports.jl @@ -199,6 +199,7 @@ import ..AbstractAlgebra: shift_left import ..AbstractAlgebra: shift_right import ..AbstractAlgebra: snf import ..AbstractAlgebra: sqrt +import ..AbstractAlgebra: sub! import ..AbstractAlgebra: symbols import ..AbstractAlgebra: tail import ..AbstractAlgebra: term_degree