@@ -2575,7 +2575,7 @@ cdef class Lambdify(object):
2575
2575
Lambdify instances are callbacks that numerically evaluate their symbolic
2576
2576
expressions from user provided input (real or complex) into (possibly user
2577
2577
provided) output buffers (real or complex). Multidimensional data are
2578
- processed in their most cache-friendly way ("ravelled").
2578
+ processed in their most cache-friendly way (i.e. "ravelled").
2579
2579
2580
2580
Parameters
2581
2581
----------
@@ -2599,7 +2599,7 @@ cdef class Lambdify(object):
2599
2599
[ 9., 24.]
2600
2600
2601
2601
"""
2602
- cdef size_t inp_size , out_size
2602
+ cdef size_t args_size , out_size
2603
2603
cdef tuple out_shape
2604
2604
cdef readonly bool real
2605
2605
cdef vector[symengine.LambdaRealDoubleVisitor] lambda_double
@@ -2615,7 +2615,7 @@ cdef class Lambdify(object):
2615
2615
int idx = 0
2616
2616
self .real = real
2617
2617
self .out_shape = get_shape(exprs)
2618
- self .inp_size = _size(args)
2618
+ self .args_size = _size(args)
2619
2619
self .out_size = reduce (mul, self .out_shape)
2620
2620
2621
2621
if isinstance (args, DenseMatrix):
@@ -2659,7 +2659,7 @@ cdef class Lambdify(object):
2659
2659
cdef vector[ValueType] inp_
2660
2660
cdef size_t idx, ninp = inp.size, nout = out.size
2661
2661
2662
- if inp.size != self .inp_size :
2662
+ if inp.size != self .args_size :
2663
2663
raise ValueError (" Size of inp incompatible with number of args." )
2664
2664
if out.size != self .out_size:
2665
2665
raise ValueError (" Size of out incompatible with number of exprs." )
@@ -2720,10 +2720,10 @@ cdef class Lambdify(object):
2720
2720
inp = tuple (inp)
2721
2721
inp_shape = (len (inp),)
2722
2722
inp_size = reduce (mul, inp_shape)
2723
- if inp_size % self .inp_size != 0 :
2723
+ if inp_size % self .args_size != 0 :
2724
2724
raise ValueError (" Broadcasting failed" )
2725
- nbroadcast = inp_size // self .inp_size
2726
- if nbroadcast > 1 and self .inp_size == 1 and inp_shape[- 1 ] != 1 : # Implicit reshape
2725
+ nbroadcast = inp_size // self .args_size
2726
+ if nbroadcast > 1 and self .args_size == 1 and inp_shape[- 1 ] != 1 : # Implicit reshape
2727
2727
inp_shape = inp_shape + (1 ,)
2728
2728
new_out_shape = inp_shape[:- 1 ] + self .out_shape
2729
2729
new_out_size = nbroadcast * self .out_size
@@ -2758,7 +2758,6 @@ cdef class Lambdify(object):
2758
2758
if out is None :
2759
2759
# allocate output container
2760
2760
if use_numpy:
2761
- nbroadcast = inp.size // self .inp_size
2762
2761
out = np.empty(new_out_size, dtype = np.float64 if
2763
2762
self .real else np.complex128)
2764
2763
else :
@@ -2771,17 +2770,20 @@ cdef class Lambdify(object):
2771
2770
reshape_out = len (new_out_shape) > 1
2772
2771
else :
2773
2772
if use_numpy:
2774
- out = np.asarray(out, dtype = np.float64 if
2775
- self .real else np.complex128) # copy if needed
2773
+ try :
2774
+ out_dtype = out.dtype
2775
+ except AttributeError :
2776
+ out = np.asarray(out)
2777
+ out_dtype = out.dtype
2778
+ if out_dtype != (np.float64 if self .real else np.complex128):
2779
+ raise TypeError (" Output array is of incorrect type" )
2776
2780
if out.size < new_out_size:
2777
2781
raise ValueError (" Incompatible size of output argument" )
2778
2782
if not out.flags[' C_CONTIGUOUS' ]:
2779
2783
raise ValueError (" Output argument needs to be C-contiguous" )
2780
2784
for idx, length in enumerate (out.shape[- len (self .out_shape)::- 1 ]):
2781
2785
if length < self .out_shape[- idx]:
2782
2786
raise ValueError (" Incompatible shape of output argument" )
2783
- if out.dtype != np.float64:
2784
- raise ValueError (" Output argument dtype not float64: %s " % out.dtype)
2785
2787
if not out.flags[' WRITEABLE' ]:
2786
2788
raise ValueError (" Output argument needs to be writeable" )
2787
2789
if out.ndim > 1 :
@@ -2798,12 +2800,12 @@ cdef class Lambdify(object):
2798
2800
if self .real:
2799
2801
real_inp_view = inp # slicing cython.view.array does not give a memview
2800
2802
real_out_view = out
2801
- self .unsafe_real(real_inp_view[idx* self .inp_size :(idx+ 1 )* self .inp_size ],
2803
+ self .unsafe_real(real_inp_view[idx* self .args_size :(idx+ 1 )* self .args_size ],
2802
2804
real_out_view[idx* self .out_size:(idx+ 1 )* self .out_size])
2803
2805
else :
2804
2806
complex_inp_view = inp
2805
2807
complex_out_view = out
2806
- self .unsafe_complex(complex_inp_view[idx* self .inp_size :(idx+ 1 )* self .inp_size ],
2808
+ self .unsafe_complex(complex_inp_view[idx* self .args_size :(idx+ 1 )* self .args_size ],
2807
2809
complex_out_view[idx* self .out_size:(idx+ 1 )* self .out_size])
2808
2810
2809
2811
if use_numpy and reshape_out:
0 commit comments