Ejemplo n.º 1
0
def test_matrix_norm(fn, exponent):
    xarr, x = noise_elements(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 = scipy.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 = NumpyFnMatrixWeighting(sparse_mat, exponent=exponent)
        assert almost_equal(w_sparse.norm(x), true_norm_sparse)

    w_dense = NumpyFnMatrixWeighting(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):
            npy_weighted_norm(sparse_mat, exponent=exponent)
    else:
        w_sparse_norm = npy_weighted_norm(sparse_mat, exponent=exponent)
        assert almost_equal(w_sparse_norm(x), true_norm_sparse)

    w_dense_norm = npy_weighted_norm(dense_mat, exponent=exponent)
    assert almost_equal(w_dense_norm(x), true_norm_dense)
Ejemplo n.º 2
0
def test_constant_norm(fn, exponent):
    xarr, x = noise_elements(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 = NumpyFnConstWeighting(constant, exponent=exponent)
    assert almost_equal(w_const.norm(x), true_norm)

    # With free function
    w_const_norm = npy_weighted_norm(constant, exponent=exponent)
    assert almost_equal(w_const_norm(x), true_norm)
Ejemplo n.º 3
0
def test_array_norm(fn, exponent):
    xarr, x = noise_elements(fn)

    weight_arr = _pos_array(fn)
    weighting_arr = NumpyFnArrayWeighting(weight_arr, exponent=exponent)

    if exponent == float('inf'):
        true_norm = np.linalg.norm(weight_arr * xarr, ord=float('inf'))
    else:
        true_norm = np.linalg.norm(weight_arr**(1 / exponent) * xarr,
                                   ord=exponent)

    assert almost_equal(weighting_arr.norm(x), true_norm)

    # With free function
    pnorm_vec = npy_weighted_norm(weight_arr, exponent=exponent)
    assert almost_equal(pnorm_vec(x), true_norm)