Skip to content
fn

cartesian_prod_op

→TensorImplPtr
int cartesian_prod_op(const int & tensors)

Python wrappers

Public Python APIs implemented by this engine symbol.

Enumerate every combination across a list of 1-D tensors.

Composite over meshgrid_op (with ij indexing) + reshape_op + stack_op. Builds the n-D grid, flattens each component, and stacks the flattened columns along a new trailing axis to produce a (N, D) matrix whose rows enumerate the full Cartesian product in row-major (ij) order. Gradient flows through MeshgridBackward, ReshapeBackward, and StackBackward.

Math

For input tensors t(0),…,t(D−1)t^{(0)}, \ldots, t^{(D-1)} of lengths n0,…,nD−1n_0, \ldots, n_{D-1}, the output rows are

(ti0(0),ti1(1),…,tiD−1(D−1)),(i0,…,iD−1)∈∏d[0,nd) \left( t^{(0)}_{i_0}, t^{(1)}_{i_1}, \ldots, t^{(D-1)}_{i_{D-1}} \right), \qquad (i_0, \ldots, i_{D-1}) \in \prod_d [0, n_d)

in row-major (lex) order with i0i_0 varying slowest.

See Also

meshgrid_op, stack_op.

Parameters

tensorsvector<TensorImplPtr>
Non-empty list of 1-D tensors. Lengths may differ.

Returns

TensorImplPtr

Tensor of shape (N, D) where N = prod(lengths) and D is the number of inputs. Dtype matches the inputs (must agree).

Raises

Failure
If the input list is empty, any element is null, or any element is not 1-D.

Examples

``cartesian_prod_op({[1, 2], [10, 20, 30]})`` returns a ``(6, 2)``
tensor: ``[[1, 10], [1, 20], [1, 30], [2, 10], [2, 20], [2, 30]]``.