# ----------------------------------------------------------------------------# SymForce - Copyright 2022, Skydio, Inc.# This source code is under the Apache 2.0 license found in the LICENSE file.# ----------------------------------------------------------------------------importsymforce.internal.symbolicassffromsymforceimporttypingasTfromsymforce.typing_utilimportget_typefromsymforce.typing_utilimportscalar_likefrom.opsimportOps
[docs]classStorageOps(Ops):""" API for symbolic data types that can be serialized to and from a vector of scalar quantities. """
[docs]@staticmethoddefstorage_dim(a:T.ElementOrType)->int:""" Size of the element's storage, aka the number of scalar values it contains. """returnStorageOps.implementation(get_type(a)).storage_dim(a)
[docs]@staticmethoddefto_storage(a:T.Element)->T.List:""" Serialization of the underlying storage into a list. This is NOT a tangent space. Args: a: Returns: list: Length equal to `storage_dim(a)` """returnStorageOps.implementation(get_type(a)).to_storage(a)
[docs]@staticmethoddeffrom_storage(a:T.ElementOrType,elements:T.Sequence[T.Scalar])->T.Element:""" Construct from a flat list representation. Opposite of :meth:`to_storage`. """returnStorageOps.implementation(get_type(a)).from_storage(a,elements)
[docs]@staticmethoddefsymbolic(a:T.ElementOrType,name:str,**kwargs:T.Dict)->T.Element:""" Construct a symbolic element with the given name prefix. Args: a: name: String prefix kwargs: Additional arguments to pass to :class:`sf.Symbol <symforce.symbolic.Symbol>` (like assumptions) Returns: Storage: """returnStorageOps.implementation(get_type(a)).symbolic(a,name,**kwargs)
[docs]@staticmethoddefevalf(a:T.Element)->T.Element:""" Evaluate to a numerical quantity (rationals, trig functions, etc). """defevalf_scalar(s:T.Scalar)->T.Scalar:ifhasattr(s,"evalf"):returns.evalf()ifscalar_like(s):returnsraiseTypeErrorreturnStorageOps.from_storage(a,[evalf_scalar(s)forsinStorageOps.to_storage(a)])
[docs]@staticmethoddefsubs(a:T.Element,*args:T.Any,**kwargs:T.Any)->T.Element:# We convert to a Matrix here so that we can call `.subs` once, which is fasterreturnStorageOps.from_storage(a,list(iter(sf.sympy.Matrix(StorageOps.to_storage(a)).subs(*args,**kwargs))))
[docs]@classmethoddefuse_latex_friendly_symbols(cls)->bool:""" Should `StorageOps.symbolic` produce symbols names that are LaTeX-friendly, as opposed to plaintext friendly? """returncls._use_latex_friendly_symbols
[docs]@classmethoddefset_use_latex_friendly_symbols(cls,value:bool)->None:""" Should `StorageOps.symbolic` produce symbols names that are LaTeX-friendly, as opposed to plaintext friendly? """cls._use_latex_friendly_symbols=value