Expression builders

xtensor provides functions to ease the build of common N-dimensional expressions. The expressions returned by these functions implement the laziness of xtensor, that is, they don’t hold any value. Values are computed upon request.

Ones and zeros

  • xt::zeros(shape): generates an expression containing zeros of the specified shape.

  • xt::ones(shape): generates an expression containing ones of the specified shape.

  • xt::eye(shape, k=0): generates an expression of the specified shape, with ones on the k-th diagonal.

  • xt::eye(n, k = 0): generates an expression of shape (n, n) with ones on the k-th diagonal.

Numerical ranges

Joining expressions

Random distributions


xtensor uses a lazy generator for random numbers. You need to assign them or use xt::eval() to keep the generated values consistent.


  • xt::meshgrid(x1, x2,...): generates N-D coordinate expressions given one-dimensional coordinate arrays x1, x2… If specified vectors have lengths Ni = len(xi), meshgrid returns (N1, N2, N3,..., Nn)-shaped arrays, with the elements of xi repeated to fill the matrix along the first dimension for x1, the second for x2 and so on.