def test_array_api(self): a = AD(1, [1., 0]) b = AD(2, [0, 1.]) x = np.array([a, b]) self.assertEqual(x.dtype, object) # Idempotent check. numpy_compare.assert_equal(x, x) # Conversion. with self.assertRaises(TypeError): # Avoid implicit coercion, as this will imply information loss. xf = np.zeros(2, dtype=np.float) xf[:] = x with self.assertRaises(TypeError): # We could define `__float__` to allow this, but then that will # enable implicit coercion, which we should avoid. xf = x.astype(dtype=np.float) # Presently, does not convert. x = np.zeros((3, 3), dtype=AD) self.assertFalse(isinstance(x[0, 0], AD)) x = np.eye(3).astype(AD) self.assertFalse(isinstance(x[0, 0], AD)) # Test implicit conversion. numpy_compare.assert_equal( autodiff_vector_pass_through([1, 2]), # int [AD(1., []), AD(2., [])]) numpy_compare.assert_equal( autodiff_vector_pass_through([1., 2.]), # float [AD(1., []), AD(2., [])])
def test_array_api(self): a = AD(1, [1., 0]) b = AD(2, [0, 1.]) x = np.array([a, b]) self.assertEqual(x.dtype, object) # Idempotent check. npc.assert_equal(x, x) # Conversion. with self.assertRaises(TypeError): # Avoid implicit coercion, as this will imply information loss. xf = np.zeros(2, dtype=np.float) xf[:] = x with self.assertRaises(TypeError): # We could define `__float__` to allow this, but then that will # enable implicit coercion, which we should avoid. xf = x.astype(dtype=np.float) # Presently, does not convert. x = np.zeros((3, 3), dtype=AD) self.assertFalse(isinstance(x[0, 0], AD)) x = np.eye(3).astype(AD) self.assertFalse(isinstance(x[0, 0], AD)) # Test implicit conversion. npc.assert_equal( autodiff_vector_pass_through([1, 2]), # int [AD(1., []), AD(2., [])]) npc.assert_equal( autodiff_vector_pass_through([1., 2.]), # float [AD(1., []), AD(2., [])])
def test_scalar_api(self): a = AD(1, [1., 0]) self.assertEqual(a.value(), 1.) npc.assert_equal(a.derivatives(), [1., 0]) self.assertEqual(str(a), "AD{1.0, nderiv=2}") self.assertEqual(repr(a), "<AutoDiffXd 1.0 nderiv=2>") npc.assert_equal(a, a) # Test construction from `float` and `int`. npc.assert_equal(AD(1), AD(1., [])) npc.assert_equal(AD(1.), AD(1., [])) # Test implicit conversion. npc.assert_equal( autodiff_scalar_pass_through(1), # int AD(1., [])) npc.assert_equal( autodiff_scalar_pass_through(1.), # float AD(1., [])) # Test multi-element pass-through. x = np.array([AD(1.), AD(2.), AD(3.)]) npc.assert_equal(autodiff_vector_pass_through(x), x) # Ensure fixed-size vectors are correctly converted (#9886). npc.assert_equal(autodiff_vector3_pass_through(x), x) # Ensure we can copy. npc.assert_equal(copy.copy(a), a) npc.assert_equal(copy.deepcopy(a), a)
def test_scalar_api(self): a = AD(1, [1., 0]) self.assertEqual(a.value(), 1.) numpy_compare.assert_equal(a.derivatives(), [1., 0]) self.assertEqual(str(a), "AD{1.0, nderiv=2}") self.assertEqual(repr(a), "<AutoDiffXd 1.0 nderiv=2>") numpy_compare.assert_equal(a, a) # Test construction from `float` and `int`. numpy_compare.assert_equal(AD(1), AD(1., [])) numpy_compare.assert_equal(AD(1.), AD(1., [])) # Test implicit conversion from a simple dtype to AutoDiff. numpy_compare.assert_equal( autodiff_scalar_pass_through(1), # int AD(1., [])) numpy_compare.assert_equal( autodiff_scalar_pass_through(1.), # float AD(1., [])) # Test explicit conversion to float. with self.assertRaises(TypeError) as cm: float(a) self.assertIn( "not 'pydrake.autodiffutils.AutoDiffXd'", str(cm.exception)) a_scalar = np.array(a) with self.assertRaises(TypeError) as cm: float(a_scalar) if np.lib.NumpyVersion(np.__version__) < "1.14.0": self.assertEqual( "don't know how to convert scalar number to float", str(cm.exception)) else: self.assertEqual( "float() argument must be a string or a number, not " "'pydrake.autodiffutils.AutoDiffXd'", str(cm.exception)) # Test multi-element pass-through. x = np.array([AD(1.), AD(2.), AD(3.)]) numpy_compare.assert_equal(autodiff_vector_pass_through(x), x) # Ensure fixed-size vectors are correctly converted (#9886). numpy_compare.assert_equal(autodiff_vector3_pass_through(x), x) # Ensure we can copy. numpy_compare.assert_equal(copy.copy(a), a) numpy_compare.assert_equal(copy.deepcopy(a), a) # Ensure that we can pickle. assert_pickle(self, a, lambda x: x)