Skip to content

Commit 8d068a3

Browse files
authored
Merge pull request #231 from isuruf/count_ops
Implement count_ops
2 parents 52ee6b8 + 8a3cbec commit 8d068a3

File tree

7 files changed

+39
-11
lines changed

7 files changed

+39
-11
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ matrix:
5353
packages:
5454
- binutils-dev
5555
- g++-4.8
56-
- env: BUILD_TYPE="Debug" WITH_BFD="yes" PYTHON_VERSION="3.5" WITH_LLVM="3.8" WITH_SCIPY="yes"
56+
- env: BUILD_TYPE="Debug" WITH_BFD="yes" PYTHON_VERSION="3.5" WITH_LLVM="3.8" WITH_SCIPY="yes" BUILD_SHARED_LIBS="yes"
5757
compiler: clang
5858
os: linux
5959
addons:

bin/install_travis.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ fi
1919

2020
if [[ "${WITH_SAGE}" == "yes" ]]; then
2121
# This is split to avoid the 10 minute limit
22-
conda install -q sagelib
23-
export conda_pkgs="${conda_pkgs} sage";
22+
conda install -q sagelib=8.1
23+
conda clean --all
24+
export conda_pkgs="${conda_pkgs} sage=8.1";
2425
fi
2526

2627
conda install -q ${conda_pkgs}
28+
conda clean --all
2729
source activate $our_install_dir;

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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4933,5 +4933,21 @@ 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+
if is_sequence(exprs):
4939+
for expr in exprs:
4940+
_flattened_vec(vec, expr)
4941+
else:
4942+
b = sympify(exprs)
4943+
vec.push_back(b.thisptr)
4944+
4945+
4946+
def count_ops(*exprs):
4947+
cdef symengine.vec_basic vec
4948+
_flattened_vec(vec, exprs)
4949+
return symengine.count_ops(vec)
4950+
4951+
49364952
# Turn on nice stacktraces:
49374953
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)