-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathtypes.jl
More file actions
359 lines (289 loc) · 11.5 KB
/
types.jl
File metadata and controls
359 lines (289 loc) · 11.5 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
## We have different types:
##
## Basic: holds a ptr to a symengine object. Faster, so is default type
##
## BasicType{Val{:XXX}}: types that can be use to control dispatch
##
## SymbolicType: is a type union of the two
##
## Basic(x::BasicType) gives a basic object; BasicType(x::Basic) gives a BasicType object. (This name needs change)
##
## To control dispatch, one might have `N(b::Basic) = N(BasicType(b))` and then define `N` for types of interest
basic_free(b::Basic) = ccall((:basic_free_stack, libsymengine), Nothing, (Ref{Basic}, ), b)
function convert(::Type{Basic}, x::Clong)
a = Basic()
ccall((:integer_set_si, libsymengine), Nothing, (Ref{Basic}, Clong), a, x)
return a
end
function convert(::Type{Basic}, x::Culong)
a = Basic()
ccall((:integer_set_ui, libsymengine), Nothing, (Ref{Basic}, Culong), a, x)
return a
end
function convert(::Type{Basic}, x::BigInt)
a = Basic()
ccall((:integer_set_mpz, libsymengine), Nothing, (Ref{Basic}, Ref{BigInt}), a, x)
return a
end
function convert(::Type{Basic}, s::String)
a = Basic()
b = ccall((:basic_parse, libsymengine), Cuint, (Ref{Basic}, Ptr{Int8}), a, s)
throw_if_error(b, s)
return a
end
function convert(::Type{Basic}, ex::Expr)
expr = copy(ex)
Basic(string(expr))
end
convert(::Type{Basic}, ex::Symbol) = Basic(string(ex))
function convert(::Type{Basic}, x::Cdouble)
a = Basic()
ccall((:real_double_set_d, libsymengine), Nothing, (Ref{Basic}, Cdouble), a, x)
return a
end
function convert(::Type{Basic}, x::BigFloat)
if (x.prec <= 53)
return convert(Basic, Cdouble(x))
elseif have_mpfr
a = Basic()
ccall((:real_mpfr_set, libsymengine), Nothing, (Ref{Basic}, Ref{BigFloat}), a, x)
return a
else
warn("SymEngine is not compiled with MPFR support. Converting will lose precision.")
return convert(Basic, Cdouble(x))
end
end
if Clong == Int32
convert(::Type{Basic}, x::Union{Int8, Int16}) = Basic(convert(Clong, x))
convert(::Type{Basic}, x::Union{UInt8, UInt16}) = Basic(convert(Culong, x))
else
convert(::Type{Basic}, x::Union{Int8, Int16, Int32}) = Basic(convert(Clong, x))
convert(::Type{Basic}, x::Union{UInt8, UInt16, UInt32}) = Basic(convert(Culong, x))
end
convert(::Type{Basic}, x::Union{Float16, Float32}) = Basic(convert(Cdouble, x))
convert(::Type{Basic}, x::Integer) = Basic(BigInt(x))
convert(::Type{Basic}, x::Rational) = Basic(numerator(x)) / Basic(denominator(x))
convert(::Type{Basic}, x::Complex) = Basic(real(x)) + Basic(imag(x)) * IM
Basic(x::T) where {T} = convert(Basic, x)
Basic(x::Basic) = x
Base.promote_rule(::Type{Basic}, ::Type{S}) where {S<:Number} = Basic
Base.promote_rule(::Type{S}, ::Type{Basic}) where {S<:Number} = Basic
Base.promote_rule(::Type{S}, ::Type{Basic}) where {S<:AbstractIrrational} = Basic
Base.promote_rule(::Type{Bool}, ::Type{Basic}) = Basic
## Class ID
get_type(s::Basic) = ccall((:basic_get_type, libsymengine), UInt, (Ref{Basic},), s)
function get_class_from_id(id::UInt)
a = ccall((:basic_get_class_from_id, libsymengine), Ptr{UInt8}, (Int,), id)
str = unsafe_string(a)
ccall((:basic_str_free, libsymengine), Nothing, (Ptr{UInt8}, ), a)
str
end
# use value of `get_type` to index into a vector storing symbols
function _get_symengine_classes()
d = Vector{Symbol}()
i = 0
while true
s = get_class_from_id(UInt(i))
if s == ""
break
end
push!(d, Symbol(s))
i+=1
end
return d
end
const symengine_classes = _get_symengine_classes()
const symengine_classes_val = [Val(c) for c in SymEngine.symengine_classes]
const symengine_classes_val_type = [Val{c} for c in SymEngine.symengine_classes]
"Get SymEngine class of an object (e.g. 1=>:Integer, 1//2 =:Rational, sin(x) => :Sin, ..."
get_symengine_class(s::Basic) = symengine_classes[get_type(s) + 1]
get_symengine_class_val(s::Basic) = symengine_classes_val[get_type(s) + 1]
get_symengine_class_val_type(s::Basic) = symengine_classes_val_type[get_type(s) + 1]
## Construct symbolic objects
## renamed, as `Symbol` conflicts with Base.Symbol
function _symbol(s::String)
a = Basic()
ccall((:symbol_set, libsymengine), Nothing, (Ref{Basic}, Ptr{Int8}), a, s)
return a
end
_symbol(s::Symbol) = _symbol(string(s))
## use SymPy name here, but no assumptions
"""
`symbols(::Symbol)` construct symbolic value
Examples:
```
a = symbols(:a)
x = symbols("x")
x,y = symbols("x y")
x,y,z = symbols("x,y,z")
```
"""
symbols(s::Symbol) = _symbol(s)
function symbols(s::String)
## handle space or comma sparation
s = replace(s, ","=> " ")
by_space = split(s, r"\s+")
Base.length(by_space) == 1 && return symbols(Symbol(s))
tuple([_symbol(Symbol(o)) for o in by_space]...)
end
## Follow, somewhat, the python names: symbols to construct symbols, @vars
"""
@vars x y[1:5] z()
Macro to define 1 or more variables or symbolic function
Example
```
@vars x y z
@vars x[1:4]
@vars u(), x
```
"""
macro vars(xs...)
# If the user separates declaration with commas, the top-level expression is a tuple
if length(xs) == 1 && isa(xs[1], Expr) && xs[1].head == :tuple
_gensyms(xs[1].args...)
elseif length(xs) > 0
_gensyms(xs...)
end
end
function _gensyms(xs...)
asstokw(a) = Expr(:kw, esc(a), true)
# Each declaration is parsed and generates a declaration using `symbols`
symdefs = map(xs) do expr
decl = parsedecl(expr)
symname = sym(decl)
symname, gendecl(decl)
end
syms, defs = collect(zip(symdefs...))
# The macro returns a tuple of Symbols that were declared
Expr(:block, defs..., :(tuple($(map(esc,syms)...))))
end
## We also have a wrapper type that can be used to control dispatch
## pros: wrapping adds overhead, so if possible best to use Basic
## cons: have to write methods meth(x::Basic, ...) = meth(BasicType(x),...)
## Parameterized type allowing for dispatch on Julia side by type of objecton SymEngine side
## Use as BasicType{Val{:Integer}}(...)
## To take advantage of this, define
## meth(x::Basic) = meth(BasicType(x))
## and then
## meth(x::BasicType{Val{:Integer}}) = ... or
## meth(x::BasicNumber) = ...
struct BasicType{T} <: Number
x::Basic
end
convert(::Type{Basic}, x::Basic) = x
SymbolicType = Union{Basic, BasicType}
convert(::Type{Basic}, x::BasicType) = x.x
Basic(x::BasicType) = x.x
BasicType(val::Basic) = BasicType{get_symengine_class_val_type(val)}(val)
convert(::Type{BasicType{T}}, val::Basic) where {T} =
BasicType{get_symengine_class_val_type(val)}(val)
# Needed for julia v0.4.7
convert(::Type{T}, x::Basic) where {T<:BasicType} = BasicType(x)
## We have Basic and BasicType{...}. We go back and forth with:
## Basic(b::BasicType) and BasicType(b::Basic)
# for mathops
Base.promote_rule(::Type{T}, ::Type{S} ) where {T<:BasicType, S<:Number} = T
Base.promote_rule(::Type{S}, ::Type{T} ) where {T<:BasicType, S<:Number} = T
# to intersperse BasicType and Basic in math ops
Base.promote_rule(::Type{T}, ::Type{Basic} ) where {T<:BasicType} = T
Base.promote_rule( ::Type{Basic}, ::Type{T} ) where {T<:BasicType} = T
Base.promote_rule(::Type{S}, ::Type{T}) where {S<:AbstractIrrational, T<:BasicType} = T
## needed for mathops
convert(::Type{T}, val::Number) where {T<:BasicType} = T(Basic(val))
## Julia v0.6 errors with ambiguous error if this method is not defined.
convert(::Type{T}, val::T) where {T<:BasicType} = val
## some type unions possibly useful for dispatch
## Names here match those returned by get_symengine_class()
real_number_types = [:Integer, :RealDouble, :Rational, :RealMPFR]
complex_number_types = [:Complex, :ComplexDouble, :ComplexMPC]
number_types = vcat(real_number_types, complex_number_types)
BasicNumber = Union{[SymEngine.BasicType{Val{i}} for i in number_types]...}
BasicRealNumber = Union{[SymEngine.BasicType{Val{i}} for i in real_number_types]...}
BasicComplexNumber = Union{[SymEngine.BasicType{Val{i}} for i in complex_number_types]...}
op_types = [:Mul, :Add, :Pow, :Symbol, :Const]
BasicOp = Union{[SymEngine.BasicType{Val{i}} for i in op_types]...}
trig_types = [:Sin, :Cos, :Tan, :Csc, :Sec, :Cot, :ASin, :ACos, :ATan, :ACsc, :ASec, :ACot]
BasicTrigFunction = Union{[SymEngine.BasicType{Val{i}} for i in trig_types]...}
###
"Is expression constant"
function is_constant(ex::Basic)
syms = CSetBasic()
ccall((:basic_free_symbols, libsymengine), Nothing, (Ref{Basic}, Ptr{Cvoid}), ex, syms.ptr)
Base.length(syms) == 0
end
"Is expression a symbol"
function is_symbol(x::SymbolicType)
res = ccall((:is_a_Symbol, libsymengine), Cuint, (Ref{Basic},), x)
Bool(convert(Int,res))
end
"Does expression contain the symbol"
function has_symbol(ex::SymbolicType, x::SymbolicType)
is_symbol(x) || throw(ArgumentError("Not a symbol"))
res = ccall((:basic_has_symbol, libsymengine), Cuint, (Ref{Basic},Ref{Basic}), ex, x)
Bool(convert(Int, res))
end
" Return free symbols in an expression as a `Set`"
function free_symbols(ex::Basic)
syms = CSetBasic()
free_symbols!(syms, ex)
convert(Vector, syms)
end
function free_symbols!(syms::CSetBasic, ex::Basic)
ccall((:basic_free_symbols, libsymengine), Nothing, (Ref{Basic}, Ptr{Cvoid}), ex, syms.ptr)
syms
end
free_symbols(ex::BasicType) = free_symbols(Basic(ex))
_flat(A) = mapreduce(x->isa(x,Array) ? _flat(x) : x, vcat, A, init=Basic[]) # from rosetta code example
free_symbols(exs::Array{T}) where {T<:SymbolicType} = unique(_flat([free_symbols(ex) for ex in exs]))
free_symbols(exs::Tuple) = unique(_flat([free_symbols(ex) for ex in exs]))
"Return function symbols in an expression as a `Set`"
function function_symbols(ex::Basic)
syms = CSetBasic()
function_symbols!(syms, ex)
convert(Vector, syms)
end
function function_symbols!(syms::CSetBasic, ex::Basic)
ccall((:basic_function_symbols, libsymengine), Nothing, (Ptr{Cvoid}, Ref{Basic}), syms.ptr, ex)
syms
end
function_symbols(ex::BasicType) = function_symbols(Basic(ex))
function_symbols(exs::Array{T}) where {T<:SymbolicType} = unique(_flat([function_symbols(ex) for ex in exs]))
function_symbols(exs::Tuple) = unique(_flat([function_symbols(ex) for ex in exs]))
"Return name of function symbol"
function get_name(ex::Basic)
a = ccall((:function_symbol_get_name, libsymengine), Cstring, (Ref{Basic}, ), ex)
string = unsafe_string(a)
ccall((:basic_str_free, libsymengine), Nothing, (Cstring, ), a)
return string
end
"Return arguments of a function call as a vector of `Basic` objects"
function get_args(ex::Basic)
args = CVecBasic()
get_args!(args, ex)
convert(Vector, args)
end
function get_args!(args::CVecBasic, ex::Basic)
ccall((:basic_get_args, libsymengine), Nothing, (Ref{Basic}, Ptr{Cvoid}), ex, args.ptr)
end
## so that Dicts will work
basic_hash(ex::Basic) = ccall((:basic_hash, libsymengine), UInt, (Ref{Basic}, ), ex)
# similar definition as in Base for general objects
Base.hash(ex::Basic, h::UInt) = Base.hash_uint(3h - basic_hash(ex))
Base.hash(ex::BasicType, h::UInt) = hash(Basic(ex), h)
function Serialization.serialize(s::Serialization.AbstractSerializer, m::Basic)
Serialization.serialize_type(s, typeof(m))
size = Ref{UInt64}(0)
serialized = ccall((:basic_dumps, libsymengine),
Ptr{Int8}, (Ref{Basic}, Ptr{UInt64}), m, size)
write(s.io, size[])
unsafe_write(s.io, serialized, size[])
end
function Serialization.deserialize(s::Serialization.AbstractSerializer, ::Type{Basic})
size = read(s.io, UInt64)
serialized_data = read(s.io, size)
a = Basic()
res = ccall((:basic_loads, libsymengine),
Cuint, (Ref{Basic}, Ptr{Int8}, UInt64), a, serialized_data, size)
throw_if_error(res)
return a
end