Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make things faster for orders #1553

Merged
merged 1 commit into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/AlgAss/AlgAss.jl
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,14 @@ function reduce_rows_mod_hnf!(M::ZZMatrix, N::ZZMatrix, rows::Vector{Int})
return M
end

function addmul!(a::AlgAssAbsOrdElem, b::ZZRingElem, c::AlgAssAbsOrdElem)
return add!(a, a, b * c)
function addmul!(a::AlgAssAbsOrdElem, b::ZZRingElem, c::AlgAssAbsOrdElem, d = parent(a)())
mul!(d, b, c)
return add!(a, a, d)
end

function addmul!(a::AbsNumFieldOrderElem, b::ZZRingElem, c::AbsNumFieldOrderElem)
return add!(a, a, b * c)
function addmul!(a::AbsNumFieldOrderElem, b::ZZRingElem, c::AbsNumFieldOrderElem, d = parent(a)())
mul!(d, b, c)
return add!(a, a, d)
end

@doc raw"""
Expand Down Expand Up @@ -358,13 +360,15 @@ function StructureConstantAlgebra(O::Union{AbsNumFieldOrder, AlgAssAbsOrd}, I::U

local _preimage

let BO = BO, basis_elts = basis_elts, r = r
temp = zero(O)

let BO = BO, basis_elts = basis_elts, r = r, temp = temp
function _preimage(a::AssociativeAlgebraElem)
z = zero(O)::eltype(BO)
ca = coefficients(a, copy = false)
for i in 1:r
l = lift(ZZ, ca[i])
addmul!(z, l, BO[basis_elts[i]])
addmul!(z, l, BO[basis_elts[i]], temp)
end
return z
#return sum(lift(coefficients(a, copy = false)[i])*BO[basis_elts[i]] for i = 1:r)
Expand Down Expand Up @@ -407,6 +411,7 @@ function StructureConstantAlgebra(I::Union{ AbsNumFieldOrderIdeal, AlgAssAbsOrdI
@assert order(I) === order(J)

O = order(I)
Oalgebra = _algebra(O)

n = degree(O)
BmatJinI = hnf(basis_matrix(J, copy = false)*basis_mat_inv(I, copy = false), :lowerleft)
Expand Down Expand Up @@ -489,9 +494,21 @@ function StructureConstantAlgebra(I::Union{ AbsNumFieldOrderIdeal, AlgAssAbsOrdI

local _preimage

let BI = BI, basis_elts = basis_elts, r = r
temppp = zero(Oalgebra)

let BI = BI, basis_elts = basis_elts, r = r, Oalgebra = Oalgebra, temppp = temppp
function _preimage(a::AssociativeAlgebraElem)
return O(sum(lift(ZZ, coefficients(a, copy = false)[i])*BI[basis_elts[i]] for i = 1:r))
acoords = map(x -> QQ(lift(ZZ, x)), coefficients(a, copy = false))
z = zero(Oalgebra)
for i in 1:r
if is_zero(acoords[i])
continue
end
temppp = mul!(temppp, acoords[i], BI[basis_elts[i]])
z = add!(z, z, temppp)
end
_zz = O(z)
return _zz
end
end

Expand Down
15 changes: 9 additions & 6 deletions src/AlgAss/AlgMatElem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,6 @@ function add!(c::T, a::T, b::T) where {T <: MatAlgebraElem}
parent(c) != parent(b) && error("Parents don't match.")
A = parent(a)

if c === a || c === b
d = A()
d = add!(d, a, b)
return d
end

c.matrix = add!(c.matrix, a.matrix, b.matrix)
c.has_coeffs = false

Expand Down Expand Up @@ -159,6 +153,15 @@ function addeq!(b::T, a::T) where {T <: MatAlgebraElem}
return b
end

function mul!(c::MatAlgebraElem{T}, a::MatAlgebraElem{T}, b::T) where {T}
parent(c) !== parent(a) && error("Parents don't match.")
A = parent(a)

c.matrix = mul!(c.matrix, a.matrix, b)
c.has_coeffs = false
return c
end

################################################################################
#
# Ad hoc operations
Expand Down
25 changes: 20 additions & 5 deletions src/AlgAss/Elem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -871,19 +871,34 @@ _set_to_copy!(M::QQMatrix, i, j, c) = M[i, j] = c

_set_to_copy!(M, i, j, c) = M[i, j] = deepcopy(c)

function _addmul!(M::MatrixElem, i, j, b, c)
function _addmul!(M::MatrixElem, i, j, b, c, temp = nothing)
return M[i, j] = addmul!(M[i, j], b, c)
end

function _addmul!(M::QQMatrix, i, j, a::QQFieldElem, b::QQFieldElem)
c = mat_entry_ptr(M, i, j)
ccall((:fmpq_addmul, libflint), Nothing, (Ptr{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), c, a, b)
function _addmul!(M::QQMatrix, i, j, a::QQFieldElem, b::QQFieldElem, temp = nothing)
GC.@preserve M begin
c = mat_entry_ptr(M, i, j)
ccall((:fmpq_addmul, libflint), Nothing, (Ptr{QQFieldElem}, Ref{QQFieldElem}, Ref{QQFieldElem}), c, a, b)
end
end

function _addmul!(M::FqMatrix, i, j, a::FqFieldElem, b::FqFieldElem, temp = base_ring(M)())
GC.@preserve M begin
c = Nemo.fq_default_mat_entry_ptr(M, i, j)
mul!(temp, a, b)
ccall((:fq_default_add, libflint), Nothing, (Ptr{FqFieldElem}, Ptr{FqFieldElem}, Ref{FqFieldElem}, Ref{FqField}), c, c, temp, base_ring(M))
end
end

function representation_matrix!(a::Union{AssociativeAlgebraElem, MatAlgebraElem}, M::MatElem, action::Symbol = :left)
A = parent(a)
acoeff = coefficients(a, copy = false)
mt = multiplication_table(A, copy = false)
if base_ring(A) isa QQField
temp = nothing
else
temp = base_ring(A)()
end
GC.@preserve M begin
if action == :left
for i = 1:dim(A)
Expand All @@ -892,7 +907,7 @@ function representation_matrix!(a::Union{AssociativeAlgebraElem, MatAlgebraElem}
end
for j = 1:dim(A)
for k = 1:dim(A)
_addmul!(M, j, k, acoeff[i], mt[i, j, k])
_addmul!(M, j, k, acoeff[i], mt[i, j, k], temp)
#M[j, k] += acoeff[i] * mt[i, j, k]
end
end
Expand Down
13 changes: 7 additions & 6 deletions src/AlgAssAbsOrd/Elem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,14 @@ function -(x::T, y::T) where { T <: Union{ AlgAssAbsOrdElem, AlgAssRelOrdElem }
end

function *(n::IntegerUnion, x::AlgAssAbsOrdElem)
O=x.parent
y=Vector{ZZRingElem}(undef, O.dim)
z=coordinates(x, copy = false)
for i=1:O.dim
y[i] = z[i] * n
#O=x.parent
O = parent(x)
y = O(n * elem_in_algebra(x, copy = false))
if x.has_coord
y.coordinates = n .* coordinates(x, copy = false)
y.has_coord = true
end
return O(y)
return y
end

*(x::AlgAssAbsOrdElem, n::IntegerUnion) = n*x
Expand Down
2 changes: 1 addition & 1 deletion src/AlgAssAbsOrd/Ideal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1141,7 +1141,7 @@ function _islocally_free_left(O::AlgAssAbsOrd, I::AlgAssAbsOrdIdl, p::Union{Int,
end

for i = 1:length(basiseIJoverZ)
a += mod(toOpO\(toOJ\(AtoOJ(BtoA(BtoC\C[i]))))*basiseIJoverZ[i], pI)
add!(a, a, mod(toOpO\(toOJ\(AtoOJ(BtoA(BtoC\C[i]))))*basiseIJoverZ[i], pI))
end
end

Expand Down
Loading