Example #1
0
 def testUnitMathModule(self):
     """ Tests the unit_math functions on Quantity objects """
     self.assertEqual(u.sqrt(1.0 * u.kilogram * u.joule),
                      1.0 * u.kilogram * u.meter / u.second)
     self.assertEqual(u.sqrt(1.0 * u.kilogram * u.calorie),
                      math.sqrt(4.184) * u.kilogram * u.meter / u.second)
     self.assertEqual(u.sqrt(9), 3)  # Test on a scalar
     self.assertEqual(u.sin(90 * u.degrees), 1)
     self.assertEqual(u.sin(math.pi / 2 * u.radians), 1)
     self.assertEqual(u.sin(math.pi / 2), 1)
     self.assertEqual(u.cos(180 * u.degrees), -1)
     self.assertEqual(u.cos(math.pi * u.radians), -1)
     self.assertEqual(u.cos(math.pi), -1)
     self.assertAlmostEqual(u.tan(45 * u.degrees), 1)
     self.assertAlmostEqual(u.tan(math.pi / 4 * u.radians), 1)
     self.assertAlmostEqual(u.tan(math.pi / 4), 1)
     acos = u.acos(1.0)
     asin = u.asin(1.0)
     atan = u.atan(1.0)
     self.assertTrue(u.is_quantity(acos))
     self.assertTrue(u.is_quantity(asin))
     self.assertTrue(u.is_quantity(atan))
     self.assertEqual(acos.unit, u.radians)
     self.assertEqual(asin.unit, u.radians)
     self.assertEqual(atan.unit, u.radians)
     self.assertEqual(acos.value_in_unit(u.degrees), 0)
     self.assertEqual(acos / u.radians, 0)
     self.assertEqual(asin.value_in_unit(u.degrees), 90)
     self.assertEqual(asin / u.radians, math.pi / 2)
     self.assertAlmostEqual(atan.value_in_unit(u.degrees), 45)
     self.assertAlmostEqual(atan / u.radians, math.pi / 4)
     # Check some sequence maths
     seq = [1, 2, 3, 4] * u.meters
     self.assertEqual(u.sum(seq), 10 * u.meters)
     self.assertEqual(u.dot(seq, seq), (1 + 4 + 9 + 16) * u.meters**2)
     self.assertEqual(u.norm(seq), math.sqrt(30) * u.meters)
Example #2
0
 def testUnitMathModule(self):
     """ Tests the unit_math functions on Quantity objects """
     self.assertEqual(u.sqrt(1.0*u.kilogram*u.joule),
                      1.0*u.kilogram*u.meter/u.second)
     self.assertEqual(u.sqrt(1.0*u.kilogram*u.calorie),
                      math.sqrt(4.184)*u.kilogram*u.meter/u.second)
     self.assertEqual(u.sqrt(9), 3) # Test on a scalar
     self.assertEqual(u.sin(90*u.degrees), 1)
     self.assertEqual(u.sin(math.pi/2*u.radians), 1)
     self.assertEqual(u.sin(math.pi/2), 1)
     self.assertEqual(u.cos(180*u.degrees), -1)
     self.assertEqual(u.cos(math.pi*u.radians), -1)
     self.assertEqual(u.cos(math.pi), -1)
     self.assertAlmostEqual(u.tan(45*u.degrees), 1)
     self.assertAlmostEqual(u.tan(math.pi/4*u.radians), 1)
     self.assertAlmostEqual(u.tan(math.pi/4), 1)
     acos = u.acos(1.0)
     asin = u.asin(1.0)
     atan = u.atan(1.0)
     self.assertTrue(u.is_quantity(acos))
     self.assertTrue(u.is_quantity(asin))
     self.assertTrue(u.is_quantity(atan))
     self.assertEqual(acos.unit, u.radians)
     self.assertEqual(asin.unit, u.radians)
     self.assertEqual(atan.unit, u.radians)
     self.assertEqual(acos.value_in_unit(u.degrees), 0)
     self.assertEqual(acos / u.radians, 0)
     self.assertEqual(asin.value_in_unit(u.degrees), 90)
     self.assertEqual(asin / u.radians, math.pi/2)
     self.assertAlmostEqual(atan.value_in_unit(u.degrees), 45)
     self.assertAlmostEqual(atan / u.radians, math.pi/4)
     # Check some sequence maths
     seq = [1, 2, 3, 4] * u.meters
     self.assertEqual(u.sum(seq), 10*u.meters)
     self.assertEqual(u.dot(seq, seq), (1+4+9+16)*u.meters**2)
     self.assertEqual(u.norm(seq), math.sqrt(30)*u.meters)
Example #3
0
 def testUnitMathModuleBadInput(self):
     """ Tests that bad units to unit_math fails appropriately """
     self.assertRaises(ArithmeticError, lambda: u.sqrt(9 * u.meters))
     self.assertRaises(TypeError, lambda: u.sin(1 * u.meters))
Example #4
0
 def testUnitMathModuleBadInput(self):
     """ Tests that bad units to unit_math fails appropriately """
     self.assertRaises(ArithmeticError, lambda: u.sqrt(9*u.meters))
     self.assertRaises(TypeError, lambda: u.sin(1*u.meters))