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.TemplateClass

Defines templated systems enabling scalar type conversion in Python.

This differs from TemplateClass in that (a) the template must specify its parameters at construction time and (b) .define is overridden to allow defining f(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. converter must 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 _construct within the same class, you should use Impl._construct(self, ...); if you use self._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 _construct or _construct_copy methods.
  • converter should always be non-None, as guaranteed by the overriding __init__. We use converter=None to 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 SystemScalarConverter support.
  • scope – Defining scope, per TemplateClass’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 type T of 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.
  • kwargs (args,) – These are passed to the constructor of TemplateSystem.