@@ -6,7 +6,6 @@ from libcpp.string cimport string
6
6
from libcpp.vector cimport vector
7
7
from cpython cimport PyObject, Py_XINCREF, Py_XDECREF, \
8
8
PyObject_CallMethodObjArgs
9
- from libc.stdlib cimport malloc, free
10
9
from libc.string cimport memcpy
11
10
import cython
12
11
import itertools
@@ -3014,6 +3013,7 @@ def has_symbol(obj, symbol=None):
3014
3013
3015
3014
IF HAVE_NUMPY:
3016
3015
# Lambdify requires NumPy (since b713a61, see gh-112)
3016
+ import os
3017
3017
cimport numpy as cnp
3018
3018
import numpy as np
3019
3019
have_numpy = True
@@ -3101,30 +3101,24 @@ IF HAVE_NUMPY:
3101
3101
cdef list out_shapes
3102
3102
cdef readonly bint real
3103
3103
cdef readonly int n_exprs
3104
- cdef int * out_sizes
3105
- cdef int * accum_out_sizes
3104
+ cdef vector[int ] accum_out_sizes
3106
3105
cdef object numpy_dtype
3107
3106
3108
3107
def __cinit__ (self , args , *exprs , bool real = True ):
3108
+ cdef vector[int ] out_sizes
3109
3109
self .real = real
3110
3110
self .numpy_dtype = np.float64 if self .real else np.complex128
3111
3111
self .out_shapes = [get_shape(expr) for expr in exprs]
3112
3112
self .n_exprs = len (exprs)
3113
3113
self .args_size = _size(args)
3114
- self .out_sizes = < int * > malloc(sizeof(int )* self .n_exprs)
3115
- self .accum_out_sizes = < int * > malloc(sizeof(int )* (self .n_exprs+ 1 ))
3116
3114
self .tot_out_size = 0
3117
3115
for idx, shape in enumerate (self .out_shapes):
3118
- self . out_sizes[idx] = reduce (mul, shape or (1 ,))
3119
- self .tot_out_size += self . out_sizes[idx]
3116
+ out_sizes.push_back( reduce (mul, shape or (1 ,) ))
3117
+ self .tot_out_size += out_sizes[idx]
3120
3118
for i in range (self .n_exprs + 1 ):
3121
- self .accum_out_sizes[i] = 0
3119
+ self .accum_out_sizes.push_back( 0 )
3122
3120
for j in range (i):
3123
- self .accum_out_sizes[i] += self .out_sizes[j]
3124
-
3125
- def __dealloc__ (self ):
3126
- free(self .out_sizes)
3127
- free(self .accum_out_sizes)
3121
+ self .accum_out_sizes[i] += out_sizes[j]
3128
3122
3129
3123
def __init__ (self , args , *exprs , bool real = True ):
3130
3124
cdef:
@@ -3329,14 +3323,19 @@ IF HAVE_NUMPY:
3329
3323
self .lambda_double[0 ].call(& out[out_offset], & inp[inp_offset])
3330
3324
3331
3325
3332
- def Lambdify (args , *exprs , bool real = True , backend = " lambda" ):
3326
+ def Lambdify (args , *exprs , bool real = True , backend = None ):
3327
+ if backend is None :
3328
+ backend = os.getenv(' SYMENGINE_LAMBDIFY_BACKEND' , " lambda" )
3333
3329
if backend == " llvm" :
3334
3330
IF HAVE_SYMENGINE_LLVM:
3335
3331
return LLVMDouble(args, * exprs, real = real)
3336
3332
ELSE :
3337
3333
raise ValueError (""" llvm backend is chosen, but symengine is not compiled
3338
3334
with llvm support.""" )
3339
-
3335
+ elif backend == " lambda" :
3336
+ pass
3337
+ else :
3338
+ warnings.warn(" Unknown SymEngine backend: %s \n Using backend='lambda'" % backend)
3340
3339
return LambdaDouble(args, * exprs, real = real)
3341
3340
3342
3341
0 commit comments