Skip to content

This repository contains Example (94), entitled “3D traversal with one for-loop using arithmetic operators”, implemented in Python, MATLAB, and JavaScript. The example demonstrates how a 3D array can be traversed without nested loops by using modular arithmetic to compute the indices of matrices, rows, and columns from a single loop variable.

License

Notifications You must be signed in to change notification settings

Gagniuc/Arithmetic-loop-traversal-of-3D-arrays

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 

Repository files navigation

One for-loop 3D traversal with arithmetic operators

Ex. (94) - 3D traversal with one for-loop using only arithmetic operators, is presented here in three programming languages: Python, MATLAB, and JavaScript. Although the implementations differ in syntax, the underlying concept remains identical across all three versions. Each code sample is reproduced from its respective volume of the series Coding Examples from Simple to Complex (Springer, 2024). This parallel presentation allows readers to explore how the same computational idea can be expressed within different programming paradigms.


In this example, a 3D array A is defined, which represents a multi-dimensional structure containing strings and numbers. The code then initializes several variables, including t (a string for storing the result), s (the number of matrices or “layers” in A), m (the number of rows in each matrix), n (the number of columns in each matrix), and i, j, d, and k as iteration and indexing variables. A loop runs from 0 to q, where q is calculated as the product of n (number of columns), m (number of rows), and s (number of matrices), which effectively iterates through all elements in the 3D array A. Within the loop, the code calculates k as the modulo of v divided by the product of m and n. Variable j is calculated as the modulo of v divided by n, i is calculated as (k - j) divided by n, and d is calculated as (v - k) divided by the product of m and n. These calculations help determine the current position within the 3D array A. The code appends information to the t string for each iteration, showing the current index v and the corresponding value in the A array at the position [d][i][j]. A line break is also added to separate the entries. At the end of the loop, the code prints the contents of the t string. Thus, this code iterates over the entire 3D array A and prints the indices d, i, j, and the corresponding element from the array, effectively displaying the entire content of the 3D array with their indices. Nevertheless, the novelty here is represented by the way variables i, j, and d are computed in order to iterate over the elements of the 3D array A:

$$k = v \bmod (m \times n)$$ $$j = v \bmod n$$ $$i = \frac{k - j}{n}$$ $$d = \frac{v - k}{m \times n}$$

Namely, variable k represents the position within a matrix (subarray), and variable i is calculated as (k - j) / n (integer division by using the Python operator: “//”), which is the result of integer division between k - j and the number of columns n. Thus, it calculates the row index within the current matrix. Variable j is calculated as the remainder of v divided by the number of columns n (j = v % n). This gives us the column index within the current matrix. Variable d is calculated as (v - k) / (m × n), which is the result of integer division between (v - k) and the total number of elements in a matrix (m × n). Thus, it calculates the index of the current matrix in the 3D array.

Example in Python:

A = [
    [
        ["a", 55, 146],
        ["b", 34, 124],
        ["c", 96, 564],
        [100, 12, "d"],
    ],
    [
        ["e", 88, 146],
        ["f", 34, 124],
        ["g", 96, 564],
        [100, 12, "h"],
    ],
    [
        ["i", 88, 146],
        ["j", 34, 124],
        ["k", 96, 564],
        [100, 12, "k"],
    ],
    [
        ["m", 88, 146],
        ["n", 34, 124],
        ["o", 96, 564],
        [100, 12, "p"],
    ],
    [
        ["q", 88, 146],
        ["r", 34, 124],
        ["s", 96, 564],
        [100, 12, "t"],
    ]
    ]

t = ""

s = len(A)           # 5 matrices
m = len(A[0])        # 4 rows
n = len(A[0][0])     # 3 columns

i = 0
j = 0
d = 0
k = 0

q = n * m * s

for v in range(q):
    
    k = v % (m*n)
    
    j = v % n
    i = (k-j) // n
    d = (v-k) // (m*n)
    
    t += f"{v} A[{d}][{i}][{j}]="
    t += f"{A[d][i][j]}\n"

print(t)

Example in Javascript:

let A = [
        [
   ["a", 55, 146],
   ["b", 34, 124],
   ["c", 96, 564],
   [100, 12, "d"],
        ],
        [
   ["e", 88, 146],
   ["f", 34, 124],
   ["g", 96, 564],
   [100, 12, "h"],
        ],
        [
   ["i", 88, 146],
   ["j", 34, 124],
   ["k", 96, 564],
   [100, 12, "k"],
        ],
        [
   ["m", 88, 146],
   ["n", 34, 124],
   ["o", 96, 564],
   [100, 12, "p"],
        ],
        [
   ["q", 88, 146],
   ["r", 34, 124],
   ["s", 96, 564],
   [100, 12, "t"],
        ]
        ];

let t = "";

let s = A.length;       // 5 matrices
let m = A[0].length;    // 4 rows
let n = A[0][0].length; // 3 columns

let i = 0;
let j = 0;
let d = 0;
let k = 0;

let q = n * m * s;

for (let v = 0; v < q; v++){
    
   k = v % (m*n);
   
   j = v % n;
   i = (k-j) / n;
   d = (v-k) / (m*n);
   
   t += v + " A["+d+"]["+i+"]["+j+"]=";
   t += A[d][i][j] + "\n";
}

print(t);

Example in Matlab:

A = {
    {
        {'a', 55, 146},
        {'b', 34, 124},
        {'c', 96, 564},
        {100, 12, 'd'}
    },
    {
        {'e', 88, 146},
        {'f', 34, 124},
        {'g', 96, 564},
        {100, 12, 'h'}
    },
    {
        {'i', 88, 146},
        {'j', 34, 124},
        {'k', 96, 564},
        {100, 12, 'k'}
    },
    {
        {'m', 88, 146},
        {'n', 34, 124},
        {'o', 96, 564},
        {100, 12, 'p'}
    },
    {
        {'q', 88, 146},
        {'r', 34, 124},
        {'s', 96, 564},
        {100, 12, 't'}
    }
};

t = "";

s = size(A, 1);        % 5 matrices
m = size(A{1}, 1);     % 4 rows
n = size(A{1}{1}, 2);  % 3 columns

q = n * m * s;

for v = 0:(q-1)

    k = mod(v, m * n);

    j = mod(v, n) + 1;
    i = (k - j + 1) / n + 1;
    d = (v - k) / (m * n) + 1;

    t = t + sprintf('%d A{%d}{%d}{%d}=%s\n', ...
    v, d, i, j, mat2str(A{d}{i}{j}));
end

disp(t);
Output:
0 A[0][0][0]=a
1 A[0][0][1]=55
2 A[0][0][2]=146
3 A[0][1][0]=b
4 A[0][1][1]=34
5 A[0][1][2]=124
6 A[0][2][0]=c
7 A[0][2][1]=96
8 A[0][2][2]=564
9 A[0][3][0]=100
10 A[0][3][1]=12
11 A[0][3][2]=d
12 A[1][0][0]=e
13 A[1][0][1]=88
14 A[1][0][2]=146
15 A[1][1][0]=f
16 A[1][1][1]=34
17 A[1][1][2]=124
18 A[1][2][0]=g
19 A[1][2][1]=96
20 A[1][2][2]=564
21 A[1][3][0]=100
22 A[1][3][1]=12
23 A[1][3][2]=h
24 A[2][0][0]=i
25 A[2][0][1]=88
26 A[2][0][2]=146
27 A[2][1][0]=j
28 A[2][1][1]=34
29 A[2][1][2]=124
30 A[2][2][0]=k
31 A[2][2][1]=96
32 A[2][2][2]=564
33 A[2][3][0]=100
34 A[2][3][1]=12
35 A[2][3][2]=k
36 A[3][0][0]=m
37 A[3][0][1]=88
38 A[3][0][2]=146
39 A[3][1][0]=n
40 A[3][1][1]=34
41 A[3][1][2]=124
42 A[3][2][0]=o
43 A[3][2][1]=96
44 A[3][2][2]=564
45 A[3][3][0]=100
46 A[3][3][1]=12
47 A[3][3][2]=p
48 A[4][0][0]=q
49 A[4][0][1]=88
50 A[4][0][2]=146
51 A[4][1][0]=r
52 A[4][1][1]=34
53 A[4][1][2]=124
54 A[4][2][0]=s
55 A[4][2][1]=96
56 A[4][2][2]=564
57 A[4][3][0]=100
58 A[4][3][1]=12
59 A[4][3][2]=t

References

  • Paul A. Gagniuc. Coding Examples from Simple to Complex - Applications in Python, Springer, 2024, pp. 1-245.
  • Paul A. Gagniuc. Coding Examples from Simple to Complex - Applications in MATLAB, Springer, 2024, pp. 1-255.
  • Paul A. Gagniuc. Coding Examples from Simple to Complex - Applications in Javascript, Springer, 2024, pp. 1-240.

About

This repository contains Example (94), entitled “3D traversal with one for-loop using arithmetic operators”, implemented in Python, MATLAB, and JavaScript. The example demonstrates how a 3D array can be traversed without nested loops by using modular arithmetic to compute the indices of matrices, rows, and columns from a single loop variable.

Topics

Resources

License

Stars

Watchers

Forks