Binary Operations on Arrays

  Tensor Einstein Nutils
1 \(\mathbf{a} \in \mathbb{R}^n\) \(\mathbf{b} \in \mathbb{R}^n\) \(c = \mathbf{a} \cdot \mathbf{b} \in \mathbb{R}\) \(c = a_i b_i\) c = (a*b).sum(-1)
2 \(\mathbf{a} \in \mathbb{R}^n\) \(\mathbf{b} \in \mathbb{R}^m\) \(\mathbf{C} = \mathbf{a} \otimes \mathbf{b} \in \mathbb{R}^{n \times m}\) \(C_{ij} = a_i b_j\) C = a[:,_]*b[_,:]
C = function.outer(a,b)
3 \(\mathbf{A} \in \mathbb{R}^{m \times n}\) \(\mathbf{b} \in \mathbb{R}^n\) \(\mathbf{c} = \mathbf{A}\mathbf{b} \in \mathbb{R}^{m}\) \(c_{i} = A_{ij} b_j\) c = (A[:,:]*b[_,:]).sum(-1)
4 \(\mathbf{A} \in \mathbb{R}^{m \times n}\) \(\mathbf{B} \in \mathbb{R}^{n \times p}\) \(\mathbf{C} = \mathbf{A} \mathbf{B} \in \mathbb{R}^{m \times p}\) \(c_{ij} = A_{ik} B_{kj}\) c = (A[:,:,_]*B[_,:,:]).sum(-2)
5 \(\mathbf{A} \in \mathbb{R}^{m \times n}\) \(\mathbf{B} \in \mathbb{R}^{p \times n}\) \(\mathbf{C} = \mathbf{A} \mathbf{B}^T \in \mathbb{R}^{m \times p}\) \(C_{ij} = A_{ik} B_{jk}\) C = (A[:,_,:]*B[_,:,:]).sum(-1)
C = function.outer(A,B).sum(-1)
6 \(\mathbf{A} \in \mathbb{R}^{m \times n}\) \(\mathbf{B} \in \mathbb{R}^{m \times n}\) \(c = \mathbf{A} : \mathbf{B} \in \mathbb{R}\) \(c = A_{ij} B_{ij}\) c = (A*B).sum([-2,-1])


  1. In the above table the summation axes are numbered backward. For example, sum(-1) is used to sum over the last axis of an array. Although forward numbering is possible in many situations, backward numbering is generally preferred in Nutils code.
  2. When a summation over multiple axes is performed (#6), these axes are to be listed. In the case of single-axis summations listing is optional (for example sum(-1) is equivalent to sum([-1])). The shorter notation sum(-1) is preferred.
  3. When the numer of dimensions of the two arguments of a binary operation mismatch, singleton axes are automatically prepended to the “shorter” argument. This property can be used to shorten notation. For example, #3 can be written as (A*b).sum(-1). To avoid ambiguities, in general, such abbreviations are discouraged.