Tensor Class

class pytaco.tensor(arg1=None, fmt=mode_format(compressed), dtype=pytaco.float, name=None)

A mathematical tensor.

A tensor object represents a mathematical tensor of arbitrary dimensions and is at the heart of this library . A tensor must consist of a homogeneous dtype and be stored in a given format. They can optionally be given a name.

Taco allows users to compressed certain dimensions of tensors which means only the non-zeros and their index information is stored. This is useful for minimizing the size of the data that needs to be stored as well as speeding up computation since in some cases, taco can ignore computing 0’d out elements.

Computations on tensors can be expressed using the Tensor Compute Functions provided or by forming Index Expressions where tensors are indexed using index_var s to specify different computations and reductions along their dimensions.

All operators on the tensor class produce compressed values by default. This can be overridden by using the Tensor Compute Functions methods provided and specifying your own format.

Parameters
arg1: int, float, iterable, optional

If this argument is a python int or float, taco will create a scalar tensor and initialize it to the with the value passed in. If arg1 is an iterable, taco will interpret this as the shape and initialize a tensor with the given shape. The default value is none meaning that taco will simply create an empty scalar tensor and ignore the fmt argument.

fmt:class:~pytaco.format, optional

format to store the tensor in.

  • A list of mode_format s can also be given in which case, the mode ordering is taken to be the same as the list.

  • If a single mode_format is given, all the dimensions of the tensor are initialized to that mode_format.

  • If not specified then all dimensions default to compressed.

dtype:class:~pytaco.dtype, optional

The dtype of the values stored by this tensor.

name: str, optional

The name of the tensor to be created. If not specified the name will be automatically ignored by taco. Most users can ignore this but specifying the name can be useful to those wanting to read the C code generated by taco.

Notes

Tensors are iterable and one can iterate over the coordinates, value pairs in a tensor as shown in the example section below. The tensor iterator only returns non-zero elements along with their coordinates to the user. An example is shown below.

To read values from a tensor they must be specified using one call to __getitem__. As a result, attempting to read an order 2 tensor A using the syntax A[0][0] is illegal in taco. The correct way to read A would be to write A[0, 0]. This holds in general tensors of all dimensions.

Inserting is done by an explicit tensor.insert() method instead of __setitem__. This is because the current insert semantics actually increment the value in the current location.

Examples

Create a scalar tensor with the value 42.

>>> import pytaco as pt
>>> t = pt.tensor(42)

Create an order 2 tensor, insert values then iterate over the non-zeros

>>> mat = pt.tensor([2, 2], pt.csr)
>>> mat.insert([1,1], 10)
>>> mat.insert([0,0], 100)
>>> for coord, value in mat:
...     print(coord, value)
[0, 0] 100.0
[1, 1] 10.0
Attributes
order

Returns an int which is the order (number of dimensions) of the tensor.

name

Returns a string representing the name of the tensor.

shape

Returns the shape of the tensor.

format

Returns the format used to store the tensor.

dtype

Returns the dtype used to store the tensor.

T

This is equivalent to transpose() with arguments shape[::-1].

Methods

transpose(new_ordering[, new_format, name])

Transposes a tensor.

pack()

Packs a tensor.

compile()

Compiles current expression.

assemble()

Assemble the indices and values in the specified sparse structures.

compute()

Compute the given expression and put the values in the tensor storage.

evaluate()

Compile, assemble, and compute as needed.

to_dense()

Converts a tensor of any format to a dense tensor.

to_array()

Same as to_array().

toarray()

Alias for to_array() for compatibility with scipy.

to_sp_csr()

Same as to_sp_csr().

to_sp_csc()

Same as to_sp_csc().

copy()

Returns a deep copy of a tensor.

insert(coords, val)

Increments the value at a given set of coordinates.

remove_explicit_zeros([new_fmt, new_dtype])

Same as remove_explicit_zeros().