Skip to content

Commit d38a3f1

Browse files
authored
Merge pull request #106 from isuruf/llvm
Use symengine's new interface for lambda double
2 parents 276d7bc + 9b7dc8e commit d38a3f1

File tree

2 files changed

+23
-39
lines changed

2 files changed

+23
-39
lines changed

symengine/lib/symengine.pxd

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -703,12 +703,18 @@ cdef extern from "<symengine/eval_double.h>" namespace "SymEngine":
703703
cdef extern from "<symengine/lambda_double.h>" namespace "SymEngine":
704704
cdef cppclass LambdaRealDoubleVisitor:
705705
LambdaRealDoubleVisitor() nogil
706-
void init(const vec_basic &x, const Basic &b) nogil except +
707-
double call(const vector[double] &x) nogil except +
706+
void init(const vec_basic &x, const vec_basic &b) nogil except +
707+
double call(double *r, const double *x) nogil except +
708708
cdef cppclass LambdaComplexDoubleVisitor:
709709
LambdaComplexDoubleVisitor() nogil
710-
void init(const vec_basic &x, const Basic &b) nogil except +
711-
double complex call(const vector[double complex] &x) nogil except +
710+
void init(const vec_basic &x, const vec_basic &b) nogil except +
711+
double complex call(double complex *r, const double complex *x) nogil except +
712+
713+
cdef extern from "<symengine/llvm_double.h>" namespace "SymEngine":
714+
cdef cppclass LLVMDoubleVisitor:
715+
LLVMDoubleVisitor() nogil
716+
void init(const vec_basic &x, const vec_basic &b) nogil except +
717+
double call(double *r, const double *x) nogil except +
712718

713719
cdef extern from "<symengine/series.h>" namespace "SymEngine":
714720
cdef cppclass SeriesCoeffInterface:

symengine/lib/symengine_wrapper.pyx

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,6 +2604,7 @@ cdef class Lambdify(object):
26042604
def __cinit__(self, args, exprs, bool real=True):
26052605
cdef:
26062606
symengine.vec_basic args_
2607+
symengine.vec_basic outs_
26072608
Basic e_
26082609
size_t ri, ci, nr, nc
26092610
symengine.MatrixBase *mtx
@@ -2626,63 +2627,40 @@ cdef class Lambdify(object):
26262627
e_ = sympify(e)
26272628
args_.push_back(e_.thisptr)
26282629

2629-
if self.real:
2630-
self.lambda_double.resize(self.out_size)
2631-
else:
2632-
self.lambda_double_complex.resize(self.out_size)
2633-
26342630
if isinstance(exprs, DenseMatrix):
26352631
nr = exprs.nrows()
26362632
nc = exprs.ncols()
26372633
mtx = (<DenseMatrix>exprs).thisptr
26382634
for ri in range(nr):
26392635
for ci in range(nc):
26402636
b_ = deref(mtx).get(ri, ci)
2641-
if real:
2642-
self.lambda_double[ri*nc+ci].init(args_, deref(b_))
2643-
else:
2644-
self.lambda_double_complex[ri*nc+ci].init(args_, deref(b_))
2637+
outs_.push_back(b_)
26452638
else:
26462639
for e in ravel(exprs):
26472640
e_ = sympify(e)
2648-
if real:
2649-
self.lambda_double[idx].init(args_, deref(e_.thisptr))
2650-
else:
2651-
self.lambda_double_complex[idx].init(args_, deref(e_.thisptr))
2652-
idx += 1
2641+
outs_.push_back(e_.thisptr)
2642+
2643+
if real:
2644+
self.lambda_double.resize(1)
2645+
self.lambda_double[0].init(args_, outs_)
2646+
else:
2647+
self.lambda_double_complex.resize(1)
2648+
self.lambda_double_complex[0].init(args_, outs_)
2649+
26532650

26542651
cdef void _eval(self, ValueType[::1] inp, ValueType[::1] out):
2655-
cdef vector[ValueType] inp_
26562652
cdef size_t idx, ninp = inp.size, nout = out.size
26572653

26582654
if inp.size != self.args_size:
26592655
raise ValueError("Size of inp incompatible with number of args.")
26602656
if out.size != self.out_size:
26612657
raise ValueError("Size of out incompatible with number of exprs.")
26622658

2663-
# Create the substitution "dict"
2664-
for idx in range(ninp):
2665-
inp_.push_back(inp[idx])
2666-
26672659
# Convert expr_subs to doubles write to out
26682660
if ValueType == cython.double:
2669-
self.as_real(inp_, out)
2661+
self.lambda_double[0].call(&out[0], &inp[0])
26702662
else:
2671-
self.as_complex(inp_, out)
2672-
2673-
@cython.wraparound(False)
2674-
@cython.boundscheck(False)
2675-
cdef void as_real(self, vector[double] &vec, double[::1] out) nogil:
2676-
cdef size_t i
2677-
for i in range(self.out_size):
2678-
out[i] = self.lambda_double[i].call(vec)
2679-
2680-
@cython.wraparound(False)
2681-
@cython.boundscheck(False)
2682-
cdef void as_complex(self, vector[double complex] &vec, double complex[::1] out) nogil:
2683-
cdef size_t i
2684-
for i in range(self.out_size):
2685-
out[i] = self.lambda_double_complex[i].call(vec)
2663+
self.lambda_double_complex[0].call(&out[0], &inp[0])
26862664

26872665
# the two cpdef:ed methods below may use void return type
26882666
# once Cython 0.23 (from 2015) is acceptable as requirement.

0 commit comments

Comments
 (0)