Skip to content

Commit fcd3a15

Browse files
authored
Merge pull request #27 from videlec/test-cython-less-than-3
also test cython<3.0.0
2 parents 5e078dc + 6a83dee commit fcd3a15

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
strategy:
1414
matrix:
1515
python-version: ["3.8", "3.9", "3.10", "3.11"]
16+
cython: ["cython", "cython<3.0.0"]
1617
steps:
1718
- uses: actions/checkout@v2
1819
with: { submodules: recursive }
@@ -21,7 +22,7 @@ jobs:
2122
- name: Install pplpy dependencies
2223
shell: bash -l {0}
2324
run: |
24-
mamba env update --quiet -n test -f environment.yml
25+
mamba install --quiet setuptools gmpy2 cysignals ppl "${{matrix.cython}}"
2526
conda list
2627
- name: Install pplpy
2728
shell: bash -l {0}

README.rst

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,12 @@ Requirements
9494

9595
- `PPL <http://bugseng.com/products/ppl/>`_
9696

97-
- `Cython <http://cython.org>`_
97+
- `Cython <http://cython.org>`_ (tested with both 0.29 and 3.0)
9898

9999
- `cysignals <https://pypi.python.org/pypi/cysignals>`_
100100

101-
- `gmpy2 <https://pypi.python.org/pypi/gmpy2>`_: version >= 2.1.0a4 (see below)
101+
- `gmpy2 <https://pypi.python.org/pypi/gmpy2>`_
102102

103-
On Debian/Ubuntu systems these can be installed with::
103+
On Debian/Ubuntu systems the dependencies can be installed with::
104104

105-
$ sudo apt-get install libgmp-dev libmpfr-dev libmpc-dev libppl-dev cython
106-
$ pip install cysignals --user
107-
$ pip install gmpy2 --pre --user
108-
109-
The pip optional option `--user` allows to install python packages for a single
110-
user with no need for administrator rights. The two pip install commands might
111-
be replaced by `sudo pip install PKG` (not recommended). On recent Debian/Ubuntu systems,
112-
cysignals is also available as a package under the name `python-cysignals` for
113-
Python 2 and `python3-cysignals` for Python 3.
105+
$ sudo apt-get install libgmp-dev libmpfr-dev libmpc-dev libppl-dev cython3 python3-gmpy2 python3-cysignals-pari

ppl/linear_algebra.pyx

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,11 @@ cdef class Variable(object):
284284
...
285285
TypeError: ppl coefficients must be integral
286286
"""
287-
return Linear_Expression(self) * other
287+
if isinstance(self, Variable):
288+
return Linear_Expression(self) * other
289+
else:
290+
# NOTE: this code path will only be executed when compiled with cython < 3.0.0
291+
return Linear_Expression(other) * self
288292

289293
def __rmul__(self, other):
290294
return Linear_Expression(self) * other
@@ -641,11 +645,12 @@ cdef class Linear_Expression(object):
641645
a = args[0]
642646
b = args[1]
643647
self.thisptr = new PPL_Linear_Expression()
644-
if isinstance(a, dict) and a:
645-
self.thisptr.set_space_dimension(1 + max(a))
646-
for i, coeff in a.items():
647-
self.thisptr.set_coefficient(PPL_Variable(i), PPL_Coefficient_from_pyobject(coeff))
648-
elif a:
648+
if isinstance(a, dict):
649+
if a:
650+
self.thisptr.set_space_dimension(1 + max(a))
651+
for i, coeff in a.items():
652+
self.thisptr.set_coefficient(PPL_Variable(i), PPL_Coefficient_from_pyobject(coeff))
653+
else:
649654
self.thisptr.set_space_dimension(len(a))
650655
for i, coeff in enumerate(a):
651656
self.thisptr.set_coefficient(PPL_Variable(i), PPL_Coefficient_from_pyobject(coeff))
@@ -1088,7 +1093,11 @@ cdef class Linear_Expression(object):
10881093
"""
10891094
cdef Linear_Expression lhs, rhs
10901095

1091-
lhs = <Linear_Expression> self
1096+
if isinstance(self, Linear_Expression):
1097+
lhs = <Linear_Expression> self
1098+
else:
1099+
# NOTE: this code path will only be executed when compiled with cython < 3.0.0
1100+
lhs = Linear_Expression(self)
10921101

10931102
if isinstance(other, Linear_Expression):
10941103
rhs = <Linear_Expression> other
@@ -1181,9 +1190,9 @@ cdef class Linear_Expression(object):
11811190
>>> from ppl import Variable
11821191
>>> x = Variable(0)
11831192
>>> y = Variable(1)
1184-
>>> 8*(x+1)
1193+
>>> 8 * (x + 1)
11851194
8*x0+8
1186-
>>> y*8
1195+
>>> y * 8
11871196
8*x1
11881197
>>> 2**128 * x
11891198
340282366920938463463374607431768211456*x0
@@ -1194,8 +1203,14 @@ cdef class Linear_Expression(object):
11941203
"""
11951204
cdef Linear_Expression e
11961205
cdef c
1197-
e = <Linear_Expression> self
1198-
c = other
1206+
1207+
if isinstance(self, Linear_Expression):
1208+
e = <Linear_Expression> self
1209+
c = other
1210+
else:
1211+
# NOTE: this code path will only be executed when compiled with cython < 3.0.0
1212+
e = <Linear_Expression> other
1213+
c = self
11991214

12001215
cdef PPL_Coefficient cc = PPL_Coefficient_from_pyobject(c)
12011216
cdef Linear_Expression result = Linear_Expression()

0 commit comments

Comments
 (0)