Exemplo n.º 1
0
 def test_scalar_type_conversion(self):
     float_system = Adder(1, 1)
     float_context = float_system.CreateDefaultContext()
     float_system.get_input_port(0).FixValue(float_context, 1.)
     for T in [float, AutoDiffXd, Expression]:
         system = Adder_[T](1, 1)
         # N.B. Current scalar conversion does not permit conversion to and
         # from the same type.
         if T != AutoDiffXd:
             methods = [Adder_[T].ToAutoDiffXd, Adder_[T].ToAutoDiffXdMaybe]
             for method in methods:
                 system_ad = method(system)
                 self.assertIsInstance(system_ad, System_[AutoDiffXd])
                 self._compare_system_instances(system, system_ad)
         if T != Expression:
             methods = [Adder_[T].ToSymbolic, Adder_[T].ToSymbolicMaybe]
             for method in methods:
                 system_sym = method(system)
                 self.assertIsInstance(system_sym, System_[Expression])
                 self._compare_system_instances(system, system_sym)
         context = system.CreateDefaultContext()
         system.FixInputPortsFrom(other_system=float_system,
                                  other_context=float_context,
                                  target_context=context)
         u = system.get_input_port(0).Eval(context)
         self.assertEqual(len(u), 1)
         if T == float:
             self.assertEqual(u[0], 1.)
         elif T == AutoDiffXd:
             self.assertEqual(u[0].value(), 1.)
         else:
             self.assertEqual(u[0].Evaluate(), 1.)
Exemplo n.º 2
0
 def test_ownership_vector(self):
     system = Adder(1, 1)
     context = system.CreateDefaultContext()
     info = Info()
     vector = DeleteListenerVector(info.record_deletion)
     system.get_input_port(0).FixValue(context, vector)
     del context
     # Same as above applications, using `py::keep_alive`.
     self.assertFalse(info.deleted)
     self.assertTrue(vector is not None)
     # Ensure that we do not get segfault behavior when accessing / mutating
     # the values.
     self.assertTrue(np.allclose(vector.get_value(), [0.]))
     vector.get_mutable_value()[:] = [10.]
     self.assertTrue(np.allclose(vector.get_value(), [10.]))
Exemplo n.º 3
0
 def __init__(self, num_inputs, size):
     Diagram.__init__(self)
     builder = DiagramBuilder()
     adder = Adder(num_inputs, size)
     builder.AddSystem(adder)
     builder.ExportOutput(adder.get_output_port(0))
     for i in range(num_inputs):
         builder.ExportInput(adder.get_input_port(i))
     builder.BuildInto(self)
Exemplo n.º 4
0
 def __init__(self, num_inputs, size):
     Diagram.__init__(self)
     builder = DiagramBuilder()
     adder = Adder(num_inputs, size)
     builder.AddSystem(adder)
     builder.ExportOutput(adder.get_output_port(0))
     for i in range(num_inputs):
         builder.ExportInput(adder.get_input_port(i))
     builder.BuildInto(self)