[Python] How to write a vector function to apply operation f(x,y)?

Discussão em 'Python' iniciado por Stack, Fevereiro 11, 2025.

  1. Stack

    Stack Membro Participativo


    scalar_function can only handle scalar input, we could use the function np.vectorize() turn it into a vectorized function. Note that the input argument of np.vectorize() should be a scalar function, and the output of np.vectorize() is a new function that can handle vector input.

    Please write a vector function vector_function, which will apply the operation (,) defined above element-wisely with input vectors with same dimension x and y.
    ​

    So for the scalar, I got :

    def scalar_function(x, y):

    if x <= y:
    return x*y
    else:
    return x/y


    For the vector function I have :

    def vector_function(x, y):

    vfunc = np.vectorize(scalar_function, otypes = [float])
    return vfunc


    From here on I am stuck.

    Continue reading...

Compartilhe esta Página