Skip to content

Commit

Permalink
Merge addeq! into add!
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Sep 16, 2024
1 parent 29fbda1 commit ea75b26
Show file tree
Hide file tree
Showing 50 changed files with 161 additions and 245 deletions.
5 changes: 2 additions & 3 deletions docs/src/ring.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,14 @@ mutate the output rather than create a new object.
zero!(a::NCRingElement)
mul!(a::T, b::T, c::T) where T <: NCRingElement
add!(a::T, b::T, c::T) where T <: NCRingElement
addeq!(a::T, b::T) where T <: NCRingElement
addmul!(a::T, b::T, c::T, t::T) where T <: NCRingElement
```

In each case the mutated object is the leftmost parameter.

The `addeq!(a, b)` operation does the same thing as `add!(a, a, b)`. The
The `add!(a, b)` operation does the same thing as `add!(a, a, b)`. The
optional `addmul!(a, b, c, t)` operation does the same thing as
`mul!(t, b, c); addeq!(a, t)` where `t` is a temporary which can be mutated so
`mul!(t, b, c); add!(a, t)` where `t` is a temporary which can be mutated so
that an addition allocation is not needed.

## Random generation
Expand Down
13 changes: 1 addition & 12 deletions docs/src/ring_interface.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,12 +562,6 @@ add!(c::MyElem, a::MyElem, b::MyElem)

Set $c$ to the value $a + b$ in place. Return the mutated value. Aliasing is permitted.

```julia
addeq!(a::MyElem, b::MyElem)
```

Set $a$ to $a + b$ in place. Return the mutated value. Aliasing is permitted.

### Random generation

The random functions are only used for test code to generate test data. They therefore
Expand Down Expand Up @@ -813,7 +807,7 @@ using Random: Random, SamplerTrivial, GLOBAL_RNG
using RandomExtensions: RandomExtensions, Make2, AbstractRNG

import AbstractAlgebra: parent_type, elem_type, base_ring, base_ring_type, parent, is_domain_type,
is_exact_type, canonical_unit, isequal, divexact, zero!, mul!, add!, addeq!,
is_exact_type, canonical_unit, isequal, divexact, zero!, mul!, add!,
get_cached!, is_unit, characteristic, Ring, RingElem, expressify

import Base: show, +, -, *, ^, ==, inv, isone, iszero, one, zero, rand,
Expand Down Expand Up @@ -980,11 +974,6 @@ function add!(f::ConstPoly{T}, g::ConstPoly{T}, h::ConstPoly{T}) where T <: Ring
return f
end

function addeq!(f::ConstPoly{T}, g::ConstPoly{T}) where T <: RingElement
f.c += g.c
return f
end

# Random generation

RandomExtensions.maketype(R::ConstPolyRing, _) = elem_type(R)
Expand Down
12 changes: 6 additions & 6 deletions src/AbsSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ function *(a::AbsPowerSeriesRingElem{T}, b::AbsPowerSeriesRingElem{T}) where T <
if lenz > i
for j = 2:min(lenb, lenz - i + 1)
t = mul!(t, coeff(a, i - 1), coeff(b, j - 1))
d[i + j - 1] = addeq!(d[i + j - 1], t)
d[i + j - 1] = add!(d[i + j - 1], t)
end
end
end
Expand Down Expand Up @@ -740,7 +740,7 @@ function Base.inv(a::AbsPowerSeriesRingElem{T}) where T <: FieldElement
x = set_precision!(x, la[n])
y = set_precision!(y, la[n])
y = mul!(y, minus_a, x)
y = addeq!(y, two)
y = add!(y, two)
x = mul!(x, x, y)
n -= 1
end
Expand Down Expand Up @@ -886,13 +886,13 @@ function sqrt_classical(a::AbsPowerSeriesRingElem; check::Bool=true)
for i = 1:div(n - 1, 2)
j = n - i
p = mul!(p, coeff(asqrt, aval2 + i), coeff(asqrt, aval2 + j))
c = addeq!(c, p)
c = add!(c, p)
end
c *= 2
if (n % 2) == 0
i = div(n, 2)
p = mul!(p, coeff(asqrt, aval2 + i), coeff(asqrt, aval2 + i))
c = addeq!(c, p)
c = add!(c, p)
end
c = coeff(a, n + aval) - c
if check
Expand Down Expand Up @@ -1039,8 +1039,8 @@ function Base.exp(a::AbsPowerSeriesRingElem{T}) where T <: FieldElement
x = set_precision!(x, la[n])
one1 = set_precision!(one1, la[n])
t = -log(x)
t = addeq!(t, one1)
t = addeq!(t, a)
t = add!(t, one1)
t = add!(t, a)
x = mul!(x, x, t)
n -= 1
end
Expand Down
1 change: 1 addition & 0 deletions src/Deprecations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ import .Generic: degree; @deprecate degree(f::Generic.MPoly{T}, i::Int, ::Type{V
change_base_ring(p::MPolyRingElem{T}, g, new_polynomial_ring) where {T<:RingElement} = map_coefficients(g, p, parent = new_polynomial_ring)
mulmod(a::S, b::S, mod::Vector{S}) where {S <: MPolyRingElem} = Base.divrem(a * b, mod)[2]
var"@attr"(__source__::LineNumberNode, __module__::Base.Module, expr::Expr) = var"@attr"(__source__, __module__, :Any, expr) # delegate `@attr functionexpression` to `@attr Any functionexpression` (macros are just functions with this weird extra syntax)
addeq!(a, b) = add!(a, b); export addeq!
10 changes: 5 additions & 5 deletions src/Fraction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -831,13 +831,13 @@ function mul!(c::FracElem{T}, a::FracElem{T}, b::FracElem{T}) where {T <: RingEl
return c
end

function addeq!(a::FracElem{T}, b::FracElem{T}) where {T <: RingElem}
function add!(a::FracElem{T}, b::FracElem{T}) where {T <: RingElem}
d1 = denominator(a, false)
d2 = denominator(b, false)
n1 = numerator(a, false)
n2 = numerator(b, false)
if d1 == d2
a.num = addeq!(a.num, b.num)
a.num = add!(a.num, b.num)
if !isone(d1)
gd = gcd(a.num, d1)
if !isone(gd)
Expand All @@ -848,20 +848,20 @@ function addeq!(a::FracElem{T}, b::FracElem{T}) where {T <: RingElem}
elseif isone(d1)
if n1 !== n2
a.num = mul!(a.num, a.num, d2)
a.num = addeq!(a.num, n2)
a.num = add!(a.num, n2)
else
a.num = n1*d2 + n2
end
a.den = deepcopy(d2)
elseif isone(d2)
a.num = addeq!(a.num, n2*d1)
a.num = add!(a.num, n2*d1)
a.den = deepcopy(d1)
else
gd = gcd(d1, d2)
if isone(gd)
if n1 !== n2
a.num = mul!(a.num, a.num, d2)
a.num = addeq!(a.num, n2*d1)
a.num = add!(a.num, n2*d1)
else
a.num = n1*d2 + n2*d1
end
Expand Down
6 changes: 3 additions & 3 deletions src/MPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -905,13 +905,13 @@ function evaluate(a::MPolyRingElem{T}, vals::Vector{U}) where {T <: RingElement,
j = i = i + 1
while iseven(j) && length(r) > 1
top = pop!(r)
r[end] = addeq!(r[end], top)
r[end] = add!(r[end], top)
j >>= 1
end
end
while length(r) > 1
top = pop!(r)
r[end] = addeq!(r[end], top)
r[end] = add!(r[end], top)
end
return r[1]
end
Expand Down Expand Up @@ -1037,7 +1037,7 @@ function __evaluate(a, vars, vals, powers)
end
M = Generic.MPolyBuildCtx(S)
push_term!(M, c, v)
addeq!(r, t*finish(M))
add!(r, t*finish(M))
end
return r
end
Expand Down
2 changes: 1 addition & 1 deletion src/MatRing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ function *(x::MatRingElem{T}, y::MatRingElem{T}) where {T <: NCRingElement}
A[i, j] = base_ring(x)()
for k = 1:ncols(x)
C = mul!(C, x[i, k], y[k, j])
A[i, j] = addeq!(A[i, j], C)
A[i, j] = add!(A[i, j], C)
end
end
end
Expand Down
Loading

0 comments on commit ea75b26

Please sign in to comment.