Skip to content

Instantly share code, notes, and snippets.

@etale-cohomology
Created October 11, 2017 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etale-cohomology/a1c0c6e3609d7c85d968d8a74ac84293 to your computer and use it in GitHub Desktop.
Save etale-cohomology/a1c0c6e3609d7c85d968d8a74ac84293 to your computer and use it in GitHub Desktop.
The fundamental theorem of calculus, in discrete version!
# The fundamental theorem of calculus, in discrete version!
import numpy as np
np.set_printoptions(linewidth=200, precision=2)
print('This Python snippet shows how the sum of many differences is one difference of endpoinds!')
print('You can consider this the "discrete fundamental theorem of calculus"!')
# ----------------------------------------------------------------------
N_VALUES = 16
vec_a = 10 * np.random.rand(N_VALUES)
diff_vec_a = np.diff(vec_a) # This is the "discrete derivative"
endpoints_vec_a = np.array([vec_a[0], vec_a[-1]])
sum_diff_vec_a = np.sum(diff_vec_a) # This is the "discrete (definite) integral"
endpoints_diff_vec_a = endpoints_vec_a[1] - endpoints_vec_a[0] # This is like taking a difference of the antiderivative!
TEMPLATE_STR = '{:24} {}'
print(TEMPLATE_STR.format('vec_a', vec_a))
print(TEMPLATE_STR.format('diff_vec_a', diff_vec_a))
print(TEMPLATE_STR.format('endpoints_vec_a', endpoints_vec_a))
print(TEMPLATE_STR.format('sum_diff_vec_a', sum_diff_vec_a))
print(TEMPLATE_STR.format('endpoints_diff_vec_a', endpoints_diff_vec_a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment