# Conversions

One of the main ideas of GAP.jl is that automatic conversions of Julia objects to GAP objects and vice versa shall be avoided whenever this is possible. For a few types of objects, such conversions are unavoidable, see Automatic GAP-to-Julia and Julia-to-GAP Conversions. In all other situations, the conversions between GAP objects and corresponding Julia objects can be performed using gap_to_julia and julia_to_gap, see Explicit GAP-to-Julia and Julia-to-GAP Conversions, respectively.

For convenience, also constructor methods are provided, for example Vector{Int64}(obj) can be used instead of GAP.gap_to_julia(Vector{Int64}, obj), where obj is a GAP list of integers; see Constructor Methods for GAP-to-Julia Conversions for a description of these methods. For Julia-to-GAP conversions, one can use for example GapObj(obj), where obj is a Julia object, see GapObj.

## Automatic GAP-to-Julia and Julia-to-GAP Conversions

When one calls a GAP function with Julia objects as arguments, or a Julia function with GAP objects as arguments, the arguments are in general not automatically converted to GAP objects or Julia objects, respectively. The exceptions are as follows.

• GAP's immediate integers (in the range -2^60 to 2^60-1) are automatically converted to Julia's Int64 objects; Julia's Int64 objects are automatically converted to GAP's immediate integers if they fit, and to GAP's large integers otherwise.

• GAP's immediate finite field elements are automatically converted to Julia's GAP.FFE objects, and vice versa.

• GAP's true and false are automatically converted to Julia's true and false, and vice versa.

## Explicit GAP-to-Julia and Julia-to-GAP Conversions

GAP.gap_to_juliaFunction
gap_to_julia(type, x, recursion_dict=nothing; recursive::Bool=true)

Try to convert the object x to a Julia object of type type. If x is a GAP.GapObj then the conversion rules are defined in the manual of the GAP package JuliaInterface. If x is another GAP.Obj (for example a Int64) then the result is defined in Julia by type.

The parameter recursion_dict is used to preserve the identity of converted subobjects and should never be given by the user.

For GAP lists and records, it makes sense to convert also the subobjects recursively, or to keep the subobjects as they are; the behaviour is controlled by recursive, which can be true or false.

Examples

julia> GAP.gap_to_julia( GAP.evalstr( "1/3" ) )
1//3

julia> GAP.gap_to_julia( GAP.evalstr( "\"abc\"" ) )
"abc"

julia> val = GapObj([ 1 2 ; 3 4 ])
GAP: [ [ 1, 2 ], [ 3, 4 ] ]

julia> GAP.gap_to_julia( val )
2-element Vector{Any}:
Any[1, 2]
Any[3, 4]

julia> GAP.gap_to_julia( val, recursive = false )
2-element Vector{Any}:
GAP: [ 1, 2 ]
GAP: [ 3, 4 ]

julia> GAP.gap_to_julia( Vector{GapObj}, val )
2-element Vector{GapObj}:
GAP: [ 1, 2 ]
GAP: [ 3, 4 ]

julia> GAP.gap_to_julia( Matrix{Int}, val )
2×2 Matrix{Int64}:
1  2
3  4
source
GAP.julia_to_gapFunction
julia_to_gap(input, recursion_dict = IdDict(); recursive::Bool = false)

Convert a julia object input to an appropriate GAP object. If recursive is set to true, recursive conversions on arrays, tuples, and dictionaries is performed.

The input recursion_dict should never be set by the user, it is meant to keep egality of input data, by converting egal data to identical objects in GAP.

Examples

julia> GAP.julia_to_gap(1//3)
GAP: 1/3

julia> GAP.julia_to_gap("abc")
GAP: "abc"

julia> GAP.julia_to_gap([ [1, 2], [3, 4]])
GAP: [ <Julia: [1, 2]>, <Julia: [3, 4]> ]

julia> GAP.julia_to_gap([ [1, 2], [3, 4]], recursive = true)
GAP: [ [ 1, 2 ], [ 3, 4 ] ]

source

## Constructor Methods for GAP-to-Julia Conversions

(For Julia-to-GAP conversions, one can use GapObj and GAP.Obj as constructors.)

Core.Int128Type
Int128(obj::GapObj)

Return the Int128 converted from the GAP integer obj. (Note that small GAP integers are represented by Julia Int64 objects, in particular they are not GapObjs; their conversion is not handled by methods installed in GAP.jl.)

Examples

julia> val = GAP.evalstr("2^80")
GAP: 1208925819614629174706176

julia> Int128(val)
1208925819614629174706176

source
Base.GMP.BigIntType
BigInt(obj::GapObj)

Return the big integer converted from the GAP integer obj. (Note that small GAP integers are not represented by GapObjs, their conversion with BigInt is handled by Julia's methods.)

Examples

julia> val = GAP.evalstr("2^64")
GAP: 18446744073709551616

julia> BigInt(val)
18446744073709551616

julia> val = GAP.evalstr("2^59")
576460752303423488

julia> isa(val, GapObj)
false

julia> BigInt(val)
576460752303423488

source
Base.RationalType
Rational{T}(obj::GapObj) where {T<:Integer}

Return the rational converted from the GAP integer or the GAP rational obj,

Examples

julia> val = GAP.evalstr("2^64")
GAP: 18446744073709551616

julia> Rational{Int128}(val)
18446744073709551616//1

julia> Rational{BigInt}(val)
18446744073709551616//1

julia> val = GAP.evalstr("1/3")
GAP: 1/3

julia> Rational{Int64}(val)
1//3

source
Core.Float64Type
Float64(obj::GapObj)

Return the float converted from the GAP float obj.

Examples

julia> val = GAP.evalstr("2.2")
GAP: 2.2

julia> Float64(val)
2.2

julia> Float32(val)
2.2f0

source
Base.bigFunction
big(obj::GapObj)

Return the big integer converted from the GAP integer obj, or the big rational converted from the GAP rational obj, or the big float converted from the GAP float obj.

Examples

julia> val = GAP.evalstr("2^64")
GAP: 18446744073709551616

julia> big(val)
18446744073709551616

julia> val = GAP.evalstr("1/3")
GAP: 1/3

julia> big(val)
1//3

julia> val = GAP.evalstr("1.1")
GAP: 1.1

julia> big(val)
1.100000000000000088817841970012523233890533447265625

source
Core.CharType
Char(obj::GapObj)

Return the character converted from the GAP character obj.

Examples

julia> val = GAP.evalstr("'x'")
GAP: 'x'

julia> Char(val)
'x': ASCII/Unicode U+0078 (category Ll: Letter, lowercase)

source
Base.CucharType
Cuchar(obj::GapObj)

Return the UInt8 that belongs to the GAP character obj.

Examples

julia> val = GAP.evalstr("'x'")
GAP: 'x'

julia> Cuchar(val)
0x78

source
Core.StringType
String(obj::GapObj)

Return the Julia string converted from the GAP string obj. Note that GAP's String function can be applied to arbitrary GAP objects, similar to Julia's string function; this behaviour is not intended for this String constructor.

Examples

julia> val = GAP.evalstr("\"abc\"")
GAP: "abc"

julia> String(val)
"abc"

julia> val = GAP.evalstr("[]")
GAP: [  ]

julia> String(val)   # an empty GAP list is a string
""

source
Core.SymbolType
Symbol(obj::GapObj)

Return the symbol converted from the GAP string obj.

Examples

julia> str = GAP.evalstr("\"abc\"")
GAP: "abc"

julia> Symbol(str)
:abc

source
Base.UnitRangeType
UnitRange(obj::GapObj)

Return the unit range converted from the GAP range obj, which has step width 1.

Examples

julia> val = GAP.evalstr("[ 1 .. 10 ]")
GAP: [ 1 .. 10 ]

julia> UnitRange(val)
1:10

julia> UnitRange{Int32}(val)
1:10

source
Base.StepRangeType
StepRange(obj::GapObj)

Return the step range converted from the GAP range obj, which may have arbitrary step width.

Examples

julia> val = GAP.evalstr("[ 1, 3 .. 11 ]")
GAP: [ 1, 3 .. 11 ]

julia> StepRange(val)
1:2:11

julia> r = StepRange{Int8,Int8}(val)
1:2:11

julia> typeof(r)
StepRange{Int8, Int8}

source
Core.TupleType
Tuple{Types...}(obj::GapObj; recursive = true)

Return the tuple converted from the GAP list obj. The entries of the list are converted to the required types Types..., using gap_to_julia. If recursive is true then the entries of the list are converted recursively, otherwise non-recursively.

Examples

julia> val = GAP.evalstr("[ 1, 5 ]")
GAP: [ 1, 5 ]

julia> Tuple{Int64,Int64}(val)
(1, 5)

julia> val = GAP.evalstr("[ [ 1 ], [ 2 ] ]")
GAP: [ [ 1 ], [ 2 ] ]

julia> Tuple{Any,Any}(val)
(Any[1], Any[2])

julia> Tuple{GapObj,GapObj}(val, recursive = false)
(GAP: [ 1 ], GAP: [ 2 ])

source
Base.BitVectorType
BitVector(obj::GapObj)

Return the bit vector converted from the GAP list of booleans obj.

Examples

julia> val = GAP.evalstr("[ true, false, true ]")
GAP: [ true, false, true ]

julia> BitVector(val)
3-element BitVector:
1
0
1

source
Base.VectorType
Vector{T}(obj::GapObj; recursive = true)

Return the 1-dimensional array converted from the GAP list obj. The entries of the list are converted to the type T, using gap_to_julia. If recursive is true then the entries of the list are converted recursively, otherwise non-recursively.

If T is UInt8 then obj may be a GAP string.

Examples

julia> val = GAP.evalstr("[ [ 1 ], [ 2 ] ]")
GAP: [ [ 1 ], [ 2 ] ]

julia> Vector{Any}(val)
2-element Vector{Any}:
Any[1]
Any[2]

julia> Vector{Any}(val, recursive = false)
2-element Vector{Any}:
GAP: [ 1 ]
GAP: [ 2 ]

julia> val = GAP.evalstr( "NewVector( IsPlistVectorRep, Integers, [ 0, 2, 5 ] )" )
GAP: <plist vector over Integers of length 3>

julia> Vector{Int64}( val )
3-element Vector{Int64}:
0
2
5

julia> val = GAP.evalstr("\"abc\"")
GAP: "abc"

julia> Vector{UInt8}(val)
3-element Vector{UInt8}:
0x61
0x62
0x63

source
Base.MatrixType
Matrix{T}(obj::GapObj; recursive = true)

Return the 2-dimensional array converted from the GAP matrix obj, which can be a GAP list of lists or a GAP matrix object. The entries of the matrix are converted to the type T, using gap_to_julia. If recursive is true then the entries are converted recursively, otherwise non-recursively.

Examples

julia> val = GAP.evalstr("[ [ 1, 2 ], [ 3, 4 ] ]")
GAP: [ [ 1, 2 ], [ 3, 4 ] ]

julia> Matrix{Int64}(val)
2×2 Matrix{Int64}:
1  2
3  4

julia> val = GAP.evalstr( "NewMatrix( IsPlistMatrixRep, Integers, 2, [ 0, 1, 2, 3 ] )" )
GAP: <2x2-matrix over Integers>

julia> Matrix{Int64}(val)
2×2 Matrix{Int64}:
0  1
2  3

source
Base.SetType
Set{T}(obj::GapObj; recursive = true)

Return the set converted from the GAP list or GAP collection obj. The elements of obj are converted to the required type T, using gap_to_julia. If recursive is true then the elements are converted recursively, otherwise non-recursively.

This constructor method is intended for situations where the result involves only native Julia objects such as integers and strings. Dealing with results containing GAP objects will be inefficient.

Examples

julia> Set{Int}(GAP.evalstr("[ 1, 2, 1 ]"))
Set{Int64} with 2 elements:
2
1

julia> Set{Vector{Int}}(GAP.evalstr("[[1], [2], [1]]"))
Set{Vector{Int64}} with 2 elements:
[1]
[2]

julia> Set{String}(GAP.evalstr("[\"a\", \"b\"]"))
Set{String} with 2 elements:
"b"
"a"

julia> Set{Any}(GAP.evalstr("[[1], [2], [1]]"))
Set{Any} with 2 elements:
Any[1]
Any[2]


In the following examples, the order in which the Julia output is shown may vary.

Examples

julia> s = Set{Any}(GAP.evalstr("[[1], [2], [1]]"), recursive = false);

julia> s == Set{Any}([GAP.evalstr("[ 1 ]"), GAP.evalstr("[ 2 ]")])
true

julia> s = Set{Any}(GAP.evalstr("SymmetricGroup(2)"), recursive = false);

julia> s == Set{Any}([GAP.evalstr("()"), GAP.evalstr("(1,2)")])
true

source
Base.DictType
Dict{Symbol,T}(obj::GapObj; recursive = true)

Return the dictionary converted from the GAP record obj. If recursive is true then the values of the record components are recursively converted to objects of the type T, using gap_to_julia, otherwise they are kept as they are.

Examples

julia> val = GAP.evalstr("rec( a:= 1, b:= 2 )")
GAP: rec( a := 1, b := 2 )

julia> Dict{Symbol,Int}(val)
Dict{Symbol, Int64} with 2 entries:
:a => 1
:b => 2

julia> val = GAP.evalstr("rec( l:= [ 1, 2 ] )")
GAP: rec( l := [ 1, 2 ] )

julia> Dict{Symbol,Any}(val, recursive = false)
Dict{Symbol, Any} with 1 entry:
:l => GAP: [ 1, 2 ]

julia> Dict{Symbol,Any}(val, recursive = true)
Dict{Symbol, Any} with 1 entry:
:l => Any[1, 2]

julia> Dict{Symbol,Vector{Int}}(val, recursive = true)
Dict{Symbol, Vector{Int64}} with 1 entry:
:l => [1, 2]

source