Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behavior when passing sliced array to ufuncify function #16773

Open
h3jia opened this issue May 3, 2019 · 0 comments
Open

Unexpected behavior when passing sliced array to ufuncify function #16773

h3jia opened this issue May 3, 2019 · 0 comments

Comments

@h3jia
Copy link

h3jia commented May 3, 2019

Hi, please see the following code:

import sympy as sp
import numpy as np
from sympy.utilities.autowrap import ufuncify

a = sp.Symbol('a', real=True)
b = sp.Symbol('b', real=True)
foo = ufuncify(args=[a, b], expr=a+b, backend='cython')

def no_copy(fun):
    
    def fun_n(mm):
        aa = mm[:, 0]
        bb = mm[:, 1]
        rr = fun(aa, bb)
        return rr
    
    return fun_n

print(no_copy(foo)(np.array([[0.,1.],[2.,3.],[4.,5.]])))
# returns [1. 3. 5.]

def with_copy(fun):
    
    def fun_w(mm):
        aa = np.copy(mm[:, 0])
        bb = np.copy(mm[:, 1])
        rr = fun(aa, bb)
        return rr
    
    return fun_w

print(with_copy(foo)(np.array([[0.,1.],[2.,3.],[4.,5.]])))
# returns [1. 5. 9.]

foo is simply adding a and b, while no_copy and with_copy aim to take in a matrix and add the elements of the first two columns in each row, but only with_copy gives the expected result.

I'm not familiar with how ufuncify works. It seems that without np.copy, ufuncify does not pass a sliced array to cython correctly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants