pydrake.systems.scalar_conversion
Provides utilities to aid in scalar type conversion.
- class pydrake.systems.scalar_conversion.TemplateSystem(name, T_list=None, T_pairs=None, scope=None)
Bases:
pydrake.common.cpp_template.TemplateClassDefines templated systems enabling scalar type conversion in Python.
This differs from
TemplateClassin that (a) the template must specify its parameters at construction time and (b) .define is overridden to allow definingf(T)for convenience.Any class that is added must:
Not define
__init__, as this will be overridden.Define
_construct(self, <args>, converter=None, <kwargs>)and_construct_copy(self, other, converter=None)instead.convertermust be present as an argument to ensure that it propagates properly.
If any of these constraints are violated, then an error will be thrown at the time of the first class instantiation.
Example
@TemplateSystem.define("MySystem_") def MySystem_(T): class Impl(LeafSystem_[T]): def _construct(self, value, converter=None): LeafSystem_[T].__init__(self, converter=converter) self.value = value def _construct_copy(self, other, converter=None): Impl._construct(self, other.value, converter=converter) return Impl MySystem = MySystem_[None] # Default instantiation.
Things to note:
When defining
_construct_copy, if you are delegating to_constructwithin the same class, you should useImpl._construct(self, ...); if you useself._construct, then you may get a child class’s constructor if you are inheriting.If you are delegating construction to a parent Python class for both constructors, use the parent class’s
__init__method, not its_constructor_construct_copymethods.convertershould always be non-None, as guaranteed by the overriding__init__. We useconverter=Noneto imply it should be positional, since Python2 does not have keyword-only arguments.
- __init__(name, T_list=None, T_pairs=None, scope=None)
Constructs
TemplateSystem.- Parameters:
T_list – List of T’s that the given system supports. By default, it is all types supported by LeafSystem.
T_pairs – List of pairs, (T, U), defining a conversion from a scalar type of U to T. If None, this will use all possible pairs that the Python bindings of
SystemScalarConvertersupport.scope – Defining
scope, perTemplateClass’s constructor.
- classmethod define(name, T_list=None, T_pairs=None, *args, scope=None, **kwargs)
Provides a decorator which can be used define a scalar-type convertible System as a template.
The decorated function must be of the form
f(T), which returns a class which will be the instantiation for typeTof the given template.- Parameters:
name – Name of the system template. This should match the name of the object being decorated.
T_list – See
__init__for more information.T_pairs – See
__init__for more information.args – These are passed to the constructor of
TemplateSystem.kwargs – These are passed to the constructor of
TemplateSystem.