Skip to content

Commit

Permalink
Clean up src/NemoStuff.jl (Nemocas#1769)
Browse files Browse the repository at this point in the history
* Clean up `src/NemoStuff.jl`

* Some tests

* Don't deprecate `zero_matrix(MatElem, ...)` and
`identity_matrix(MatElem, ...)`
  • Loading branch information
joschmitt authored Aug 27, 2024
1 parent ad40661 commit d607b88
Show file tree
Hide file tree
Showing 32 changed files with 445 additions and 445 deletions.
8 changes: 8 additions & 0 deletions src/AbsSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ function Base.hash(a::AbsPowerSeriesRingElem, h::UInt)
return b
end

function lift(R::PolyRing{T}, s::AbsPowerSeriesRingElem{T}) where {T}
t = R()
for x = 0:pol_length(s)
setcoeff!(t, x, polcoeff(s, x))
end
return t
end

###############################################################################
#
# Similar and zero
Expand Down
4 changes: 2 additions & 2 deletions src/AbstractAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,11 @@ using .Solve

################################################################################
#
# Stuff moved from Nemo (to be cleaned up eventually)
# Number fields (some stuff moved from Nemo)
#
################################################################################

include("NemoStuff.jl")
include("NumFields.jl")

################################################################################
#
Expand Down
3 changes: 3 additions & 0 deletions src/Deprecations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ import .Generic: exponent; @deprecate exponent(a::Generic.MPoly{T}, i::Int, j::I
import .Generic: set_exponent_vector!; @deprecate set_exponent_vector!(a::Generic.MPoly{T}, i::Int, exps::Vector{Int}, ::Type{Val{ord}}) where {T <: RingElement, ord} set_exponent_vector!(a, i, exps, Val(ord))
import .Generic: is_gen; @deprecate is_gen(x::Generic.MPoly{T}, ::Type{Val{ord}}) where {T <: RingElement, ord} is_gen(x, Val(ord))
import .Generic: degree; @deprecate degree(f::Generic.MPoly{T}, i::Int, ::Type{Val{ord}}) where {T <: RingElement, ord} degree(f, i, Val(ord))

# deprecated during 0.42.*
@deprecate change_base_ring(p::MPolyRingElem{T}, g, new_polynomial_ring) where {T<:RingElement} map_coefficients(g, p, parent = new_polynomial_ring)
49 changes: 49 additions & 0 deletions src/MPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,55 @@ function coefficients_of_univariate(p::MPolyRingElem,
return coeffs
end

###############################################################################
#
# As univariate polynomials
#
###############################################################################

@doc raw"""
coefficients(f::MPolyRingElem, i::Int)
Return the coefficients of `f` when viewed as a univariate polynomial in the `i`-th
variable.
"""
function coefficients(f::MPolyRingElem, i::Int)
d = degree(f, i)
cf = [MPolyBuildCtx(parent(f)) for j = 0:d]
for (c, e) = zip(coefficients(f), exponent_vectors(f))
a = e[i]
e[i] = 0
push_term!(cf[a+1], c, e)
end
return map(finish, cf)
end

#check with Nemo/ Dan if there are better solutions
#the block is also not used here I think
#functionality to view mpoly as upoly in variable `i`, so the
#coefficients are mpoly's without variable `i`.
function leading_coefficient(f::MPolyRingElem, i::Int)
g = MPolyBuildCtx(parent(f))
d = degree(f, i)
for (c, e) = zip(coefficients(f), exponent_vectors(f))
if e[i] == d
e[i] = 0
push_term!(g, c, e)
end
end
return finish(g)
end

@doc raw"""
content(f::MPolyRingElem, i::Int)
Return the content of `f` as a polynomial in the variable `i`, i.e. the gcd of
all the coefficients when viewed as univariate polynomial in `i`.
"""
function content(f::MPolyRingElem, i::Int)
return reduce(gcd, coefficients(f, i))
end

################################################################################
#
# Change base ring
Expand Down
2 changes: 2 additions & 0 deletions src/Map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ end

Base.broadcastable(M::Map) = Ref(M)

Base.:\(f::Map, x) = preimage(f, x)

###############################################################################
#
# CompositeMap
Expand Down
72 changes: 72 additions & 0 deletions src/Matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ function _to_indices(x, rows, cols)
(rows, cols)
end

sub(M::MatElem, r::AbstractVector{<:Integer}, c::AbstractVector{<:Integer}) = M[r, c]

function Base.view(M::MatElem,
rows::Union{Int,Colon,AbstractVector{Int}},
cols::Union{Int,Colon,AbstractVector{Int}})
Expand Down Expand Up @@ -790,6 +792,52 @@ function *(x::MatElem{T}, y::MatElem{T}) where {T <: NCRingElement}
return A
end

function add!(c::MatrixElem{T}, a::MatrixElem{T}, b::MatrixElem{T}) where T <: NCRingElement
check_parent(a, b)
check_parent(a, c)
for i = 1:nrows(c)
for j = 1:ncols(c)
c[i, j] = add!(c[i, j], a[i, j], b[i, j])
end
end
return c
end

function mul!(c::MatElem{T}, a::MatElem{T}, b::MatElem{T}) where T <: NCRingElement
@assert base_ring(a) === base_ring(b) && base_ring(a) === base_ring(c)
ncols(a) != nrows(b) && error("Incompatible matrix dimensions")
nrows(c) != nrows(a) && error("Incompatible matrix dimensions")
ncols(c) != ncols(b) && error("Incompatible matrix dimensions")

if c === a || c === b
d = parent(a)()
return mul!(d, a, b)
end

t = base_ring(a)()
for i = 1:nrows(a)
for j = 1:ncols(b)
c[i, j] = zero!(c[i, j])
for k = 1:ncols(a)
c[i, j] = addmul_delayed_reduction!(c[i, j], a[i, k], b[k, j], t)
end
c[i, j] = reduce!(c[i, j])
end
end
return c
end

function sub!(c::MatrixElem{T}, a::MatrixElem{T}, b::MatrixElem{T}) where T <: NCRingElement
check_parent(a, b)
check_parent(a, c)
for i = 1:nrows(c)
for j = 1:ncols(c)
c[i, j] = sub!(c[i, j], a[i, j], b[i, j])
end
end
return c
end

###############################################################################
#
# Ad hoc binary operators
Expand Down Expand Up @@ -1013,6 +1061,25 @@ function *(x::Vector{T}, y::MatrixElem{T}) where T <: NCRingElement
return mul!(T[base_ring(y)() for j in 1:ncols(y)], x, y)
end

function mul!(c::MatrixElem{T}, a::MatrixElem{T}, b::T) where T <: NCRingElement
@assert base_ring(a) === parent(b) && base_ring(a) === base_ring(c)
nrows(c) != nrows(a) && error("Incompatible matrix dimensions")
ncols(c) != ncols(a) && error("Incompatible matrix dimensions")

if c === a
d = parent(a)()
return mul!(d, a, b)
end

t = base_ring(a)()
for i = 1:nrows(a)
for j = 1:ncols(a)
c[i, j] = mul!(c[i, j], a[i, j], b)
end
end
return c
end

################################################################################
#
# Promotion
Expand Down Expand Up @@ -1373,6 +1440,7 @@ julia> B = transpose(A)
"""
transpose(x::MatrixElem{T}) where T <: NCRingElement

transpose!(A::MatrixElem) = transpose(A)

###############################################################################
#
Expand Down Expand Up @@ -6506,6 +6574,8 @@ function zero_matrix(R::NCRing, r::Int, c::Int)
return z
end

zero_matrix(::Type{MatElem}, R::Ring, n::Int, m::Int) = zero_matrix(R, n, m)

################################################################################
#
# Ones matrix
Expand Down Expand Up @@ -6560,6 +6630,8 @@ function identity_matrix(M::MatElem{T}, n::Int) where T <: NCRingElement
z
end

identity_matrix(::Type{MatElem}, R::Ring, n::Int) = identity_matrix(R, n)

################################################################################
#
# Scalar matrix
Expand Down
2 changes: 2 additions & 0 deletions src/NCRings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ function addmul!(z::T, x::T, y::T, c::T) where T <: NCRingElem
return z
end

addmul!(z::T, x::T, y::T) where T <: NCRingElem = addmul!(z, x, y, parent(z)())

###############################################################################
#
# Basic manipulation
Expand Down
Loading

0 comments on commit d607b88

Please sign in to comment.