Index mappings $n2o: N\rightarrow O$ and $o2n: O\rightarrow N\cup\bot$ can be seen as functions between two type domains $O=1...n$ and $N=1...m.\, m<n$. The inverse operation $o2n=f(n2o,n)$ is defined uniquely by a given $n2o$. This scheme (selecting a remaining subset by $n2o$ and then modifying remaining references and subsets) is very common in all non-functional array programming.
A practical example is in place. Let $O=1...10$, with two subsets $O_1=1..5$ and $O_2=6...10$. Let $n2o=[1,3,8,9]= \{i\in O\,|\, \phi(i)\}$ be the indices of the selected elements. Then $o2n= [1,-1,2,-1,-1,-1,-1,-1,3,4,-1]$. One can then update the new values of the subsets:
$O_1'\cup\bot= o2n(O_1)= \{1,2,-1\}$ and $O_2'\cup\bot= o2n(O_2)= \{3,4,-1\}$.
The above notation uses ordered sets for the convenience reasons. The rule is to place the 'bottom' marker -1 ($\bot=\{-1\}$ on the index domain) as the last value.
Here is a pseudocode for the inversion, a very useful little function indeed:
def f(n2o, n):
o2n= [-1]*n # [-1,-1,-1,...]
o2n[n2o]= 1..m
All of the above happens at the realm of indices. Such languages as julia allow a direct application of o2n, e.g. in this way:
Asubset= A[subsetInds]
Anew= A[n2o] // initial selection by indices
AsubsetNew= At[o2n[subsetInds]]
AsubsetNew[-1]= [] // removal of the index slot -1
No comments:
Post a Comment