def test_functional_right_vector_mult(): r3 = odl.Rn(3) Aop = SumFunctional(r3) vec = r3.element([1, 2, 3]) x = r3.element([4, 5, 6]) y = 2.0 # Test a range of scalars (scalar multiplication could implement # optimizations for (-1, 0, 1). C = OperatorRightVectorMult(Aop, vec) assert C.is_linear assert C.adjoint.is_linear assert all_almost_equal(C(x), np.sum(vec * x)) assert all_almost_equal(C.adjoint(y), vec * y) assert all_almost_equal(C.adjoint.adjoint(x), C(x)) # Using operator overloading assert all_almost_equal((Aop * vec)(x), np.sum(vec * x)) assert all_almost_equal((Aop * vec).adjoint(y), vec * y)
def test_linear_right_vector_mult(): A = np.random.rand(4, 3) Aop = MatVecOperator(A) vec = Aop.domain.element([1, 2, 3]) x = Aop.domain.element([4, 5, 6]) y = Aop.range.element([5, 6, 7, 8]) # Test a range of scalars (scalar multiplication could implement # optimizations for (-1, 0, 1). C = OperatorRightVectorMult(Aop, vec) assert C.is_linear assert C.adjoint.is_linear assert all_almost_equal(C(x), np.dot(A, vec * x)) assert all_almost_equal(C.adjoint(y), vec * np.dot(A.T, y)) assert all_almost_equal(C.adjoint.adjoint(x), C(x)) # Using operator overloading assert all_almost_equal((Aop * vec)(x), np.dot(A, vec * x)) assert all_almost_equal((Aop * vec).adjoint(y), vec * np.dot(A.T, y))
def test_nonlinear_right_vector_mult(): A = np.random.rand(4, 3) Aop = MultiplyAndSquareOp(A) vec = Aop.domain.element([1, 2, 3]) x = Aop.domain.element([4, 5, 6]) # Test a range of scalars (scalar multiplication could implement # optimizations for (-1, 0, 1). C = OperatorRightVectorMult(Aop, vec) assert not C.is_linear assert all_almost_equal(C(x), mult_sq_np(A, vec * x)) # Using operator overloading assert all_almost_equal((Aop * vec)(x), mult_sq_np(A, vec * x))
def test_linear_right_vector_mult(dom_eq_ran): """Check call and adjoint of linear operator x vector.""" if dom_eq_ran: mat = np.random.rand(3, 3) else: mat = np.random.rand(4, 3) op = MatrixOperator(mat) (xarr, mul_arr), (x, mul) = noise_elements(op.domain, n=2) yarr, y = noise_elements(op.range) # Explicit instantiation rmult_op = OperatorRightVectorMult(op, mul) assert rmult_op.is_linear assert rmult_op.adjoint.is_linear check_call(rmult_op, x, np.dot(mat, mul_arr * xarr)) check_call(rmult_op.adjoint, y, mul_arr * np.dot(mat.T, yarr)) # Using operator overloading check_call(op * mul, x, np.dot(mat, mul_arr * xarr)) check_call((op * mul).adjoint, y, mul_arr * np.dot(mat.T, yarr))
def test_nonlinear_vector_mult(): A = np.random.rand(4, 3) Aop = MultiplyAndSquareOp(A) rvec = Aop.domain.element([1, 2, 3]) lvec = Aop.range.element([1, 2, 3, 4]) x = Aop.domain.element([4, 5, 6]) # Test a range of scalars (scalar multiplication could implement # optimizations for (-1, 0, 1). rmult = OperatorRightVectorMult(Aop, rvec) lmult = OperatorLeftVectorMult(Aop, lvec) assert not rmult.is_linear assert not lmult.is_linear check_call(rmult, x, mult_sq_np(A, rvec * x)) check_call(lmult, x, lvec * mult_sq_np(A, x)) # Using operator overloading check_call(Aop * rvec, x, mult_sq_np(A, rvec * x)) check_call(lvec * Aop, x, lvec * mult_sq_np(A, x))
def test_operator_vector_mult(dom_eq_ran): """Check operator-vector multiplication against NumPy reference.""" if dom_eq_ran: mat = np.random.rand(3, 3) else: mat = np.random.rand(4, 3) op = MultiplyAndSquareOp(mat) right = op.domain.element(np.arange(op.domain.size)) left = op.range.element(np.arange(op.range.size)) xarr, x = noise_elements(op.domain) rmult_op = OperatorRightVectorMult(op, right) lmult_op = OperatorLeftVectorMult(op, left) assert not rmult_op.is_linear assert not lmult_op.is_linear check_call(rmult_op, x, mult_sq_np(mat, right * xarr)) check_call(lmult_op, x, left * mult_sq_np(mat, xarr)) # Using operator overloading check_call(op * right, x, mult_sq_np(mat, right * xarr)) check_call(left * op, x, left * mult_sq_np(mat, xarr))