Skip to content

Commit

Permalink
Add add!, sub! for FreeAssAlgElem
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Oct 10, 2024
1 parent 016e801 commit 5191947
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions src/generic/FreeAssociativeAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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


################################################################################
#
Expand Down

0 comments on commit 5191947

Please sign in to comment.