def test_vector_dist(exponent): rn = odl.CudaRn(5) xarr, yarr, x, y = _vectors(rn, n=2) weight = _pos_vector(odl.CudaRn(5)) weighting = CudaFnVectorWeighting(weight, exponent=exponent) if exponent in (1.0, float('inf')): true_dist = np.linalg.norm(weight.asarray() * (xarr - yarr), ord=exponent) else: true_dist = np.linalg.norm( weight.asarray() ** (1 / exponent) * (xarr - yarr), ord=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): weighting.dist(x, y) else: assert almost_equal(weighting.dist(x, y), true_dist) # Same with free function pdist = odl.cu_weighted_dist(weight, exponent=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): pdist(x, y) else: assert almost_equal(pdist(x, y), true_dist)
def test_vector_norm(exponent): rn = CudaRn(5) xarr, x = example_vectors(rn) weight = _pos_vector(CudaRn(5)) weighting = CudaFnVectorWeighting(weight, exponent=exponent) if exponent in (1.0, float('inf')): true_norm = np.linalg.norm(weight.asarray() * xarr, ord=exponent) else: true_norm = np.linalg.norm(weight.asarray() ** (1 / exponent) * xarr, ord=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): weighting.norm(x) else: assert almost_equal(weighting.norm(x), true_norm) # Same with free function pnorm = odl.cu_weighted_norm(weight, exponent=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): pnorm(x) else: assert almost_equal(pnorm(x), true_norm)
def test_vector_norm(exponent): rn = odl.CudaRn(5) xarr, x = _vectors(rn) weight = _pos_vector(odl.CudaRn(5)) weighting = CudaFnVectorWeighting(weight, exponent=exponent) if exponent in (1.0, float('inf')): true_norm = np.linalg.norm(weight.asarray() * xarr, ord=exponent) else: true_norm = np.linalg.norm(weight.asarray() ** (1 / exponent) * xarr, ord=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): weighting.norm(x) else: assert almost_equal(weighting.norm(x), true_norm) # Same with free function pnorm = odl.cu_weighted_norm(weight, exponent=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): pnorm(x) else: assert almost_equal(pnorm(x), true_norm)
def test_vector_dist(exponent): rn = CudaRn(5) [xarr, yarr], [x, y] = example_vectors(rn, n=2) weight = _pos_vector(CudaRn(5)) weighting = CudaFnVectorWeighting(weight, exponent=exponent) if exponent in (1.0, float('inf')): true_dist = np.linalg.norm(weight.asarray() * (xarr - yarr), ord=exponent) else: true_dist = np.linalg.norm( weight.asarray() ** (1 / exponent) * (xarr - yarr), ord=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): weighting.dist(x, y) else: assert almost_equal(weighting.dist(x, y), true_dist) # Same with free function pdist = odl.cu_weighted_dist(weight, exponent=exponent) if exponent == float('inf'): # Not yet implemented, should raise with pytest.raises(NotImplementedError): pdist(x, y) else: assert almost_equal(pdist(x, y), true_dist)
def test_vector_inner(): rn = odl.CudaRn(5) xarr, yarr, x, y = _vectors(rn, 2) weight_vec = _pos_array(odl.Rn(5)) weight_elem = rn.element(weight_vec) weighting_vec = CudaFnVectorWeighting(weight_vec) weighting_elem = CudaFnVectorWeighting(weight_elem) true_inner = np.vdot(yarr, xarr * weight_vec) assert almost_equal(weighting_vec.inner(x, y), true_inner) assert almost_equal(weighting_elem.inner(x, y), true_inner) # Same with free function inner_vec = odl.cu_weighted_inner(weight_vec) inner_elem = odl.cu_weighted_inner(weight_elem) assert almost_equal(inner_vec(x, y), true_inner) assert almost_equal(inner_elem(x, y), true_inner) # Exponent != 2 -> no inner product, should raise with pytest.raises(NotImplementedError): CudaFnVectorWeighting(weight_vec, exponent=1.0).inner(x, y)
def test_vector_equals(): rn = CudaRn(5) weight = _pos_vector(rn) weighting = CudaFnVectorWeighting(weight) weighting2 = CudaFnVectorWeighting(weight) assert weighting == weighting2
def test_vector_isvalid(): rn = odl.CudaRn(5) weight = _pos_vector(rn) weighting = CudaFnVectorWeighting(weight) assert weighting.vector_is_valid() # Invalid weight[0] = 0 weighting = CudaFnVectorWeighting(weight) assert not weighting.vector_is_valid()
def test_init_weighting(exponent): const = 1.5 weight_vec = _pos_vector(CudaRn(3)) weight_elem = CudaFn(3, dtype='float32').element(weight_vec) f3_none = CudaFn(3, dtype='float32', exponent=exponent) f3_const = CudaFn(3, dtype='float32', weight=const, exponent=exponent) f3_vec = CudaFn(3, dtype='float32', weight=weight_vec, exponent=exponent) f3_elem = CudaFn(3, dtype='float32', weight=weight_elem, exponent=exponent) weighting_none = CudaFnNoWeighting(exponent=exponent) weighting_const = CudaFnConstWeighting(const, exponent=exponent) weighting_vec = CudaFnVectorWeighting(weight_vec, exponent=exponent) weighting_elem = CudaFnVectorWeighting(weight_elem, exponent=exponent) assert f3_none.weighting == weighting_none assert f3_const.weighting == weighting_const assert f3_vec.weighting == weighting_vec assert f3_elem.weighting == weighting_elem
def test_vector_inner(): rn = CudaRn(5) [xarr, yarr], [x, y] = example_vectors(rn, 2) weight = _pos_vector(CudaRn(5)) weighting = CudaFnVectorWeighting(weight) true_inner = np.vdot(yarr, xarr * weight.asarray()) assert almost_equal(weighting.inner(x, y), true_inner) # Same with free function inner_vec = odl.cu_weighted_inner(weight) assert almost_equal(inner_vec(x, y), true_inner) # Exponent != 2 -> no inner product, should raise with pytest.raises(NotImplementedError): CudaFnVectorWeighting(weight, exponent=1.0).inner(x, y)
def test_vector_isvalid(): rn = odl.CudaRn(5) weight_vec = _pos_array(rn) weight_elem = rn.element(weight_vec) weighting_vec = CudaFnVectorWeighting(weight_vec) weighting_elem = CudaFnVectorWeighting(weight_elem) assert weighting_vec.vector_is_valid() assert weighting_elem.vector_is_valid() # Invalid weight_vec[0] = 0 weight_elem[0] = 0 weighting_vec = CudaFnVectorWeighting(weight_vec) weighting_elem = CudaFnVectorWeighting(weight_elem) assert not weighting_vec.vector_is_valid() assert not weighting_elem.vector_is_valid()
def test_vector_is_valid(): rn = CudaRn(5) weight = _pos_vector(rn) weighting = CudaFnVectorWeighting(weight) assert weighting.is_valid() # Invalid weight[0] = 0 weighting = CudaFnVectorWeighting(weight) assert not weighting.is_valid()
def test_vector_norm(exponent): rn = odl.CudaRn(5) xarr, x = _vectors(rn) weight_vec = _pos_array(odl.Rn(5)) weight_elem = rn.element(weight_vec) weighting_vec = CudaFnVectorWeighting(weight_vec, exponent=exponent) weighting_elem = CudaFnVectorWeighting(weight_elem, exponent=exponent) if exponent in (1.0, float('inf')): true_norm = np.linalg.norm(weight_vec * xarr, ord=exponent) else: true_norm = np.linalg.norm(weight_vec ** (1 / exponent) * xarr, ord=exponent) if exponent == float('inf') or int(exponent) != exponent: # Not yet implemented, should raise with pytest.raises(NotImplementedError): weighting_vec.norm(x) with pytest.raises(NotImplementedError): weighting_elem.norm(x) else: assert almost_equal(weighting_vec.norm(x), true_norm) assert almost_equal(weighting_elem.norm(x), true_norm) # Same with free function pnorm_vec = odl.cu_weighted_norm(weight_vec, exponent=exponent) pnorm_elem = odl.cu_weighted_norm(weight_elem, exponent=exponent) if exponent == float('inf') or int(exponent) != exponent: # Not yet implemented, should raise with pytest.raises(NotImplementedError): pnorm_vec(x) with pytest.raises(NotImplementedError): pnorm_elem(x) else: assert almost_equal(pnorm_vec(x), true_norm) assert almost_equal(pnorm_elem(x), true_norm)
def test_vector_dist(exponent): rn = odl.CudaRn(5) xarr, yarr, x, y = _vectors(rn, n=2) weight_vec = _pos_array(odl.Rn(5)) weight_elem = rn.element(weight_vec) weighting_vec = CudaFnVectorWeighting(weight_vec, exponent=exponent) weighting_elem = CudaFnVectorWeighting(weight_elem, exponent=exponent) if exponent in (1.0, float('inf')): true_dist = np.linalg.norm(weight_vec * (xarr - yarr), ord=exponent) else: true_dist = np.linalg.norm( weight_vec ** (1 / exponent) * (xarr - yarr), ord=exponent) if exponent == float('inf') or int(exponent) != exponent: # Not yet implemented, should raise with pytest.raises(NotImplementedError): weighting_vec.dist(x, y) with pytest.raises(NotImplementedError): weighting_elem.dist(x, y) else: assert almost_equal(weighting_vec.dist(x, y), true_dist) assert almost_equal(weighting_elem.dist(x, y), true_dist) # Same with free function pdist_vec = odl.cu_weighted_dist(weight_vec, exponent=exponent) pdist_elem = odl.cu_weighted_dist(weight_elem, exponent=exponent) if exponent == float('inf') or int(exponent) != exponent: # Not yet implemented, should raise with pytest.raises(NotImplementedError): pdist_vec(x, y) with pytest.raises(NotImplementedError): pdist_elem(x, y) else: assert almost_equal(pdist_vec(x, y), true_dist) assert almost_equal(pdist_elem(x, y), true_dist)
def test_vector_init(): rn = CudaRn(5) weight_vec = _pos_vector(rn) CudaFnVectorWeighting(weight_vec) CudaFnVectorWeighting(rn.element(weight_vec))