Skip to content

Commit

Permalink
Merge pull request #81 from pynbody/equality-testing
Browse files Browse the repository at this point in the history
Add ==, <= and >= comparison ops to live calculation
  • Loading branch information
apontzen authored Dec 6, 2018
2 parents 69cb3c6 + 37396a4 commit 7f36946
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
12 changes: 12 additions & 0 deletions tangos/live_calculation/builtin_functions/arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ def greater(halos, vals1, vals2):
def less(halos, vals1, vals2):
return arithmetic_binary_op(vals1, vals2, np.less)

@BuiltinFunction.register
def equal(halos, vals1, vals2):
return arithmetic_binary_op(vals1, vals2, np.equal)

@BuiltinFunction.register
def greater_equal(halos, vals1, vals2):
return arithmetic_binary_op(vals1, vals2, np.greater_equal)

@BuiltinFunction.register
def less_equal(halos, vals1, vals2):
return arithmetic_binary_op(vals1, vals2, np.less_equal)

@BuiltinFunction.register
def logical_and(halos, vals1, vals2):
return arithmetic_binary_op(vals1, vals2, np.logical_and)
Expand Down
5 changes: 4 additions & 1 deletion tangos/live_calculation/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ def pack_args(for_function):
(">", "greater"),
("<", "less"),
("|", "logical_or"),
("&", "logical_and")]
("&", "logical_and"),
("==", "equal"),
(">=", "greater_equal"),
("<=", "less_equal")]

UNARY_OPS = [("!", "logical_not")]

Expand Down
11 changes: 11 additions & 0 deletions tests/test_live_calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,17 @@ def test_arithmetic():
assert h.calculate("at(1.0,dummy_property_1)*at(5.0,dummy_property_1)") ==\
h.calculate("at(1.0,dummy_property_1)") * h.calculate("at(5.0,dummy_property_1)")

def test_comparison():
h = tangos.get_halo("sim/ts1/1")
assert h.calculate("1.0<2.0")
assert not h.calculate("1.0>2.0")
assert h.calculate("1.0==1.0")
assert h.calculate("1.0>=1.0")
assert h.calculate("1.0<=1.0")
assert h.calculate("1.0>=0.5")
assert not h.calculate("1.0<=0.5")


def test_calculate_array():
h = tangos.get_halo("sim/ts1/1")

Expand Down

0 comments on commit 7f36946

Please sign in to comment.