Created
October 11, 2017 21:44
-
-
Save etale-cohomology/a1c0c6e3609d7c85d968d8a74ac84293 to your computer and use it in GitHub Desktop.
The fundamental theorem of calculus, in discrete version!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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