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

  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/sympy/simplify/simplify.py", line 603, in simplify
    original_expr = expr = collect_abs(signsimp(expr))
  File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/sympy/simplify/radsimp.py", line 625, in collect_abs
    return expr.replace(
AttributeError: 'NoneType' object has no attribute 'replace'

Note

Converts to a SymPy Matrix and 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:

symbolic

>>> 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:

symbolic

>>> 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:

symbolic

>>> 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 (symbolic) – expression to simplify

Returns:

-1

Return type:

symbolic

>>> 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 names

  • real (bool, optional) – assume variable is real, defaults to True

Returns:

SymPy symbols

Return type:

sympy

>>> 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 q

above will be subscripted.

Seealso:

sympy.symbols()

tan(theta)[source]

Generalized tangent function

Parameters:

θ (float or symbolic) – argument

Returns:

tan(θ)

Return type:

float or symbolic

>>> from spatialmath.base.symbolic import *
>>> theta = symbol('theta')
>>> tan(theta)
tan(theta)
>>> tan(0.5)
0.5463024898437905
Seealso:

sympy.cos()

zero()[source]

Symbolic constant: zero

Returns:

0

Return type:

symbolic

>>> from spatialmath.base.symbolic import *
>>> x = symbol('x')
>>> zero()
0
>>> x + zero()
x
Seealso:

sympy.S.Zero()