From 70ea5999b961777243e60f77407862e5dd906537 Mon Sep 17 00:00:00 2001 From: Rodolfo Ferro Date: Wed, 4 Oct 2017 12:58:24 -0500 Subject: [PATCH 1/4] Uploaded pi approximation and plotting example scripts. Updated README. --- README.md | 21 ++++++++++++++++++++- pi.py | 15 +++++++++++++++ plot_example.py | 8 ++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 pi.py create mode 100644 plot_example.py diff --git a/README.md b/README.md index 37389ba..092da91 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,25 @@ Directly prints results if run directly. May also be imported, yielding results one by one. +### Approximating *pi* + +This script is useful to show a way to approximate the value of pi using a Monte Carlo method. It is also optimized using the `@jit` (*just-in-time*) decorator from the [numba](https://numba.pydata.org/) library. + +To see different approximations you just need to modify the argument passed to the main function. + +```bash` +python pi.py +``` + + +### Plotting a function + +This script contains an example of plotting a function using [`matplotlib`](http://matplotlib.org/). Feel free to modify the value of `y` to obtain different functions that depend on `x`. + +```bash +python plot_example.py +``` + ## Release History * 0.0.1 @@ -141,4 +160,4 @@ The following people helped in creating the above content. * Kayvan Mazaheri * Lakshay Kalbhor * Pradhvan Bisht -* David Antonini \ No newline at end of file +* David Antonini diff --git a/pi.py b/pi.py new file mode 100644 index 0000000..4500089 --- /dev/null +++ b/pi.py @@ -0,0 +1,15 @@ +import numpy as np +from numba import jit + +@jit +def aprox_pi(N): + points = 2 * np.random.rand(N, 2) - 1 + M = 0 + + for k in range(N): + if points[k,0]**2 + points[k,1]**2 < 1.: + M += 1 + + return 4.*M/N + +print(aprox_pi(1e8)) diff --git a/plot_example.py b/plot_example.py new file mode 100644 index 0000000..4aec0a2 --- /dev/null +++ b/plot_example.py @@ -0,0 +1,8 @@ +import numpy as np +import matplotlib.pyplot as plt + +x = np.linspace(-3, 3, 100) +y = x**2 + +plt.plot(x, y) +plt.show() From a18a5147550aa08005c19623bce3c4bf75ae54f0 Mon Sep 17 00:00:00 2001 From: Rodolfo Ferro Date: Wed, 4 Oct 2017 12:59:49 -0500 Subject: [PATCH 2/4] Updated README. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 092da91..3355b90 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ This script is useful to show a way to approximate the value of pi using a Monte To see different approximations you just need to modify the argument passed to the main function. -```bash` +```bash python pi.py ``` From 01071b9bbdb37822e01474126f117dfcb3f377bf Mon Sep 17 00:00:00 2001 From: Rodolfo Ferro Date: Wed, 4 Oct 2017 13:02:14 -0500 Subject: [PATCH 3/4] Previous status. --- README.md | 21 +-------------------- pi.py | 15 --------------- plot_example.py | 8 -------- 3 files changed, 1 insertion(+), 43 deletions(-) delete mode 100644 pi.py delete mode 100644 plot_example.py diff --git a/README.md b/README.md index 3355b90..37389ba 100644 --- a/README.md +++ b/README.md @@ -111,25 +111,6 @@ Directly prints results if run directly. May also be imported, yielding results one by one. -### Approximating *pi* - -This script is useful to show a way to approximate the value of pi using a Monte Carlo method. It is also optimized using the `@jit` (*just-in-time*) decorator from the [numba](https://numba.pydata.org/) library. - -To see different approximations you just need to modify the argument passed to the main function. - -```bash -python pi.py -``` - - -### Plotting a function - -This script contains an example of plotting a function using [`matplotlib`](http://matplotlib.org/). Feel free to modify the value of `y` to obtain different functions that depend on `x`. - -```bash -python plot_example.py -``` - ## Release History * 0.0.1 @@ -160,4 +141,4 @@ The following people helped in creating the above content. * Kayvan Mazaheri * Lakshay Kalbhor * Pradhvan Bisht -* David Antonini +* David Antonini \ No newline at end of file diff --git a/pi.py b/pi.py deleted file mode 100644 index 4500089..0000000 --- a/pi.py +++ /dev/null @@ -1,15 +0,0 @@ -import numpy as np -from numba import jit - -@jit -def aprox_pi(N): - points = 2 * np.random.rand(N, 2) - 1 - M = 0 - - for k in range(N): - if points[k,0]**2 + points[k,1]**2 < 1.: - M += 1 - - return 4.*M/N - -print(aprox_pi(1e8)) diff --git a/plot_example.py b/plot_example.py deleted file mode 100644 index 4aec0a2..0000000 --- a/plot_example.py +++ /dev/null @@ -1,8 +0,0 @@ -import numpy as np -import matplotlib.pyplot as plt - -x = np.linspace(-3, 3, 100) -y = x**2 - -plt.plot(x, y) -plt.show() From 9a267eab821bd34ad947c9828caad75569c406b0 Mon Sep 17 00:00:00 2001 From: Rodolfo Ferro Date: Wed, 4 Oct 2017 13:04:37 -0500 Subject: [PATCH 4/4] Uploaded pi approximation and plotting example scripts. Updated README. --- README.md | 21 ++++++++++++++++++++- pi.py | 15 +++++++++++++++ plot_example.py | 8 ++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 pi.py create mode 100644 plot_example.py diff --git a/README.md b/README.md index 37389ba..3355b90 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,25 @@ Directly prints results if run directly. May also be imported, yielding results one by one. +### Approximating *pi* + +This script is useful to show a way to approximate the value of pi using a Monte Carlo method. It is also optimized using the `@jit` (*just-in-time*) decorator from the [numba](https://numba.pydata.org/) library. + +To see different approximations you just need to modify the argument passed to the main function. + +```bash +python pi.py +``` + + +### Plotting a function + +This script contains an example of plotting a function using [`matplotlib`](http://matplotlib.org/). Feel free to modify the value of `y` to obtain different functions that depend on `x`. + +```bash +python plot_example.py +``` + ## Release History * 0.0.1 @@ -141,4 +160,4 @@ The following people helped in creating the above content. * Kayvan Mazaheri * Lakshay Kalbhor * Pradhvan Bisht -* David Antonini \ No newline at end of file +* David Antonini diff --git a/pi.py b/pi.py new file mode 100644 index 0000000..4500089 --- /dev/null +++ b/pi.py @@ -0,0 +1,15 @@ +import numpy as np +from numba import jit + +@jit +def aprox_pi(N): + points = 2 * np.random.rand(N, 2) - 1 + M = 0 + + for k in range(N): + if points[k,0]**2 + points[k,1]**2 < 1.: + M += 1 + + return 4.*M/N + +print(aprox_pi(1e8)) diff --git a/plot_example.py b/plot_example.py new file mode 100644 index 0000000..4aec0a2 --- /dev/null +++ b/plot_example.py @@ -0,0 +1,8 @@ +import numpy as np +import matplotlib.pyplot as plt + +x = np.linspace(-3, 3, 100) +y = x**2 + +plt.plot(x, y) +plt.show()