mindchemistry.e3.utils.Ncon

View Source On Gitee
class mindchemistry.e3.utils.Ncon(con_list)[source]

Multiple-tensor contraction operator which has similar function to Einsum.

Parameters

con_list (List[List[int]]) – lists of indices for each tensor. The number of each list in con_list should coincide with the corresponding tensor's dimensions. The positive indices indicate the dimensions to be contracted or summed. The negative indices indicate the dimensions to be keeped (as batch dimensions).

Inputs:
  • input (List[Tensor]) - Tensor List.

Outputs:
  • output (Tensor) - The shape of tensor depends on the input and the computation process.

Raises

ValueError – If the number of commands is not match the number of operations.

Supported Platforms:

Ascend

Examples

>>> from mindspore import ops
>>> from mindchemistry.e3.utils import Ncon
Trace of a matrix:
>>> a = ops.ones((3, 3))
>>> Ncon([[1, 1]])([a])
3.0
Diagonal of a matrix:
>>> Ncon([[-1, -1]])([a])
[1. 1. 1.]
Outer product:
>>> b = ops.ones((2))
>>> c = ops.ones((3))
>>> Ncon([[-1], [-2]])([b, c]).shape
(2, 3)
Batch matrix multiplication
>>> d = ops.ones((2, 3, 4))
>>> e = ops.ones((2, 4, 1))
>>> Ncon([[-1, -2, 1], [-1, 1, -3]])([d, e]).shape
(2, 3, 1)