index arithmetic

kero.werithmetic.index_arithmetic.py

class index_arith:
  def __init__(self, full_list): 
    self.listform  
    self.N_set
    self.max_index
    self.linear_to_index_map
    self.index_to_linear_map
    return
  def get_index_by_linear(self, linear_index):
    # see example 2
    return
  def get_linear_by_index(self, index)
    # see example 2
    return
  def get_element_by_index(self, index):
    # see example 3
    return
  def get_element_by_linear_index(self, linear_index):
    # see example 3
    return

 

Properties Description
listform List of list. This is the list whose indices will be computed. The property is initiated when the index_arith object is initiated.
N_set List of integers. k-th entry of N_set is the number of elements in the k-th entry of listform.
max_index List of integers, this is the same as N_set, except every entry is subtracted by 1.
linear_to_index_map List. k-th entry of the list is the index. See example 1 for a clearer understanding.
Index_to_linear_map List of list. Index-th entry of the list is the linear index. See example 1 for a clearer understanding.

Given a list, for example [[1,2],[10,20]], we may want to access the elements in an orderly manner. A natural way to arrange this linearly is, for example, to assign 0 to 1, 1 to 2, 2 to 10 and 3 to 20. But we can arrange it using tuple indices like (0,0) to 1, (1,0) to 2, (0,1) to 10 and (1,1) to 20. For whatever reason, we might want to know either of the indices.

Example 1

import kero.werithmetic.index_arithmetic as wer
import numpy as np

mylist = [[1, 2, 3, 4], [0.1 ,0.3, 0.3], [66 ,77, 88]]
ia = wer.index_arith(mylist)

for i in range(ia.no_of_elements):
    temp = ia.linear_to_index_map[i]
    temp2 = ia.index_to_linear_map[tuple(temp)]
    print(temp2, " : " ,temp)

linear_to_index_map is the property of this object that stores the tuple indices, for example [0,0,0], [1,0,0] etc, and the key to access each of these indices is called the linear index, for example 0, 1, 2 etc. On the other hand, index_to_linear_map is the reverse; it is property of this object that stores the linear index 0,1,2 etc with the tuple indices [0,0,0], [1,0,0] etc as the key. Since mylist has

The output is the following.

0 : [0, 0, 0]
1 : [1, 0, 0]
2 : [2, 0, 0]
3 : [3, 0, 0]
4 : [0, 1, 0]
5 : [1, 1, 0]
6 : [2, 1, 0]
7 : [3, 1, 0]
8 : [0, 2, 0]
9 : [1, 2, 0]
10 : [2, 2, 0]
11 : [3, 2, 0]
12 : [0, 0, 1]
13 : [1, 0, 1]
14 : [2, 0, 1]
15 : [3, 0, 1]
16 : [0, 1, 1]
17 : [1, 1, 1]
18 : [2, 1, 1]
19 : [3, 1, 1]
20 : [0, 2, 1]
21 : [1, 2, 1]
22 : [2, 2, 1]
23 : [3, 2, 1]
24 : [0, 0, 2]
25 : [1, 0, 2]
26 : [2, 0, 2]
27 : [3, 0, 2]
28 : [0, 1, 2]
29 : [1, 1, 2]
30 : [2, 1, 2]
31 : [3, 1, 2]
32 : [0, 2, 2]
33 : [1, 2, 2]
34 : [2, 2, 2]
35 : [3, 2, 2]

Example 2

In this example. we want to compute what is the tuple index if we know the linear index (specified as the variable choose_linear_index) and vice versa. These are done using get_index_by_linear() and get_linear_by_index() respectively.

import kero.werithmetic.index_arithmetic as wer
import numpy as np

mylist = [[1, 2, 3, 4], [0.1, 0.3, 0.3], [66, 77, 88]]
ia = wer.index_arith(mylist)
choose_linear_index = [0, 1, 11, 12, 20, 21]
for x in choose_linear_index:
    ind = ia.get_index_by_linear(x)
    linear_index = ia.get_linear_by_index(ind)
    print("index = ", ind, "// linear index = ", linear_index)

The output is the following.

index = [0, 0, 0] // linear index = 0
index = [1, 0, 0] // linear index = 1
index = [3, 2, 0] // linear index = 11
index = [0, 0, 1] // linear index = 12
index = [0, 2, 1] // linear index = 20
index = [1, 2, 1] // linear index = 21

Example 3

In this example,

import kero.werithmetic.index_arithmetic as wer
import numpy as np

mylist = [[1, 2, 3, 4], [0.1, 0.3, 0.3], [66, 77, 88]]
ia = wer.index_arith(mylist)
choose_linear_index = range(ia.no_of_elements)
# choose_linear_index = [0, 1, 11, 12, 20, 21]
# choose_linear_index = [0]
for x in choose_linear_index:
    ind = ia.get_index_by_linear(x)
    linear_index = ia.get_linear_by_index(ind)
    print("index = ", ind, "// linear index = ", linear_index)
    print(" - by linear : ", ia.get_element_by_linear_index(x))
    print(" - by index  : ", ia.get_element_by_index(ind))

In this example, we show how to get the element of the list given either the linear index or the tuple index. These are done using get_element_by_linear_index() and get_element_by_index() respectively.

index = [0, 0, 0] // linear index = 0
- by linear : [1, 0.1, 66]
- by index : [1, 0.1, 66]
index = [1, 0, 0] // linear index = 1
- by linear : [2, 0.1, 66]
- by index : [2, 0.1, 66]
index = [2, 0, 0] // linear index = 2
- by linear : [3, 0.1, 66]
- by index : [3, 0.1, 66]
index = [3, 0, 0] // linear index = 3
- by linear : [4, 0.1, 66]
- by index : [4, 0.1, 66]
index = [0, 1, 0] // linear index = 4
- by linear : [1, 0.3, 66]
- by index : [1, 0.3, 66]
index = [1, 1, 0] // linear index = 5
- by linear : [2, 0.3, 66]
- by index : [2, 0.3, 66]

kero version: 0.1 and above