def test_matrix_norm(fn, exponent): xarr, x = _vectors(fn) sparse_mat = _sparse_matrix(fn) sparse_mat_as_dense = np.asarray(sparse_mat.todense()) dense_mat = _dense_matrix(fn) # Compute true matrix-weighted norm if exponent == 1.0: # ||x||_{A,1} = ||Ax||_1 true_norm_sparse = np.linalg.norm(np.dot(sparse_mat_as_dense, xarr), ord=exponent) true_norm_dense = np.linalg.norm(np.dot(dense_mat, xarr), ord=exponent) elif exponent == 2.0: # ||x||_{A,2} = sqrt(<x, Ax>) true_norm_sparse = np.sqrt( np.vdot(xarr, np.dot(sparse_mat_as_dense, xarr))) true_norm_dense = np.sqrt(np.vdot(xarr, np.dot(dense_mat, xarr))) elif exponent == float('inf'): # ||x||_{A,inf} = ||Ax||_inf true_norm_sparse = np.linalg.norm(sparse_mat_as_dense.dot(xarr), ord=exponent) true_norm_dense = np.linalg.norm(dense_mat.dot(xarr), ord=exponent) else: # ||x||_{A,p} = ||A^{1/p} x||_p # Calculate matrix power eigval, eigvec = sp.linalg.eigh(dense_mat) eigval **= 1.0 / exponent mat_pow = (eigval * eigvec).dot(eigvec.conj().T) true_norm_dense = np.linalg.norm(np.dot(mat_pow, xarr), ord=exponent) # Test weighting if exponent in (1.0, 2.0, float('inf')): w_sparse = FnMatrixWeighting(sparse_mat, exponent=exponent) assert almost_equal(w_sparse.norm(x), true_norm_sparse) w_dense = FnMatrixWeighting(dense_mat, exponent=exponent) assert almost_equal(w_dense.norm(x), true_norm_dense) # With free functions if exponent not in (1.0, 2.0, float('inf')): with pytest.raises(NotImplementedError): weighted_norm(sparse_mat, exponent=exponent) else: w_sparse_norm = weighted_norm(sparse_mat, exponent=exponent) assert almost_equal(w_sparse_norm(x), true_norm_sparse) w_dense_norm = weighted_norm(dense_mat, exponent=exponent) assert almost_equal(w_dense_norm(x), true_norm_dense)
def test_matrix_norm(fn, exponent): xarr, x = example_vectors(fn) sparse_mat = _sparse_matrix(fn) sparse_mat_as_dense = np.asarray(sparse_mat.todense()) dense_mat = _dense_matrix(fn) # Compute true matrix-weighted norm if exponent == 1.0: # ||x||_{A,1} = ||Ax||_1 true_norm_sparse = np.linalg.norm(np.dot(sparse_mat_as_dense, xarr), ord=exponent) true_norm_dense = np.linalg.norm(np.dot(dense_mat, xarr), ord=exponent) elif exponent == 2.0: # ||x||_{A,2} = sqrt(<x, Ax>) true_norm_sparse = np.sqrt( np.vdot(xarr, np.dot(sparse_mat_as_dense, xarr))) true_norm_dense = np.sqrt(np.vdot(xarr, np.dot(dense_mat, xarr))) elif exponent == float('inf'): # ||x||_{A,inf} = ||Ax||_inf true_norm_sparse = np.linalg.norm(sparse_mat_as_dense.dot(xarr), ord=exponent) true_norm_dense = np.linalg.norm(dense_mat.dot(xarr), ord=exponent) else: # ||x||_{A,p} = ||A^{1/p} x||_p # Calculate matrix power eigval, eigvec = sp.linalg.eigh(dense_mat) eigval **= 1.0 / exponent mat_pow = (eigval * eigvec).dot(eigvec.conj().T) true_norm_dense = np.linalg.norm(np.dot(mat_pow, xarr), ord=exponent) # Test weighting if exponent in (1.0, 2.0, float('inf')): w_sparse = FnMatrixWeighting(sparse_mat, exponent=exponent) assert almost_equal(w_sparse.norm(x), true_norm_sparse) w_dense = FnMatrixWeighting(dense_mat, exponent=exponent) assert almost_equal(w_dense.norm(x), true_norm_dense) # With free functions if exponent not in (1.0, 2.0, float('inf')): with pytest.raises(NotImplementedError): weighted_norm(sparse_mat, exponent=exponent) else: w_sparse_norm = weighted_norm(sparse_mat, exponent=exponent) assert almost_equal(w_sparse_norm(x), true_norm_sparse) w_dense_norm = weighted_norm(dense_mat, exponent=exponent) assert almost_equal(w_dense_norm(x), true_norm_dense)
def test_constant_norm(fn, exponent): xarr, x = _vectors(fn) constant = 1.5 if exponent == float('inf'): factor = constant else: factor = constant ** (1 / exponent) true_norm = factor * np.linalg.norm(xarr, ord=exponent) w_const = FnConstWeighting(constant, exponent=exponent) assert almost_equal(w_const.norm(x), true_norm) # With free function w_const_norm = weighted_norm(constant, exponent=exponent) assert almost_equal(w_const_norm(x), true_norm)
def test_constant_norm(fn, exponent): xarr, x = example_vectors(fn) constant = 1.5 if exponent == float('inf'): factor = constant else: factor = constant**(1 / exponent) true_norm = factor * np.linalg.norm(xarr, ord=exponent) w_const = FnConstWeighting(constant, exponent=exponent) assert almost_equal(w_const.norm(x), true_norm) # With free function w_const_norm = weighted_norm(constant, exponent=exponent) assert almost_equal(w_const_norm(x), true_norm)
def test_vector_norm(fn, exponent): xarr, x = _vectors(fn) weight_vec = _pos_array(fn) weighting_vec = FnVectorWeighting(weight_vec, exponent=exponent) if exponent == float('inf'): true_norm = np.linalg.norm(weight_vec * xarr, ord=float('inf')) else: true_norm = np.linalg.norm(weight_vec ** (1 / exponent) * xarr, ord=exponent) assert almost_equal(weighting_vec.norm(x), true_norm) # With free function pnorm_vec = weighted_norm(weight_vec, exponent=exponent) assert almost_equal(pnorm_vec(x), true_norm)
def test_vector_norm(fn, exponent): xarr, x = example_vectors(fn) weight_vec = _pos_array(fn) weighting_vec = FnVectorWeighting(weight_vec, exponent=exponent) if exponent == float('inf'): true_norm = np.linalg.norm(weight_vec * xarr, ord=float('inf')) else: true_norm = np.linalg.norm(weight_vec**(1 / exponent) * xarr, ord=exponent) assert almost_equal(weighting_vec.norm(x), true_norm) # With free function pnorm_vec = weighted_norm(weight_vec, exponent=exponent) assert almost_equal(pnorm_vec(x), true_norm)