fork download
  1. import numpy as np
  2. from scipy import integrate
  3.  
  4.  
  5. def my_integrate(func, a, b):
  6. a = np.asarray(a)
  7. b = np.asarray(b)
  8. if a.ndim == 0 and b.ndim == 0:
  9. return np.squeeze(integrate.quad(func, a, b))
  10. elif a.ndim == 0:
  11. a = np.full_like(b, a)
  12. elif b.ndim == 0:
  13. b = np.full_like(a, b)
  14. r = [integrate.quad(func, x0, x1)[0] for x0, x1 in zip(a, b)]
  15. return np.array(r)
  16.  
  17.  
  18. if __name__ == "__main__":
  19. y = lambda t: 2*t
  20. x = np.linspace(0, 3, 3)
  21. inty = my_integrate(y, np.zeros(3), x)
  22. print(inty)
  23.  
Success #stdin #stdout 0.26s 43636KB
stdin
Standard input is empty
stdout
[0.   2.25 9.  ]