8000 continue refactoring · bdaiinstitute/spatialmath-python@18dcbd1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 18dcbd1

Browse files
committed
continue refactoring
1 parent b3ce0f4 commit 18dcbd1

18 files changed

+228
-263
lines changed

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ matplotlib = ">=3.9.3"
4545
ansitable = "*"
4646
typing_extensions = "*"
4747
pre-commit = "*"
48+
sympy = "^1.13.3"
4849

4950
[tool.poetry.group.dev.dependencies]
5051
sympy = "*"
@@ -72,6 +73,9 @@ line-length = 88
7273
target-version = "py38"
7374
exclude = ["camera_derivatives.py"]
7475

76+
[tool.ruff.lint]
77+
ignore = ["E741", "E731"]
78+
7579
[tool.ruff.format]
7680
quote-style = "double"
7781
indent-style = "space"

spatialmath/base/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from spatialmath.base.graphics import * # lgtm [py/polluting-import]
1414
from spatialmath.base.numeric import * # lgtm [py/polluting-import]
1515

16+
1617
from spatialmath.base.argcheck import (
1718
assertmatrix,
1819
ismatrix,

spatialmath/base/graphics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from matplotlib import colors
66
from typing import Callable
77
from numpy.typing import NDArray
8-
from abc import overload
8+
from typing import overload
99

1010
from spatialmath import base as smb
1111
from spatialmath.base.types import (

spatialmath/base/symbolic.py

Lines changed: 8 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,13 @@
1-
# Part of Spatial Math Toolbox for Python
2-
# Copyright (c) 2000 Peter Corke
3-
# MIT Licence, see details in top-level file: LICENCE
4-
5-
"""
6-
This package provides a light-weight wrapper to support use of SymPy. It
7-
generalizes some common functions so that they can accept numerical or
8-
Symbolic arguments.
9-
10-
If SymPy is not installed then only the standard numeric operations are
11-
supported.
12-
"""
13-
141
import math
15-
from spatialmath.base.types import *
16-
17-
try: # pragma: no cover
18-
# print('Using SymPy')
19-
import sympy
2+
from typing import Any, overload
3+
import sympy
4+
from sympy import Symbol
205

21-
_symbolics = True
22-
symtype = (sympy.Expr,)
23-
from sympy import Symbol
24-
25-
except ImportError: # pragma: no cover
26-
# SymPy is not installed
27-
_symbolics = False
28-
symtype = ()
29-
Symbol = Any
306

7+
symtype = (sympy.Expr,)
318

329
# ---------------------------------------------------------------------------------------#
3310

34-
if _symbolics:
35-
36-
def symbol(
37-
name: str, real: Optional[bool] = True
38-
) -> Union[Symbol, Tuple[Symbol, ...]]:
39-
"""
40-
Create symbolic variables
41-
42-
:param name: symbol names
43-
:type name: str
44-
:param real: assume variable is real, defaults to True
45-
:type real: bool, optional
46-
:return: SymPy symbols
47-
:rtype: sympy
48-
49-
.. runblock:: pycon
50-
51-
>>> from spatialmath.base.symbolic import *
52-
>>> theta = symbol('theta')
53-
>>> theta
54-
>>> theta, psi = symbol('theta psi')
55-
>>> theta
56-
>>> psi
57-
>>> q = symbol('q_:6')
58-
>>> q
59-
60-
.. note:: In Jupyter symbols are pretty printed.
61-
62-
- symbols named after greek letters will appear as greek letters
63-
- underscore means subscript as it does in LaTex, so the symbols ``q``
64-
above will be subscripted.
65-
66-
:seealso: :func:`sympy.symbols`
67-
"""
68-
return sympy.symbols(name, real=real)
69-
7011

7112
def issymbol(var: Any) -> bool:
7213
"""
@@ -84,13 +25,10 @@ def issymbol(var: Any) -> bool:
8425
>>> issymbol(3.4)
8526
8627
"""
87-
if _symbolics:
88-
if isinstance(var, (list, tuple)):
89-
return any([isinstance(x, symtype) for x in var])
90-
else:
91-
return isinstance(var, symtype)
28+
if isinstance(var, (list, tuple)):
29+
return any([isinstance(x, symtype) for x in var])
9230
else:
93-
return False
31+
return isinstance(var, symtype)
9432

9533

9634
@overload
@@ -316,10 +254,7 @@ def simplify(x: Symbol) -> Symbol:
316254
317255
:seealso: :func:`sympy.simplify`
318256
"""
319-
if _symbolics:
320-
return sympy.simplify(x)
321-
else:
322-
return x
257+
return sympy.simplify(x)
323258

324259

325260
def det(x):

tests/base/test_quaternions.py

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,26 @@
1-
# This file is part of the SpatialMath toolbox for Python
2-
# https://github.com/petercorke/spatialmath-python
3-
#
4-
# MIT License
5-
#
6-
# Copyright (c) 1993-2020 Peter Corke
7-
#
8-
# Permission is hereby granted, free of charge, to any person obtaining a copy
9-
# of this software and associated documentation files (the "Software"), to deal
10-
# in the Software without restriction, including without limitation the rights
11-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12-
# copies of the Software, and to permit persons to whom the Software is
13-
# furnished to do so, subject to the following conditions:
14-
#
15-
# The above copyright notice and this permission notice shall be included in all
16-
# copies or substantial portions of the Software.
17-
#
18-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24-
# SOFTWARE.
25-
26-
# Contributors:
27-
#
28-
# 1. Luis Fernando Lara Tobar and Peter Corke, 2008
29-
# 2. Josh Carrigg Hodson, Aditya Dua, Chee Ho Chan, 2017 (robopy)
30-
# 3. Peter Corke, 2020
31-
321
import numpy.testing as nt
332
import unittest
34-
35-
from spatialmath.base.vectors import *
3+
import math
4+
import numpy as np
5+
from spatialmath.base.vectors import isunitvec
366
import spatialmath.base as tr
37-
from spatialmath.base.quaternions import *
7+
from spatialmath.base.quaternions import (
8+
qeye,
9+
qpure,
10+
qnorm,
11+
qunit,
12+
qqmul,
13+
qmatrix,
14+
qpow,
15+
qisequal,
16+
qprint,
17+
qrand,
18+
r2q,
19+
q2r,
20+
qvmul,
21+
qslerp,
22+
qangle,
23+
)
3824
import spatialmath as sm
3925

4026

tests/base/test_symbolic.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import unittest
22
import math
3-
4-
try:
5-
import sympy as sp
6-
7-
_symbolics = True
8-
except ImportError:
9-
_symbolics = False
10-
11-
from spatialmath.base.symbolic import *
3+
import sympy as sp
4+
5+
# from spatialmath.base.symbolic import *
6+
from spatialmath.base.symbolic import (
7+
symbol,
8+
sin,
9+
cos,
10+
sqrt,
11+
simplify,
12+
zero,
13+
one,
14+
negative_one,
15+
pi,
16+
issymbol,
17+
)
1218

1319

1420
class Test_symbolic(unittest.TestCase):
15-
@unittest.skipUnless(_symbolics, "sympy required")
1621
def test_symbol(self):
1722
theta = symbol("theta")
1823
self.assertTrue(isinstance(theta, sp.Expr))
@@ -36,15 +41,13 @@ def test_symbol(self):
3641
self.assertTrue(isinstance(_, sp.Expr))
3742
self.assertTrue(_.is_real)
3843

39-
@unittest.skipUnless(_symbolics, "sympy required")
4044
def test_issymbol(self):
4145
theta = symbol("theta")
4246
self.assertFalse(issymbol(3))
4347
self.assertFalse(issymbol("not a symbol"))
4448
self.assertFalse(issymbol([1, 2]))
4549
self.assertTrue(issymbol(theta))
4650

47-
@unittest.skipUnless(_symbolics, "sympy required")
4851
def test_functions(self):
4952
theta = symbol("theta")
5053
self.assertTrue(isinstance(sin(theta), sp.Expr))
@@ -59,7 +62,6 @@ def test_functions(self):
5962
x = (theta - 1) * (theta + 1) - theta**2
6063
self.assertTrue(math.isclose(simplify(x).evalf(), -1))
6164

62-
@unittest.skipUnless(_symbolics, "sympy required")
6365
def test_constants(self):
6466
x = zero()
6567
self.assertTrue(isinstance(x, sp.Expr))

tests/base/test_transforms.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,32 @@
1313
from math import pi
1414
from scipy.linalg import logm, expm
1515

16-
from spatialmath.base import *
16+
# from spatialmath.base import *
17+
from spatialmath.base import (
18+
vex,
19+
skew,
20+
vexa,
21+
skewa,
22+
trlog,
23+
trexp,
24+
trnorm,
25+
transl,
26+
isrot,
27+
ishom,
28+
isrot2,
29+
ishom2,
30+
rotx,
31+
roty,
32+
rotz,
33+
trotx,
34+
troty,
35+
trotz,
36+
t2r,
37+
trexp2,
38+
rot2,
39+
transl2,
40+
trot2,
41+
)
1742

1843

1944
class TestLie(unittest.TestCase):

tests/base/test_transforms2d.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,29 @@
1616
import pytest
1717
import sys
1818

19-
from spatialmath.base.transforms2d import *
19+
# from spatialmath.base.transforms2d import *
20+
from spatialmath.base.transforms2d import (
21+
rot2,
22+
trot2,
23+
trlog2,
24+
trexp2,
25+
trnorm2,
26+
transl2,
27+
pos2tr2,
28+
tr2jac2,
29+
trinv2,
30+
tradjoint2,
31+
points2tr2,
32+
xyt2tr,
33+
isrot2,
34+
ishom2,
35+
tr2pos2,
36+
ICP2d,
37+
trprint2,
38+
trinterp2,
39+
trplot2,
40+
)
41+
from spatialmath.base import smb
2042
from spatialmath.base.transformsNd import (
2143
isR,
2244
t2r,
@@ -224,7 +246,6 @@ def test_checks(self):
224246
nt.assert_equal(ishom2(T, True), False)
225247

226248
def test_trinterp2(self):
227-
R0 = rot2(-0.3)
228249
R1 = rot2(0.3)
229250

230251
nt.assert_array_almost_equal(trinterp2(start=None, end=R1, s=0), np.eye(2))

0 commit comments

Comments
 (0)
0