Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4033356
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:50:17+00:00 2026-05-20T11:50:17+00:00

Let’s say we have a particularly simple function like import scipy as sp def

  • 0

Let’s say we have a particularly simple function like

import scipy as sp
def func(x, y):
   return x + y

This function evidently works for several builtin python datatypes of x and y like string, list, int, float, array, etc. Since we are particularly interested in arrays, we consider two arrays:

x = sp.array([-2, -1, 0, 1, 2])
y = sp.array([-2, -1, 0, 1, 2])

xx = x[:, sp.newaxis]
yy = y[sp.newaxis, :]

>>> func(xx, yy)

this returns

array([[-4, -3, -2, -1,  0],
  [-3, -2, -1,  0,  1],
  [-2, -1,  0,  1,  2],
  [-1,  0,  1,  2,  3],
  [ 0,  1,  2,  3,  4]])

just as we would expect.

Now what if one wants to throw in arrays as the inputs for the following function?

def func2(x, y):
  if x > y:
     return x + y
  else:
     return x - y

doing >>>func(xx, yy) would raise an error.

The first obvious method that one would come up with is the sp.vectorize function in scipy/numpy. This method, nevertheless has been proved to be not very efficient. Can anyone think of a more robust way of broadcasting any function in general on to numpy arrays?

If re-writing the code in an array-friendly fashion is the only way, it would help if you could mention it here too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:50:17+00:00Added an answer on May 20, 2026 at 11:50 am

    np.vectorize is a general way to convert Python functions that operate on numbers into numpy functions that operate on ndarrays.

    However, as you point out, it isn’t very fast, since it is using a Python loop “under the hood”.

    To achieve better speed, you have to hand-craft a function that expects numpy arrays as input and takes advantage of that numpy-ness:

    import numpy as np
    
    def func2(x, y):
        return np.where(x>y,x+y,x-y)      
    
    x = np.array([-2, -1, 0, 1, 2])
    y = np.array([-2, -1, 0, 1, 2])
    
    xx = x[:, np.newaxis]
    yy = y[np.newaxis, :]
    
    print(func2(xx, yy))
    # [[ 0 -1 -2 -3 -4]
    #  [-3  0 -1 -2 -3]
    #  [-2 -1  0 -1 -2]
    #  [-1  0  1  0 -1]
    #  [ 0  1  2  3  0]]
    

    Regarding performance:

    test.py:

    import numpy as np
    
    def func2a(x, y):
        return np.where(x>y,x+y,x-y)      
    
    def func2b(x, y):
        ind=x>y
        z=np.empty(ind.shape,dtype=x.dtype)
        z[ind]=(x+y)[ind]
        z[~ind]=(x-y)[~ind]
        return z
    
    def func2c(x, y):
        # x, y= x[:, None], y[None, :]
        A, L= x+ y, x<= y
        A[L]= (x- y)[L]
        return A
    
    N=40
    x = np.random.random(N)
    y = np.random.random(N)
    
    xx = x[:, np.newaxis]
    yy = y[np.newaxis, :]
    

    Running:

    With N=30:

    % python -mtimeit -s'import test' 'test.func2a(test.xx,test.yy)'
    1000 loops, best of 3: 219 usec per loop
    
    % python -mtimeit -s'import test' 'test.func2b(test.xx,test.yy)'
    1000 loops, best of 3: 488 usec per loop
    
    % python -mtimeit -s'import test' 'test.func2c(test.xx,test.yy)'
    1000 loops, best of 3: 248 usec per loop
    

    With N=1000:

    % python -mtimeit -s'import test' 'test.func2a(test.xx,test.yy)'
    10 loops, best of 3: 93.7 msec per loop
    
    % python -mtimeit -s'import test' 'test.func2b(test.xx,test.yy)'
    10 loops, best of 3: 367 msec per loop
    
    % python -mtimeit -s'import test' 'test.func2c(test.xx,test.yy)'
    10 loops, best of 3: 186 msec per loop
    

    This seems to suggest that func2a is slightly faster than func2c (and func2b is horribly slow).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a class like this: public class Person { private String
Let's say I have a table that looks something like this: ------------------------------- id|column2|column3 |column4
Let's say that I have an arbitrary string like `A man + a plan
Let's say I have a link in a table like: <td class=ms-vb width=100%> <a
Let's say I have this: public DefaultListModel model = new DefaultListModel(); how do i
Let presume we have something like this: <div1> <h1>text1</h1> <h1>text2</h1> </div1> <div2> <h1>text3</h1> </div2>
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.