<jupyter_start><jupyter_text>CDES Honours - Lab 0 Instructions* Read all the instructions carefully.* Do not rename the notebook, simply answer the questions and resubmit the file to Moodle.* **Numpy** has a help file for every function if you get stuck. See: https://docs.scipy.org/doc/numpy-1.15.4/reference/* See these useful links:    * https://docs.scipy.org/doc/numpy/user/numpy-for-matlab-users.html    * https://docs.scipy.org/doc/numpy/user/quickstart.html* **Numpy** is not always required.* There are also numerous sources available on the internet, Google is your friend!  Warm-up ExercisesComplete the following warm-up tasks without the use of numpy. Question 1Given an array of integers, write a function to compute the sum of its elements. You may not use the `sum` function.<jupyter_code># Question 1
def simpleArraySum(ar):
    # YOUR CODE HERE
    raise NotImplementedError()

# Run this test cell to check your code
# Do not delete this cell
# 1 mark
# Unit test
test = [1, 2, 3 , 4, 10, 11]
assert(simpleArraySum(test) == 31)
print('Test case passed!!!')
# Hidden test
# No output will be produced
# 4 marks<jupyter_output><empty_output><jupyter_text>Question 2Write a function that takes as inputs, a word (as a string) and an array of letter heights. The function must compute the area required to highlight the entire word. For example, if the string is `'abc'`, and the specific heights of `a`, `b` and `c` are 1, 3, and 1 mm respectively - then the background area to highlight the entire string is the length of the string times the maximum height, therefore $3 \times 3 = 9$mm.<jupyter_code>def highlighter(h, word):
    # YOUR CODE HERE
    raise NotImplementedError()


# Run this test cell to check your code
# Do not delete this cell
# 1 mark
# Unit test
test = 'abc'
h    = [1, 3, 1, 3, 1, 4, 1, 3, 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7]
assert(highlighter(h, test) == 9)
print('Test case passed!!!')
# Hidden test
# No output will be produced
# 9 marks<jupyter_output><empty_output><jupyter_text>Question 3Write a function that takes in an array, `ar`, of $n$ integers and a positive integer `k`. Find the number of $(i, j)$ pairs where $i < j$ and $ar(i) + ar(j)$ is divisible by `k`. The function should return the answer as a scalar.<jupyter_code>def divisibleSumPairs(k, ar):
    # YOUR CODE HERE
    raise NotImplementedError()

# Run this test cell to check your code
# Do not delete this cell
# 1 mark
# Unit test
k    = 3
test = [1, 3, 2, 6, 1, 2]
assert(divisibleSumPairs(k, test) == 5)
print('Test case passed!!!')
# Run this test cell to check your code
# Do not delete this cell
# 1 mark
# Unit test
k    = 3
test = [1, 3, 2, 6, 1, 2, 5, 9, 4]
assert(divisibleSumPairs(k, test) == 12)
print('Test case passed!!!')
# Hidden test
# No output will be produced
# 4 marks
# Hidden test
# No output will be produced
# 4 marks<jupyter_output><empty_output><jupyter_text>Main ExercisesComplete the following problems: Question 1Write a function implements a finite difference scheme to solve the heat equation. Specifically, the function should implement an explicit scheme which is forward difference in time and central difference in space. It should take as inputs a time step `dt`, a spatial step `dx`, a number of iterations to march forward in time `N`, the coeffient `D`, an initial function `f(x)` passed as a handle and boundary values `alpha` and `beta`. The function should output the solution space matrix `u`. Recall the heat equation problem as:$$u_{t} = Du_{xx},\ \ \quad u(0, t) = \alpha, \ \ u(1, t) = \beta,\ \ \qquad u(x, 0) = f(x) $$Note: the solution matrix here is matrix of all $x$ through time $t$. That is row one is at time zero.<jupyter_code>def heat_eq(dt, dx, N, f, D, alpha, beta):
    # YOUR CODE HERE
    raise NotImplementedError()

# Run this test cell to check your code
# Do not delete this cell
# 2 mark
# Unit test
dt    = 0.0013
dx    = 0.05
N     = 50
f     = lambda x: np.sin(np.pi*x)
D     = 1
alpha = 0
beta  = 0
ans   = heat_eq(dt, dx, N, f, D, alpha, beta)
t1    = np.array([0.      , 0.156434, 0.309017, 0.45399 , 0.587785, 0.707107,
       0.809017, 0.891007, 0.951057, 0.987688, 1.      , 0.987688,
       0.951057, 0.891007, 0.809017, 0.707107, 0.587785, 0.45399 ,
       0.309017, 0.156434, 0.      ])
t2    = np.array([0.      , 0.082129, 0.162237, 0.238349, 0.308592, 0.371237,
       0.424741, 0.467786, 0.499313, 0.518545, 0.525009, 0.518545,
       0.499313, 0.467786, 0.424741, 0.371237, 0.308592, 0.238349,
       0.162237, 0.082129, 0.      ])
nt.assert_array_almost_equal(ans[0, ::], t1, 5)
nt.assert_array_almost_equal(ans[-1, ::], t2, 5)
print('Test case passed!!!')
# Hidden test
# No output will be produced
# 13 marks<jupyter_output><empty_output><jupyter_text>Question 2Consider the differential equation given below:$$\frac{d^{2} u}{d x^{2}}+\frac{2}{x} \frac{d u}{d x}-\frac{2}{x^{2}} u-\sin (\log x)=0, \qquad a<x<b,\quad u(a)=\alpha \text { and } u(b)=\beta.$$Write a function which implements a finite difference scheme which is central difference in space. The function should return the final approximation `u`.<jupyter_code>def ode(a, b, dx, alpha, beta):
    # YOUR CODE HERE
    raise NotImplementedError()
# Run this test cell to check your code
# Do not delete this cell
# 2 mark
# Unit test
a     = 1
b     = 2
dx    = 1/10
alpha = 1
beta  = 2
nt.assert_array_almost_equal(np.array([1.       , 1.0843325, 1.1719112, 1.2627144, 1.35683  , 1.4544006,
                                       1.5555929, 1.6605808, 1.7695365, 1.8826246, 2.       ]), ode(a, b, dx, alpha, beta))
print('Test case passed!!!')
# Hidden test
# No output will be produced
# 13 marks<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12193841847445766, 'fraction_numerical': 0.08030090972708187, 'mean_word_length': 3.610483870967742, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 21, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '329935', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2272, 'n_tokens_neox': 1930, 'n_words': 771, 'path': '2019/release/Lab0/.ipynb_checkpoints/lab0-checkpoint.ipynb', 'repo_name': 'mattw89/APPM4057-Computational-Differential-Equations', 'size': '5716'}