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

Conversation

lgoettgens
Copy link
Collaborator

@lgoettgens lgoettgens commented Oct 17, 2024

Resolves #1865

(tests copied from MPoly-test.jl, and added another few lines for QQ/Rational{Int} tests)

Comment on lines 640 to 670
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

@lgoettgens lgoettgens force-pushed the lg/adhoc-free-ass-alg branch 2 times, most recently from ae2f362 to df5df26 Compare October 17, 2024 17:19
Copy link

codecov bot commented Oct 17, 2024

Codecov Report

Attention: Patch coverage is 81.81818% with 6 lines in your changes missing coverage. Please review.

Project coverage is 88.15%. Comparing base (5ffdd2d) to head (df5df26).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/generic/FreeAssociativeAlgebra.jl 81.81% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1866      +/-   ##
==========================================
- Coverage   88.17%   88.15%   -0.02%     
==========================================
  Files         120      120              
  Lines       30220    30250      +30     
==========================================
+ Hits        26645    26668      +23     
- Misses       3575     3582       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fingolfin fingolfin merged commit 983f6ec into Nemocas:master Oct 18, 2024
28 of 30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scalar multiplication of FreeAssociativeAlgebraElems does not work as expected from printing
3 participants