Skip to content

Commit a8b8d3f

Browse files
committed
Implement count_ops
1 parent 52ee6b8 commit a8b8d3f

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

symengine/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
LessThan, StrictGreaterThan, StrictLessThan, Eq, Ne, Ge, Le,
1212
Gt, Lt, golden_ratio as GoldenRatio, catalan as Catalan,
1313
eulergamma as EulerGamma, Dummy, perfect_power, integer_nthroot,
14-
isprime, sqrt_mod, Expr, cse)
14+
isprime, sqrt_mod, Expr, cse, count_ops)
1515
from .utilities import var, symbols
1616
from .functions import *
1717

symengine/lib/symengine.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,7 @@ cdef extern from "<symengine/visitor.h>" namespace "SymEngine":
893893
bool has_symbol(const Basic &b, const Symbol &x) nogil except +
894894
rcp_const_basic coeff(const Basic &b, const Basic &x, const Basic &n) nogil except +
895895
set_basic free_symbols(const Basic &b) nogil except +
896+
unsigned count_ops(const vec_basic &a) nogil
896897

897898
cdef extern from "<symengine/logic.h>" namespace "SymEngine":
898899
cdef cppclass Boolean(Basic):

symengine/lib/symengine_wrapper.pyx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4933,5 +4933,22 @@ def cse(exprs):
49334933
return (vec_pair_to_list(replacements), vec_basic_to_list(reduced_exprs))
49344934

49354935

4936+
cdef _flattened_vec(symengine.vec_basic &vec, exprs):
4937+
cdef Basic b
4938+
from symengine.compatibility import is_sequence
4939+
if is_sequence(exprs):
4940+
for expr in exprs:
4941+
_flattened_vec(vec, expr)
4942+
else:
4943+
b = sympify(exprs)
4944+
vec.push_back(b.thisptr)
4945+
4946+
4947+
def count_ops(*exprs):
4948+
cdef symengine.vec_basic vec
4949+
_flattened_vec(vec, exprs)
4950+
return symengine.count_ops(vec)
4951+
4952+
49364953
# Turn on nice stacktraces:
49374954
symengine.print_stack_on_segfault()

symengine/tests/test_arit.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from symengine.utilities import raises
22

3-
from symengine import Symbol, Integer, Add, Mul, Pow, Rational, sqrt
4-
from symengine.lib.symengine_wrapper import Zero, One, NegativeOne, Half, I
3+
from symengine import (Symbol, Integer, Add, Mul, Pow, Rational, sqrt,
4+
symbols, S, I, count_ops)
55

66

77
def test_arit1():
@@ -221,10 +221,10 @@ def test_copy():
221221

222222

223223
def test_special_constants():
224-
assert Zero() == Integer(0)
225-
assert One() == Integer(1)
226-
assert NegativeOne() == Integer(-1)
227-
assert Half() == Rational(1, 2)
224+
assert S.Zero == Integer(0)
225+
assert S.One == Integer(1)
226+
assert S.NegativeOne == Integer(-1)
227+
assert S.Half == Rational(1, 2)
228228

229229

230230
def test_bool():
@@ -233,3 +233,12 @@ def test_bool():
233233
assert True
234234
if (x**2).args[1] < 0:
235235
assert False
236+
237+
238+
def test_count_ops():
239+
x, y = symbols("x, y")
240+
assert count_ops(x+y) == 1
241+
assert count_ops((x+y, x*y)) == 2
242+
assert count_ops([[x**y], [x+y-1]]) == 3
243+
assert count_ops(x+y, x*y) == 2
244+

symengine_version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fff6755331226a08f0b14571bfbce2b23001d911
1+
b3a2159b7e6950726157626069c30712427d55b2

0 commit comments

Comments
 (0)