<jupyter_start><jupyter_text>Tensor traversalIllustrate both concordant and discordant traversal of the ranks of a rank-2 tensor.First, include some libraries<jupyter_code>%run ../prelude.py --style=tree --animation=movie<jupyter_output><empty_output><jupyter_text>Initialize setupThe following cell just creates some sliders to control the creation of the input operand tensors. Interesting variants to try:- Density = 0.2- Density = 1.0<jupyter_code># Initial values

M = 8
K = 6
density = 0.4
seed = 10


tm = TensorMaker()
tm.addTensor("A",
             rank_ids=["M", "K"],
             shape=[M, K],
             density=density,
             color="blue",
             seed=seed)
tm.displayControls()<jupyter_output><empty_output><jupyter_text>Create an input tensorsBecause this notebook tends to use both the original and rank swapped versions of the operands, the tensor names are suffixed with the ordered names of the ranks.<jupyter_code>a_MK = tm.makeTensor("A")
a_MK.setName("a_MK")
displayTensor(a_MK)<jupyter_output><empty_output><jupyter_text>Concordant traversal of the entire tensorConcordant traversal of the entire tensor, i.e., the top rank coordinate (__m__) moving slowest, just requires nested loops that interate through the fibers in the tensor.<jupyter_code>a_m = a_MK.getRoot()

canvas = createCanvas(a_MK)
print(f"(M, K) -> Value")

for m, a_k in a_m:
    for k, a_val in a_k:
        print(f"({m}, {k}) -> {a_val}")
        canvas.addFrame((m,k))
        
displayCanvas(canvas)<jupyter_output><empty_output><jupyter_text>Discordant traversal of the entire tensorIf we want to traverse the tensor in a reverse rank order, i.e., the lower rank coordinate (__k__) moving slowest, we cannot iterate through the fibers in the desired order, so we must iterate over the coordinates, and use __Fiber.getPayload()__ to get the values in the desired order.Note: using the keyword "allocate=False" causes __Fiber.getPayload()__ to return __None__ for empty coordinates.<jupyter_code>a_m = a_MK.getRoot()

canvas = createCanvas(a_MK)
print(f"(K, M) -> Value")

for k in range(K):
    for m in range(M):
        a_val = a_MK.getPayload(m, k, allocate=False)
        if a_val is not None:
            print(f"({k}, {m}) -> {a_val}")
            canvas.addFrame((m, k))
        
displayCanvas(canvas)<jupyter_output><empty_output><jupyter_text>Concordant traversal of a given tile in a tensorGiven an _axis aligned hyper-rectangle_ (AAHR) specified as a start coordinate and size (in coordinates) for each rank, we can traverse the tile using __Fiber.getRange()__.<jupyter_code># sample delta AAHR specified as a start coord and size

m_range = [0,3]
k_range = [3,3]

a_m = a_MK.getRoot()

canvas = createCanvas(a_MK)
print(f"(M, K) -> Value")

for m, a_k in a_m.getRange(*m_range):
    for k, a_val in a_k.getRange(*k_range):
        print(f"({m}, {k}) -> {a_val}")
        canvas.addFrame((m,k))
        
displayCanvas(canvas)<jupyter_output><empty_output><jupyter_text>Disconcordant traversal of a given tile in a tensorDiscordant traversal of a AAHR in a tensor can be accomplished using __Fiber.getPayload()__.<jupyter_code># sample delta AAHR specified as a start coord and size

m_range = [0,3]
k_range = [3,3]

a_m = a_MK.getRoot()

canvas = createCanvas(a_MK)
print(f"(K, M) -> Value")

for k in range(k_range[0], k_range[0]+k_range[1]):
    for m in range(m_range[0],m_range[0]+m_range[1]):
        a_val = a_MK.getPayload(m, k, allocate=False)
        if a_val is not None:
            print(f"({k}, {m}) -> {a_val}")
            canvas.addFrame((m, k))
        
displayCanvas(canvas)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1128500823723229, 'fraction_numerical': 0.006864360241625481, 'mean_word_length': 4.174715909090909, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '152487', 'license': 'MIT', 'n_tokens_mistral': 1264, 'n_tokens_neox': 1223, 'n_words': 376, 'path': 'notebooks/basic/traversal.ipynb', 'repo_name': 'FPSG-UIUC/fibertree-notebooks', 'size': '3642'}