Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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