seamsay.xyz Musings on science, software, and cooking.

Data Array Worked Examples

When learning about new topics in maths or related fields, by far the best approach is practice solving problems yourself. In my introduction to multidimensional arrays I set a few little problems that I wanted you to have a go at. On this page I will provide some answers, as well as a method for generating new problems that you can tackle!

Linear Index Conversion

def get_array_element(data, size, cartesian_indices):
    linear_index = 0
    for i, cartesian_index in enumerate(cartesian_indices):
        section_size = np.prod(size[i + 1:])
        linear_index += section_size * cartesian_index

    return data[linear_index]

Data Array Practice

array = np.zeros((2, 4, 3))
array[:, :, 0] = [
    [0.7, 0.9, 0.2, 0.6],
    [0.3, 0.0, 0.2, 0.7],
]
array[:, :, 1] = [
    [0.1, 0.5, 0.5, 0.2],
    [0.0, 0.4, 0.7, 0.8],
]
array[:, :, 2] = [
    [0.8, 0.8, 0.1, 0.4],
    [0.4, 0.9, 0.8, 0.7],
]
\[ \begin{aligned}A_{i,j,0} & = { \begin{pmatrix} 0.7 & 0.9 & 0.2 & 0.6 \\ 0.3 & 0.0 & 0.2 & 0.7 \end{pmatrix} }_{i,j} \\ A_{i,j,1} & = { \begin{pmatrix} 0.1 & 0.5 & 0.5 & 0.2 \\ 0.4 & 0.9 & 0.7 & 0.8 \end{pmatrix} }_{i,j} \\ A_{i,j,2} & = { \begin{pmatrix} 0.8 & 0.8 & 0.1 & 0.4 \\ 0.4 & 0.9 & 0.8 & 0.7 \end{pmatrix} }_{i,j}\end{aligned} \] \[ \mathbf{d} = ( \underset{i = 0}{\underbrace{ \underset{j = 0}{\underbrace{\underset{0}{0.7},\, \underset{1}{0.1},\, \underset{2}{0.8}}},\: \underset{j = 1}{\underbrace{\underset{0}{0.9},\, \underset{1}{0.5},\, \underset{2}{0.8}}},\: \underset{j = 2}{\underbrace{\underset{0}{0.2},\, \underset{1}{0.5},\, \underset{2}{0.1}}},\: \underset{j = 3}{\underbrace{\underset{0}{0.6},\, \underset{1}{0.2},\, \underset{2}{0.4}}} }},\; \underset{i = 1}{\underbrace{ \underset{j = 0}{\underbrace{\underset{0}{0.3},\, \underset{1}{0.0},\, \underset{2}{0.4}}},\: \underset{j = 1}{\underbrace{\underset{0}{0.0},\, \underset{1}{0.4},\, \underset{2}{0.9}}},\: \underset{j = 2}{\underbrace{\underset{0}{0.2},\, \underset{1}{0.7},\, \underset{2}{0.8}}},\: \underset{j = 3}{\underbrace{\underset{0}{0.7},\, \underset{1}{0.8},\, \underset{2}{0.7}}} }} ) \]
array = np.zeros((2, 4, 3))
array[:, 0, :] = [
    [0.4, 0.9, 0.1],
    [0.0, 0.4, 0.4],
]
array[:, 1, :] = [
    [0.3, 0.9, 0.1],
    [0.7, 0.8, 0.1],
]
array[:, 2, :] = [
    [0.5, 0.9, 0.4],
    [1.0, 0.4, 0.5],
]
array[:, 3, :] = [
    [0.2, 0.7, 0.3],
    [0.0, 0.9, 0.7],
]
\[ \begin{aligned}A_{i,0,k} & = { \begin{pmatrix} 0.4 & 0.9 & 0.1 \\ 0.0 & 0.4 & 0.4 \end{pmatrix} }_{i,k} \\ A_{i,1,k} & = { \begin{pmatrix} 0.3 & 0.9 & 0.1 \\ 0.7 & 0.8 & 0.1 \end{pmatrix} }_{i,k} \\ A_{i,2,k} & = { \begin{pmatrix} 0.5 & 0.9 & 0.4 \\ 1.0 & 0.4 & 0.5 \end{pmatrix} }_{i,k} \\ A_{i,3,k} & = { \begin{pmatrix} 0.2 & 0.7 & 0.3 \\ 0.0 & 0.9 & 0.7 \end{pmatrix} }_{i,k}\end{aligned} \] \[ \mathbf{d} = ( \underset{i = 0}{\underbrace{ \underset{j = 0}{\underbrace{\underset{0}{0.4},\, \underset{1}{0.9},\, \underset{2}{0.1}}},\: \underset{j = 1}{\underbrace{\underset{0}{0.3},\, \underset{1}{0.9},\, \underset{2}{0.1}}},\: \underset{j = 2}{\underbrace{\underset{0}{0.5},\, \underset{1}{0.9},\, \underset{2}{0.4}}},\: \underset{j = 3}{\underbrace{\underset{0}{0.2},\, \underset{1}{0.7},\, \underset{2}{0.3}}} }},\; \underset{i = 1}{\underbrace{ \underset{j = 0}{\underbrace{\underset{0}{0.0},\, \underset{1}{0.4},\, \underset{2}{0.4}}},\: \underset{j = 1}{\underbrace{\underset{0}{0.7},\, \underset{1}{0.8},\, \underset{2}{0.1}}},\: \underset{j = 2}{\underbrace{\underset{0}{1.0},\, \underset{1}{0.4},\, \underset{2}{0.5}}},\: \underset{j = 3}{\underbrace{\underset{0}{0.0},\, \underset{1}{0.9},\, \underset{2}{0.7}}} }} ) \]