SHARE
TWEET

Untitled

a guest Oct 12th, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # The fundamental theorem of calculus, in discrete version!
  2.  
  3. import numpy as np
  4. np.set_printoptions(linewidth=200, precision=2)
  5.  
  6. print('This Python snippet shows how the sum of many differences is one difference of endpoinds!')
  7. print('You can consider this the "discrete fundamental theorem of calculus"!')
  8.  
  9. # ----------------------------------------------------------------------
  10. N_VALUES = 16
  11. vec_a = 10 * np.random.rand(N_VALUES)
  12. diff_vec_a = np.diff(vec_a)  # This is the "discrete derivative"
  13. endpoints_vec_a = np.array([vec_a[0], vec_a[-1]])
  14. sum_diff_vec_a = np.sum(diff_vec_a)  # This is the "discrete (definite) integral"
  15. endpoints_diff_vec_a = endpoints_vec_a[1] - endpoints_vec_a[0]  # This is like taking a difference of the antiderivative!
  16.  
  17. TEMPLATE_STR = '{:24}  {}'
  18. print(TEMPLATE_STR.format('vec_a', vec_a))
  19. print(TEMPLATE_STR.format('diff_vec_a', diff_vec_a))
  20. print(TEMPLATE_STR.format('endpoints_vec_a', endpoints_vec_a))
  21. print(TEMPLATE_STR.format('sum_diff_vec_a', sum_diff_vec_a))
  22. print(TEMPLATE_STR.format('endpoints_diff_vec_a', endpoints_diff_vec_a))
RAW Paste Data
Top