forked from symengine/SymEngine.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntests.jl
193 lines (157 loc) · 4.76 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
using Base.Test
using SymEngine
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)
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 = 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 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
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 norm(lambdify(sin(Basic(1))) - sin(1)) <= 1e-14
fn = lambdify(exp(PI/2*x))
@test norm(fn(1) - exp(pi/2)) <= 1e-14
for val in samples
ex = sin(x + val)
fn = lambdify(ex)
@test norm(fn(val) - sin(2*val)) <= 1e-14
end
@test lambdify(x^2)(3) == 9
## N
for val in samples
@test N(Basic(val)) == val
end
for val in [π, γ, e, φ, catalan]
@test N(Basic(val)) == val
end
## 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 norm(f(x)) >= 1e-14
x = x - f(x)/fp(x)
end
x
end
@test norm(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 string(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)