Skip to content

Commit

Permalink
Add components method to Abelian group (#1632)
Browse files Browse the repository at this point in the history
* Add another getindex method

To be able to call `x[begin:end]`.

Co-authored-by: Tommy Hofmann <[email protected]>
  • Loading branch information
paemurru and thofma authored Oct 18, 2024
1 parent 6b7d0e0 commit 542c23e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/src/manual/abelian/elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ parent(x::FinGenAbGroupElem)
## Access

```@docs
getindex(x::FinGenAbGroupElem, v::AbstractVector{Int})
getindex(x::FinGenAbGroupElem, i::Int)
```

Expand Down
20 changes: 20 additions & 0 deletions src/GrpAb/Elem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,26 @@ function getindex(x::FinGenAbGroupElem, i::Int)
return x.coeff[1, i]
end

@doc raw"""
getindex(x::FinGenAbGroupElem, v::AbstractVector{Int}) -> Vector{ZZRingElem}
Returns the $i$-th components of the element $x$ where $i \in v$.
!!! note
This function is inefficient since the elements are internally stored using ZZMatrix but this function outputs a vector.
"""
function getindex(x::FinGenAbGroupElem, v::AbstractVector{Int})
return [x.coeff[1, i] for i in v]
end

function Base.firstindex(x::FinGenAbGroupElem)
return Int(1)
end

function Base.lastindex(x::FinGenAbGroupElem)
return ngens(parent(x))
end

################################################################################
#
# Comparison
Expand Down
3 changes: 3 additions & 0 deletions test/GrpAb/Elem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
a = @inferred FinGenAbGroupElem(G, N)
@test parent(a) == G
@test a.coeff == N
@test a[begin:end] == [0, 0, 0]

G = @inferred abelian_group([3, 0])
N = FlintZZ[1 1]
a = @inferred FinGenAbGroupElem(G, N)
@test @inferred parent(a) == G
@test a.coeff == N
@test a[begin:end] == [1, 1]

N = matrix(FlintZZ, 1, 2, [ 1, 1 ])
a = @inferred G(N)
Expand All @@ -21,6 +23,7 @@
a = @inferred G(N)
@test @inferred parent(a) == G
@test a.coeff == transpose(N)
@test a[begin:end] == [1, 1]
end

@testset "Generators" begin
Expand Down

0 comments on commit 542c23e

Please sign in to comment.