marcinofulus / jupyter4edu

GNU General Public License v3.0
3 stars 7 forks source link

Augsburg exercises #33

Open gertingold opened 5 years ago

gertingold commented 5 years ago
marcinofulus commented 5 years ago

@BirthdayProblem.ipynb

One can add a test with analytical solution, e.g. this after a plot will add nice spot: from math import factorial f = lambda n:factorial(365)/(365*nfactorial(365-n)) plt.plot([11],1-f(11),'ro')

gertingold commented 5 years ago

Thanks for the suggestion. I will be absent for the rest of the week but I will consider your proposal next week.

marcinofulus commented 4 years ago

@JuliaSet.ipynb

Julia is relatively slow as it uses intepretes Python inside innermost loop, having this type of code i.e. with all loops over scalars, there is a nice trick to use numba:


import numba
from numba import prange

@numba.njit(parallel=True)
def compute_julia(ndim=1000, maxiter = 1000):
    julia_data = np.zeros(ndim**2)
    dx = (xmax-xmin)/(ndim-1)
    dy = (ymax-ymin)/(ndim-1)
    for ny in prange(ndim):
        y = ymin+ny*dy
        for nx in range(ndim):
            x = xmin+nx*dx
            z = complex(x, y)
            niter = 0
            while niter < maxiter and abs(z) <= threshold:
                niter = niter+1
                z = z**2 + julia_c
            color = niter/maxiter
            julia_data[ny*ndim+nx] = color
    return julia_data

I am not sure if it is the most efficient solution i.e. if type inferer does really good job, but it looks like doing ~1e9 loops/s on single core which means that it might be close.

Interestingly, with numba it is a good idea to write all loops down to scalars in Python.

gertingold commented 4 years ago

This is correct. However, the use of Numba does not fit the use case of an introductory programming course. Therefore, I prefer not to talk about Numba in this context. In advanced courses, it is one of the topics I am teaching, though.

And of course you are right that with Numba one goes back to a "Fortran style" of programming which can be good or bad dependent on one's taste.

marcinofulus commented 4 years ago

3d visualization of 2d GameOfLife can be done with by plotting all history of iterations.

The code below makes it, but there is one modification needed to "update" method - namely - it should not used uninitialized self.im.

import k3d
c = Conway(100)
nsteps =  150
xyt = np.zeros( (nsteps,c.size,c.size),dtype=np.uint8)

for i in range(nsteps):
    xyt[i,...] = c.world.astype(np.uint8)
    c.update()

plot = k3d.plot(name='gameoflife', grid_visible=False)
plt_vox = k3d.voxels(xyt,outlines=True) #outlines=False  for better performance
plot += plt_vox
plot
gertingold commented 4 years ago

As I mentioned in the comments of #49, this would nicely fit in a separate demo folder where one could mention that k3d is used in other parts of the project (notebooks on general relativity) and that the topic "game of life" is treated in the exercises.

If you find such a demo folder a good idea, I could put a readme in the general relativity folder explaining the interest of k3d and making a link to the demo folder.

marcinofulus commented 4 years ago

I perfectly agree with the demo folder idea.