Example #1
0
 def test_alpha_norm_of_fvec(self):
     a = fvec(2)
     self.assertEquals (alpha_norm(a, 1), 0)
     a[0] = 1
     self.assertEquals (alpha_norm(a, 1), 0.5)
     a[1] = 1
     self.assertEquals (alpha_norm(a, 1), 1)
     a = array([0, 1], dtype='float32')
     from math import sqrt
     assert_almost_equal (alpha_norm(a, 2), sqrt(2)/2.)
Example #2
0
 def test_alpha_norm_of_fvec(self):
     a = fvec(2)
     self.assertEquals (alpha_norm(a, 1), 0)
     a[0] = 1
     self.assertEquals (alpha_norm(a, 1), 0.5)
     a[1] = 1
     self.assertEquals (alpha_norm(a, 1), 1)
     a = array([0, 1], dtype='float32')
     from math import sqrt
     assert_almost_equal (alpha_norm(a, 2), sqrt(2)/2.)
Example #3
0
 def test_alpha_norm_of_array_of_float32(self):
     # check scalar fails
     a = array(1, dtype="float32")
     self.assertRaises(ValueError, alpha_norm, a, 1)
     # check 2d array fails
     a = array([[2], [4]], dtype="float32")
     self.assertRaises(ValueError, alpha_norm, a, 1)
     # check 1d array
     a = array(range(10), dtype="float32")
     self.assertEquals(alpha_norm(a, 1), 4.5)
Example #4
0
 def test_alpha_norm_of_array_of_float32(self):
     # check scalar fails
     a = array(1, dtype = 'float32')
     self.assertRaises (ValueError, alpha_norm, a, 1)
     # check 2d array fails
     a = array([[2],[4]], dtype = 'float32')
     self.assertRaises (ValueError, alpha_norm, a, 1)
     # check 1d array
     a = array(range(10), dtype = 'float32')
     self.assertEquals (alpha_norm(a, 1), 4.5)
Example #5
0
 def test_alpha_norm_of_random(self):
     x = np.random.rand(1024).astype(float_type)
     alpha = np.random.rand() * 5.
     x_alpha_norm = (np.sum(np.abs(x)**alpha) / len(x))**(1 / alpha)
     assert_almost_equal(alpha_norm(x, alpha), x_alpha_norm, decimal=5)
Example #6
0
 def test_alpha_norm_of_random(self):
     x = np.random.rand(1024).astype(float_type)
     alpha = np.random.rand() * 5.
     x_alpha_norm = (np.sum(np.abs(x)**alpha)/len(x))**(1/alpha)
     assert_almost_equal(alpha_norm(x, alpha), x_alpha_norm, decimal = 4)