/
runtests.jl
263 lines (218 loc) · 6.67 KB
/
runtests.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
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
using SymEngine
using Compat
using Test
import Base: MathConstants.γ, MathConstants.e, MathConstants.φ, MathConstants.catalan
include("test-dense-matrix.jl")
x = symbols("x")
y = symbols(:y)
@vars z
# Check Basic conversions
@test eltype([Basic(u) for u in [1, 1/2, 1//2, pi, e]]) == Basic
# make sure @vars defines in a local scope
let
@vars w
end
@test_throws UndefVarError isdefined(w)
@test_throws Exception show(Basic())
a = x^2 + x/2 - x*y*5
b = diff(a, x)
@test b == 2*x + 1//2 - 5*y
c = x + Rational(1, 5)
c = expand(c * 5)
@test c == 5*x + 1
c = sum(convert(SymEngine.CVecBasic, [x, x, 1]))
@test c == 2*x + 1
@test x + x + 1 == 2*x + 1
@test x + 1 + 1 == x + 2
@test 1 + x + 1 == x + 2
c = x ^ 5
@test diff(c, x) == 5 * x ^ 4
c = x ^ y
@test c != y^x
c = Basic(-5)
@test abs(c) == 5
@test abs(c) != 4
show(a)
println()
show(b)
println()
## mathfuns
@test abs(Basic(-1)) == 1
@test sin(Basic(1)) == subs(sin(x), x, 1)
@test sin(PI) == 0
@test subs(sin(x), x, pi) == 0
@test sind(Basic(30)) == 1 // 2
## calculus
x,y = symbols("x y")
n = Basic(2)
ex = sin(x*y)
@test diff(log(x),x) == 1/x
@test diff(ex, x) == y * cos(x*y)
@test diff(ex, x, 2) == diff(diff(ex,x), x)
@test diff(ex, x, n) == diff(diff(ex,x), x)
@test diff(ex, x, y) == diff(diff(ex,x), y)
@test diff(ex, x, y,x) == diff(diff(diff(ex,x), y), x)
@test series(sin(x), x, 0, 2) == x
@test series(sin(x), x, 0, 3) == x - x^3/6
## ntheory
@test mod(Basic(10), Basic(4)) == 2
for j in [-3, 3], p in [-5,5]
@test mod(Basic(j), Basic(p)) == mod(j, p)
end
@test mod(Basic(10), 4) == 2 # mod(::Basic, ::Number)
@test_throws MethodError mod(10, Basic(4)) # no mod(::Number, ::Basic)
@test gcd(Basic(10), Basic(4)) == 2
@test lcm(Basic(10), Basic(4)) == 20
@test binomial(Basic(5), 2) == 10
## type information
a = Basic(1)
b = Basic(1//2)
c = Basic(0.125)
@test isa(SymEngine.BasicType(a+a), SymEngine.BasicType{Val{:Integer}})
@test isa(SymEngine.BasicType(a+b), SymEngine.BasicType{Val{:Rational}})
@test isa(SymEngine.BasicType(a+c), SymEngine.BasicType{Val{:RealDouble}})
@test isa(SymEngine.BasicType(b+c), SymEngine.BasicType{Val{:RealDouble}})
@test isa(SymEngine.BasicType(c+c), SymEngine.BasicType{Val{:RealDouble}})
## can we do math with items of BasicType?
a1 = SymEngine.BasicType(a)
tot = a1
for i in 1:100
global tot
tot = tot + a1
end
@test tot == 101
sin(a1)
# samples of different types:
# (Int, Rational{Int}, Complex{Int}, Float64, Complex{Float64})
samples = (1, 1//2, (1 + 2im), 1.0, (1.0 + 0im))
## subs - check all different syntaxes and types
ex = x^2 + y^2
for val in samples
@test subs(ex, x, val) == val^2 + y^2
@test subs(ex, (x, val)) == val^2 + y^2
@test subs(ex, x => val) == val^2 + y^2
@test subs(ex, SymEngine.CMapBasicBasic(Dict(x=>val))) == val^2 + y^2
@test subs(ex, Dict(x=>val)) == val^2 + y^2
end
# This probably results in a number of redundant tests (operator order).
for val1 in samples, val2 in samples
@test subs(ex, (x, val1), (y, val2)) == val1^2 + val2^2
@test subs(ex, x => val1, y => val2) == val1^2 + val2^2
end
## lambidfy
@test abs(lambdify(sin(Basic(1))) - sin(1)) <= 1e-14
fn = lambdify(exp(PI/2*x))
@test abs(fn(1) - exp(pi/2)) <= 1e-14
for val in samples
ex2 = sin(x + val)
fn2 = lambdify(ex2)
@test abs(fn2(val) - sin(2*val)) <= 1e-14
end
@test lambdify(x^2)(3) == 9
A = [x 2; x 1]
@test lambdify(A, [x])(0) == [0 2; 0 1]
@test lambdify(A)(0) == [0 2; 0 1]
A = [x 2]
@test lambdify(A, [x])(1) == [1 2]
@test lambdify(A)(1) == [1 2]
@test isa(convert.(Expr, [0 x x+1]), Array{Expr})
## N
for val in samples
@test N(Basic(val)) == val
end
for val in [π, γ, e, φ, catalan]
@test N(Basic(val)) == val
end
@test !isnan(x)
@test isnan(Basic(0)/0)
## generic linear algebra
x = symbols("x")
A = [x 2; x 1]
#@test det(A) == -x
#@test det(inv(A)) == - 1/x
#(A \ [1,2])[1] == 3/x
## check that unique work (hash)
x,y,z = symbols("x y z")
@test length(SymEngine.free_symbols([x*y, y,z])) == 3
## check that callable symengine expressions can be used as functions for duck-typed functions
@vars x
function simple_newton(f, fp, x0)
x = float(x0)
while abs(f(x)) >= 1e-14
x = x - f(x)/fp(x)
end
x
end
@test abs(simple_newton(sin(x), diff(sin(x), x), 3) - pi) <= 1e-14
## Check conversions SymEngine -> Julia
z,flt, rat, ima, cplx = btypes = [Basic(1), Basic(1.23), Basic(3//5), Basic(2im), Basic(1 + 2im)]
@test Int(z) == 1
@test BigInt(z) == 1
@test Float64(flt) ≈ 1.23
@test Real(flt) ≈ 1.23
@test convert(Rational{Int}, rat) == 3//5
@test convert(Complex{Int}, ima) == 2im
@test convert(Complex{Int}, cplx) == 1 + 2im
@test_throws InexactError convert(Int, flt)
@test_throws InexactError convert(Int, rat)
x = symbols("x")
Number[1 2 3 x]
@test_throws ArgumentError Int[1 2 3 x]
t = BigFloat(1.23)
@test !SymEngine.have_component("mpfr") || t == convert(BigFloat, convert(Basic, t))
@test typeof(N(Basic(-1))) != BigInt
# Check that libversion works. VersionNumber should always be >= 0.2.0
# since 0.2.0 is the first public release
@test SymEngine.libversion >= VersionNumber("0.2.0")
# Check that constructing Basic from Expr works
@vars x y
@test Basic(:(-2*x)) == -2*x
@test Basic(:(-3*x*y)) == -3*x*y
@test Basic(:((x-y)*-3)) == (x-y)*(-3)
@test Basic(:(-y)) == -y
@test Basic(:(-2*(x-2*y))) == -2*(x-2*y)
@test Basic(0)/0 == NAN
@test subs(1/x, x, 0) == Basic(1)/0
d = Dict(x=>y, y=>x)
@test subs(x + 2*y, d) == y + 2*x
@test sin(x+PI/4) != sin(x)
@test sin(PI/2-x) == cos(x)
f = SymFunction("f")
@test string(f(x, y)) == "f(x, y)"
@test string(f([x, y])) == "f(x, y)"
@test string(f(2*x)) == "f(2*x)"
@funs g, h
@test string(g(x, y)) == "g(x, y)"
@test string(h(x, y)) == "h(x, y)"
if SymEngine.libversion >= VersionNumber("0.4.0")
# Function symbols
@funs f, g, h, s
@vars n, m
expr = Basic("f(n) + f(n+1) + g(m)^2 - 3*h(n)*s(n)^3")
@test function_symbols(expr) == [h(n), f(n), s(n), g(m), f(n+1)]
@test get_name(f(n+1)) == "f"
@test get_args(f(n+1)) == [n+1]
@test get_args(f(n, m^2)) == [n, m^2]
# Coefficients
@vars x y
expr = x^3 + 3*x^2*y + 3*x*y^2 + y^3 + 1
@test coeff(expr, x, Basic(3)) == 1
@test coeff(expr, x, Basic(2)) == 3*y
@test coeff(expr, x, Basic(1)) == 3*y^2
if SymEngine.libversion >= VersionNumber("0.5.0")
@test coeff(expr, x, Basic(0)) == y^3 + 1
end
end
# Check that infinities are handled correctly
@test_throws DomainError exp(zoo)
@test_throws DomainError sin(zoo)
@test_throws DomainError sin(oo)
@test_throws DomainError subs(sin(log(y - y/x)), x => 1)
# Some basic checks for complex numbers
@testset "Complex numbers" begin
for T in (Int, Float64, BigFloat)
j = one(T) * IM
@test j == imag(j) * IM
@test conj(j) == -j
end
end