[docs]defsympy_expr_to_float(expression:Sequence[Number]|Matrix|Number,tolerance:float=1e-9)->list[float]|float:""" Convert a sympy expression to (numerical) floats. If the given ``expression`` is a ``Sequence`` of ``Numbers`` or a ``Matrix``, then each individual element is evaluated and a list of ``float`` is returned. If the input is just a single sympy expression, it is evaluated and returned as a ``float``. Parameters ---------- expression: The sympy expression. tolerance: Intended level of numerical accuracy. Notes ----- The method :func:`.data_type.point_to_vector` is used to ensure that the input is consistent with the sympy format. """try:ifisinstance(expression,list|tuple|Matrix):return[float(sp.sympify(coord).evalf(int(round(2.5*log10(tolerance**(-1)+1)))))forcoordinpoint_to_vector(expression)]returnfloat(sp.sympify(expression).evalf(int(round(2.5*log10(tolerance**(-1)+1)))))exceptsp.SympifyError:raiseValueError(f"The expression `{expression}` could not be parsed by sympy.")
[docs]defpoint_to_vector(point:Point)->Matrix:""" Return point as single column sympy Matrix. """ifisinstance(point,MatrixBase)orisinstance(point,np.ndarray):if(len(point.shape)>1andpoint.shape[0]!=1andpoint.shape[1]!=1)orlen(point.shape)>2:raiseValueError("Point could not be interpreted as column vector.")ifisinstance(point,np.ndarray):point=np.array([point])iflen(point.shape)==1elsepointpoint=Matrix([[float(point[i,j])foriinrange(point.shape[0])]forjinrange(point.shape[1])])returnpointif(point.shape[1]==1)elsepoint.transpose()ifnotisinstance(point,Sequence)orisinstance(point,str):raiseTypeError("The point must be a Sequence of Numbers.")try:res=Matrix(point)exceptExceptionase:raiseValueError("A coordinate could not be interpreted by sympify:\n"+str(e))ifres.shape[0]!=1andres.shape[1]!=1:raiseValueError("Point could not be interpreted as column vector.")returnresif(res.shape[1]==1)elseres.transpose()
def_rgb_to_hex_array(color_array):""" Map an array of colors to their hexadecimal representation or string representation so that `svg`-based methods can read the colors. """rgb_factor=1forcolincolor_array:if(not(isinstance(col,list)orisinstance(col,tuple)orisinstance(col,str))or(isinstance(col,list)orisinstance(col,tuple))andnotlen(col)==3):raiseValueError("The `color_array` does not only consist of "+f"`RGB` and `str` values. One entry is {col}.")if(isinstance(col,list)orisinstance(col,tuple))andany(isinstance(c,float)andc<1forcincol):# Distinguish between rgb and RGBrgb_factor=255return[("#"+"".join(f"{int(round(c*rgb_factor)):02x}"forcincol)ifisinstance(col,list)orisinstance(col,tuple)elsecol)forcolincolor_array]