-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathsymbolics_02.py
123 lines (111 loc) · 2.51 KB
/
symbolics_02.py
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
from sympy import Symbol, pi, Add, Mul, Pow
from lpython import S
def test_symbolic_operations():
x: S = Symbol('x')
y: S = Symbol('y')
pi1: S = pi
pi2: S = pi
# Addition
z: S = x + y
z1: bool = z.func == Add
z2: bool = z.func == Mul
assert(z == x + y)
assert(z1 == True)
assert(z2 == False)
if z.func == Add:
assert True
else:
assert False
assert(z.func == Add)
assert(z.args[0] == x or z.args[0] == y)
assert(z.args[1] == y or z.args[1] == x)
print(z)
# Subtraction
w: S = x - y
w1: bool = w.func == Add
assert(w == x - y)
assert(w1 == True)
if w.func == Add:
assert True
else:
assert False
assert(w.func == Add)
print(w)
# Multiplication
u: S = x * y
u1: bool = u.func == Mul
assert(u == x * y)
assert(u1 == True)
if u.func == Mul:
assert True
else:
assert False
assert(u.func == Mul)
assert(u.args[0] == x)
assert(u.args[1] == y)
print(u)
# Division
v: S = x / y
v1: bool = v.func == Mul
assert(v == x / y)
assert(v1 == True)
if v.func == Mul:
assert True
else:
assert False
assert(v.func == Mul)
print(v)
# Power
p: S = x ** y
p1: bool = p.func == Pow
p2: bool = p.func == Add
p3: bool = p.func == Mul
assert(p == x ** y)
assert(p1 == True)
assert(p2 == False)
assert(p3 == False)
if p.func == Pow:
assert True
else:
assert False
assert(p.func == Pow)
print(p)
# Casting
a: S = S(100)
b: S = S(-100)
c: S = a + b
assert(c == S(0))
print(c)
# Comparison
b1: bool = pi1 == pi2
print(b1)
assert(b1 == True)
b2: bool = pi1 != pi
print(b2)
assert(b2 == False)
b3: bool = pi1 != x
print(b3)
assert(b3 == True)
b4: bool = pi == Symbol("x")
print(b4)
assert(b4 == False)
# is_integer check
assert(pi1.is_integer == False)
assert(a.is_integer == True)
assert(c.is_integer == True)
# is_positive check
assert(a.is_positive == True)
assert(b.is_positive == False)
assert(c.is_positive == False)
# logical binop check
l1: bool = True and p.func == Pow
l2: bool = False or p.func == Pow
l3: bool = False and u.func == Mul
l4: bool = True or u.func == Add
if p.func == Pow and u.func == Mul:
print(True)
assert(l1)
assert(l2)
assert(not l3)
assert(l4)
test_symbolic_operations()