Argument checking
Utility functions for testing and converting passed arguments. Used in all spatialmath functions and classes to provides for flexibility in argument types that can be passed.
- assertmatrix(m, shape=(None, None))[source]
Assert that argument is a 2D matrix
- Parameters:
m (
Any) – value to testshape (
Tuple[Optional[int],Optional[int]]) – required shape
- Raises:
TypeError – if value is not a real Numpy array
ValueError – if value is not of the specified shape
- Return type:
None
Tests if the argument is a real 2D matrix with a specified shape
shapebut the valueNoneindicate an unspecified (wildcard, don’t care) dimension.assertsmatrix(A)raises an exception ifmis not convertible to a 2D arrayassertsmatrix(A, (N,M))as above butmmust have shape (N,``M``)assertsmatrix(A, (N,None))as above butmmust haveNrowsassertsmatrix(A, (None,M))as above butmmust haveMcolumns
- Seealso:
- assertvector(v, dim=None, msg=None)[source]
Assert that argument is a real vector
- Parameters:
v (
Any) – passed vectordim (
Optional[int]) – required dimension
- Raises:
ValueError – if not a vector of specified length
- Return type:
None
assertvector(vec)raise an exception ifvecis not a vector, ie. it is not any of:a Python native int or float, a 1-vector
Python native list or tuple
numPy real 1D array, ie. shape=(N,)
numPy real 2D array with a singleton dimension, ie. shape=(1,N) or (N,1)
assertvector(vec, N)as above but must also check the length isN.
- Seealso:
- getmatrix(m, shape, dtype=<class 'numpy.float64'>)[source]
Convert argument to 2D array
- Parameters:
- Raises:
ValueError – if
mis inconsistent withshapeTypeError – if
mis not required typeTypeError – if value is not a scalar or Numpy array
ValueError – if value is not of the specified shape
- Returns:
a 2D array
- Return type:
getmatrix(m, shape)is a 2D matrix with shapeshapeformed frommwhich can be a 2D array, 1D array-like or a scalar.>>> from spatialmath.base import getmatrix >>> import numpy as np >>> getmatrix(3, (1,1)) array([[3.]]) >>> getmatrix([3,4], (1,2)) array([[3., 4.]]) >>> getmatrix([3,4], (2, 1)) array([[3.], [4.]]) >>> getmatrix([3,4,5,6], (2,2)) array([[3., 4.], [5., 6.]]) >>> getmatrix(np.r_[3,4,5,6], (2,2)) array([[3., 4.], [5., 6.]])
Note
If
mis a 2D array its shape is compared toshape- a 2-tuple whereNonestands for unspecified, ie.(None, 2)will match any array where the second dimension is 2.If
mis a 1D array its shape is checked to see if it can be reshaped toshape. A n-array could be reshaped as (n,1) or (1,n) or any other shape with the correct number of elements. A value ofNonein the shape stands for unspecified, ie.(None, 2)will attempt to reshapemas an array with shape (k,2) where \(k \times 2 \eq n\).If
mis a scalar, return an array of shape (1,1)
- Seealso:
- SymPy:
supported
- getunit(v, unit='rad', dim=None, vector=True)[source]
Convert values according to angular units
- Parameters:
v (
Union[float,List[float],Tuple[float,...],ndarray[Any,dtype[floating]]]) – the value in radians or degreesunit (
str) – the angular unit, “rad” or “deg”dim (
Optional[int]) – expected dimension of input, defaults to don’t check (None)vector (
bool) – return a scalar as a 1d vector, defaults to True
- Returns:
the converted value in radians
- Return type:
Union[float,NDArray]- Raises:
ValueError – argument is not a valid angular unit
The input value is assumed to be in units of
unitand is converted to radians.>>> from spatialmath.base import getunit >>> import numpy as np >>> getunit(1.5, 'rad') array([1.5]) >>> getunit(90, 'deg') array([1.5708]) >>> getunit(90, 'deg', vector=False) # force a scalar output np.float64(1.5707963267948966) >>> getunit(1.5, 'rad', dim=0) # check argument is scalar array([1.5]) >>> getunit(1.5, 'rad', dim=3) # check argument is a 3-vector !! [RUNBLOCK-ERROR] base/argcheck.py:docstring of spatialmath.base.argcheck.getunit:130 ValueError: for dim==0 input must be a scalar >>> getunit([1.5], 'rad', dim=1) # check argument is a 1-vector array([1.5]) >>> getunit([1.5], 'rad', dim=3) # check argument is a 3-vector !! [RUNBLOCK-ERROR] base/argcheck.py:docstring of spatialmath.base.argcheck.getunit:130 ValueError: incorrect vector length: expected 3, got 1 >>> getunit([90, 180], 'deg') array([1.5708, 3.1416]) >>> getunit(np.r_[90, 180], 'deg') array([1.5708, 3.1416]) >>> getunit(np.r_[90, 180], 'deg', dim=2) # check argument is a 2-vector array([1.5708, 3.1416]) >>> getunit([90, 180], 'deg', dim=3) # check argument is a 3-vector !! [RUNBLOCK-ERROR] base/argcheck.py:docstring of spatialmath.base.argcheck.getunit:130 ValueError: incorrect vector length: expected 3, got 2
- Note:
the input value is processed by
getvector()and the argumentdimcan be used to check thatvis the desired length. Note that 0 means a scalar, whereas 1 means a 1-element array.the output is always an ndarray except if the input is a scalar and
vector=False.
- Seealso:
- getvector(v, dim=None, out='array', dtype=<class 'numpy.float64'>)[source]
Return a vector value
- Parameters:
- Return type:
Union[NDArray,List[float],Tuple[float,...]]- Returns:
vector value in specified format
- Raises:
TypeError – value is not a list or NumPy array
ValueError – incorrect number of elements
getvector(vec)isvecconverted to the output formatoutwherevecis any of:a Python native int or float, a 1-vector
Python native list or tuple
numPy real 1D array, ie. shape=(N,)
numPy real 2D array with a singleton dimension, ie. shape=(1,N) or (N,1)
getvector(vec, N)as above but must be anN-element vector.
The returned vector will be in the format specified by
out:format
return type
‘sequence’
Python list, or tuple if a tuple was passed in
‘list’
Python list
‘array’
1D numPy array, shape=(N,) [default]
‘row’
row vector, a 2D numPy array, shape=(1,N)
‘col’
column vector, 2D numPy array, shape=(N,1)
>>> from spatialmath.base import getvector >>> import numpy as np >>> getvector([1,2]) # list array([1., 2.]) >>> getvector([1,2], out='row') # list array([[1., 2.]]) >>> getvector([1,2], out='col') # list array([[1.], [2.]]) >>> getvector((1,2)) # tuple array([1., 2.]) >>> getvector(np.r_[1,2,3], out='sequence') # numpy array [np.int64(1), np.int64(2), np.int64(3)] >>> getvector(1) # scalar array([1.]) >>> getvector([1]) array([1.]) >>> getvector([[1]]) array([[1.]]) >>> getvector([1,2], 2) array([1., 2.]) >>> # getvector([1,2], 3) --> ValueError
Note
For ‘array’, ‘row’ or ‘col’ output the NumPy dtype defaults to the
dtypeofvif it is a NumPy array, otherwise it is set to the value specified by thedtypekeyword which defaults tonp.float64.If
vis symbolic thedtypeis retained as'O'
- Seealso:
- isinteger(x)[source]
Test if argument is a scalar integer
- Parameters:
x (
Any) – value to test- Returns:
whether value is a scalar
- Return type:
bool
isinteger(x)isTrueifxis a Python or numPy int or real float.
- islistof(value, what, n=None)[source]
Test if argument is a list of specified type
- Parameters:
value (
Any) – the value to testwhat (
Union[Type,Callable]) – type, tuple of types or functionn (
Optional[int]) – length of list, defaults to None
- Returns:
whether
valueis a specified list- Return type:
bool
Tests that every element of
valueis of the desired type. The type is specified bywhatand can be:a single type, eg.
inta tuple of types, eg.
(int, float)a reference to a function which is passed each elemnent of the list and returns True if it is a valid member of the list.
The length of the list can also be tested by specifying the argument
n.
- ismatrix(m, shape)[source]
Test if argument is a real 2D matrix
- Parameters:
m (
Any) – value to testshape (
Tuple[Optional[int],Optional[int]]) – required shape
- Return type:
bool- Returns:
True if value is of specified shape :rtype: bool
Tests if the argument is a real 2D matrix with a specified shape
shapebut the valueNoneindicate an unspecified (wildcard, don’t care) dimension, for example:Note
Unlike
verifymatrixthis function: - checks the argument is real valued - allows the shape to have an unspecified dimension- Seealso:
- isnumberlist(x)[source]
Test if argument is a list of scalars
- Parameters:
x (
Any) – the value to test- Returns:
True if the argument is a list of real scalars
- Return type:
bool
isscalarlist(x)isTrueifx``is a list of scalars.>>> from spatialmath.base import isnumberlist >>> import numpy as np >>> isnumberlist((1,2,3)) True >>> isnumberlist([1.1, 2.2, 3.3]) True >>> isnumberlist(1) False >>> isnumberlist(np.r_[1,2]) False
- isscalar(x)[source]
Test if argument is a real scalar
- Parameters:
x (
Any) – value to test- Returns:
whether value is a scalar
- Return type:
bool
isscalar(x)isTrueifxis a Python or numPy int or real float.
- isvector(v, dim=None)[source]
Test if argument is a real vector
- Parameters:
v (
Any) – value to testdim (
Optional[int]) – required dimension
- Returns:
whether value is a valid vector
- Return type:
bool
isvector(vec)isTrueifvecis a vector, ie. any of:a Python native int or float, a 1-vector
Python native list or tuple
numPy real 1D array, ie. shape=(N,)
numPy real 2D array with a singleton dimension, ie. shape=(1,N) or (N,1)
isvector(vec, N)as above but must also be anN-element vector.
- Seealso:
- isvectorlist(x, n)[source]
Test if argument is a list of vectors
- Parameters:
x (
Any) – the value to test- Returns:
True if the argument is a list of n-vectors
- Return type:
bool
isvectorlist(x, n)isTrueifxis a list or tuple of 1D numPy arrays of shape=(n,).>>> from spatialmath.base import isvectorlist >>> import numpy as np >>> isvectorlist([np.r_[1,2], np.r_[3,4], np.r_[5,6]], 2) True >>> isvectorlist([(1,2), (3,4), (5,6)], 2) False >>> isvectorlist([np.r_[1,2], np.r_[3,4], np.r_[5,6,7]], 2) False
- verifymatrix(m, shape)[source]
Assert that argument is array of specified size
- Parameters:
m (
ndarray) – value to be testedshape (
Tuple[Optional[int],Optional[int]]) – desired shape of value
- Raises:
TypeError – argument is not a NumPy array
ValueError – argument has incorrect shape
- Return type:
None
Raises an exception if the argument
mis not a NumPy array of the specified shape.Note
Unlike
assertmatrixthe specified shape cannot have wildcard dimensions.- Seealso:
assertmatrix(),:func:getmatrix,ismatrix()