-
Notifications
You must be signed in to change notification settings - Fork 63
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
Conversation
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 |
There was a problem hiding this comment.
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:
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) |
There was a problem hiding this comment.
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
ae2f362
to
df5df26
Compare
Codecov ReportAttention: Patch coverage is
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. |
Resolves #1865
(tests copied from MPoly-test.jl, and added another few lines for QQ/Rational{Int} tests)