Skip to content

Commit e110250

Browse files
committed
Add outline of numpy version
1 parent 02cd27b commit e110250

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

calc_pi_numpy.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""A way to estimate the value of the pi constant using NumPy."""
2+
3+
import argparse
4+
import timeit
5+
6+
import numpy as np
7+
8+
from utils import format_time
9+
10+
def point_in_circle(x, y, radius=1):
11+
"""
12+
Checks whether a point (x, y) is part of a circle with a set radius.
13+
14+
example
15+
-------
16+
>>> point_in_circle(0, 0)
17+
True
18+
19+
"""
20+
...
21+
22+
def calculate_pi_timeit(points):
23+
"""
24+
Wrapper function to build calculate_pi with a particular number of points
25+
and returns the function to be timed.
26+
"""
27+
def calculate_pi():
28+
"""
29+
Calculates an approximated value of pi by the Monte Carlo method.
30+
"""
31+
...
32+
return calculate_pi
33+
34+
35+
def command():
36+
"""
37+
entry point of the script to accept arguments
38+
"""
39+
40+
parser = argparse.ArgumentParser(description="Calculates an approximate value of PI and how long it takes using NumPy",
41+
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
42+
parser.add_argument('--npoints', '-np', default=10_000, type=int, help="Number of random points to use")
43+
parser.add_argument('--number', '-n', default=100, type=int, help="Number of times to execute the calculations")
44+
parser.add_argument('--repeat', '-r', default=5, type=int, help="How many times to repeat the timer")
45+
46+
arguments = parser.parse_args()
47+
48+
calc_pi = calculate_pi_timeit(arguments.npoints)
49+
print(f"pi = {calc_pi()} (with {arguments.npoints})")
50+
result = timeit.repeat(calc_pi, number=arguments.number, repeat=arguments.repeat)
51+
best = min(result) / arguments.number
52+
print(f"{arguments.number} loops, best of {arguments.repeat}: {format_time(best)} per loop")
53+
54+
55+
if __name__ == '__main__':
56+
command()

0 commit comments

Comments
 (0)