forked from rai-opensource/spatialmath-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_numeric.py
More file actions
executable file
·116 lines (83 loc) · 3.08 KB
/
test_numeric.py
File metadata and controls
executable file
·116 lines (83 loc) · 3.08 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Apr 10 14:19:04 2020
@author: corkep
"""
import numpy as np
import unittest
from spatialmath.base.numeric import *
class TestNumeric(unittest.TestCase):
def test_numjac(self):
pass
def test_array2str(self):
x = [1.2345678]
s = array2str(x)
self.assertIsInstance(s, str)
self.assertEqual(s, "[ 1.23 ]")
s = array2str(x, fmt="{:.5f}")
self.assertEqual(s, "[ 1.23457 ]")
s = array2str([1, 2, 3])
self.assertEqual(s, "[ 1, 2, 3 ]")
s = array2str([1, 2, 3], valuesep=":")
self.assertEqual(s, "[ 1:2:3 ]")
s = array2str([1, 2, 3], brackets=("<< ", " >>"))
self.assertEqual(s, "<< 1, 2, 3 >>")
s = array2str([1, 2e-8, 3])
self.assertEqual(s, "[ 1, 2e-08, 3 ]")
s = array2str([1, -2e-14, 3])
self.assertEqual(s, "[ 1, 0, 3 ]")
x = np.array([[1, 2, 3], [4, 5, 6]])
s = array2str(x)
self.assertEqual(s, "[ 1, 2, 3 | 4, 5, 6 ]")
def test_bresenham(self):
x, y = bresenham((-10, -10), (20, 10))
self.assertIsInstance(x, np.ndarray)
self.assertEqual(x.ndim, 1)
self.assertIsInstance(y, np.ndarray)
self.assertEqual(y.ndim, 1)
self.assertEqual(len(x), len(y))
# test points are no more than sqrt(2) apart
z = np.array([x, y])
d = np.diff(z, axis=1)
d = np.linalg.norm(d, axis=0)
self.assertTrue(all(d <= np.sqrt(2)))
x, y = bresenham((20, 10), (-10, -10))
# test points are no more than sqrt(2) apart
z = np.array([x, y])
d = np.diff(z, axis=1)
d = np.linalg.norm(d, axis=0)
self.assertTrue(all(d <= np.sqrt(2)))
x, y = bresenham((-10, -10), (10, 20))
# test points are no more than sqrt(2) apart
z = np.array([x, y])
d = np.diff(z, axis=1)
d = np.linalg.norm(d, axis=0)
self.assertTrue(all(d <= np.sqrt(2)))
x, y = bresenham((10, 20), (-10, -10))
# test points are no more than sqrt(2) apart
z = np.array([x, y])
d = np.diff(z, axis=1)
d = np.linalg.norm(d, axis=0)
self.assertTrue(all(d <= np.sqrt(2)))
def test_mpq(self):
data = np.array([[-1, 1, 1, -1], [-1, -1, 1, 1]])
self.assertEqual(mpq_point(data, 0, 0), 4)
self.assertEqual(mpq_point(data, 1, 0), 0)
self.assertEqual(mpq_point(data, 0, 1), 0)
def test_gauss1d(self):
x = np.arange(-10, 10, 0.02)
y = gauss1d(2, 1, x)
self.assertEqual(len(x), len(y))
m = np.argmax(y)
self.assertAlmostEqual(x[m], 2)
def test_gauss2d(self):
r = np.arange(-10, 10, 0.02)
X, Y = np.meshgrid(r, r)
Z = gauss2d([2, 3], np.eye(2), X, Y)
m = np.unravel_index(np.argmax(Z, axis=None), Z.shape)
self.assertAlmostEqual(r[m[0]], 3)
self.assertAlmostEqual(r[m[1]], 2)
# ---------------------------------------------------------------------------------------#
if __name__ == "__main__":
unittest.main()