def mformat(self, X): # X is an ndarray value to be display # self provides set type for formatting out = '' n = self.N # dimension of rotation submatrix for rownum, row in enumerate(X): rowstr = ' ' # format the columns for colnum, element in enumerate(row): if sym.issymbol(element): s = '{:<12s}'.format(str(element)) else: if tol > 0 and abs(element) < tol * _eps: element = 0 s = '{:< 12g}'.format(element) if rownum < n: if colnum < n: # rotation part s = rotcol + bgcol + s + reset else: # translation part s = trcol + bgcol + s + reset else: # bottom row s = constcol + bgcol + s + reset rowstr += s out += rowstr + bgcol + ' ' + reset + '\n' return out
def getvector(v, dim=None, out="array", dtype=np.float64): """ Return a vector value :param v: passed vector :param dim: required dimension, or None if any length is ok :type dim: int or None :param out: output format, default is 'array' :type out: str :param dtype: datatype for numPy array return (default np.float64) :type dtype: numPy type :return: vector value in specified format :raises TypeError: value is not a list or NumPy array :raises ValueError: incorrect number of elements - ``getvector(vec)`` is ``vec`` converted to the output format ``out`` where ``vec`` is 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 an ``N``-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) ========== =============================================== .. runblock:: pycon >>> from spatialmath.base import getvector >>> import numpy as np >>> getvector([1,2]) # list >>> getvector([1,2], out='row') # list >>> getvector([1,2], out='col') # list >>> getvector((1,2)) # tuple >>> getvector(np.r_[1,2,3], out='sequence') # numpy array >>> getvector(1) # scalar >>> getvector([1]) >>> getvector([[1]]) .. note:: - For 'array', 'row' or 'col' output the NumPy dtype defaults to the ``dtype`` of ``v`` if it is a NumPy array, otherwise it is set to the value specified by the ``dtype`` keyword which defaults to ``np.float64``. - If ``v`` is symbolic the ``dtype`` is retained as ``'O'`` :seealso: :func:`isvector` """ dt = dtype if isinstance(v, _scalartypes): # handle scalar case v = [v] if isinstance(v, (list, tuple)): # list or tuple was passed in if sym.issymbol(v): dt = None if dim is not None and v and len(v) != dim: raise ValueError("incorrect vector length") if out == "sequence": return v elif out == "list": return list(v) elif out == "array": return np.array(v, dtype=dt) elif out == "row": return np.array(v, dtype=dt).reshape(1, -1) elif out == "col": return np.array(v, dtype=dt).reshape(-1, 1) else: raise ValueError("invalid output specifier") elif isinstance(v, np.ndarray): s = v.shape if dim is not None: if not (s == (dim, ) or s == (1, dim) or s == (dim, 1)): raise ValueError( "incorrect vector length: expected {}, got {}".format( dim, s)) v = v.flatten() if v.dtype.kind == "O": dt = "O" if out in ("sequence", "list"): return list(v.flatten()) elif out == "array": return v.astype(dt) elif out == "row": return v.astype(dt).reshape(1, -1) elif out == "col": return v.astype(dt).reshape(-1, 1) else: raise ValueError("invalid output specifier") else: raise TypeError("invalid input type")
def getvector(v, dim=None, out='array', dtype=np.float64): """ Return a vector value :param v: passed vector :param dim: required dimension, or None if any length is ok :type dim: int or None :param out: output format, default is 'array' :type out: str :param dtype: datatype for numPy array return (default np.float64) :type dtype: numPy type :return: vector value in specified format The passed vector can be any of: - Python native list or tuple - numPy 1D array, ie. shape=(N,) - numPy 2D array with a singleton dimension, ie. shape=(1,N) or (N,1) 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 'array' 1D numPy array, shape=(N,) 'row' row vector, a 2D numPy array, shape=(1,N) 'col' column vector, 2D numPy array, shape=(N,1) ========== =============================================== For 'array', 'row' or 'col' the numPy dtype defaults to ``np.float64`` but can be overriden using the ``dtype`` argument. """ dt = dtype if isinstance(v, _scalartypes): # handle scalar case v = [v] if isinstance(v, (list, tuple)): # list or tuple was passed in if sym.issymbol(v): dt = None if dim is not None and v and len(v) != dim: raise ValueError("incorrect vector length") if out == 'sequence': return v elif out == 'array': return np.array(v, dtype=dt) elif out == 'row': return np.array(v, dtype=dt).reshape(1, -1) elif out == 'col': return np.array(v, dtype=dt).reshape(-1, 1) else: raise ValueError("invalid output specifier") elif isinstance(v, np.ndarray): s = v.shape if dim is not None: if not (s == (dim, ) or s == (1, dim) or s == (dim, 1)): raise ValueError( "incorrect vector length: expected {}, got {}".format( dim, s)) v = v.flatten() if v.dtype.kind != 'O': dt = dtype if out == 'sequence': return list(v.flatten()) elif out == 'array': return v.astype(dt) elif out == 'row': return v.astype(dt).reshape(1, -1) elif out == 'col': return v.astype(dt).reshape(-1, 1) else: raise ValueError("invalid output specifier") else: raise TypeError("invalid input type")