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

Remove caching of Lie algebras #3894

Merged
merged 1 commit into from
Jul 1, 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
82 changes: 34 additions & 48 deletions experimental/LieAlgebras/src/AbstractLieAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,37 @@
R::Field,
struct_consts::Matrix{SRow{C}},
s::Vector{Symbol};
cached::Bool=true,
check::Bool=true,
) where {C<:FieldElem}
return get_cached!(
AbstractLieAlgebraDict, (R, struct_consts, s), cached
) do
(n1, n2) = size(struct_consts)
@req n1 == n2 "Invalid structure constants dimensions."
dimL = n1
@req length(s) == dimL "Invalid number of basis element names."
if check
@req all(
r -> all(e -> parent(last(e)) === R, r), struct_consts
) "Invalid structure constants."
@req all(
iszero, struct_consts[i, i][k] for i in 1:dimL, k in 1:dimL
) "Not anti-symmetric."
@req all(
iszero,
struct_consts[i, j][k] + struct_consts[j, i][k] for i in 1:dimL, j in 1:dimL,
k in 1:dimL
) "Not anti-symmetric."
@req all(
iszero,
sum(
struct_consts[i, j][k] * struct_consts[k, l][m] +
struct_consts[j, l][k] * struct_consts[k, i][m] +
struct_consts[l, i][k] * struct_consts[k, j][m] for k in 1:dimL
) for i in 1:dimL, j in 1:dimL, l in 1:dimL, m in 1:dimL
) "Jacobi identity does not hold."
end
new{C}(R, dimL, struct_consts, s)
end::AbstractLieAlgebra{C}
(n1, n2) = size(struct_consts)
@req n1 == n2 "Invalid structure constants dimensions."
dimL = n1
@req length(s) == dimL "Invalid number of basis element names."
if check
@req all(
r -> all(e -> parent(last(e)) === R, r), struct_consts
) "Invalid structure constants."
@req all(
iszero, struct_consts[i, i][k] for i in 1:dimL, k in 1:dimL
) "Not anti-symmetric."
@req all(
iszero,
struct_consts[i, j][k] + struct_consts[j, i][k] for i in 1:dimL, j in 1:dimL,
k in 1:dimL
) "Not anti-symmetric."
@req all(
iszero,
sum(
struct_consts[i, j][k] * struct_consts[k, l][m] +
struct_consts[j, l][k] * struct_consts[k, i][m] +
struct_consts[l, i][k] * struct_consts[k, j][m] for k in 1:dimL
) for i in 1:dimL, j in 1:dimL, l in 1:dimL, m in 1:dimL
) "Jacobi identity does not hold."
end
return new{C}(R, dimL, struct_consts, s)
end
end

const AbstractLieAlgebraDict = CacheDictType{
Tuple{Field,Matrix{SRow},Vector{Symbol}},AbstractLieAlgebra
}()

struct AbstractLieAlgebraElem{C<:FieldElem} <: LieAlgebraElem{C}
parent::AbstractLieAlgebra{C}
mat::MatElem{C}
Expand Down Expand Up @@ -181,7 +172,7 @@ end
###############################################################################

@doc raw"""
lie_algebra(R::Field, struct_consts::Matrix{SRow{elem_type(R)}}, s::Vector{<:VarName}; cached::Bool, check::Bool) -> AbstractLieAlgebra{elem_type(R)}
lie_algebra(R::Field, struct_consts::Matrix{SRow{elem_type(R)}}, s::Vector{<:VarName}; check::Bool) -> AbstractLieAlgebra{elem_type(R)}

Construct the Lie algebra over the ring `R` with structure constants `struct_consts`
and with basis element names `s`.
Expand All @@ -193,23 +184,21 @@ such that $[x_i, x_j] = \sum_k a_{i,j,k} x_k$.

* `s`: A vector of basis element names. This is
`[Symbol("x_$i") for i in 1:size(struct_consts, 1)]` by default.
* `cached`: If `true`, cache the result. This is `true` by default.
* `check`: If `true`, check that the structure constants are anti-symmetric and
satisfy the Jacobi identity. This is `true` by default.
"""
function lie_algebra(
R::Field,
struct_consts::Matrix{SRow{C}},
s::Vector{<:VarName}=[Symbol("x_$i") for i in 1:size(struct_consts, 1)];
cached::Bool=true,
check::Bool=true,
) where {C<:FieldElem}
@req C == elem_type(R) "Invalid coefficient type."
return AbstractLieAlgebra{elem_type(R)}(R, struct_consts, Symbol.(s); cached, check)
return AbstractLieAlgebra{elem_type(R)}(R, struct_consts, Symbol.(s); check)
end

@doc raw"""
lie_algebra(R::Field, struct_consts::Array{elem_type(R),3}, s::Vector{<:VarName}; cached::Bool, check::Bool) -> AbstractLieAlgebra{elem_type(R)}
lie_algebra(R::Field, struct_consts::Array{elem_type(R),3}, s::Vector{<:VarName}; check::Bool) -> AbstractLieAlgebra{elem_type(R)}

Construct the Lie algebra over the ring `R` with structure constants `struct_consts`
and with basis element names `s`.
Expand All @@ -221,7 +210,6 @@ such that $[x_i, x_j] = \sum_k a_{i,j,k} x_k$.

* `s`: A vector of basis element names. This is
`[Symbol("x_$i") for i in 1:size(struct_consts, 1)]` by default.
* `cached`: If `true`, cache the result. This is `true` by default.
* `check`: If `true`, check that the structure constants are anti-symmetric and
satisfy the Jacobi identity. This is `true` by default.

Expand Down Expand Up @@ -266,7 +254,6 @@ function lie_algebra(
R::Field,
struct_consts::Array{C,3},
s::Vector{<:VarName}=[Symbol("x_$i") for i in 1:size(struct_consts, 1)];
cached::Bool=true,
check::Bool=true,
) where {C<:FieldElem}
@req C == elem_type(R) "Invalid coefficient type."
Expand All @@ -279,7 +266,7 @@ function lie_algebra(
)
end

return AbstractLieAlgebra{elem_type(R)}(R, struct_consts2, Symbol.(s); cached, check)
return AbstractLieAlgebra{elem_type(R)}(R, struct_consts2, Symbol.(s); check)
end

function lie_algebra(
Expand All @@ -304,14 +291,13 @@ function lie_algebra(
end

@doc raw"""
lie_algebra(R::Field, dynkin::Tuple{Char,Int}; cached::Bool) -> AbstractLieAlgebra{elem_type(R)}
lie_algebra(R::Field, dynkin::Tuple{Char,Int}) -> AbstractLieAlgebra{elem_type(R)}

Construct the simple Lie algebra over the ring `R` with Dynkin type given by `dynkin`.
The internally used basis of this Lie algebra is the Chevalley basis.

If `cached` is `true`, the constructed Lie algebra is cached.
"""
function lie_algebra(R::Field, S::Symbol, n::Int; cached::Bool=true)
function lie_algebra(R::Field, S::Symbol, n::Int)
rs = root_system(S, n)
cm = cartan_matrix(rs)
@req is_cartan_matrix(cm; generalized=false) "The type does not correspond to a classical root system"
Expand Down Expand Up @@ -355,7 +341,7 @@ function lie_algebra(R::Field, S::Symbol, n::Int; cached::Bool=true)
[Symbol("h_$i") for i in 1:nsimp]
]

L = lie_algebra(R, struct_consts, s; cached, check=true) # TODO: remove check
L = lie_algebra(R, struct_consts, s; check=true) # TODO: remove check
=#

# start temporary workaround # TODO: reenable code above
Expand All @@ -373,7 +359,7 @@ function lie_algebra(R::Field, S::Symbol, n::Int; cached::Bool=true)
[Symbol("h_$i") for i in 1:nsimp]
]
L = codomain(
_iso_gap_oscar_abstract_lie_algebra(LG, s; coeffs_iso, cached)
_iso_gap_oscar_abstract_lie_algebra(LG, s; coeffs_iso)
)::AbstractLieAlgebra{elem_type(R)}
# end temporary workaround

Expand Down
10 changes: 4 additions & 6 deletions experimental/LieAlgebras/src/LieAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -477,26 +477,24 @@
###############################################################################

@doc raw"""
lie_algebra(gapL::GapObj, s::Vector{<:VarName}; cached::Bool) -> LieAlgebra{elem_type(R)}
lie_algebra(gapL::GapObj, s::Vector{<:VarName}) -> LieAlgebra{elem_type(R)}

Construct a Lie algebra isomorphic to the GAP Lie algebra `gapL`. Its basis element are named by `s`,
or by `x_i` by default.
We require `gapL` to be a finite-dimensional GAP Lie algebra. The return type is dependent on
properties of `gapL`, in particular, whether GAP knows about a matrix representation.

If `cached` is `true`, the constructed Lie algebra is cached.
"""
function lie_algebra(
gapL::GapObj,
s::Vector{<:VarName}=[Symbol("x_$i") for i in 1:GAPWrap.Dimension(gapL)];
cached::Bool=true,
)
@req GAPWrap.IsLieAlgebra(gapL) "gapL must be a Lie algebra."
@req GAPWrap.IsLieAlgebra(gapL) "input must be a Lie algebra."

Check warning on line 492 in experimental/LieAlgebras/src/LieAlgebra.jl

View check run for this annotation

Codecov / codecov/patch

experimental/LieAlgebras/src/LieAlgebra.jl#L492

Added line #L492 was not covered by tests
if GAPWrap.IsFiniteDimensional(gapL)
if GAPWrap.IsLieObjectCollection(gapL)
return codomain(_iso_gap_oscar_linear_lie_algebra(gapL, s; cached))
return codomain(_iso_gap_oscar_linear_lie_algebra(gapL, s))

Check warning on line 495 in experimental/LieAlgebras/src/LieAlgebra.jl

View check run for this annotation

Codecov / codecov/patch

experimental/LieAlgebras/src/LieAlgebra.jl#L495

Added line #L495 was not covered by tests
else
return codomain(_iso_gap_oscar_abstract_lie_algebra(gapL, s; cached))
return codomain(_iso_gap_oscar_abstract_lie_algebra(gapL, s))

Check warning on line 497 in experimental/LieAlgebras/src/LieAlgebra.jl

View check run for this annotation

Codecov / codecov/patch

experimental/LieAlgebras/src/LieAlgebra.jl#L497

Added line #L497 was not covered by tests
end
end
error("Not implemented.")
Expand Down
2 changes: 1 addition & 1 deletion experimental/LieAlgebras/src/LieAlgebras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Oscar: GAPWrap, IntegerUnion, MapHeader
import Random

# not importet in Oscar
using AbstractAlgebra: CacheDictType, ProductIterator, get_cached!, ordinal_number_string
using AbstractAlgebra: ProductIterator, ordinal_number_string

using AbstractAlgebra.PrettyPrinting

Expand Down
42 changes: 16 additions & 26 deletions experimental/LieAlgebras/src/LinearLieAlgebra.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,22 @@
n::Int,
basis::Vector{<:MatElem{C}},
s::Vector{Symbol};
cached::Bool=true,
check::Bool=true,
) where {C<:FieldElem}
return get_cached!(
LinearLieAlgebraDict, (R, n, basis, s), cached
) do
@req all(b -> size(b) == (n, n), basis) "Invalid basis element dimensions."
@req length(s) == length(basis) "Invalid number of basis element names."
L = new{C}(R, n, length(basis), basis, s)
if check
@req all(b -> all(e -> parent(e) === R, b), basis) "Invalid matrices."
# TODO: make work
# for xi in basis(L), xj in basis(L)
# @req (xi * xj) in L
# end
end
return L
end::LinearLieAlgebra{C}
@req all(b -> size(b) == (n, n), basis) "Invalid basis element dimensions."
@req length(s) == length(basis) "Invalid number of basis element names."
L = new{C}(R, n, length(basis), basis, s)
if check
@req all(b -> all(e -> parent(e) === R, b), basis) "Invalid matrices."
# TODO: make work
# for xi in basis(L), xj in basis(L)
# @req (xi * xj) in L
# end
end
return L
end
end

const LinearLieAlgebraDict = CacheDictType{
Tuple{Field,Int,Vector{<:MatElem},Vector{Symbol}},LinearLieAlgebra
}()

struct LinearLieAlgebraElem{C<:FieldElem} <: LieAlgebraElem{C}
parent::LinearLieAlgebra{C}
mat::MatElem{C}
Expand Down Expand Up @@ -195,24 +186,23 @@ end
###############################################################################

@doc raw"""
lie_algebra(R::Field, n::Int, basis::Vector{<:MatElem{elem_type(R)}}, s::Vector{<:VarName}; cached::Bool) -> LinearLieAlgebra{elem_type(R)}
lie_algebra(R::Field, n::Int, basis::Vector{<:MatElem{elem_type(R)}}, s::Vector{<:VarName}; check::Bool=true) -> LinearLieAlgebra{elem_type(R)}

Construct the Lie algebra over the field `R` with basis `basis` and basis element names
given by `s`. The basis elements must be square matrices of size `n`.
We require `basis` to be linearly independent, and to contain the Lie bracket of any
two basis elements in its span.

If `cached` is `true`, the constructed Lie algebra is cached.
We require `basis` to be linearly independent, and to contain the Lie bracket of any
two basis elements in its span (this is currently not checked).
Setting `check=false` disables these checks (once they are in place).
"""
function lie_algebra(
R::Field,
n::Int,
basis::Vector{<:MatElem{C}},
s::Vector{<:VarName};
cached::Bool=true,
check::Bool=true,
) where {C<:FieldElem}
return LinearLieAlgebra{elem_type(R)}(R, n, basis, Symbol.(s); cached)
return LinearLieAlgebra{elem_type(R)}(R, n, basis, Symbol.(s); check)
end

function lie_algebra(
Expand Down
14 changes: 6 additions & 8 deletions experimental/LieAlgebras/src/iso_gap_oscar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ function _iso_gap_oscar_abstract_lie_algebra(
LG::GapObj,
s::Vector{<:VarName}=[Symbol("x_$i") for i in 1:GAPWrap.Dimension(LG)];
coeffs_iso::Map{GapObj}=Oscar.iso_gap_oscar(GAPWrap.LeftActingDomain(LG)),
cached::Bool=true,
)
LO = _abstract_lie_algebra_from_GAP(LG, coeffs_iso, s; cached)
LO = _abstract_lie_algebra_from_GAP(LG, coeffs_iso, s)
finv, f = _iso_oscar_gap_lie_algebra_functions(LO, LG, inv(coeffs_iso))

iso = MapFromFunc(LG, LO, f, finv)
Expand All @@ -36,9 +35,8 @@ function _iso_gap_oscar_linear_lie_algebra(
LG::GapObj,
s::Vector{<:VarName}=[Symbol("x_$i") for i in 1:GAPWrap.Dimension(LG)];
coeffs_iso::Map{GapObj}=Oscar.iso_gap_oscar(GAPWrap.LeftActingDomain(LG)),
cached::Bool=true,
)
LO = _linear_lie_algebra_from_GAP(LG, coeffs_iso, s; cached)
LO = _linear_lie_algebra_from_GAP(LG, coeffs_iso, s)
finv, f = _iso_oscar_gap_lie_algebra_functions(LO, LG, inv(coeffs_iso))

iso = MapFromFunc(LG, LO, f, finv)
Expand All @@ -47,7 +45,7 @@ function _iso_gap_oscar_linear_lie_algebra(
end

function _abstract_lie_algebra_from_GAP(
LG::GapObj, coeffs_iso::Map{GapObj}, s::Vector{<:VarName}; cached::Bool=true
LG::GapObj, coeffs_iso::Map{GapObj}, s::Vector{<:VarName}
)
RO = codomain(coeffs_iso)
dimL = GAPWrap.Dimension(LG)
Expand All @@ -67,12 +65,12 @@ function _abstract_lie_algebra_from_GAP(
)
end

LO = AbstractLieAlgebra{elem_type(RO)}(RO, struct_consts, Symbol.(s); cached, check=false)
LO = AbstractLieAlgebra{elem_type(RO)}(RO, struct_consts, Symbol.(s); check=false)
return LO
end

function _linear_lie_algebra_from_GAP(
LG::GapObj, coeffs_iso::Map{GapObj}, s::Vector{<:VarName}; cached::Bool=true
LG::GapObj, coeffs_iso::Map{GapObj}, s::Vector{<:VarName}
)
@req GAPWrap.IsLieObjectCollection(LG) "Input is not a linear Lie algebra."

Expand All @@ -81,6 +79,6 @@ function _linear_lie_algebra_from_GAP(
map_entries(coeffs_iso, GAPWrap.UnderlyingRingElement(b)) for b in GAPWrap.Basis(LG)
]
n = size(basis[1])[1]
LO = LinearLieAlgebra{elem_type(RO)}(RO, n, basis, Symbol.(s); cached)
LO = LinearLieAlgebra{elem_type(RO)}(RO, n, basis, Symbol.(s); check=false)
return LO
end
Loading