Skip to content

array-broadcasting in sp.lambdify, Lambdify and LambdifyCSE #174

@raphaelquast

Description

@raphaelquast

Hi,
i tried to implement symengine's LambdifyCSE to speedup my code..
however, while investigating this possibility, i found that LambdifyCSE
seems to have different array-broadcasting than Lambdify (and also sympy's lambdify)

x = var('x')
funcs = [sp.cos(x)**i for i in range(5)]

#ordinary sympy
f_sympy = sp.lambdify((x), funcs, modules = 'numpy')

#ordinary symengine
f_seng = Lambdify([x], funcs)

#symengine with cse
f_seng_cse = LambdifyCSE([x], funcs)

ideally, the functions should give the same output for the same input...
however, in order to get the same output i have to do the following:
(aside of the fact that sympy's lambdify treats constants rather strange...)

a = np.array([1, 2, 3])

s1 = f_sympy(a)
s2 = f_seng(a).T
s3 = f_seng_cse(a[:,np.newaxis]).T

furthermore calling

f_seng_cse(a)

gives

"ValueError: all the input arrays must have same number of dimensions"

which is rather strange since there is only one input-array....

for higher dimensional inputs this generalizes to

a = np.array([[1,2,3],[1,2,3],[1,2,3]])
s1 = f_sympy(a)
s2 = f_seng(a).T
s3 = f_seng_cse(a[:,:,np.newaxis]).T

In order to get the same outputs with functions taking more than one input-arguments,
i have to use

x, y = var('x, y')

funcs = [sp.cos(x)**i + sp.cos(y)**i for i in range(5)]

f_sympy = sp.lambdify((x, y),funcs, modules = 'numpy')
f_seng = Lambdify([x, y], funcs)
f_seng_cse = LambdifyCSE([x, y], funcs)

a = np.array([[1,2,3],[1,2,3],[1,2,3]])

s1 = f_sympy(a, a)
s2 = np.reshape(f_seng(np.reshape(np.ravel([a,a], order = 'F'), (-1, 2), order = 'C')).T, (-1, *a.shape), order = 'F')
s3 = np.reshape(f_seng_cse(np.reshape(np.ravel([a,a], order = 'F'), (-1, 2), order = 'C')).T, (-1, *a.shape), order = 'F')

which is certainly not very readable any more...
is there any obvious reason for this or is this some kind of a bug?
or even better, could you eventually tell me a clear way on how to perform correct
array-broadcasting of multidimensional functions in LambdifyCSE ?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions