Skip to content

Commit

Permalink
Better unsafe funcs for UnivPoly and a few others (#1838)
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin authored Oct 10, 2024
1 parent 69ba0a5 commit 5170909
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 19 deletions.
22 changes: 14 additions & 8 deletions src/Matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,6 @@ zero(x::MatrixElem{T}, R::NCRing=base_ring(x)) where T <: NCRingElement = zero(x
zero(x::MatrixElem{T}, R::NCRing, r::Int, c::Int) where T <: NCRingElement = zero!(similar(x, R, r, c))
zero(x::MatrixElem{T}, r::Int, c::Int) where T <: NCRingElement = zero(x, base_ring(x), r, c)

function zero!(x::MatrixElem{T}) where T <: NCRingElement
R = base_ring(x)
for i = 1:nrows(x), j = 1:ncols(x)
x[i, j] = zero(R)
end
x
end

@doc raw"""
one(a::MatSpace)
Expand Down Expand Up @@ -867,6 +859,20 @@ function *(x::MatElem{T}, y::MatElem{T}) where {T <: NCRingElement}
return A
end

###############################################################################
#
# Unsafe functions
#
###############################################################################

function zero!(x::MatrixElem{T}) where T <: NCRingElement
R = base_ring(x)
for i = 1:nrows(x), j = 1:ncols(x)
x[i, j] = zero(R)
end
x
end

function add!(c::MatrixElem{T}, a::MatrixElem{T}, b::MatrixElem{T}) where T <: NCRingElement
check_parent(a, b)
check_parent(a, c)
Expand Down
2 changes: 1 addition & 1 deletion src/generic/AbsMSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ end

###############################################################################
#
# Unsafe operators
# Unsafe functions
#
###############################################################################

Expand Down
2 changes: 1 addition & 1 deletion src/generic/FunctionField.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ end

###############################################################################
#
# Unsafe operators
# Unsafe functions
#
###############################################################################

Expand Down
2 changes: 1 addition & 1 deletion src/generic/MatRing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ end

###############################################################################
#
# Unsafe operators
# Unsafe functions
#
###############################################################################

Expand Down
2 changes: 1 addition & 1 deletion src/generic/PuiseuxSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ rand(S::PuiseuxSeriesRingOrField, val_range, scale_range, v...) =

###############################################################################
#
# Unsafe operations
# Unsafe functions
#
###############################################################################

Expand Down
2 changes: 1 addition & 1 deletion src/generic/RationalFunctionField.jl
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ end

###############################################################################
#
# Unsafe operators and functions
# Unsafe functions
#
###############################################################################

Expand Down
17 changes: 13 additions & 4 deletions src/generic/TotalFraction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -391,18 +391,27 @@ end

###############################################################################
#
# Unsafe operators and functions
# Unsafe functions
#
###############################################################################

function zero!(c::TotFrac)
c.num = zero!(c.num)
if !isone(c.den)
c.den = one(base_ring(c))
end
c.den = one!(c.den)
return c
end

function one!(c::TotFrac)
c.num = one!(c.num)
c.den = one!(c.den)
return c
end

function neg!(z::TotFrac, c::TotFrac)
z.num = neg!(z.num, c.num)
return z
end

function mul!(c::TotFrac{T}, a::TotFrac{T}, b::TotFrac{T}) where {T <: RingElem}
d1 = denominator(a, false)
d2 = denominator(b, false)
Expand Down
38 changes: 36 additions & 2 deletions src/generic/UnivPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,21 @@ end
#
###############################################################################

function zero!(a::UnivPoly{T}) where {T <: RingElement}
a.p = zero!(a.p)
return a
end

function one!(a::UnivPoly{T}) where {T <: RingElement}
a.p = one!(a.p)
return a
end

function neg!(z::UnivPoly{T}, a::UnivPoly{T}) where {T <: RingElement}
z.p = neg!(a.p)
return z
end

function fit!(a::UnivPoly, n::Int)
fit!(data(a), n)
end
Expand All @@ -1013,21 +1028,40 @@ function add!(a::UnivPoly{T}, b::UnivPoly{T}, c::UnivPoly{T}) where {T <: RingEl
return a
end

function add!(a::UnivPoly{T}, b::UnivPoly{T}, c::RingElement) where {T <: RingElement}
a.p = add!(a.p, b.p, c)
return a
end

add!(a::UnivPoly{T}, b::RingElement, c::UnivPoly{T}) where {T <: RingElement} = add!(a, c, b)

function sub!(a::UnivPoly{T}, b::UnivPoly{T}, c::UnivPoly{T}) where {T <: RingElement}
a.p = sub!(a.p, b.p, c.p)
return a
end

function sub!(a::UnivPoly{T}, b::UnivPoly{T}, c::RingElement) where {T <: RingElement}
a.p = sub!(a.p, b.p, c)
return a
end

function sub!(a::UnivPoly{T}, b::RingElement, c::UnivPoly{T}) where {T <: RingElement}
a.p = sub!(a.p, b, c.p)
return a
end

function mul!(a::UnivPoly{T}, b::UnivPoly{T}, c::UnivPoly{T}) where {T <: RingElement}
a.p = mul!(a.p, b.p, c.p)
return a
end

function zero!(a::UnivPoly{T}) where {T <: RingElement}
a.p = zero!(a.p)
function mul!(a::UnivPoly{T}, b::UnivPoly{T}, c::RingElement) where {T <: RingElement}
a.p = mul!(a.p, b.p, c)
return a
end

mul!(a::UnivPoly{T}, b::RingElement, c::UnivPoly{T}) where {T <: RingElement} = mul!(a, c, b)

###############################################################################
#
# Promotion rules
Expand Down
3 changes: 3 additions & 0 deletions src/generic/imports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ import ..AbstractAlgebra: number_of_generators
import ..AbstractAlgebra: number_of_rows
import ..AbstractAlgebra: number_of_variables
import ..AbstractAlgebra: numerator
import ..AbstractAlgebra: one!
import ..AbstractAlgebra: order
import ..AbstractAlgebra: parent_type
import ..AbstractAlgebra: pol_length
Expand All @@ -198,6 +199,8 @@ import ..AbstractAlgebra: shift_left
import ..AbstractAlgebra: shift_right
import ..AbstractAlgebra: snf
import ..AbstractAlgebra: sqrt
import ..AbstractAlgebra: sub!
import ..AbstractAlgebra: submul!
import ..AbstractAlgebra: symbols
import ..AbstractAlgebra: tail
import ..AbstractAlgebra: term_degree
Expand Down

0 comments on commit 5170909

Please sign in to comment.