如何让我的python集成更快?

 武艺最新单曲问月09 发布于 2023-02-11 19:31

嗨,我想整合一个从0到几个不同上限(大约1000)的函数.我已经使用for循环编写了一段代码来执行此操作,并将每个值附加到空数组.然而,我意识到我可以通过做更小的积分然后将前一个积分结果添加到刚刚计算的结果来使代码更快.所以我会做相同数量的积分,但是在更小的间隔内,然后只需添加前一个积分就可以得到从0到上限的积分.这是我的代码:

import numpy as np                              #importing all relevant modules and functions
from scipy.integrate import quad
import pylab as plt
import datetime
t0=datetime.datetime.now()                      #initial time
num=np.linspace(0,10,num=1000)                  #setting up array of values for t
Lt=np.array([])                                 #empty array that values for L(t) are appended to
def L(t):                                       #defining function for L
    return np.cos(2*np.pi*t)
for g in num:                                   #setting up for loop to do integrals for L at the different values for t
    Lval,x=quad(L,0,g)                          #using the quad function to get the values for L. quad takes the function, where to start the integral from, where to end the integration
    Lv=np.append(Lv,[Lval])                     #appending the different values for L at different values for t

我建议的优化技术需要做哪些更改?

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有