Skip to content

Commit

Permalink
Move stuff, part 3
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Sep 4, 2023
1 parent 3317911 commit 9ca2ddf
Showing 1 changed file with 192 additions and 1 deletion.
193 changes: 192 additions & 1 deletion src/NemoStuff.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Base.:(:)(x::Int, y::Nothing) = 1:0

export neg!

sub!(z::T, x::T, y::T) where {T} = x - y
Expand Down Expand Up @@ -159,7 +161,7 @@ function identity_matrix(::Type{MatElem}, R::Ring, n::Int)
return identity_matrix(R, n)
end

function is_zero_row(M::Matrix{T}, i::Int) where {T<:Integer}
function is_zero_row(M::Matrix, i::Int)
for j = 1:Base.size(M, 2)
if !iszero(M[i, j])
return false
Expand Down Expand Up @@ -244,6 +246,8 @@ end
# needed by ^ (the generic power in Base using square and multiply)
Base.copy(f::Generic.MPoly) = deepcopy(f)
Base.copy(f::Generic.Poly) = deepcopy(f)
Base.copy(a::PolyRingElem) = deepcopy(a)
Base.copy(a::SeriesElem) = deepcopy(a)

################################################################################
#
Expand Down Expand Up @@ -313,6 +317,129 @@ function kernel(M::MatrixElem, R::Ring; side::Symbol=:right)
return kernel(MP, side=side)
end

################################################################################
#
# Concatenation of matrices
#
################################################################################

@doc raw"""
vcat(A::Vector{Mat}) -> Mat
Forms a big matrix by vertically concatenating the matrices in $A$.
All component matrices need to have the same number of columns.
"""
function Base.vcat(A::Vector{T}) where {S<:RingElem,T<:MatElem{S}}
if any(x -> ncols(x) != ncols(A[1]), A)
error("Matrices must have same number of columns")
end
M = zero_matrix(base_ring(A[1]), sum(nrows, A), ncols(A[1]))
s = 0
for i = A
for j = 1:nrows(i)
for k = 1:ncols(i)
M[s+j, k] = i[j, k]
end
end
s += nrows(i)
end
return M
end

function Base.hcat(A::Vector{T}) where {S<:RingElem,T<:MatElem{S}}
if any(x -> nrows(x) != nrows(A[1]), A)
error("Matrices must have same number of rows")
end
M = zero_matrix(base_ring(A[1]), nrows(A[1]), sum(ncols, A))
s = 0
for i = A
for j = 1:ncols(i)
for k = 1:nrows(i)
M[k, s+j] = i[k, j]
end
end
s += ncols(i)
end
return M
end

function Base.hcat(A::MatElem...)
r = nrows(A[1])
c = ncols(A[1])
R = base_ring(A[1])
for i = 2:length(A)
@assert nrows(A[i]) == r
@assert base_ring(A[i]) == R
c += ncols(A[i])
end
X = zero_matrix(R, r, c)
o = 1
for i = 1:length(A)
for j = 1:ncols(A[i])
X[:, o] = A[i][:, j]
o += 1
end
end
return X
end

function Base.cat(A::MatElem...; dims)
@assert dims == (1, 2) || isa(dims, Int)

if isa(dims, Int)
if dims == 1
return hcat(A...)
elseif dims == 2
return vcat(A...)
else
error("dims must be 1, 2, or (1,2)")
end
end

local X
for i = 1:length(A)
if i == 1
X = hcat(A[1], zero_matrix(base_ring(A[1]), nrows(A[1]), sum(Int[ncols(A[j]) for j = 2:length(A)])))
else
X = vcat(X, hcat(zero_matrix(base_ring(A[1]), nrows(A[i]), sum(ncols(A[j]) for j = 1:i-1)), A[i], zero_matrix(base_ring(A[1]), nrows(A[i]), sum(Int[ncols(A[j]) for j = i+1:length(A)]))))
end
end
return X
end

#= seems to be in AA now
function Base.hvcat(rows::Tuple{Vararg{Int}}, A::MatElem...)
B = hcat([A[i] for i=1:rows[1]]...)
o = rows[1]
for j=2:length(rows)
C = hcat([A[i+o] for i=1:rows[j]]...)
o += rows[j]
B = vcat(B, C)
end
return B
end
=#

function Base.vcat(A::MatElem...)
r = nrows(A[1])
c = ncols(A[1])
R = base_ring(A[1])
for i = 2:length(A)
@assert ncols(A[i]) == c
@assert base_ring(A[i]) == R
r += nrows(A[i])
end
X = zero_matrix(R, r, c)
o = 1
for i = 1:length(A)
for j = 1:nrows(A[i])
X[o, :] = A[i][j, :]
o += 1
end
end
return X
end

gens(L::SimpleNumField{T}) where {T} = [gen(L)]

function gen(L::SimpleNumField{T}, i::Int) where {T}
Expand Down Expand Up @@ -351,10 +478,50 @@ function coefficients(f::MPolyRingElem, i::Int)
return map(finish, cf)
end

function change_base_ring(p::MPolyRingElem{T}, g, new_polynomial_ring) where {T<:RingElement}
cvzip = zip(coefficients(p), exponent_vectors(p))
M = MPolyBuildCtx(new_polynomial_ring)
for (c, v) in cvzip
res = g(c)
if !iszero(res)
push_term!(M, g(c), v)
end
end
return finish(M)::elem_type(new_polynomial_ring)
end

function leading_coefficient(f::Generic.MPoly)
return f.coeffs[1]
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

function polynomial_ring(R::Ring; cached::Bool=false)
return polynomial_ring(R, "x", cached=cached)
end

"""
`content` 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

function content(a::PolyRingElem{<:FieldElem})
return one(base_ring(a))
end
Expand All @@ -381,3 +548,27 @@ function Base.lcm(a::T, b::T) where {T<:SeriesElem}
iszero(b) && return b
return gen(parent(a))^max(valuation(a), valuation(b))
end

function gen(R::Union{Generic.ResidueRing{T},Generic.ResidueField{T}}) where {T<:PolyRingElem}
return R(gen(base_ring(R)))
end

function characteristic(R::Union{Generic.ResidueRing{T},Generic.ResidueField{T}}) where {T<:PolyRingElem}
return characteristic(base_ring(base_ring(R)))
end

function size(R::Union{Generic.ResidueRing{T},Generic.ResidueField{T}}) where {T<:ResElem}
return size(base_ring(base_ring(R)))^degree(modulus(R))
end

function size(R::Union{Generic.ResidueRing{T},Generic.ResidueField{T}}) where {T<:PolyRingElem}
return size(base_ring(base_ring(R)))^degree(R.modulus)
end

function lift(a::Generic.ResidueRingElem)
return a.data
end

function lift(a::Generic.ResidueFieldElem)
return a.data
end

0 comments on commit 9ca2ddf

Please sign in to comment.