Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add adhoc arithmetics for FreeAssociativeAlgebraElem #1866

Merged
merged 4 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions src/generic/FreeAssociativeAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,14 @@ function (a::FreeAssociativeAlgebra{T})(b::T) where T
return FreeAssociativeAlgebraElem{T}(a, T[b], [Int[]], 1)
end

function (a::FreeAssociativeAlgebra{T})(b::Integer) where T
iszero(b) && return zero(a)
function (a::FreeAssociativeAlgebra{T})(b::Union{Integer, Rational, AbstractFloat}) where T
R = base_ring(a)
return FreeAssociativeAlgebraElem{T}(a, T[R(b)], [Int[]], 1)
return a(R(b))
end

function (a::FreeAssociativeAlgebra{T})(b::T) where {T <: Union{Integer, Rational, AbstractFloat}}
iszero(b) && return zero(a)
return FreeAssociativeAlgebraElem{T}(a, T[b], [Int[]], 1)
end

function (a::FreeAssociativeAlgebra{T})(b::FreeAssociativeAlgebraElem{T}) where T <: RingElement
Expand Down Expand Up @@ -633,6 +637,42 @@ end
#
###############################################################################

function *(a::FreeAssociativeAlgebraElem, n::Union{Integer, Rational, AbstractFloat})
z = zero(a)
fit!(z, length(a))
j = 1
for i = 1:length(a)
c = a.coeffs[i]*n
if !iszero(c)
z.coeffs[j] = c
z.exps[j] = a.exps[i]
j += 1
end
end
z.length = j - 1
return z
end

function *(a::FreeAssociativeAlgebraElem{T}, n::T) where {T <: RingElem}
z = zero(a)
fit!(z, length(a))
j = 1
for i = 1:length(a)
c = a.coeffs[i]*n
if !iszero(c)
z.coeffs[j] = c
z.exps[j] = a.exps[i]
j += 1
end
end
z.length = j - 1
return z
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about reducing code duplication and increasing flexibility by doing this instead:

Suggested change
function *(a::FreeAssociativeAlgebraElem, n::Union{Integer, Rational, AbstractFloat})
z = zero(a)
fit!(z, length(a))
j = 1
for i = 1:length(a)
c = a.coeffs[i]*n
if !iszero(c)
z.coeffs[j] = c
z.exps[j] = a.exps[i]
j += 1
end
end
z.length = j - 1
return z
end
function *(a::FreeAssociativeAlgebraElem{T}, n::T) where {T <: RingElem}
z = zero(a)
fit!(z, length(a))
j = 1
for i = 1:length(a)
c = a.coeffs[i]*n
if !iszero(c)
z.coeffs[j] = c
z.exps[j] = a.exps[i]
j += 1
end
end
z.length = j - 1
return z
end
function mul!(z::T, a::T, n::RingElement) where {T <: FreeAssociativeAlgebraElem}
fit!(z, length(a))
j = 1
for i = 1:length(a)
c = a.coeffs[i]*n
if !iszero(c)
z.coeffs[j] = c
z.exps[j] = a.exps[i]
j += 1
end
end
z.length = j - 1
return z
end
*(a::FreeAssociativeAlgebraElem, n:: Union{Integer, Rational, AbstractFloat}) = mul!(z, a, n)
*(a::FreeAssociativeAlgebraElem{T}, n::T) where {T <: RingElem} = mul!(z, a, n)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature for mul! here is too wide, as this would capture mul!(::T, ::T, ::T) where T <: FreeAssociativeAlgebraElem as well. But I added something similar, with a restricted signature


*(n::Union{Integer, Rational, AbstractFloat}, a::FreeAssociativeAlgebraElem) = a*n

*(n::T, a::FreeAssociativeAlgebraElem{T}) where {T <: RingElem} = a*n

function divexact(
a::FreeAssociativeAlgebraElem{T},
b::Integer;
Expand Down
58 changes: 58 additions & 0 deletions test/generic/FreeAssociativeAlgebra-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,64 @@ end
end
end

@testset "Generic.FreeAssociativeAlgebra.adhoc_binary" begin
R, x = ZZ["y"]

for num_vars = 1:10
var_names = ["x$j" for j in 1:num_vars]

S, varlist = free_associative_algebra(R, var_names)

for iter = 1:100
f = rand(S, 0:5, 0:100, 0:0, -100:100)

d1 = rand(-20:20)
d2 = rand(-20:20)
g1 = rand(R, 0:2, -10:10)
g2 = rand(R, 0:2, -10:10)

@test f*d1 + f*d2 == (d1 + d2)*f
@test f*BigInt(d1) + f*BigInt(d2) == (BigInt(d1) + BigInt(d2))*f
@test f*g1 + f*g2 == (g1 + g2)*f

@test f + d1 + d2 == d1 + d2 + f
@test f + BigInt(d1) + BigInt(d2) == BigInt(d1) + BigInt(d2) + f
@test f + g1 + g2 == g1 + g2 + f

@test f - d1 - d2 == -((d1 + d2) - f)
@test f - BigInt(d1) - BigInt(d2) == -((BigInt(d1) + BigInt(d2)) - f)
@test f - g1 - g2 == -((g1 + g2) - f)

@test f + d1 - d1 == f
@test f + BigInt(d1) - BigInt(d1) == f
@test f + g1 - g1 == f

if !iszero(d1)
@test divexact(d1 * f, d1) == f
@test divexact(d1 * f, BigInt(d1)) == f
end
end
end

S, (x,y) = free_associative_algebra(QQ, [:x, :y])
f = x^2 + x*y^2*x + QQ(5)*y - QQ(1//2)*y*x

@test 2 + f == QQ(2) + f
@test f + 2 == f + QQ(2)
@test 1//2 + f == QQ(1//2) + f
@test f + 1//2 == f + QQ(1//2)

@test 2 - f == QQ(2) - f
@test f - 2 == f - QQ(2)
@test 1//2 - f == QQ(1//2) - f
@test f - 1//2 == f - QQ(1//2)

@test 2 * f == QQ(2) * f
@test f * 2 == f * QQ(2)
@test 1//2 * f == QQ(1//2) * f
@test f * 1//2 == f * QQ(1//2)
end

@testset "Generic.FreeAssociativeAlgebra.NCRing_interface" begin
S, = free_associative_algebra(ZZ, 3)
test_NCRing_interface(S)
Expand Down
Loading