IT代写 COMPSCI 369 Lab 1

COMPSCI 369 Lab 1
Introduction to scientific computing in Python Cellular Automata

Tutor contact details

Copyright By PowCoder代写 加微信 powcoder

Arthur-Tomes

● Notebook environment
● Mix code and markdown cells to produce ‘dynamic documents’
● Code execution order is important
● Mainly for Python but has some support for other languages
● Runs in browser on local web server (local version)
● Can also run on remote cloud servers (cloud version)

Option 1: JupyterLab local install (recommended)
(pip install jupyterlab)
● Install JupyterLab
● Install matplotlib and numpy (pip install matplotlib; pip install numpy)
● Starting a notebook
○ Open terminal / powershell, type the following
○ > jupyter-lab
○ Go to localhost:8888/tree on your browser
● Closing a notebook
○ ctrl+c from terminal / powershell

Option 2: Jupyter cloud version
● If you have not installed Jupyter, you can use the cloud version ○ https://jupyter.org/try
○ Select “JupyterLab”
● Upload your files to the cloud
○ Click the upload button
○ Upload “Lab 1 – 1D Cellular automata.ipynb”
○ Create a folder called “img”, and upload images from lab1_1D_CA__v3/img/
● Download your files before exiting the browser!

Notebook shortcuts
● Shortcuts are shown as grey boxes in the Notebook Menu
● Insert cell above — ‘a’
● Insert cell below — ‘b’
● Delete cell — double press ‘d’
● Run cell — ‘ctrl + enter’
Can change cell type from dropdown menu (e.g. code, markdown, heading)

Python scientific ecosystem
○ Numerical operations
○ Specialised maths/stats/science functionality
● Matplotlib
○ Visualisation
○ Pandas: data frames
○ statsmodels
○ SciKit Learn: machine learning
○ Tensorflow/PyTorch/JAX: computational graph/autodiff/deep learning

Introduction to Cellular Automata (CA)
● The state of a Cellular Automata is a vector of Boolean values of length N
● We use a rule to describe how the system changes over time
● For elementary CA, the state of the next cell depends on its previous state, and the state of its two neighbours.

Cellular Automata update rules
00000010 is the Binary of 2

Cellular Automata update rules
𝑥𝑡,𝑝 = 𝑓(𝑥 𝑡−1,𝑝−1 , 𝑥𝑡−1,𝑝 , 𝑥𝑡−1,𝑝+1 )
111 110 101 100 011 010 001 000
0 1 0 1 1 0 1 0 =Rule90
https://www.rapidtables.com/convert/number/decimal- to-binary.html
Example trajectory POSITION
N=7 Row 0 Row 1 Row 2

Example of different CAs
● Cellular automata can generate a variety of different patterns

Some useful functions
● Decimal to Binary converter
○ https://www.rapidtables.com/convert/number/decimal-to-binary.html
● Generating a random initial vector
○ https://numpy.org/doc/stable/reference/generated/numpy.zeros.html
○ https://numpy.org/doc/stable/reference/random/generated/numpy.random.rand.html
import numpy as np
y = np.zeros((600, 500)) # generates a 600 x 500 matrix (rows x cols)
y[0, :] = np.random.rand(1, 500) # fills Row 0 with 500 random values between [0, 1) # still need to map these numbers to 0’s and 1’s
● Plot trajectories
from matplotlib import pyplot as plt plt.imshow(y, interpolation=“none”)

Asking for help
● Ask me on Zoom, or send me a chat message
○ We can share screen with class, and discuss together☺
○ Or 1-on-1 help in a private zoom room
● In the last 10 minutes, I will go through answers with the class
● Lab sample solutions will be available on the course page on the following Monday

Free coding time (see below for extra resources)

NumPy arrays
● Tutorial
● Abstraction for number objects ○ Vector
○ Higher dimensions
● Wraps native implementation
○ >10x times performance improvement
● Broadcasting

NumPy programming style
● Use vectorised operations where possible
○ Avoid for loops
○ Math operations
○ Fancy indexing
● Suggestion: Start writing code how you’re used to, then adapt to NumPy style

Matplotlib
● Visualisation
● A bit of a dark art! Look at the examples.
○ https://matplotlib.org/tutorials/introductory/sample_plots.html ○ https://matplotlib.org/3.1.1/gallery/index.html
● Very rich
○ Animation
○ 3D plots
● Integrates well with Jupyter ○ Magic commands
○ %matplotlib inline
○ %matplotlib notebook

Skeleton code for Task 1
import numpy as np
from matplotlib import pyplot as plt
# update each row in the trajectory starting from Row 1
def iterate(y):
# define the cellular automata update rules
def nextVal(neighbors):
# setup initial empty trajectory
cols = 500 # N = 500 (cols)
its = 600 # number of iterations (rows) y = np.zeros((its, cols))
# initial state vector with a single 1 at center
y[0,:] = np.zeros((1, cols)) # generate a single row of 0’s y[0, int(cols/2)] = 1 # update cell at [0, 250] to value 1

More links on Cellular Automata
● https://en.wikipedia.org/wiki/Elementary_cellular_automaton#Random_initial_state ● https://en.wikipedia.org/wiki/Rule_90

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com