-
Notifications
You must be signed in to change notification settings - Fork 19
/
julia_to_gap.jl
232 lines (204 loc) · 6.95 KB
/
julia_to_gap.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
## Converters
"""
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 equal data to identical objects in GAP.
# Examples
```jldoctest
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 ] ]
```
The following `julia_to_gap` conversions are supported by GAP.jl.
(Other Julia packages may provide conversions for more Julia objects.)
| Julia type | GAP filter |
|--------------------------------------|--------------|
| `Int8`, `Int16`, ..., `BigInt` | `IsInt` |
| `GapFFE` | `IsFFE` |
| `Bool` | `IsBool` |
| `Rational{T}` | `IsRat` |
| `Float16`, `Float32`, `Float64` | `IsFloat` |
| `AbstractString` | `IsString` |
| `Symbol` | `IsString` |
| `Char` | `IsChar` |
| `Vector{T}` | `IsList` |
| `Vector{Bool}`, `BitVector` | `IsBList` |
| `Tuple{T}` | `IsList` |
| `Matrix{T}` | `IsList` |
| `Dict{String, T}`, `Dict{Symbol, T}` | `IsRecord` |
| `UnitRange{T}`, `StepRange{T, S}` | `IsRange` |
| `Function` | `IsFunction` |
"""
julia_to_gap(x::FFE) = x # Default for actual GAP objects is to do nothing
julia_to_gap(x::Bool) = x # Default for actual GAP objects is to do nothing
## Integers: general case first deal with things that fit into immediate
## integers, then falls back to converting to BigInt and calling into the GAP
## kernel API.
## TODO: we could provide more efficient conversion for UInt64, Int128, UInt128
## which avoids the conversion to BigInt, if we wanted to.
function julia_to_gap(x::Integer)
# if it fits into a GAP immediate integer, convert x to Int64
x in -1<<60:(1<<60-1) && return Int64(x)
# for the general case, fall back to BigInt
return julia_to_gap(BigInt(x))
end
## Small integers types always fit into GAP immediate integers, and thus are
## represented by Int64 on the Julia side.
julia_to_gap(x::Int64) = x
julia_to_gap(x::Int32) = Int64(x)
julia_to_gap(x::Int16) = Int64(x)
julia_to_gap(x::Int8) = Int64(x)
julia_to_gap(x::UInt32) = Int64(x)
julia_to_gap(x::UInt16) = Int64(x)
julia_to_gap(x::UInt8) = Int64(x)
## BigInts are converted via a ccall
function julia_to_gap(x::BigInt)
x in -1<<60:(1<<60-1) && return Int64(x)
return GC.@preserve x ccall((:MakeObjInt, libgap), GapObj, (Ptr{UInt64}, Cint), x.d, x.size)
end
## Rationals
function julia_to_gap(x::Rational{T}) where {T<:Integer}
denom_julia = denominator(x)
numer_julia = numerator(x)
if denom_julia == 0
if numer_julia >= 0
return Globals.infinity
else
return -Globals.infinity
end
end
numer = julia_to_gap(numer_julia)
denom = julia_to_gap(denom_julia)
return Wrappers.QUO(numer, denom)
end
## Floats
julia_to_gap(x::Float64) = NEW_MACFLOAT(x)
julia_to_gap(x::Float32) = NEW_MACFLOAT(Float64(x))
julia_to_gap(x::Float16) = NEW_MACFLOAT(Float64(x))
## Chars
julia_to_gap(x::Char) = CharWithValue(Cuchar(x))
## Strings and symbols
julia_to_gap(x::AbstractString) = MakeString(string(x))
julia_to_gap(x::Symbol) = MakeString(string(x))
## Generic caller for optional arguments
julia_to_gap(obj::Any; recursive::Bool) = julia_to_gap(obj)
julia_to_gap(obj::Any, recursion_dict::IdDict{Any,Any}; recursive::Bool = true) = julia_to_gap(obj)
## Arrays (including BitVector)
function julia_to_gap(
obj::Vector{T},
recursion_dict::IdDict{Any,Any} = IdDict();
recursive::Bool = false,
) where {T}
len = length(obj)
ret_val = NewPlist(len)
if recursive
recursion_dict[obj] = ret_val
end
for i = 1:len
x = obj[i]
if x === nothing
continue
end
if recursive
x = get!(recursion_dict, x) do
julia_to_gap(x, recursion_dict; recursive)
end
end
ret_val[i] = x
end
return ret_val
end
## Convert two dimensional arrays
function julia_to_gap(
obj::Matrix{T},
recursion_dict::IdDict{Any,Any} = IdDict();
recursive::Bool = false,
) where {T}
(rows, cols) = size(obj)
if haskey(recursion_dict, obj)
return recursion_dict[obj]
end
ret_val = NewPlist(rows)
if recursive
recursion_dict[obj] = ret_val
end
for i = 1:rows
ret_val[i] = julia_to_gap(obj[i, :], recursion_dict; recursive)
end
return ret_val
end
## Tuples
function julia_to_gap(
obj::Tuple,
recursion_dict::IdDict{Any,Any} = IdDict();
recursive::Bool = false,
)
array = collect(Any, obj)
return julia_to_gap(array, recursion_dict; recursive)
end
## Ranges
function julia_to_gap(r::AbstractRange{<:Integer})
res = NewRange(length(r), first(r), step(r))
Wrappers.IsRange(res) || throw(ConversionError(r, GapObj))
return res
end
## Dictionaries
function julia_to_gap(
obj::Dict{T,S},
recursion_dict::IdDict{Any,Any} = IdDict();
recursive::Bool = false,
) where {S} where {T<:Union{Symbol,AbstractString}}
record = NewPrecord(0)
if recursive
recursion_dict[obj] = record
end
for (x, y) in obj
x = Wrappers.RNamObj(MakeString(string(x)))
if recursive
y = get!(recursion_dict, y) do
julia_to_gap(y, recursion_dict; recursive)
end
end
Wrappers.ASS_REC(record, x, y)
end
return record
end
## GAP objects:
## We have to do something only if recursive conversion is required,
## and if `obj` contains Julia subobjects;
## in this case, `obj` is a GAP list or record.
## An example of such an `obj` is `GAP.julia_to_gap([[1]])`.
function julia_to_gap(
obj::GapObj,
recursion_dict::IdDict{Any,Any} = IdDict();
recursive::Bool = false,
)
if ! recursive
ret_val = obj
elseif Wrappers.IsList(obj)
len = length(obj)
ret_val = NewPlist(len)
recursion_dict[obj] = ret_val
for i = 1:len
ret_val[i] = julia_to_gap(obj[i], recursion_dict; recursive)
end
elseif Wrappers.IsRecord(obj)
ret_val = NewPrecord(0)
recursion_dict[obj] = ret_val
for x in Vector{String}(Wrappers.RecNames(obj))
Wrappers.ASS_REC(ret_val, x, julia_to_gap(Wrappers.ELM_REC(obj, x), recursion_dict; recursive = true))
end
else
ret_val = obj
end
return ret_val
end
julia_to_gap(func::Function) = WrapJuliaFunc(func)