Skip to content

Commit dc1877e

Browse files
committed
Workaround not to use cdef vector[int] in cdef class.
Travis CI tests for Python 3.2 using gcc 3.7 give an error: Cython generated C contains uninitialized references.
1 parent 3b1fae7 commit dc1877e

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

symengine/lib/symengine_wrapper.pyx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ from libcpp.string cimport string
66
from libcpp.vector cimport vector
77
from cpython cimport PyObject, Py_XINCREF, Py_XDECREF, \
88
PyObject_CallMethodObjArgs
9+
from libc.stdlib cimport malloc, free
910
from libc.string cimport memcpy
1011
import cython
1112
import itertools
@@ -2118,7 +2119,7 @@ cdef class DenseMatrixBase(MatrixBase):
21182119
return self.nrows()*self.ncols()
21192120

21202121
def ravel(self):
2121-
return [self.get(i, j) for i in range(self.nrows()) for j in range(self.ncols())]
2122+
return [self._get(i, j) for i in range(self.nrows()) for j in range(self.ncols())]
21222123

21232124
def reshape(self, rows, cols):
21242125
if len(self) != rows*cols:
@@ -3110,7 +3111,8 @@ cdef class _Lambdify(object):
31103111
"""
31113112
cdef size_t args_size, tot_out_size
31123113
cdef list out_shapes
3113-
cdef vector[int] out_sizes, accum_out_sizes
3114+
cdef int *out_sizes
3115+
cdef int *accum_out_sizes
31143116
cdef readonly bool real
31153117
cdef readonly int n_exprs
31163118

@@ -3119,9 +3121,20 @@ cdef class _Lambdify(object):
31193121
self.out_shapes = [get_shape(expr) for expr in exprs]
31203122
self.n_exprs = len(exprs)
31213123
self.args_size = _size(args)
3122-
self.out_sizes = [reduce(mul, shape or (1,)) for shape in self.out_shapes]
3123-
self.accum_out_sizes = [sum(self.out_sizes[:i]) for i in range(self.n_exprs + 1)]
3124-
self.tot_out_size = sum(self.out_sizes)
3124+
self.out_sizes = <int *>malloc(sizeof(int)*self.n_exprs)
3125+
self.accum_out_sizes = <int *>malloc(sizeof(int)*(self.n_exprs+1))
3126+
self.tot_out_size = 0
3127+
for idx, shape in enumerate(self.out_shapes):
3128+
self.out_sizes[idx] = reduce(mul, shape or (1,))
3129+
self.tot_out_size += self.out_sizes[idx]
3130+
for i in range(self.n_exprs + 1):
3131+
self.accum_out_sizes[i] = 0
3132+
for j in range(i):
3133+
self.accum_out_sizes[i] += self.out_sizes[j]
3134+
3135+
def __dealloc__(self):
3136+
free(self.out_sizes)
3137+
free(self.accum_out_sizes)
31253138

31263139
def __init__(self, args, *exprs, bool real=True):
31273140
cdef:
@@ -3221,7 +3234,7 @@ cdef class _Lambdify(object):
32213234
if nbroadcast > 1 and self.args_size == 1 and inp_shape[-1] != 1: # Implicit reshape
32223235
inp_shape = inp_shape + (1,)
32233236
new_out_shapes = [inp_shape[:-1] + out_shape for out_shape in self.out_shapes]
3224-
new_out_sizes = [nbroadcast*out_size for out_size in self.out_sizes]
3237+
new_out_sizes = [nbroadcast*self.out_sizes[i] for i in range(self.n_exprs)]
32253238
new_tot_out_size = nbroadcast * self.tot_out_size
32263239
if use_numpy is None:
32273240
try:

0 commit comments

Comments
 (0)