简体   繁体   中英

Call function with internal sum over 2D array

Suppose I have a python function f() that accepts 2 scalar and 1 array_like parameters:

def f(a, b, arr):
    X = a * np.exp(-arr**2 / b)
    return np.sum(a * np.log(X) - arr)

What I want to do is to evaluate f() for different values of a and b while keeping the same arr:

XX, YY = np.meshgrid(A_axis, B_axis)
arr = np.arange(10)
ZZ = np.empty_like(XX)
for i in range(XX.shape[0]):
    for j in range(YY.shape[1]):
        ZZ[i,j] = f(XX[i,j], YY[i,j], arr)

Is there a way to vectorize this? I'm thinking of converting XX, YY and arr into 3D arrays of the same shape but the np.sum() in f() will always return a scalar.

  1. Construct an open mesh from xaxis, yaxis, arr data by np.ix_()

  2. Call np.sum() with axis=-1 .

Here is the code:

import numpy as np

### original code
def f(a, b, arr):
    X = a * np.exp(-arr**2 / b)
    return np.sum(a * np.log(X) - arr)

A_axis = np.linspace(1, 5, 8)
B_axis = np.linspace(1, 2, 9)
XX, YY = np.meshgrid(A_axis, B_axis)
arr = np.arange(10)
ZZ = np.empty_like(XX)
for i in range(XX.shape[0]):
    for j in range(YY.shape[1]):
        ZZ[i,j] = f(XX[i,j], YY[i,j], arr)

### use broadcast        
def f(a, b, arr):
    X = a * np.exp(-arr**2 / b)
    return np.sum(a * np.log(X) - arr, axis=-1)
B, A, C = np.ix_(B_axis, A_axis, arr)
result = f(A, B, C)

print np.allclose(ZZ, result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM