RWalib C Array Library User Guide > Array Indexing > alibIndexNdTo1d
  

alibIndexNdTo1d
For an n-dimensional array, convert a set of n-dimensional indices into a set of one-dimensional indices, i.e., convert a set of multidimensional indices into their flat equivalents. The PV-WAVE API for this routine is the INDEX_CONV Function.
Prototype
void alibIndexNdTo1d( wvlong n, wvlong *d, wvlong m, wvlong p, wvlong **i,wvlong *j )
Parameters
n — (Input) The number of dimensions for the reference array. This array, possibly virtual, is the array to which the indices refer.
*d — (Input) The n-element array of dimensions for the reference array.
m — (Input) The number of indices to be converted.
p — (Input) The number of coordinates supplied in the set of n-dimensional indices. Normally p = n, but setting p < n lets only the first p coordinates be specified, with the remaining coordinates implicitly assuming the value 0.
**i — (Input) The p-element array of pointers to m-element arrays defining the set of m n-dimensional indices into the reference array.
*j — (Input/Output) The m-element array. On return, j contains the one-dimensional equivalents to n-dimensional indices i. For a reference array c, the relationship between the sets of indices is:
c[ j[k] ] = c[ i[0][k], ..., i[p-1][k], 0, ..., 0 ]
where if p = n then:
c[ j[k] ] = c[ i[0][k], ..., i[n-1][k] ]
Example
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "alib.h"
void main() {
   /* array of dimensions to use for two-dimensional, 
      three-dimensional, and four-dimensional examples */
   wvlong dims[4] = {2,2,3,4};
   /* allocate index arrays big enough for two-dimensional, 
      three-dimensional, and four-dimensional examples */
   wvlong k, *h, *j, *i[4];
   h = (wvlong*)malloc(48*sizeof(wvlong));
   j = (wvlong*)malloc(48*sizeof(wvlong));
   for (k=0; k<4; k++) i[k] = (wvlong*)malloc(48*sizeof(wvlong));
   /* make enough n-dimensional indices to use for the 
      two-dimensional, three-dimensional, and four-dimensional 
      examples */
   alibinit( NULL, NULL, NULL, NULL );
   alibIndexl( 48, 0, 1, h );
   alibIndex1dToNd( 4, dims, 48, h, i );
   printf( "\n\n for a (3,4) array, convert its set of two-dimensional indices \n" );
   printf( " to one-dimensional indices, print both sets of indices side-by-side \n" );
      alibIndexNdTo1d( 2, &dims[2], 12, 2, &i[2], j );
         for ( k=0; k<12; k++ ) {
            printf( "%4lld %4lld %16lld\n",
                    i[2][k], i[3][k], j[k] );
         }
   printf( "\n\n for a (2,3,4) array, convert its set of three-dimensional indices \n" );
   printf( " to one-dimensional indices, print both sets of indices side-by-side \n" );
      alibIndexNdTo1d( 3, &dims[1], 24, 3, &i[1], j );
         for ( k=0; k<24; k++ ) {
            printf( "%4lld %4lld %4lld %16lld\n",
                    i[1][k], i[2][k], i[3][k], j[k] );
         }
   printf( "\n\n for a (2,2,3,4) array, convert its set of four-dimensional indices \n" );
   printf( " to one-dimensional indices, print both sets of indices side-by-side \n" );
      alibIndexNdTo1d( 4, dims, 48, 4, i, j );
         for ( k=0; k<48; k++ ) {
            printf( "%4lld %4lld %4lld %4lld %16lld\n",
                     i[0][k], i[1][k], i[2][k], i[3][k], j[k] );
            }
}
 
Output:
 
 for a (3,4) array, convert its set of two-dimensional indices 
 to one-dimensional indices, print both sets of indices side-by-side 
   0    0                0
   0    1                1
   0    2                2
   0    3                3
   1    0                4
   1    1                5
   1    2                6
   1    3                7
   2    0                8
   2    1                9
   2    2               10
   2    3               11
 
 for a (2,3,4) array, convert its set of three-dimensional indices 
 to one-dimensional indices, print both sets of indices side-by-side 
   0    0    0                0
   0    0    1                1
   0    0    2                2
   0    0    3                3
   0    1    0                4
   0    1    1                5
   0    1    2                6
   0    1    3                7
   0    2    0                8
   0    2    1                9
   0    2    2               10
   0    2    3               11
   1    0    0               12
   1    0    1               13
   1    0    2               14
   1    0    3               15
   1    1    0               16
   1    1    1               17
   1    1    2               18
   1    1    3               19
   1    2    0               20
   1    2    1               21
   1    2    2               22
   1    2    3               23
 
 for a (2,2,3,4) array, convert its set of four-dimensional indices 
 to one-dimensional indices, print both sets of indices side-by-side 
   0    0    0    0                0
   0    0    0    1                1
   0    0    0    2                2
   0    0    0    3                3
   0    0    1    0                4
   0    0    1    1                5
   0    0    1    2                6
   0    0    1    3                7
   0    0    2    0                8
   0    0    2    1                9
   0    0    2    2               10
   0    0    2    3               11
   0    1    0    0               12
   0    1    0    1               13
   0    1    0    2               14
   0    1    0    3               15
   0    1    1    0               16
   0    1    1    1               17
   0    1    1    2               18
   0    1    1    3               19
   0    1    2    0               20
   0    1    2    1               21
   0    1    2    2               22
   0    1    2    3               23
   1    0    0    0               24
   1    0    0    1               25
   1    0    0    2               26
   1    0    0    3               27
   1    0    1    0               28
   1    0    1    1               29
   1    0    1    2               30
   1    0    1    3               31
   1    0    2    0               32
   1    0    2    1               33
   1    0    2    2               34
   1    0    2    3               35
   1    1    0    0               36
   1    1    0    1               37
   1    1    0    2               38
   1    1    0    3               39
   1    1    1    0               40
   1    1    1    1               41
   1    1    1    2               42
   1    1    1    3               43
   1    1    2    0               44
   1    1    2    1               45
   1    1    2    2               46
   1    1    2    3               47

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.