Skip to content

Commit

Permalink
ENH: add 'conj' method to FunctionSpaceVector
Browse files Browse the repository at this point in the history
  • Loading branch information
kohr-h committed Mar 11, 2016
1 parent 696f68a commit 8c205fb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
18 changes: 17 additions & 1 deletion odl/space/fspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,18 @@ def imagpart_oop(x):
rspace = self.astype(rdtype)
return rspace.element(imagpart_oop)

def _conj(self, x):
"""Function returning the complex conjugate of a result."""
x_call_oop = x._call_out_of_place

def conj_oop(x):
return np.asarray(x_call_oop(x), dtype=self.out_dtype).conj()

if is_real_dtype(self.out_dtype):
return x
else:
return self.element(conj_oop)

@property
def element_type(self):
"""`FunctionSpaceVector`"""
Expand Down Expand Up @@ -1018,13 +1030,17 @@ def __ipow__(self, p):

@property
def real(self):
"""Function returning the real part of a result."""
"""Pointwise real part of this function."""
return self.space._realpart(self)

@property
def imag(self):
"""Pointwise imaginary part of this function."""
return self.space._imagpart(self)

def conj(self):
"""Pointwise complex conjugate of this function."""
return self.space._conj(self)

if __name__ == '__main__':
from doctest import testmod, NORMALIZE_WHITESPACE
Expand Down
11 changes: 11 additions & 0 deletions test/space/fspace_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ def test_fspace_vector_real_imag():
rect, _, mg = _standard_setup_2d()
cspace = FunctionSpace(rect, field=odl.ComplexNumbers())
f = cspace.element(cfunc_2d_vec_oop)

# real / imag on complex functions
assert all_equal(f.real(mg), cfunc_2d_vec_oop(mg).real)
assert all_equal(f.imag(mg), cfunc_2d_vec_oop(mg).imag)
out_mg = np.empty((2, 3))
Expand All @@ -410,11 +412,20 @@ def test_fspace_vector_real_imag():
f.imag(mg, out=out_mg)
assert all_equal(out_mg, cfunc_2d_vec_oop(mg).imag)

# real / imag on real functions, should be the function itself / zero
rspace = FunctionSpace(rect)
f = rspace.element(func_2d_vec_oop)
assert all_equal(f.real(mg), f(mg))
assert all_equal(f.imag(mg), rspace.zero()(mg))

# Complex conjugate
f = cspace.element(cfunc_2d_vec_oop)
fbar = f.conj()
assert all_equal(fbar(mg), cfunc_2d_vec_oop(mg).conj())
out_mg = np.empty((2, 3), dtype='complex128')
fbar(mg, out=out_mg)
assert all_equal(out_mg, cfunc_2d_vec_oop(mg).conj())


def test_fspace_zero():
rect, points, mg = _standard_setup_2d()
Expand Down

0 comments on commit 8c205fb

Please sign in to comment.