numpy中的frompyfunc用法

通过frompyfunc()可以将计算单个值的函数转换为能对数组的每个元素进行计算的ufunc函数,其调用格式为

frompyfunc(func, nin, nout)

func是计算单个元素的函数,nin是func输入参数的个数,nout是func的返回值的个数。

实例
import numpy as np
import matplotlib.pyplot as plt

def y(t):
    if t <= -1 / 2:
        r = -3 * t + 1
    elif t >= 2:
        r = 3 * t - 1
    else:
        r = 3 + t
    return r


x = np.linspace(-5, 6, 1000)
triangle_ufunc1 = np.frompyfunc(y,1,1)
y = triangle_ufunc1(x)

plt.plot(x, y)
plt.show()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44864262/article/details/107788411
今日推荐