Skip to main content
edited tags
Link
Mast
  • 13.9k
  • 12
  • 57
  • 128
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Source Link
Pii
  • 303
  • 1
  • 7

Python numerical integration

Could the time complexity of this definite integral algorithm be improved?

import numpy as np def integral(f, left, right, epsilon): w = int((right-left)/epsilon) domain = np.linspace(left, right, w) heights = [f(((a+b)*0.5)) for a, b in zip(domain[:-1], domain[1:])] return sum(heights)*epsilon # Test an example print('Result:', integral(lambda x: np.sin(x), 1, 10, 0.01))