🔍 Code Extractor

function cross_index

Maturity: 46

Efficiently indexes into a Cartesian product of iterables without materializing the full product, using a linear index to retrieve the corresponding tuple of values.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
1934 - 1952
Complexity:
moderate

Purpose

This function provides memory-efficient access to elements in a Cartesian product by computing the multi-dimensional index from a single linear index. Instead of generating all combinations (which can be memory-intensive for large products), it calculates which element from each iterable corresponds to the given linear index position. This is useful for iterating through combinations in a memory-constrained environment or for random access to specific combinations.

Source Code

def cross_index(values, index):
    """
    Allows efficiently indexing into a cartesian product without
    expanding it. The values should be defined as a list of iterables
    making up the cartesian product and a linear index, returning
    the cross product of the values at the supplied index.
    """
    lengths = [len(v) for v in values]
    length = np.product(lengths)
    if index >= length:
        raise IndexError('Index %d out of bounds for cross-product of size %d'
                         % (index, length))
    indexes = []
    for i in range(1, len(values))[::-1]:
        p = np.product(lengths[-i:])
        indexes.append(index//p)
        index -= indexes[-1] * p
    indexes.append(index)
    return tuple(v[i] for v, i in zip(values, indexes))

Parameters

Name Type Default Kind
values - - positional_or_keyword
index - - positional_or_keyword

Parameter Details

values: A list of iterables (lists, tuples, ranges, etc.) that define the Cartesian product. Each iterable represents one dimension of the product. For example, [[1,2], ['a','b']] represents the product {(1,'a'), (1,'b'), (2,'a'), (2,'b')}.

index: A non-negative integer representing the linear position in the Cartesian product (0-indexed). Must be less than the total size of the Cartesian product (product of all iterable lengths), otherwise raises IndexError.

Return Value

Returns a tuple containing one element from each input iterable, corresponding to the position specified by the linear index. The tuple length equals the number of iterables in 'values'. For example, if values=[[1,2], ['a','b']] and index=2, returns (2, 'a').

Dependencies

  • numpy

Required Imports

import numpy as np

Usage Example

import numpy as np

def cross_index(values, index):
    lengths = [len(v) for v in values]
    length = np.product(lengths)
    if index >= length:
        raise IndexError('Index %d out of bounds for cross-product of size %d'
                         % (index, length))
    indexes = []
    for i in range(1, len(values))[::-1]:
        p = np.product(lengths[-i:])
        indexes.append(index//p)
        index -= indexes[-1] * p
    indexes.append(index)
    return tuple(v[i] for v, i in zip(values, indexes))

# Example usage
colors = ['red', 'green', 'blue']
sizes = ['S', 'M', 'L']
styles = ['plain', 'striped']

# Access the 5th element in the Cartesian product
result = cross_index([colors, sizes, styles], 5)
print(result)  # Output: ('green', 'M', 'striped')

# Iterate through all combinations using linear indexing
total_combinations = len(colors) * len(sizes) * len(styles)
for i in range(total_combinations):
    combo = cross_index([colors, sizes, styles], i)
    print(f"Index {i}: {combo}")

Best Practices

  • Ensure all elements in 'values' are indexable (support len() and integer indexing)
  • The index parameter must be within bounds (0 <= index < product of all lengths), otherwise IndexError is raised
  • This function is most beneficial when the Cartesian product is large and you only need to access specific elements rather than all combinations
  • For small Cartesian products, itertools.product() may be simpler and more readable
  • The function modifies the 'index' variable internally during computation, so the original index value is not preserved within the function scope
  • Performance is O(n) where n is the number of iterables in 'values', making it very efficient for sparse access patterns

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function cartesian_product 66.0% similar

    Computes the Cartesian product of multiple 1D arrays, returning expanded array views for each dimension with optional flattening and copying.

    From: /tf/active/vicechatdev/patches/util.py
  • function unique_zip 45.2% similar

    Returns a unique list of tuples created by zipping multiple iterables together, removing any duplicate tuples while preserving order.

    From: /tf/active/vicechatdev/patches/util.py
  • function search_indices 42.2% similar

    Finds the indices of specified values within a source array by using sorted search for efficient lookup.

    From: /tf/active/vicechatdev/patches/util.py
  • function arglexsort 41.8% similar

    Returns the indices that would lexicographically sort multiple arrays, treating them as columns of a structured array.

    From: /tf/active/vicechatdev/patches/util.py
  • function group_select 41.2% similar

    Recursively groups a list of key tuples into a nested dictionary structure to optimize indexing operations by avoiding duplicate key lookups.

    From: /tf/active/vicechatdev/patches/util.py
← Back to Browse