Symbolic computation
This package provides a light-weight wrapper to support use of SymPy. It generalizes some common functions so that they can accept numerical or Symbolic arguments.
If SymPy is not installed then only the standard numeric operations are supported.
- cos(theta)[source]
Generalized cosine function
- Parameters:
θ (float or symbolic) – argument
- Returns:
cos(θ)
- Return type:
float or symbolic
>>> from spatialmath.base.symbolic import * >>> theta = symbol('theta') >>> cos(theta) cos(theta) >>> cos(0.5) 0.8775825618903728
- Seealso:
sympy.cos()
- det(x)[source]
Symbolic determinant
- Parameters:
m – matrix
- Returns:
determinant
- Return type:
ndarray with symbolic elements
>>> from spatialmath.base.symbolic import * >>> from spatialmath.base import rot2 >>> theta = symbol('theta') >>> R = rot2(theta) >>> print(R) [[cos(theta) -sin(theta)] [sin(theta) cos(theta)]] >>> print(det(R)) sin(theta)**2 + cos(theta)**2 >>> print(simplify(det(R))) 1
Note
Converts to a SymPy
Matrixand then back again.
- issymbol(var)[source]
Test if variable is symbolic
- Parameters:
var (
Any) – variable to test- Returns:
whether variable is symbolic
- Return type:
bool
>>> from spatialmath.base.symbolic import * >>> theta = symbol('theta') >>> issymbol(theta) True >>> issymbol(3.4) False
- negative_one()[source]
Symbolic constant: negative one
- Returns:
-1
- Return type:
Symbol
>>> from spatialmath.base.symbolic import * >>> x = symbol('x') >>> negative_one() -1 >>> negative_one() * x -x
- Seealso:
sympy.S.NegativeOne()
- one()[source]
Symbolic constant: one
- Returns:
1
- Return type:
Symbol
>>> from spatialmath.base.symbolic import * >>> x = symbol('x') >>> one() 1 >>> one() * x x
- Seealso:
sympy.S.One()
- pi()[source]
Symbolic constant: pi
- Returns:
π
- Return type:
Symbol
>>> from spatialmath.base.symbolic import * >>> import math >>> sin(pi()) 0 >>> sin(math.pi) 1.2246467991473532e-16
- Seealso:
sympy.S.Pi()
- simplify(x)[source]
Symbolic simplification
- Parameters:
x (
Symbol) – expression to simplify- Returns:
-1
- Return type:
Symbol
>>> from spatialmath.base.symbolic import * >>> x = symbol('x') >>> y = (x - 1) * (x + 1) - x ** 2 >>> y -x**2 + (x - 1)*(x + 1) >>> simplify(y) -1
- Seealso:
sympy.simplify()
- sin(theta)[source]
Generalized sine function
- Parameters:
θ (float or symbolic) – argument
- Returns:
sin(θ)
- Return type:
float or symbolic
>>> from spatialmath.base.symbolic import * >>> theta = symbol('theta') >>> sin(theta) sin(theta) >>> sin(0.5) 0.479425538604203
- Seealso:
sympy.sin()
- sqrt(v)[source]
Generalized sqrt function
- Parameters:
v (float or symbolic) – argument
- Returns:
√ v
- Return type:
float or symbolic
>>> from spatialmath.base.symbolic import * >>> x = symbol('x') >>> sqrt(x ** 2) Abs(x) >>> sqrt(4) 2.0
- Seealso:
sympy.sqrt()
- symbol(name, real=True)[source]
Create symbolic variables
- Parameters:
name (
str) – symbol namesreal (
Optional[bool]) – assume variable is real, defaults to True
- Returns:
SymPy symbols
- Return type:
Union[Symbol,Tuple[Symbol,...]]
>>> from spatialmath.base.symbolic import * >>> theta = symbol('theta') >>> theta theta >>> theta, psi = symbol('theta psi') >>> theta theta >>> psi psi >>> q = symbol('q_:6') >>> q (q_0, q_1, q_2, q_3, q_4, q_5)
Note
In Jupyter symbols are pretty printed.
symbols named after greek letters will appear as greek letters
underscore means subscript as it does in LaTex, so the symbols
qabove will be subscripted.
- Seealso:
sympy.symbols()