Index Expression Objects

class pytaco.index_expression(num)

Creates an Index Expression.

This direct invocation is only used to convert python ints and floats to index expressions. In all other cases, index expression will be formed by accessing a tensor using index_var s and different operations on that access as seen in the Index Expression Functions section.

Note that in general, actually performing computations using index expressions require users to specify an output tensor with the correct shape. Dimensions indexed by the same index_var must have the same shape. As a result, determining the output shape is easy once the expression has been written. See the examples section.

num: int, float

The scalar value to use to make an index expression.


Construction index expressions in this way can largely be ignored since taco will implicitly convert python scalars to index expressions.

Creating index expressions from 0 order tensors must be done by indexing the 0 order tensor with None. This tells taco that there are no dimensions to access.

The evaluate() function allows users to represent index notation as a string using parenthesis instead of square brackets and not requiring that scalars be indexed with None. This function can infer the output dimension of the tensor given the input string but does not currently support all of the index expression functions available.


>>> import pytaco as pt
>>> pt.index_expression(3)

Implicit conversion

>>> i, j = pt.get_index_vars(2)
>>> t = pt.tensor([3,3])
>>> t[i,j] = 10 # All values set to 10 since 10 implied to be index expr

Scalar access

>>> s = pt.tensor(100)
>>> scalar_expr = s[None] * t[i,j]

An example of determining the output shape using matrix multiply:

>>> a = pt.tensor([4, 2])
>>> b = pt.tensor([2, 10])

We can represent matrix multiply as C[i, j] = A[i, k] * B[k, j]. Since we have the representation of the computation and we know that dimensions indexed by the same index variable must have the same shape, we can construct C by letting its first dimension have size A.shape[0] since both dimensions are indexed by i and letting its second dimension have size B.shape[1] since j indexes both of those dimensions. This, we would contiue the above as follows:

>>> c = pt.tensor([a.shape[0], b.shape[1]])

Then we could write >>> i, j, k = pt.get_index_vars(3) >>> c[i, j] = a[i, k] * b[k, j]


Returns the data type this expression will output after computation.