Exemple #1
0
def test_linear_scale():
    A = np.random.rand(4, 3)
    x = np.random.rand(3)
    y = np.random.rand(4)

    Aop = MatVecOperator(A)
    xvec = Aop.domain.element(x)
    yvec = Aop.range.element(y)

    # Test a range of scalars (scalar multiplication could implement
    # optimizations for (-1, 0, 1).
    scalars = [-1.432, -1, 0, 1, 3.14]
    for scale in scalars:
        C = OperatorRightScalarMult(Aop, scale)

        assert C.is_linear
        assert C.adjoint.is_linear

        assert all_almost_equal(C(xvec), scale * np.dot(A, x))
        assert all_almost_equal(C.adjoint(yvec), scale * np.dot(A.T, y))

        # Using operator overloading
        assert all_almost_equal((scale * Aop)(xvec), scale * np.dot(A, x))
        assert all_almost_equal((Aop * scale)(xvec), np.dot(A, scale * x))
        assert all_almost_equal((scale * Aop).adjoint(yvec),
                                scale * np.dot(A.T, y))
        assert all_almost_equal((Aop * scale).adjoint(yvec),
                                np.dot(A.T, scale * y))
Exemple #2
0
def test_functional_scale():
    r3 = odl.Rn(3)

    Aop = SumFunctional(r3)
    x = r3.element([1, 2, 3])
    y = 1

    # Test a range of scalars (scalar multiplication could implement
    # optimizations for (-1, 0, 1).
    scalars = [-1.432, -1, 0, 1, 3.14]
    for scale in scalars:
        C = OperatorRightScalarMult(Aop, scale)

        assert C.is_linear
        assert C.adjoint.is_linear

        assert C(x) == scale * np.sum(x)
        assert all_almost_equal(C.adjoint(y), scale * y * np.ones(3))
        assert all_almost_equal(C.adjoint.adjoint(x), C(x))

        # Using operator overloading
        assert (scale * Aop)(x) == scale * np.sum(x)
        assert (Aop * scale)(x) == scale * np.sum(x)
        assert all_almost_equal((scale * Aop).adjoint(y),
                                scale * y * np.ones(3))
        assert all_almost_equal((Aop * scale).adjoint(y),
                                scale * y * np.ones(3))
Exemple #3
0
def test_functional_scale():
    r3 = odl.Rn(3)

    Aop = SumFunctional(r3)
    x = r3.element([1, 2, 3])
    y = 1

    # Test a range of scalars (scalar multiplication could implement
    # optimizations for (-1, 0, 1).
    scalars = [-1.432, -1, 0, 1, 3.14]
    for scale in scalars:
        C = OperatorRightScalarMult(Aop, scale)

        assert C.is_linear
        assert C.adjoint.is_linear

        assert C(x) == scale * np.sum(x)
        assert all_almost_equal(C.adjoint(y), scale * y * np.ones(3))
        assert all_almost_equal(C.adjoint.adjoint(x), C(x))

        # Using operator overloading
        assert (scale * Aop)(x) == scale * np.sum(x)
        assert (Aop * scale)(x) == scale * np.sum(x)
        assert all_almost_equal((scale * Aop).adjoint(y),
                                scale * y * np.ones(3))
        assert all_almost_equal((Aop * scale).adjoint(y),
                                scale * y * np.ones(3))
Exemple #4
0
def test_linear_scale():
    A = np.random.rand(4, 3)
    x = np.random.rand(3)
    y = np.random.rand(4)

    Aop = MatVecOperator(A)
    xvec = Aop.domain.element(x)
    yvec = Aop.range.element(y)

    # Test a range of scalars (scalar multiplication could implement
    # optimizations for (-1, 0, 1).
    scalars = [-1.432, -1, 0, 1, 3.14]
    for scale in scalars:
        C = OperatorRightScalarMult(Aop, scale)

        assert C.is_linear
        assert C.adjoint.is_linear

        assert all_almost_equal(C(xvec), scale * np.dot(A, x))
        assert all_almost_equal(C.adjoint(yvec), scale * np.dot(A.T, y))

        # Using operator overloading
        assert all_almost_equal((scale * Aop)(xvec),
                                scale * np.dot(A, x))
        assert all_almost_equal((Aop * scale)(xvec),
                                np.dot(A, scale * x))
        assert all_almost_equal((scale * Aop).adjoint(yvec),
                                scale * np.dot(A.T, y))
        assert all_almost_equal((Aop * scale).adjoint(yvec),
                                np.dot(A.T, scale * y))
Exemple #5
0
def test_linear_operator_scaling(dom_eq_ran):
    """Check call and adjoint of a scaled linear operator."""
    if dom_eq_ran:
        mat = np.random.rand(3, 3)
    else:
        mat = np.random.rand(4, 3)

    op = MatrixOperator(mat)
    xarr, x = noise_elements(op.domain)
    yarr, y = noise_elements(op.range)

    # Test a range of scalars (scalar multiplication could implement
    # optimizations for (-1, 0, 1).
    scalars = [-1.432, -1, 0, 1, 3.14]
    for scalar in scalars:
        # Explicit instantiation
        scaled_op = OperatorRightScalarMult(op, scalar)
        assert scaled_op.is_linear
        assert scaled_op.adjoint.is_linear
        check_call(scaled_op, x, scalar * np.dot(mat, xarr))
        check_call(scaled_op.adjoint, y, scalar * np.dot(mat.T, yarr))

        # Using operator overloading
        check_call(scalar * op, x, scalar * np.dot(mat, xarr))
        check_call(op * scalar, x, scalar * np.dot(mat, xarr))
        check_call((scalar * op).adjoint, y, scalar * np.dot(mat.T, yarr))
        check_call((op * scalar).adjoint, y, scalar * np.dot(mat.T, yarr))
Exemple #6
0
def test_operator_scaling(dom_eq_ran):
    """Check operator scaling against NumPy reference."""
    if dom_eq_ran:
        mat = np.random.rand(3, 3)
    else:
        mat = np.random.rand(4, 3)

    op = MultiplyAndSquareOp(mat)
    xarr, x = noise_elements(op.domain)

    # Test a range of scalars (scalar multiplication could implement
    # optimizations for (-1, 0, 1)).
    scalars = [-1.432, -1, 0, 1, 3.14]
    for scalar in scalars:
        lscaled = OperatorLeftScalarMult(op, scalar)
        rscaled = OperatorRightScalarMult(op, scalar)

        assert not lscaled.is_linear
        assert not rscaled.is_linear

        # Explicit
        check_call(lscaled, x, scalar * mult_sq_np(mat, xarr))
        check_call(rscaled, x, mult_sq_np(mat, scalar * xarr))

        # Using operator overloading
        check_call(scalar * op, x, scalar * mult_sq_np(mat, xarr))
        check_call(op * scalar, x, mult_sq_np(mat, scalar * xarr))

    # Fail when scaling by wrong scalar type (complex number)
    wrongscalars = [1j, [1, 2], (1, 2)]
    for wrongscalar in wrongscalars:
        with pytest.raises(TypeError):
            OperatorLeftScalarMult(op, wrongscalar)

        with pytest.raises(TypeError):
            OperatorRightScalarMult(op, wrongscalar)

        with pytest.raises(TypeError):
            op * wrongscalar

        with pytest.raises(TypeError):
            wrongscalar * op
Exemple #7
0
def test_nonlinear_scale():
    A = np.random.rand(4, 3)
    x = np.random.rand(3)

    Aop = MultiplyAndSquareOp(A)
    xvec = Aop.domain.element(x)

    # Test a range of scalars (scalar multiplication could implement
    # optimizations for (-1, 0, 1)).
    scalars = [-1.432, -1, 0, 1, 3.14]
    for scale in scalars:
        lscaled = OperatorLeftScalarMult(Aop, scale)
        rscaled = OperatorRightScalarMult(Aop, scale)

        assert not lscaled.is_linear
        assert not rscaled.is_linear

        # Explicit
        check_call(lscaled, xvec, scale * mult_sq_np(A, x))
        check_call(rscaled, xvec, mult_sq_np(A, scale * x))

        # Using operator overloading
        check_call(scale * Aop, xvec, scale * mult_sq_np(A, x))
        check_call(Aop * scale, xvec, mult_sq_np(A, scale * x))

    # Fail when scaling by wrong scalar type (A complex number)
    wrongscalars = [1j, [1, 2], (1, 2)]
    for wrongscalar in wrongscalars:
        with pytest.raises(TypeError):
            print(OperatorLeftScalarMult(Aop, wrongscalar))

        with pytest.raises(TypeError):
            print(OperatorRightScalarMult(Aop, wrongscalar))

        with pytest.raises(TypeError):
            print(Aop * wrongscalar)

        with pytest.raises(TypeError):
            print(wrongscalar * Aop)