Skip to content

Commit

Permalink
ENH: add float, complex, int and long to base_ntuples
Browse files Browse the repository at this point in the history
  • Loading branch information
adler-j committed Jul 14, 2016
1 parent ce81541 commit d8868df
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 3 deletions.
66 changes: 66 additions & 0 deletions odl/space/base_ntuples.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,72 @@ def __ne__(self, other):
"""Return ``self != other``."""
return not self.__eq__(other)

def __int__(self):
"""Return ``int(self)``.
Returns
-------
int : `int`
Integer representing this vector.
Raises
------
TypeError : If the vector is of `size` != 1.
"""
if self.size != 1:
raise TypeError('only size 1 vectors can be converted to int')
return int(self[0])

def __long__(self):
"""Return ``long(self)``.
The `long` method is only available in Python 2.
Returns
-------
long : `long`
Integer representing this vector.
Raises
------
TypeError : If the vector is of `size` != 1.
"""
if self.size != 1:
raise TypeError('only size 1 vectors can be converted to long')
return long(self[0])

def __float__(self):
"""Return ``float(self)``.
Returns
-------
float : `float`
Floating point number representing this vector.
Raises
------
TypeError : If the vector is of `size` != 1.
"""
if self.size != 1:
raise TypeError('only size 1 vectors can be converted to float')
return float(self[0])

def __complex__(self):
"""Return ``complex(self)``.
Returns
-------
complex : `complex`
Complex floating point number representing this vector.
Raises
------
TypeError : If the vector is of `size` != 1.
"""
if self.size != 1:
raise TypeError('only size 1 vectors can be converted to complex')
return complex(self[0])

def __str__(self):
"""Return ``str(self)``."""
return array1d_str(self)
Expand Down
4 changes: 2 additions & 2 deletions test/solvers/advanced/douglas_rachford_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ def test_primal_dual_with_li():
douglas_rachford_pd(x, prox_f, prox_cc_g, lin_ops, tau=0.5,
sigma=[1.0], niter=20, prox_cc_l=prox_cc_ls)

assert lower_lim - 10 ** -LOW_ACCURACY <= x[0]
assert x[0] <= upper_lim + 10 ** -LOW_ACCURACY
assert lower_lim - 10 ** -LOW_ACCURACY <= float(x)
assert float(x) <= upper_lim + 10 ** -LOW_ACCURACY


if __name__ == '__main__':
Expand Down
41 changes: 40 additions & 1 deletion test/space/ntuples_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
example_vectors)
from odl.util.ufuncs import UFUNCS, REDUCTIONS

# Check for python3
from sys import version_info
PYTHON2 = version_info < (3, 0)


# Helpers to generate data
def _pos_array(fn):
Expand Down Expand Up @@ -730,7 +734,42 @@ def test_nbytes(fn):
assert x.nbytes == x.itemsize * x.size


# Numpy Array tests
# Type conversion tests


def test_scalar_method():
# Too large space
space = odl.rn(2)
element = space.one()

with pytest.raises(TypeError):
int(element)
with pytest.raises(TypeError):
float(element)
with pytest.raises(TypeError):
complex(element)
if PYTHON2:
with pytest.raises(TypeError):
long(element)

# Size 1 real space
space = odl.rn(1)
value = 1.5
element = space.element(value)

assert int(element) == int(value)
assert float(element) == float(value)
assert complex(element) == complex(value)
if PYTHON2:
assert long(element) == long(value)

# Size 1 complex space
space = odl.cn(1)
value = 1.5 + 0.5j
element = space.element(value)

assert complex(element) == complex(value)


def test_array_method(fn):
# Verify that the __array__ method works
Expand Down

0 comments on commit d8868df

Please sign in to comment.