Skip to content

Commit f2c6503

Browse files
committed
updates
1 parent ff43ce6 commit f2c6503

1 file changed

Lines changed: 11 additions & 10 deletions

File tree

lectures/numba.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,17 @@ For GPU-based parallelization, see our {doc}`lectures on JAX <jax_intro>`.
436436

437437
## Exercises
438438

439+
{ref}`speed_ex1` and {ref}`numba_ex3` both estimate $\pi$ by Monte Carlo from random samples in the unit square.
440+
441+
We generate them here and store them in `u_draws` and `v_draws` so that we can use them in both exercises and compare results
442+
443+
```{code-cell} ipython3
444+
n = 1_000_000
445+
rng = np.random.default_rng()
446+
u_draws = rng.uniform(size=n)
447+
v_draws = rng.uniform(size=n)
448+
```
449+
439450
```{exercise}
440451
:label: speed_ex1
441452
@@ -454,11 +465,6 @@ Compare speed with and without Numba when the sample size is large.
454465
Here is one solution:
455466

456467
```{code-cell} ipython3
457-
n = 1_000_000
458-
rng = np.random.default_rng()
459-
u_draws = rng.uniform(size=n)
460-
v_draws = rng.uniform(size=n)
461-
462468
@jit
463469
def calculate_pi(u_draws, v_draws):
464470
n = len(u_draws)
@@ -686,11 +692,6 @@ For the size of the Monte Carlo simulation, use something substantial, such as
686692
Here is one solution:
687693

688694
```{code-cell} ipython3
689-
n = 1_000_000
690-
rng = np.random.default_rng()
691-
u_draws = rng.uniform(size=n)
692-
v_draws = rng.uniform(size=n)
693-
694695
@jit(parallel=True)
695696
def calculate_pi(u_draws, v_draws):
696697
n = len(u_draws)

0 commit comments

Comments
 (0)