forked from rai-opensource/spatialmath-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_symbolic.py
More file actions
86 lines (63 loc) · 2.48 KB
/
test_symbolic.py
File metadata and controls
86 lines (63 loc) · 2.48 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
import unittest
import math
try:
import sympy as sp
_symbolics = True
except ImportError:
_symbolics = False
from spatialmath.base.symbolic import *
class Test_symbolic(unittest.TestCase):
@unittest.skipUnless(_symbolics, "sympy required")
def test_symbol(self):
theta = symbol("theta")
self.assertTrue(isinstance(theta, sp.Expr))
self.assertTrue(theta.is_real)
theta = symbol("theta", real=False)
self.assertTrue(isinstance(theta, sp.Expr))
self.assertFalse(theta.is_real)
theta, psi = symbol("theta, psi")
self.assertTrue(isinstance(theta, sp.Expr))
self.assertTrue(isinstance(psi, sp.Expr))
theta, psi = symbol("theta psi")
self.assertTrue(isinstance(theta, sp.Expr))
self.assertTrue(isinstance(psi, sp.Expr))
q = symbol("q:6")
self.assertEqual(len(q), 6)
for _ in q:
self.assertTrue(isinstance(_, sp.Expr))
self.assertTrue(_.is_real)
@unittest.skipUnless(_symbolics, "sympy required")
def test_issymbol(self):
theta = symbol("theta")
self.assertFalse(issymbol(3))
self.assertFalse(issymbol("not a symbol"))
self.assertFalse(issymbol([1, 2]))
self.assertTrue(issymbol(theta))
@unittest.skipUnless(_symbolics, "sympy required")
def test_functions(self):
theta = symbol("theta")
self.assertTrue(isinstance(sin(theta), sp.Expr))
self.assertTrue(isinstance(sin(1.0), float))
self.assertTrue(isinstance(cos(theta), sp.Expr))
self.assertTrue(isinstance(cos(1.0), float))
self.assertTrue(isinstance(sqrt(theta), sp.Expr))
self.assertTrue(isinstance(sqrt(1.0), float))
x = (theta - 1) * (theta + 1) - theta ** 2
self.assertEqual(simplify(x).evalf(), -1)
@unittest.skipUnless(_symbolics, "sympy required")
def test_constants(self):
x = zero()
self.assertTrue(isinstance(x, sp.Expr))
self.assertEqual(x.evalf(), 0)
x = one()
self.assertTrue(isinstance(x, sp.Expr))
self.assertEqual(x.evalf(), 1)
x = negative_one()
self.assertTrue(isinstance(x, sp.Expr))
self.assertEqual(x.evalf(), -1)
x = pi()
self.assertTrue(isinstance(x, sp.Expr))
self.assertEqual(x.evalf(), math.pi)
# ---------------------------------------------------------------------------------------#
if __name__ == "__main__": # pragma: no cover
unittest.main()