python - Recombine numpy arrays via original boolean index - Stack Overflow

I have been working with numpy arrays that require a specific carve, due to a rare area in scipy that d

I have been working with numpy arrays that require a specific carve, due to a rare area in scipy that doesn't work as planned.

Therefore, I need to use mpmath for that area, and then I want to recombine the array for further operations that depend upon the original array position.

import mpmath as mp
from scipy.special import hyp2f1 as sc_hyp2f1


def carve(z):
    """Scipy hyp2f1 cannot deal with a specific region."""
    return (np.abs(z) > 0.9) & (np.abs(z) < 1.1) & (np.abs(1 - z) >= 0.9) & (z.real >= 0)

def hf2(z, a, b, c):
    """Wrapper to handle hyp2f1"""
    mp_hyp2f1 = np.frompyfunc(mp.hyp2f1, 4, 1)
    f_idx = carve(z)
    zm = z[f_idx]   # values for mpmath (slow!)
    zs = z[~f_idx]  # values for scipy (fast!)
    ma = mp_hyp2f1(a, b, c, zm).astype(npplex128)
    sa = sc_hyp2f1(a, b, c, zs)
    return np.hstack([sa, ma])  # Eep! All the values have been moved!

I have left out the (minimal) logic that avoids attempting to evaluate empty areas.

Hmm I think I have the answer to this..

    ....
    ans = np.zeros_like(z)
    ans[~f_idx] = sa
    ans[f_idx] = ma
    return ans

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743757510a4502024.html

相关推荐

  • python - Recombine numpy arrays via original boolean index - Stack Overflow

    I have been working with numpy arrays that require a specific carve, due to a rare area in scipy that d

    6天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信