Skip to content

Commit

Permalink
Move stuff, part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Sep 4, 2023
1 parent c28e89c commit 8909eb1
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/AbstractAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,14 @@ include("Groups.jl")

include("Deprecations.jl")

################################################################################
#
# Stuff moved from Nemo
#
################################################################################

include("NemoStuff.jl")

###############################################################################
#
# Array creation functions
Expand Down
163 changes: 163 additions & 0 deletions src/NemoStuff.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
export neg!

sub!(z::Rational{Int}, x::Rational{Int}, y::Int) = x - y

Check warning on line 3 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L3

Added line #L3 was not covered by tests

neg!(z::Rational{Int}, x::Rational{Int}) = -x

Check warning on line 5 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L5

Added line #L5 was not covered by tests

add!(z::Rational{Int}, x::Rational{Int}, y::Int) = x + y

Check warning on line 7 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L7

Added line #L7 was not covered by tests

mul!(z::Rational{Int}, x::Rational{Int}, y::Int) = x * y

Check warning on line 9 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L9

Added line #L9 was not covered by tests

################################################################################
#
# Diagonal (block) matrix creation
#
################################################################################

@doc raw"""
diagonal_matrix(x::T...) where T <: RingElem -> MatElem{T}
diagonal_matrix(x::Vector{T}) where T <: RingElem -> MatElem{T}
diagonal_matrix(Q, x::Vector{T}) where T <: RingElem -> MatElem{T}
Returns a diagonal matrix whose diagonal entries are the elements of $x$.
# Examples
```jldoctest

Check failure on line 26 in src/NemoStuff.jl

View workflow job for this annotation

GitHub Actions / Documentation

doctest failure in ~/work/AbstractAlgebra.jl/AbstractAlgebra.jl/src/NemoStuff.jl:26-38 ```jldoctest julia> diagonal_matrix(QQ(1), QQ(2)) [1 0] [0 2] julia> diagonal_matrix([QQ(3), QQ(4)]) [3 0] [0 4] julia> diagonal_matrix(QQ, [5, 6]) [5 0] [0 6] ``` Subexpression: diagonal_matrix(QQ(1), QQ(2)) Evaluated output: ERROR: UndefVarError: `QQ` not defined Stacktrace: [1] top-level scope @ none:1 Expected output: [1 0] [0 2] diff = Warning: Diff output requires color. [1 0] [0 2]ERROR: UndefVarError: `QQ` not defined Stacktrace: [1] top-level scope @ none:1

Check failure on line 26 in src/NemoStuff.jl

View workflow job for this annotation

GitHub Actions / Documentation

doctest failure in ~/work/AbstractAlgebra.jl/AbstractAlgebra.jl/src/NemoStuff.jl:26-38 ```jldoctest julia> diagonal_matrix(QQ(1), QQ(2)) [1 0] [0 2] julia> diagonal_matrix([QQ(3), QQ(4)]) [3 0] [0 4] julia> diagonal_matrix(QQ, [5, 6]) [5 0] [0 6] ``` Subexpression: diagonal_matrix([QQ(3), QQ(4)]) Evaluated output: ERROR: UndefVarError: `QQ` not defined Stacktrace: [1] top-level scope @ none:1 Expected output: [3 0] [0 4] diff = Warning: Diff output requires color. [3 0] [0 4]ERROR: UndefVarError: `QQ` not defined Stacktrace: [1] top-level scope @ none:1

Check failure on line 26 in src/NemoStuff.jl

View workflow job for this annotation

GitHub Actions / Documentation

doctest failure in ~/work/AbstractAlgebra.jl/AbstractAlgebra.jl/src/NemoStuff.jl:26-38 ```jldoctest julia> diagonal_matrix(QQ(1), QQ(2)) [1 0] [0 2] julia> diagonal_matrix([QQ(3), QQ(4)]) [3 0] [0 4] julia> diagonal_matrix(QQ, [5, 6]) [5 0] [0 6] ``` Subexpression: diagonal_matrix(QQ, [5, 6]) Evaluated output: ERROR: UndefVarError: `QQ` not defined Stacktrace: [1] top-level scope @ none:1 Expected output: [5 0] [0 6] diff = Warning: Diff output requires color. [5 0] [0 6]ERROR: UndefVarError: `QQ` not defined Stacktrace: [1] top-level scope @ none:1
julia> diagonal_matrix(QQ(1), QQ(2))
[1 0]
[0 2]
julia> diagonal_matrix([QQ(3), QQ(4)])
[3 0]
[0 4]
julia> diagonal_matrix(QQ, [5, 6])
[5 0]
[0 6]
```
"""
function diagonal_matrix(R::Ring, x::Vector{<:RingElement})
x = R.(x)
M = zero_matrix(R, length(x), length(x))
for i = 1:length(x)
M[i, i] = x[i]

Check warning on line 44 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L40-L44

Added lines #L40 - L44 were not covered by tests
end
return M

Check warning on line 46 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L46

Added line #L46 was not covered by tests
end

function diagonal_matrix(x::T, xs::T...) where {T<:RingElem}
return diagonal_matrix(collect((x, xs...)))

Check warning on line 50 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L49-L50

Added lines #L49 - L50 were not covered by tests
end

diagonal_matrix(x::Vector{<:RingElement}) = diagonal_matrix(parent(x[1]), x)

Check warning on line 53 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L53

Added line #L53 was not covered by tests

@doc raw"""
diagonal_matrix(x::Vector{T}) where T <: MatElem -> MatElem
Returns a block diagonal matrix whose diagonal blocks are the matrices in $x$.
"""
function diagonal_matrix(x::Vector{T}) where {T<:MatElem}
return cat(x..., dims=(1, 2))::T

Check warning on line 61 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L60-L61

Added lines #L60 - L61 were not covered by tests
end

function diagonal_matrix(x::T, xs::T...) where {T<:MatElem}
return cat(x, xs..., dims=(1, 2))::T

Check warning on line 65 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L64-L65

Added lines #L64 - L65 were not covered by tests
end

function diagonal_matrix(R::Ring, x::Vector{<:MatElem})
if length(x) == 0
return zero_matrix(R, 0, 0)

Check warning on line 70 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L68-L70

Added lines #L68 - L70 were not covered by tests
end
x = [change_base_ring(R, i) for i in x]
return diagonal_matrix(x)

Check warning on line 73 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L72-L73

Added lines #L72 - L73 were not covered by tests
end

base_ring(::Vector{Int}) = Int

Check warning on line 76 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L76

Added line #L76 was not covered by tests

function is_symmetric(M::MatElem)
for i in 1:nrows(M)
for j in i:ncols(M)
if M[i, j] != M[j, i]
return false

Check warning on line 82 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L78-L82

Added lines #L78 - L82 were not covered by tests
end
end
end
return true

Check warning on line 86 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L86

Added line #L86 was not covered by tests
end

zero_matrix(::Type{Int}, r, c) = zeros(Int, r, c)

Check warning on line 89 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L89

Added line #L89 was not covered by tests

#TODO: should be done in Nemo/AbstractAlgebra s.w.
# 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)

Check warning on line 94 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L93-L94

Added lines #L93 - L94 were not covered by tests

################################################################################
#
# Minpoly and Charpoly
#
################################################################################

function minpoly(M::MatElem)
k = base_ring(M)
kx, x = polynomial_ring(k, cached=false)
return minpoly(kx, M)

Check warning on line 105 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L102-L105

Added lines #L102 - L105 were not covered by tests
end

function charpoly(M::MatElem)
k = base_ring(M)
kx, x = polynomial_ring(k, cached=false)
return charpoly(kx, M)

Check warning on line 111 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L108-L111

Added lines #L108 - L111 were not covered by tests
end

###############################################################################
#
# Sub
#
###############################################################################

function sub(M::MatElem, rows::Vector{Int}, cols::Vector{Int})
N = zero_matrix(base_ring(M), length(rows), length(cols))
for i = 1:length(rows)
for j = 1:length(cols)
N[i, j] = M[rows[i], cols[j]]

Check warning on line 124 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L120-L124

Added lines #L120 - L124 were not covered by tests
end
end
return N

Check warning on line 127 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L127

Added line #L127 was not covered by tests
end

function sub(M::MatElem{T}, r::AbstractUnitRange{<:Integer}, c::AbstractUnitRange{<:Integer}) where {T}
z = similar(M, length(r), length(c))
for i in 1:length(r)
for j in 1:length(c)
z[i, j] = M[r[i], c[j]]

Check warning on line 134 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L130-L134

Added lines #L130 - L134 were not covered by tests
end
end
return z

Check warning on line 137 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L137

Added line #L137 was not covered by tests
end

#trivia to make life easier

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

Check warning on line 142 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L142

Added line #L142 was not covered by tests

function gen(L::SimpleNumField{T}, i::Int) where {T}
i == 1 || error("index must be 1")
return gen(L)

Check warning on line 146 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L144-L146

Added lines #L144 - L146 were not covered by tests
end

function Base.getindex(L::SimpleNumField{T}, i::Int) where {T}
if i == 0
return one(L)
elseif i == 1
return gen(L)

Check warning on line 153 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L149-L153

Added lines #L149 - L153 were not covered by tests
else
error("index has to be 0 or 1")

Check warning on line 155 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L155

Added line #L155 was not covered by tests
end
end

ngens(L::SimpleNumField{T}) where {T} = 1

Check warning on line 159 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L159

Added line #L159 was not covered by tests

is_unit(a::NumFieldElem) = !iszero(a)

Check warning on line 161 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L161

Added line #L161 was not covered by tests

canonical_unit(a::NumFieldElem) = a

Check warning on line 163 in src/NemoStuff.jl

View check run for this annotation

Codecov / codecov/patch

src/NemoStuff.jl#L163

Added line #L163 was not covered by tests

0 comments on commit 8909eb1

Please sign in to comment.