计算阶跃函数的总和 [英] Evaluate sum of step functions

查看:84
本文介绍了计算阶跃函数的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有相当多的(大约 1000 个)阶跃函数,每个函数只有两个区间.我想总结一下,然后找到最大值.做这个的最好方式是什么?我已经尝试过 sympy,代码如下:

from sympy import Piecewise,piecewise_fold, evalf从 sympy.abc 导入 x从 sympy.plotting 导入 *将 numpy 导入为 npS = 20t = np.random.random(20)sum_piecewise = 无对于范围内的 s(S):p = 分段((np.random.random(), x=t[s]))如果不是 sum_piecewise:sum_piecewise = p别的:sum_piecewise += p打印 sum_piecewise.evalf(0.2)

然而,这会输出一个很大的符号表达式而不是实际值,这正是我想要的.

解决方案

当您考虑数值函数时,使用 Numpy 会更好(在性能方面).这是一种方法:

将 numpy 导入为 np导入 matplotlib.pyplot 作为 pltnp.random.seed(10)S = 20 # 分段函数的数量# 生成S函数参数.# 例如,第k个函数定义为等于# p_values[k,0] 当 t= t_values[k]t_values = np.random.random(S)p_values = np.random.random((S,2))# 给定函数参数定义分段函数def p_func(t, t0, p0):返回 np.piecewise(t, [t < t0, t >= t0], p0)# 定义一个函数,它对一组对应的分段函数求和# 参数数组 t_values 和 p_valuesdef p_sum(t, t_values, p_values):return np.sum([p_func(t, t0, p0) for t0, p0 in zip(t_values,p_values)])

这是函数总和的图:

t_range = np.linspace(0,1,1000)plt.plot(t_range, [p_sum(tt,t_values,p_values) for tt in t_range])

显然,为了找到最大值,只考虑包含在 t_values 中的 S 时刻就足够了.对于这个例子,

np.max([p_sum(tt,t_values,p_values) for tt in t_values])

<块引用>

11.945901591934897

I have a fairly large number (around 1000) of step functions, each with only two intervals. I'd like to sum them up and then find the maximum value. What is the best way to do this? I've tried out sympy, with code as follows:

from sympy import Piecewise, piecewise_fold, evalf 
from sympy.abc import x
from sympy.plotting import *
import numpy as np

S = 20

t = np.random.random(20)

sum_piecewise = None

for s in range(S):
    p = Piecewise((np.random.random(), x<t[s]), (np.random.random(), x>=t[s]))
    if not sum_piecewise:
        sum_piecewise = p 
    else:
        sum_piecewise += p

print sum_piecewise.evalf(0.2)

However, this outputs a large symbolic expression and not an actual value, which is what I want.

解决方案

As it appears that you consider numerical functions, it is better (in terms of performance) to work with Numpy. Here's one approach:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(10)
S = 20 # number of piecewise functions

# generate S function parameters. 
# For example, the k-th function is defined as equal to 
# p_values[k,0] when t<t_values[k] and equal to
# p_values[k,1] when t>= t_values[k]
t_values = np.random.random(S)
p_values = np.random.random((S,2))

# define a piecewise function given the function's parameters
def p_func(t, t0, p0):
    return np.piecewise(t, [t < t0, t >= t0], p0)

# define a function that sums a set of piecewise functions corresponding to
# parameter arrays t_values and p_values
def p_sum(t, t_values, p_values):
    return np.sum([p_func(t, t0, p0) for t0, p0 in zip(t_values,p_values)])

Here is the plot of the sum of functions:

t_range = np.linspace(0,1,1000)
plt.plot(t_range, [p_sum(tt,t_values,p_values) for tt in t_range])

Clearly, in order to find the maximum, it suffices to consider only the S time instants contained in t_values. For this example,

np.max([p_sum(tt,t_values,p_values) for tt in t_values])

11.945901591934897

这篇关于计算阶跃函数的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆