Backend

Numpy based computation backend.

geomstats._backend.numpy.array_from_sparse(indices, data, target_shape)[source]

Create an array of given shape, with values at specific indices.

The rest of the array will be filled with zeros.

Parameters
  • indices (iterable(tuple(int))) – Index of each element which will be assigned a specific value.

  • data (iterable(scalar)) – Value associated at each index.

  • target_shape (tuple(int)) – Shape of the output array.

Returns

a (array, shape=target_shape) – Array of zeros with specified values assigned to specified indices.

geomstats._backend.numpy.assignment(x, values, indices, axis=0)[source]

Assign values at given indices of an array.

Parameters
  • x (array-like, shape=[dim]) – Initial array.

  • values ({float, list(float)}) – Value or list of values to be assigned.

  • indices ({int, tuple, list(int), list(tuple)}) – Single int or tuple, or list of ints or tuples of indices where value is assigned. If the length of the tuples is shorter than ndim(x), values are assigned to each copy along axis.

  • axis (int, optional) – Axis along which values are assigned, if vectorized.

Returns

x_new (array-like, shape=[dim]) – Copy of x with the values assigned at the given indices.

Notes

If a single value is provided, it is assigned at all the indices. If a list is given, it must have the same length as indices.

geomstats._backend.numpy.assignment_by_sum(x, values, indices, axis=0)[source]

Add values at given indices of an array.

Parameters
  • x (array-like, shape=[dim]) – Initial array.

  • values ({float, list(float)}) – Value or list of values to be assigned.

  • indices ({int, tuple, list(int), list(tuple)}) – Single int or tuple, or list of ints or tuples of indices where value is assigned. If the length of the tuples is shorter than ndim(x), values are assigned to each copy along axis.

  • axis (int, optional) – Axis along which values are assigned, if vectorized.

Returns

x_new (array-like, shape=[dim]) – Copy of x with the values assigned at the given indices.

Notes

If a single value is provided, it is assigned at all the indices. If a list is given, it must have the same length as indices.

geomstats._backend.numpy.get_mask_i_float(i, n)[source]

Create a 1D array of zeros with one element at one, with floating type.

Parameters
  • i (int) – Index of the non-zero element.

  • n (n) – Length of the created array.

Returns

mask_i_float (array-like, shape=[n,]) – 1D array of zeros except at index i, where it is one

geomstats._backend.numpy.get_slice(x, indices)[source]

Return a slice of an array, following Numpy’s style.

Parameters
  • x (array-like, shape=[dim]) – Initial array.

  • indices (iterable(iterable(int))) – Indices which are kept along each axis, starting from 0.

Returns

slice (array-like) – Slice of x given by indices.

Notes

This follows Numpy’s convention: indices are grouped by axis.

Examples

>>> a = np.array(range(30)).reshape(3,10)
>>> get_slice(a, ((0, 2), (8, 9)))
array([8, 29])
geomstats._backend.numpy.set_diag(x, new_diag)[source]

Set the diagonal along the last two axis.

Parameters
  • x (array-like, shape=[dim]) – Initial array.

  • new_diag (array-like, shape=[dim[-2]]) – Values to set on the diagonal.

Returns

None

Notes

This mimics tensorflow.linalg.set_diag(x, new_diag), when new_diag is a 1-D array, but modifies x instead of creating a copy.

Pytorch based computation backend.

geomstats._backend.pytorch.amax()

max(input) -> Tensor

Returns the maximum value of all elements in the input tensor.

Warning

This function produces deterministic (sub)gradients unlike max(dim=0)

Args:

input (Tensor): the input tensor.

Example:

>>> a = torch.randn(1, 3)
>>> a
tensor([[ 0.6763,  0.7445, -2.2369]])
>>> torch.max(a)
tensor(0.7445)
geomstats._backend.pytorch.max(input, dim, keepdim=False, *, out=None)

Returns a namedtuple (values, indices) where values is the maximum value of each row of the input tensor in the given dimension dim. And indices is the index location of each maximum value found (argmax).

If keepdim is True, the output tensors are of the same size as input except in the dimension dim where they are of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensors having 1 fewer dimension than input.

Note

If there are multiple maximal values in a reduced row then the indices of the first maximal value are returned.

Args:

input (Tensor): the input tensor. dim (int): the dimension to reduce. keepdim (bool): whether the output tensor has dim retained or not. Default: False.

Keyword args:

out (tuple, optional): the result tuple of two output tensors (max, max_indices)

Example:

>>> a = torch.randn(4, 4)
>>> a
tensor([[-1.2360, -0.2942, -0.1222,  0.8475],
        [ 1.1949, -1.1127, -2.2379, -0.6702],
        [ 1.5717, -0.9207,  0.1297, -1.8768],
        [-0.6172,  1.0036, -0.6060, -0.2432]])
>>> torch.max(a, 1)
torch.return_types.max(values=tensor([0.8475, 1.1949, 1.5717, 1.0036]), indices=tensor([3, 0, 0, 1]))
geomstats._backend.pytorch.max(input, other, *, out=None)Tensor

See torch.maximum().

geomstats._backend.pytorch.amin()

min(input) -> Tensor

Returns the minimum value of all elements in the input tensor.

Warning

This function produces deterministic (sub)gradients unlike min(dim=0)

Args:

input (Tensor): the input tensor.

Example:

>>> a = torch.randn(1, 3)
>>> a
tensor([[ 0.6750,  1.0857,  1.7197]])
>>> torch.min(a)
tensor(0.6750)
geomstats._backend.pytorch.min(input, dim, keepdim=False, *, out=None)

Returns a namedtuple (values, indices) where values is the minimum value of each row of the input tensor in the given dimension dim. And indices is the index location of each minimum value found (argmin).

If keepdim is True, the output tensors are of the same size as input except in the dimension dim where they are of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensors having 1 fewer dimension than input.

Note

If there are multiple minimal values in a reduced row then the indices of the first minimal value are returned.

Args:

input (Tensor): the input tensor. dim (int): the dimension to reduce. keepdim (bool): whether the output tensor has dim retained or not.

Keyword args:

out (tuple, optional): the tuple of two output tensors (min, min_indices)

Example:

>>> a = torch.randn(4, 4)
>>> a
tensor([[-0.6248,  1.1334, -1.1899, -0.2803],
        [-1.4644, -0.2635, -0.3651,  0.6134],
        [ 0.2457,  0.0384,  1.0128,  0.7015],
        [-0.1153,  2.9849,  2.1458,  0.5788]])
>>> torch.min(a, 1)
torch.return_types.min(values=tensor([-1.1899, -1.4644,  0.0384, -0.1153]), indices=tensor([2, 0, 1, 0]))
geomstats._backend.pytorch.min(input, other, *, out=None)Tensor

See torch.minimum().

geomstats._backend.pytorch.arange(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)Tensor

Returns a 1-D tensor of size \(\left\lceil \frac{\text{end} - \text{start}}{\text{step}} \right\rceil\) with values from the interval [start, end) taken with common difference step beginning from start.

Note that non-integer step is subject to floating point rounding errors when comparing against end; to avoid inconsistency, we advise adding a small epsilon to end in such cases.

\[\text{out}_{{i+1}} = \text{out}_{i} + \text{step}\]
Args:

start (Number): the starting value for the set of points. Default: 0. end (Number): the ending value for the set of points step (Number): the gap between each pair of adjacent points. Default: 1.

Keyword args:

out (Tensor, optional): the output tensor. dtype (torch.dtype, optional): the desired data type of returned tensor.

Default: if None, uses a global default (see torch.set_default_tensor_type()). If dtype is not given, infer the data type from the other input arguments. If any of start, end, or stop are floating-point, the dtype is inferred to be the default dtype, see get_default_dtype(). Otherwise, the dtype is inferred to be torch.int64.

layout (torch.layout, optional): the desired layout of returned Tensor.

Default: torch.strided.

device (torch.device, optional): the desired device of returned tensor.

Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

requires_grad (bool, optional): If autograd should record operations on the

returned tensor. Default: False.

Example:

>>> torch.arange(5)
tensor([ 0,  1,  2,  3,  4])
>>> torch.arange(1, 4)
tensor([ 1,  2,  3])
>>> torch.arange(1, 2.5, 0.5)
tensor([ 1.0000,  1.5000,  2.0000])
geomstats._backend.pytorch.arccos()

acos(input, *, out=None) -> Tensor

Computes the inverse cosine of each element in input.

\[\text{out}_{i} = \cos^{-1}(\text{input}_{i})\]
Args:

input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([ 0.3348, -0.5889,  0.2005, -0.1584])
>>> torch.acos(a)
tensor([ 1.2294,  2.2004,  1.3690,  1.7298])
geomstats._backend.pytorch.arcsin()

asin(input, *, out=None) -> Tensor

Returns a new tensor with the arcsine of the elements of input.

\[\text{out}_{i} = \sin^{-1}(\text{input}_{i})\]
Args:

input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([-0.5962,  1.4985, -0.4396,  1.4525])
>>> torch.asin(a)
tensor([-0.6387,     nan, -0.4552,     nan])
geomstats._backend.pytorch.arctan2()

atan2(input, other, *, out=None) -> Tensor

Element-wise arctangent of \(\text{input}_{i} / \text{other}_{i}\) with consideration of the quadrant. Returns a new tensor with the signed angles in radians between vector \((\text{other}_{i}, \text{input}_{i})\) and vector \((1, 0)\). (Note that \(\text{other}_{i}\), the second parameter, is the x-coordinate, while \(\text{input}_{i}\), the first parameter, is the y-coordinate.)

The shapes of input and other must be broadcastable.

Args:

input (Tensor): the first input tensor other (Tensor): the second input tensor

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([ 0.9041,  0.0196, -0.3108, -2.4423])
>>> torch.atan2(a, torch.randn(4))
tensor([ 0.9833,  0.0811, -1.9743, -1.4151])
geomstats._backend.pytorch.argmin(input)LongTensor

Returns the indices of the minimum value of all elements in the input tensor.

This is the second value returned by torch.min(). See its documentation for the exact semantics of this method.

Note

If there are multiple minimal values then the indices of the first minimal value are returned.

Args:

input (Tensor): the input tensor.

Example:

>>> a = torch.randn(4, 4)
>>> a
tensor([[ 0.1139,  0.2254, -0.1381,  0.3687],
        [ 1.0100, -1.1975, -0.0102, -0.4732],
        [-0.9240,  0.1207, -0.7506, -1.0213],
        [ 1.7809, -1.2960,  0.9384,  0.1438]])
>>> torch.argmin(a)
tensor(13)
geomstats._backend.pytorch.argmin(input, dim, keepdim=False)LongTensor

Returns the indices of the minimum values of a tensor across a dimension.

This is the second value returned by torch.min(). See its documentation for the exact semantics of this method.

Args:

input (Tensor): the input tensor. dim (int): the dimension to reduce. If None, the argmin of the flattened input is returned. keepdim (bool): whether the output tensor has dim retained or not. Ignored if dim=None.

Example:

>>> a = torch.randn(4, 4)
>>> a
tensor([[ 0.1139,  0.2254, -0.1381,  0.3687],
        [ 1.0100, -1.1975, -0.0102, -0.4732],
        [-0.9240,  0.1207, -0.7506, -1.0213],
        [ 1.7809, -1.2960,  0.9384,  0.1438]])
>>> torch.argmin(a, dim=1)
tensor([ 2,  1,  3,  1])
geomstats._backend.pytorch.array_from_sparse(indices, data, target_shape)[source]

Create an array of given shape, with values at specific indices.

The rest of the array will be filled with zeros.

Parameters
  • indices (iterable(tuple(int))) – Index of each element which will be assigned a specific value.

  • data (iterable(scalar)) – Value associated at each index.

  • target_shape (tuple(int)) – Shape of the output array.

Returns

a (array, shape=target_shape) – Array of zeros with specified values assigned to specified indices.

geomstats._backend.pytorch.assignment(x, values, indices, axis=0)[source]

Assign values at given indices of an array.

Parameters
  • x (array-like, shape=[dim]) – Initial array.

  • values ({float, list(float)}) – Value or list of values to be assigned.

  • indices ({int, tuple, list(int), list(tuple)}) – Single int or tuple, or list of ints or tuples of indices where value is assigned. If the length of the tuples is shorter than ndim(x), values are assigned to each copy along axis.

  • axis (int, optional) – Axis along which values are assigned, if vectorized.

Returns

x_new (array-like, shape=[dim]) – Copy of x with the values assigned at the given indices.

Notes

If a single value is provided, it is assigned at all the indices. If a list is given, it must have the same length as indices.

geomstats._backend.pytorch.assignment_by_sum(x, values, indices, axis=0)[source]

Add values at given indices of an array.

Parameters
  • x (array-like, shape=[dim]) – Initial array.

  • values ({float, list(float)}) – Value or list of values to be assigned.

  • indices ({int, tuple, list(int), list(tuple)}) – Single int or tuple, or list of ints or tuples of indices where value is assigned. If the length of the tuples is shorter than ndim(x), values are assigned to each copy along axis.

  • axis (int, optional) – Axis along which values are assigned, if vectorized.

Returns

x_new (array-like, shape=[dim]) – Copy of x with the values assigned at the given indices.

Notes

If a single value is provided, it is assigned at all the indices. If a list is given, it must have the same length as indices.

geomstats._backend.pytorch.ceil(input, *, out=None)Tensor

Returns a new tensor with the ceil of the elements of input, the smallest integer greater than or equal to each element.

\[\text{out}_{i} = \left\lceil \text{input}_{i} \right\rceil = \left\lfloor \text{input}_{i} \right\rfloor + 1\]
Args:

input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([-0.6341, -1.4208, -1.0900,  0.5826])
>>> torch.ceil(a)
tensor([-0., -1., -1.,  1.])
geomstats._backend.pytorch.clip()

clamp(input, min, max, *, out=None) -> Tensor

Clamp all elements in input into the range [ min, max ] and return a resulting tensor:

\[\begin{split}y_i = \begin{cases} \text{min} & \text{if } x_i < \text{min} \\ x_i & \text{if } \text{min} \leq x_i \leq \text{max} \\ \text{max} & \text{if } x_i > \text{max} \end{cases}\end{split}\]

If input is of type FloatTensor or DoubleTensor, args min and max must be real numbers, otherwise they should be integers.

Args:

input (Tensor): the input tensor. min (Number): lower-bound of the range to be clamped to max (Number): upper-bound of the range to be clamped to

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([-1.7120,  0.1734, -0.0478, -0.0922])
>>> torch.clamp(a, min=-0.5, max=0.5)
tensor([-0.5000,  0.1734, -0.0478, -0.0922])
geomstats._backend.pytorch.clamp(input, *, min, out=None)Tensor

Clamps all elements in input to be larger or equal min.

If input is of type FloatTensor or DoubleTensor, value should be a real number, otherwise it should be an integer.

Args:

input (Tensor): the input tensor.

Keyword args:

min (Number): minimal value of each element in the output out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([-0.0299, -2.3184,  2.1593, -0.8883])
>>> torch.clamp(a, min=0.5)
tensor([ 0.5000,  0.5000,  2.1593,  0.5000])
geomstats._backend.pytorch.clamp(input, *, max, out=None)Tensor

Clamps all elements in input to be smaller or equal max.

If input is of type FloatTensor or DoubleTensor, value should be a real number, otherwise it should be an integer.

Args:

input (Tensor): the input tensor.

Keyword args:

max (Number): maximal value of each element in the output out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([ 0.7753, -0.4702, -0.4599,  1.1899])
>>> torch.clamp(a, max=0.5)
tensor([ 0.5000, -0.4702, -0.4599,  0.5000])
geomstats._backend.pytorch.cos(input, *, out=None)Tensor

Returns a new tensor with the cosine of the elements of input.

\[\text{out}_{i} = \cos(\text{input}_{i})\]
Args:

input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([ 1.4309,  1.2706, -0.8562,  0.9796])
>>> torch.cos(a)
tensor([ 0.1395,  0.2957,  0.6553,  0.5574])
geomstats._backend.pytorch.cosh(input, *, out=None)Tensor

Returns a new tensor with the hyperbolic cosine of the elements of input.

\[\text{out}_{i} = \cosh(\text{input}_{i})\]
Args:

input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([ 0.1632,  1.1835, -0.6979, -0.7325])
>>> torch.cosh(a)
tensor([ 1.0133,  1.7860,  1.2536,  1.2805])
geomstats._backend.pytorch.cross(input, other, dim=None, *, out=None)Tensor

Returns the cross product of vectors in dimension dim of input and other.

input and other must have the same size, and the size of their dim dimension should be 3.

If dim is not given, it defaults to the first dimension found with the size 3. Note that this might be unexpected.

Args:

input (Tensor): the input tensor. other (Tensor): the second input tensor dim (int, optional): the dimension to take the cross-product in.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4, 3)
>>> a
tensor([[-0.3956,  1.1455,  1.6895],
        [-0.5849,  1.3672,  0.3599],
        [-1.1626,  0.7180, -0.0521],
        [-0.1339,  0.9902, -2.0225]])
>>> b = torch.randn(4, 3)
>>> b
tensor([[-0.0257, -1.4725, -1.2251],
        [-1.1479, -0.7005, -1.9757],
        [-1.3904,  0.3726, -1.1836],
        [-0.9688, -0.7153,  0.2159]])
>>> torch.cross(a, b, dim=1)
tensor([[ 1.0844, -0.5281,  0.6120],
        [-2.4490, -1.5687,  1.9792],
        [-0.8304, -1.3037,  0.5650],
        [-1.2329,  1.9883,  1.0551]])
>>> torch.cross(a, b)
tensor([[ 1.0844, -0.5281,  0.6120],
        [-2.4490, -1.5687,  1.9792],
        [-0.8304, -1.3037,  0.5650],
        [-1.2329,  1.9883,  1.0551]])
geomstats._backend.pytorch.divide()

div(input, other, *, out=None) -> Tensor

Divides each element of the input input by the corresponding element of other.

\[\text{out}_i = \frac{\text{input}_i}{\text{other}_i}\]

Note

Performs a “true” division like Python 3. See torch.floor_divide() for floor division.

Supports broadcasting to a common shape, type promotion, and integer, float, and complex inputs. Always promotes integer types to the default scalar type.

Args:

input (Tensor): the dividend other (Tensor or Number): the divisor

Keyword args:

out (Tensor, optional): the output tensor.

Examples:

>>> a = torch.randn(5)
>>> a
tensor([ 0.3810,  1.2774, -0.2972, -0.3719,  0.4637])
>>> torch.div(a, 0.5)
tensor([ 0.7620,  2.5548, -0.5944, -0.7439,  0.9275])
>>> a = torch.randn(4, 4)
>>> a
tensor([[-0.3711, -1.9353, -0.4605, -0.2917],
        [ 0.1815, -1.0111,  0.9805, -1.5923],
        [ 0.1062,  1.4581,  0.7759, -1.2344],
        [-0.1830, -0.0313,  1.1908, -1.4757]])
>>> b = torch.randn(4)
>>> b
tensor([ 0.8032,  0.2930, -0.8113, -0.2308])
>>> torch.div(a, b)
tensor([[-0.4620, -6.6051,  0.5676,  1.2637],
        [ 0.2260, -3.4507, -1.2086,  6.8988],
        [ 0.1322,  4.9764, -0.9564,  5.3480],
        [-0.2278, -0.1068, -1.4678,  6.3936]])
geomstats._backend.pytorch.empty_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format)Tensor

Returns an uninitialized tensor with the same size as input. torch.empty_like(input) is equivalent to torch.empty(input.size(), dtype=input.dtype, layout=input.layout, device=input.device).

Args:

input (Tensor): the size of input will determine size of the output tensor.

Keyword args:
dtype (torch.dtype, optional): the desired data type of returned Tensor.

Default: if None, defaults to the dtype of input.

layout (torch.layout, optional): the desired layout of returned tensor.

Default: if None, defaults to the layout of input.

device (torch.device, optional): the desired device of returned tensor.

Default: if None, defaults to the device of input.

requires_grad (bool, optional): If autograd should record operations on the

returned tensor. Default: False.

memory_format (torch.memory_format, optional): the desired memory format of

returned Tensor. Default: torch.preserve_format.

Example:

>>> torch.empty((2,3), dtype=torch.int64)
tensor([[ 9.4064e+13,  2.8000e+01,  9.3493e+13],
        [ 7.5751e+18,  7.1428e+18,  7.5955e+18]])
geomstats._backend.pytorch.eq(input, other, *, out=None)Tensor

Computes element-wise equality

The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Args:

input (Tensor): the tensor to compare other (Tensor or float): the tensor or value to compare

Keyword args:

out (Tensor, optional): the output tensor.

Returns:

A boolean tensor that is True where input is equal to other and False elsewhere

Example:

>>> torch.eq(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
tensor([[ True, False],
        [False, True]])
geomstats._backend.pytorch.erf(input, *, out=None)Tensor

Computes the error function of each element. The error function is defined as follows:

\[\mathrm{erf}(x) = \frac{2}{\sqrt{\pi}} \int_{0}^{x} e^{-t^2} dt\]
Args:

input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> torch.erf(torch.tensor([0, -1., 10.]))
tensor([ 0.0000, -0.8427,  1.0000])
geomstats._backend.pytorch.exp(input, *, out=None)Tensor

Returns a new tensor with the exponential of the elements of the input tensor input.

\[y_{i} = e^{x_{i}}\]
Args:

input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> torch.exp(torch.tensor([0, math.log(2.)]))
tensor([ 1.,  2.])
geomstats._backend.pytorch.eye(n, m=None, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)Tensor

Returns a 2-D tensor with ones on the diagonal and zeros elsewhere.

Args:

n (int): the number of rows m (int, optional): the number of columns with default being n out (Tensor, optional): the output tensor. dtype (torch.dtype, optional): the desired data type of returned tensor.

Default: if None, uses a global default (see torch.set_default_tensor_type()).

layout (torch.layout, optional): the desired layout of returned Tensor.

Default: torch.strided.

device (torch.device, optional): the desired device of returned tensor.

Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

requires_grad (bool, optional): If autograd should record operations on the

returned tensor. Default: False.

Returns:

Tensor: A 2-D tensor with ones on the diagonal and zeros elsewhere

Example:

>>> torch.eye(3)
tensor([[ 1.,  0.,  0.],
        [ 0.,  1.,  0.],
        [ 0.,  0.,  1.]])
geomstats._backend.pytorch.flatten(input, start_dim=0, end_dim=- 1)Tensor

Flattens a contiguous range of dims in a tensor.

Args:

input (Tensor): the input tensor. start_dim (int): the first dim to flatten end_dim (int): the last dim to flatten

Example:

>>> t = torch.tensor([[[1, 2],
                       [3, 4]],
                      [[5, 6],
                       [7, 8]]])
>>> torch.flatten(t)
tensor([1, 2, 3, 4, 5, 6, 7, 8])
>>> torch.flatten(t, start_dim=1)
tensor([[1, 2, 3, 4],
        [5, 6, 7, 8]])
geomstats._backend.pytorch.floor(input, *, out=None)Tensor

Returns a new tensor with the floor of the elements of input, the largest integer less than or equal to each element.

\[\text{out}_{i} = \left\lfloor \text{input}_{i} \right\rfloor\]
Args:

input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([-0.8166,  1.5308, -0.2530, -0.2091])
>>> torch.floor(a)
tensor([-1.,  1., -1., -1.])
geomstats._backend.pytorch.get_mask_i_float(i, n)[source]

Create a 1D array of zeros with one element at one, with floating type.

Parameters
  • i (int) – Index of the non-zero element.

  • n (n) – Length of the created array.

Returns

mask_i_float (array-like, shape=[n,]) – 1D array of zeros except at index i, where it is one

geomstats._backend.pytorch.get_slice(x, indices)[source]

Return a slice of an array, following Numpy’s style.

Parameters
  • x (array-like, shape=[dim]) – Initial array.

  • indices (iterable(iterable(int))) – Indices which are kept along each axis, starting from 0.

Returns

slice (array-like) – Slice of x given by indices.

Notes

This follows Numpy’s convention: indices are grouped by axis.

Examples

>>> a = torch.tensor(range(30)).reshape(3,10)
>>> get_slice(a, ((0, 2), (8, 9)))
tensor([8, 29])
geomstats._backend.pytorch.greater()

gt(input, other, *, out=None) -> Tensor

Computes \(\text{input} > \text{other}\) element-wise.

The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Args:

input (Tensor): the tensor to compare other (Tensor or float): the tensor or value to compare

Keyword args:

out (Tensor, optional): the output tensor.

Returns:

A boolean tensor that is True where input is greater than other and False elsewhere

Example:

>>> torch.gt(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
tensor([[False, True], [False, False]])
geomstats._backend.pytorch.isnan(input)Tensor

Returns a new tensor with boolean elements representing if each element of input is NaN or not. Complex values are considered NaN when either their real and/or imaginary part is NaN.

Arguments:

input (Tensor): the input tensor.

Returns:

A boolean tensor that is True where input is NaN and False elsewhere

Example:

>>> torch.isnan(torch.tensor([1, float('nan'), 2]))
tensor([False, True, False])
geomstats._backend.pytorch.less()

lt(input, other, *, out=None) -> Tensor

Computes \(\text{input} < \text{other}\) element-wise.

The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Args:

input (Tensor): the tensor to compare other (Tensor or float): the tensor or value to compare

Keyword args:

out (Tensor, optional): the output tensor.

Returns:

A boolean tensor that is True where input is less than other and False elsewhere

Example:

>>> torch.lt(torch.tensor([[1, 2], [3, 4]]), torch.tensor([[1, 1], [4, 4]]))
tensor([[False, False], [True, False]])
geomstats._backend.pytorch.log(input, *, out=None)Tensor

Returns a new tensor with the natural logarithm of the elements of input.

\[y_{i} = \log_{e} (x_{i})\]
Args:

input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(5)
>>> a
tensor([-0.7168, -0.5471, -0.8933, -1.4428, -0.1190])
>>> torch.log(a)
tensor([ nan,  nan,  nan,  nan,  nan])
geomstats._backend.pytorch.matmul(input, other, *, out=None)Tensor

Matrix product of two tensors.

The behavior depends on the dimensionality of the tensors as follows:

  • If both tensors are 1-dimensional, the dot product (scalar) is returned.

  • If both arguments are 2-dimensional, the matrix-matrix product is returned.

  • If the first argument is 1-dimensional and the second argument is 2-dimensional, a 1 is prepended to its dimension for the purpose of the matrix multiply. After the matrix multiply, the prepended dimension is removed.

  • If the first argument is 2-dimensional and the second argument is 1-dimensional, the matrix-vector product is returned.

  • If both arguments are at least 1-dimensional and at least one argument is N-dimensional (where N > 2), then a batched matrix multiply is returned. If the first argument is 1-dimensional, a 1 is prepended to its dimension for the purpose of the batched matrix multiply and removed after. If the second argument is 1-dimensional, a 1 is appended to its dimension for the purpose of the batched matrix multiple and removed after. The non-matrix (i.e. batch) dimensions are broadcasted (and thus must be broadcastable). For example, if input is a \((j \times 1 \times n \times m)\) tensor and other is a \((k \times m \times p)\) tensor, out will be an \((j \times k \times n \times p)\) tensor.

This operator supports TensorFloat32.

Note

The 1-dimensional dot product version of this function does not support an out parameter.

Arguments:

input (Tensor): the first tensor to be multiplied other (Tensor): the second tensor to be multiplied

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> # vector x vector
>>> tensor1 = torch.randn(3)
>>> tensor2 = torch.randn(3)
>>> torch.matmul(tensor1, tensor2).size()
torch.Size([])
>>> # matrix x vector
>>> tensor1 = torch.randn(3, 4)
>>> tensor2 = torch.randn(4)
>>> torch.matmul(tensor1, tensor2).size()
torch.Size([3])
>>> # batched matrix x broadcasted vector
>>> tensor1 = torch.randn(10, 3, 4)
>>> tensor2 = torch.randn(4)
>>> torch.matmul(tensor1, tensor2).size()
torch.Size([10, 3])
>>> # batched matrix x batched matrix
>>> tensor1 = torch.randn(10, 3, 4)
>>> tensor2 = torch.randn(10, 4, 5)
>>> torch.matmul(tensor1, tensor2).size()
torch.Size([10, 3, 5])
>>> # batched matrix x broadcasted matrix
>>> tensor1 = torch.randn(10, 3, 4)
>>> tensor2 = torch.randn(4, 5)
>>> torch.matmul(tensor1, tensor2).size()
torch.Size([10, 3, 5])
geomstats._backend.pytorch.mean(input)Tensor

Returns the mean value of all elements in the input tensor.

Args:

input (Tensor): the input tensor.

Example:

>>> a = torch.randn(1, 3)
>>> a
tensor([[ 0.2294, -0.5481,  1.3288]])
>>> torch.mean(a)
tensor(0.3367)
geomstats._backend.pytorch.mean(input, dim, keepdim=False, *, out=None)Tensor

Returns the mean value of each row of the input tensor in the given dimension dim. If dim is a list of dimensions, reduce over all of them.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Args:

input (Tensor): the input tensor. dim (int or tuple of ints): the dimension or dimensions to reduce. keepdim (bool): whether the output tensor has dim retained or not.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4, 4)
>>> a
tensor([[-0.3841,  0.6320,  0.4254, -0.7384],
        [-0.9644,  1.0131, -0.6549, -1.4279],
        [-0.2951, -1.3350, -0.7694,  0.5600],
        [ 1.0842, -0.9580,  0.3623,  0.2343]])
>>> torch.mean(a, 1)
tensor([-0.0163, -0.5085, -0.4599,  0.1807])
>>> torch.mean(a, 1, True)
tensor([[-0.0163],
        [-0.5085],
        [-0.4599],
        [ 0.1807]])
geomstats._backend.pytorch.mod()

fmod(input, other, *, out=None) -> Tensor

Computes the element-wise remainder of division.

The dividend and divisor may contain both for integer and floating point numbers. The remainder has the same sign as the dividend input.

When other is a tensor, the shapes of input and other must be broadcastable.

Args:

input (Tensor): the dividend other (Tensor or float): the divisor, which may be either a number or a tensor of the same shape as the dividend

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> torch.fmod(torch.tensor([-3., -2, -1, 1, 2, 3]), 2)
tensor([-1., -0., -1.,  1.,  0.,  1.])
>>> torch.fmod(torch.tensor([1., 2, 3, 4, 5]), 1.5)
tensor([ 1.0000,  0.5000,  0.0000,  1.0000,  0.5000])
geomstats._backend.pytorch.nonzero(input, *, out=None, as_tuple=False)LongTensor or tuple of LongTensors

Note

torch.nonzero(..., as_tuple=False) (default) returns a 2-D tensor where each row is the index for a nonzero value.

torch.nonzero(..., as_tuple=True) returns a tuple of 1-D index tensors, allowing for advanced indexing, so x[x.nonzero(as_tuple=True)] gives all nonzero values of tensor x. Of the returned tuple, each index tensor contains nonzero indices for a certain dimension.

See below for more details on the two behaviors.

When input is on CUDA, torch.nonzero() causes host-device synchronization.

When as_tuple is ``False`` (default):

Returns a tensor containing the indices of all non-zero elements of input. Each row in the result contains the indices of a non-zero element in input. The result is sorted lexicographically, with the last index changing the fastest (C-style).

If input has \(n\) dimensions, then the resulting indices tensor out is of size \((z \times n)\), where \(z\) is the total number of non-zero elements in the input tensor.

When as_tuple is ``True``:

Returns a tuple of 1-D tensors, one for each dimension in input, each containing the indices (in that dimension) of all non-zero elements of input .

If input has \(n\) dimensions, then the resulting tuple contains \(n\) tensors of size \(z\), where \(z\) is the total number of non-zero elements in the input tensor.

As a special case, when input has zero dimensions and a nonzero scalar value, it is treated as a one-dimensional tensor with one element.

Args:

input (Tensor): the input tensor.

Keyword args:

out (LongTensor, optional): the output tensor containing indices

Returns:

LongTensor or tuple of LongTensor: If as_tuple is False, the output tensor containing indices. If as_tuple is True, one 1-D tensor for each dimension, containing the indices of each nonzero element along that dimension.

Example:

>>> torch.nonzero(torch.tensor([1, 1, 1, 0, 1]))
tensor([[ 0],
        [ 1],
        [ 2],
        [ 4]])
>>> torch.nonzero(torch.tensor([[0.6, 0.0, 0.0, 0.0],
                                [0.0, 0.4, 0.0, 0.0],
                                [0.0, 0.0, 1.2, 0.0],
                                [0.0, 0.0, 0.0,-0.4]]))
tensor([[ 0,  0],
        [ 1,  1],
        [ 2,  2],
        [ 3,  3]])
>>> torch.nonzero(torch.tensor([1, 1, 1, 0, 1]), as_tuple=True)
(tensor([0, 1, 2, 4]),)
>>> torch.nonzero(torch.tensor([[0.6, 0.0, 0.0, 0.0],
                                [0.0, 0.4, 0.0, 0.0],
                                [0.0, 0.0, 1.2, 0.0],
                                [0.0, 0.0, 0.0,-0.4]]), as_tuple=True)
(tensor([0, 1, 2, 3]), tensor([0, 1, 2, 3]))
>>> torch.nonzero(torch.tensor(5), as_tuple=True)
(tensor([0]),)
geomstats._backend.pytorch.ones(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)Tensor

Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument size.

Args:
size (int…): a sequence of integers defining the shape of the output tensor.

Can be a variable number of arguments or a collection like a list or tuple.

out (Tensor, optional): the output tensor. dtype (torch.dtype, optional): the desired data type of returned tensor.

Default: if None, uses a global default (see torch.set_default_tensor_type()).

layout (torch.layout, optional): the desired layout of returned Tensor.

Default: torch.strided.

device (torch.device, optional): the desired device of returned tensor.

Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

requires_grad (bool, optional): If autograd should record operations on the

returned tensor. Default: False.

Example:

>>> torch.ones(2, 3)
tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.]])

>>> torch.ones(5)
tensor([ 1.,  1.,  1.,  1.,  1.])
geomstats._backend.pytorch.ones_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format)Tensor

Returns a tensor filled with the scalar value 1, with the same size as input. torch.ones_like(input) is equivalent to torch.ones(input.size(), dtype=input.dtype, layout=input.layout, device=input.device).

Warning

As of 0.4, this function does not support an out keyword. As an alternative, the old torch.ones_like(input, out=output) is equivalent to torch.ones(input.size(), out=output).

Args:

input (Tensor): the size of input will determine size of the output tensor. dtype (torch.dtype, optional): the desired data type of returned Tensor.

Default: if None, defaults to the dtype of input.

layout (torch.layout, optional): the desired layout of returned tensor.

Default: if None, defaults to the layout of input.

device (torch.device, optional): the desired device of returned tensor.

Default: if None, defaults to the device of input.

requires_grad (bool, optional): If autograd should record operations on the

returned tensor. Default: False.

memory_format (torch.memory_format, optional): the desired memory format of

returned Tensor. Default: torch.preserve_format.

Example:

>>> input = torch.empty(2, 3)
>>> torch.ones_like(input)
tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.]])
geomstats._backend.pytorch.outer()

ger(input, vec2, *, out=None) -> Tensor

Alias of torch.outer().

Warning

This function is deprecated and will be removed in a future PyTorch release. Use torch.outer() instead.

geomstats._backend.pytorch.polygamma(n, input, *, out=None)Tensor

Computes the \(n^{th}\) derivative of the digamma function on input. \(n \geq 0\) is called the order of the polygamma function.

\[\psi^{(n)}(x) = \frac{d^{(n)}}{dx^{(n)}} \psi(x)\]

Note

This function is implemented only for nonnegative integers \(n \geq 0\).

Args:

n (int): the order of the polygamma function input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example::
>>> a = torch.tensor([1, 0.5])
>>> torch.polygamma(1, a)
tensor([1.64493, 4.9348])
>>> torch.polygamma(2, a)
tensor([ -2.4041, -16.8288])
>>> torch.polygamma(3, a)
tensor([ 6.4939, 97.4091])
>>> torch.polygamma(4, a)
tensor([ -24.8863, -771.4742])
geomstats._backend.pytorch.power()

pow(input, exponent, *, out=None) -> Tensor

Takes the power of each element in input with exponent and returns a tensor with the result.

exponent can be either a single float number or a Tensor with the same number of elements as input.

When exponent is a scalar value, the operation applied is:

\[\text{out}_i = x_i ^ \text{exponent}\]

When exponent is a tensor, the operation applied is:

\[\text{out}_i = x_i ^ {\text{exponent}_i}\]

When exponent is a tensor, the shapes of input and exponent must be broadcastable.

Args:

input (Tensor): the input tensor. exponent (float or tensor): the exponent value

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([ 0.4331,  1.2475,  0.6834, -0.2791])
>>> torch.pow(a, 2)
tensor([ 0.1875,  1.5561,  0.4670,  0.0779])
>>> exp = torch.arange(1., 5.)

>>> a = torch.arange(1., 5.)
>>> a
tensor([ 1.,  2.,  3.,  4.])
>>> exp
tensor([ 1.,  2.,  3.,  4.])
>>> torch.pow(a, exp)
tensor([   1.,    4.,   27.,  256.])
geomstats._backend.pytorch.pow(self, exponent, *, out=None)Tensor

self is a scalar float value, and exponent is a tensor. The returned tensor out is of the same shape as exponent

The operation applied is:

\[\text{out}_i = \text{self} ^ {\text{exponent}_i}\]
Args:

self (float): the scalar base value for the power operation exponent (Tensor): the exponent tensor

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> exp = torch.arange(1., 5.)
>>> base = 2
>>> torch.pow(base, exp)
tensor([  2.,   4.,   8.,  16.])
geomstats._backend.pytorch.repeat()

repeat_interleave(input, repeats, dim=None) -> Tensor

Repeat elements of a tensor.

Warning

This is different from torch.Tensor.repeat() but similar to numpy.repeat.

Args:

input (Tensor): the input tensor. repeats (Tensor or int): The number of repetitions for each element.

repeats is broadcasted to fit the shape of the given axis.

dim (int, optional): The dimension along which to repeat values.

By default, use the flattened input array, and return a flat output array.

Returns:
Tensor: Repeated tensor which has the same shape as input, except along the

given axis.

Example:

>>> x = torch.tensor([1, 2, 3])
>>> x.repeat_interleave(2)
tensor([1, 1, 2, 2, 3, 3])
>>> y = torch.tensor([[1, 2], [3, 4]])
>>> torch.repeat_interleave(y, 2)
tensor([1, 1, 2, 2, 3, 3, 4, 4])
>>> torch.repeat_interleave(y, 3, dim=1)
tensor([[1, 1, 1, 2, 2, 2],
        [3, 3, 3, 4, 4, 4]])
>>> torch.repeat_interleave(y, torch.tensor([1, 2]), dim=0)
tensor([[1, 2],
        [3, 4],
        [3, 4]])
geomstats._backend.pytorch.repeat_interleave(repeats)Tensor

If the repeats is tensor([n1, n2, n3, …]), then the output will be tensor([0, 0, …, 1, 1, …, 2, 2, …, …]) where 0 appears n1 times, 1 appears n2 times, 2 appears n3 times, etc.

geomstats._backend.pytorch.reshape(input, shape)Tensor

Returns a tensor with the same data and number of elements as input, but with the specified shape. When possible, the returned tensor will be a view of input. Otherwise, it will be a copy. Contiguous inputs and inputs with compatible strides can be reshaped without copying, but you should not depend on the copying vs. viewing behavior.

See torch.Tensor.view() on when it is possible to return a view.

A single dimension may be -1, in which case it’s inferred from the remaining dimensions and the number of elements in input.

Args:

input (Tensor): the tensor to be reshaped shape (tuple of ints): the new shape

Example:

>>> a = torch.arange(4.)
>>> torch.reshape(a, (2, 2))
tensor([[ 0.,  1.],
        [ 2.,  3.]])
>>> b = torch.tensor([[0, 1], [2, 3]])
>>> torch.reshape(b, (-1,))
tensor([ 0,  1,  2,  3])
geomstats._backend.pytorch.set_diag(x, new_diag)[source]

Set the diagonal along the last two axis.

Parameters
  • x (array-like, shape=[dim]) – Initial array.

  • new_diag (array-like, shape=[dim[-2]]) – Values to set on the diagonal.

Returns

None

Notes

This mimics tensorflow.linalg.set_diag(x, new_diag), when new_diag is a 1-D array, but modifies x instead of creating a copy.

geomstats._backend.pytorch.sign(input, *, out=None)Tensor

Returns a new tensor with the signs of the elements of input.

\[\text{out}_{i} = \operatorname{sgn}(\text{input}_{i})\]
Args:

input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.tensor([0.7, -1.2, 0., 2.3])
>>> a
tensor([ 0.7000, -1.2000,  0.0000,  2.3000])
>>> torch.sign(a)
tensor([ 1., -1.,  0.,  1.])
geomstats._backend.pytorch.sin(input, *, out=None)Tensor

Returns a new tensor with the sine of the elements of input.

\[\text{out}_{i} = \sin(\text{input}_{i})\]
Args:

input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([-0.5461,  0.1347, -2.7266, -0.2746])
>>> torch.sin(a)
tensor([-0.5194,  0.1343, -0.4032, -0.2711])
geomstats._backend.pytorch.sinh(input, *, out=None)Tensor

Returns a new tensor with the hyperbolic sine of the elements of input.

\[\text{out}_{i} = \sinh(\text{input}_{i})\]
Args:

input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([ 0.5380, -0.8632, -0.1265,  0.9399])
>>> torch.sinh(a)
tensor([ 0.5644, -0.9744, -0.1268,  1.0845])
geomstats._backend.pytorch.stack(tensors, dim=0, *, out=None)Tensor

Concatenates a sequence of tensors along a new dimension.

All tensors need to be of the same size.

Arguments:

tensors (sequence of Tensors): sequence of tensors to concatenate dim (int): dimension to insert. Has to be between 0 and the number

of dimensions of concatenated tensors (inclusive)

Keyword args:

out (Tensor, optional): the output tensor.

geomstats._backend.pytorch.std(input, unbiased=True)Tensor

Returns the standard-deviation of all elements in the input tensor.

If unbiased is False, then the standard-deviation will be calculated via the biased estimator. Otherwise, Bessel’s correction will be used.

Args:

input (Tensor): the input tensor. unbiased (bool): whether to use the unbiased estimation or not

Example:

>>> a = torch.randn(1, 3)
>>> a
tensor([[-0.8166, -1.3802, -0.3560]])
>>> torch.std(a)
tensor(0.5130)
geomstats._backend.pytorch.std(input, dim, unbiased=True, keepdim=False, *, out=None)Tensor

Returns the standard-deviation of each row of the input tensor in the dimension dim. If dim is a list of dimensions, reduce over all of them.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see torch.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

If unbiased is False, then the standard-deviation will be calculated via the biased estimator. Otherwise, Bessel’s correction will be used.

Args:

input (Tensor): the input tensor. dim (int or tuple of ints): the dimension or dimensions to reduce. unbiased (bool): whether to use the unbiased estimation or not keepdim (bool): whether the output tensor has dim retained or not.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4, 4)
>>> a
tensor([[ 0.2035,  1.2959,  1.8101, -0.4644],
        [ 1.5027, -0.3270,  0.5905,  0.6538],
        [-1.5745,  1.3330, -0.5596, -0.6548],
        [ 0.1264, -0.5080,  1.6420,  0.1992]])
>>> torch.std(a, dim=1)
tensor([ 1.0311,  0.7477,  1.2204,  0.9087])
geomstats._backend.pytorch.tan(input, *, out=None)Tensor

Returns a new tensor with the tangent of the elements of input.

\[\text{out}_{i} = \tan(\text{input}_{i})\]
Args:

input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([-1.2027, -1.7687,  0.4412, -1.3856])
>>> torch.tan(a)
tensor([-2.5930,  4.9859,  0.4722, -5.3366])
geomstats._backend.pytorch.tanh(input, *, out=None)Tensor

Returns a new tensor with the hyperbolic tangent of the elements of input.

\[\text{out}_{i} = \tanh(\text{input}_{i})\]
Args:

input (Tensor): the input tensor.

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(4)
>>> a
tensor([ 0.8986, -0.7279,  1.1745,  0.2611])
>>> torch.tanh(a)
tensor([ 0.7156, -0.6218,  0.8257,  0.2553])
geomstats._backend.pytorch.tril(input, diagonal=0, *, out=None)Tensor

Returns the lower triangular part of the matrix (2-D tensor) or batch of matrices input, the other elements of the result tensor out are set to 0.

The lower triangular part of the matrix is defined as the elements on and below the diagonal.

The argument diagonal controls which diagonal to consider. If diagonal = 0, all elements on and below the main diagonal are retained. A positive value includes just as many diagonals above the main diagonal, and similarly a negative value excludes just as many diagonals below the main diagonal. The main diagonal are the set of indices \(\lbrace (i, i) \rbrace\) for \(i \in [0, \min\{d_{1}, d_{2}\} - 1]\) where \(d_{1}, d_{2}\) are the dimensions of the matrix.

Args:

input (Tensor): the input tensor. diagonal (int, optional): the diagonal to consider

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.randn(3, 3)
>>> a
tensor([[-1.0813, -0.8619,  0.7105],
        [ 0.0935,  0.1380,  2.2112],
        [-0.3409, -0.9828,  0.0289]])
>>> torch.tril(a)
tensor([[-1.0813,  0.0000,  0.0000],
        [ 0.0935,  0.1380,  0.0000],
        [-0.3409, -0.9828,  0.0289]])

>>> b = torch.randn(4, 6)
>>> b
tensor([[ 1.2219,  0.5653, -0.2521, -0.2345,  1.2544,  0.3461],
        [ 0.4785, -0.4477,  0.6049,  0.6368,  0.8775,  0.7145],
        [ 1.1502,  3.2716, -1.1243, -0.5413,  0.3615,  0.6864],
        [-0.0614, -0.7344, -1.3164, -0.7648, -1.4024,  0.0978]])
>>> torch.tril(b, diagonal=1)
tensor([[ 1.2219,  0.5653,  0.0000,  0.0000,  0.0000,  0.0000],
        [ 0.4785, -0.4477,  0.6049,  0.0000,  0.0000,  0.0000],
        [ 1.1502,  3.2716, -1.1243, -0.5413,  0.0000,  0.0000],
        [-0.0614, -0.7344, -1.3164, -0.7648, -1.4024,  0.0000]])
>>> torch.tril(b, diagonal=-1)
tensor([[ 0.0000,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000],
        [ 0.4785,  0.0000,  0.0000,  0.0000,  0.0000,  0.0000],
        [ 1.1502,  3.2716,  0.0000,  0.0000,  0.0000,  0.0000],
        [-0.0614, -0.7344, -1.3164,  0.0000,  0.0000,  0.0000]])
geomstats._backend.pytorch.zeros(*size, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False)Tensor

Returns a tensor filled with the scalar value 0, with the shape defined by the variable argument size.

Args:
size (int…): a sequence of integers defining the shape of the output tensor.

Can be a variable number of arguments or a collection like a list or tuple.

Keyword args:

out (Tensor, optional): the output tensor. dtype (torch.dtype, optional): the desired data type of returned tensor.

Default: if None, uses a global default (see torch.set_default_tensor_type()).

layout (torch.layout, optional): the desired layout of returned Tensor.

Default: torch.strided.

device (torch.device, optional): the desired device of returned tensor.

Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.

requires_grad (bool, optional): If autograd should record operations on the

returned tensor. Default: False.

Example:

>>> torch.zeros(2, 3)
tensor([[ 0.,  0.,  0.],
        [ 0.,  0.,  0.]])

>>> torch.zeros(5)
tensor([ 0.,  0.,  0.,  0.,  0.])
geomstats._backend.pytorch.zeros_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format)Tensor

Returns a tensor filled with the scalar value 0, with the same size as input. torch.zeros_like(input) is equivalent to torch.zeros(input.size(), dtype=input.dtype, layout=input.layout, device=input.device).

Warning

As of 0.4, this function does not support an out keyword. As an alternative, the old torch.zeros_like(input, out=output) is equivalent to torch.zeros(input.size(), out=output).

Args:

input (Tensor): the size of input will determine size of the output tensor.

Keyword args:
dtype (torch.dtype, optional): the desired data type of returned Tensor.

Default: if None, defaults to the dtype of input.

layout (torch.layout, optional): the desired layout of returned tensor.

Default: if None, defaults to the layout of input.

device (torch.device, optional): the desired device of returned tensor.

Default: if None, defaults to the device of input.

requires_grad (bool, optional): If autograd should record operations on the

returned tensor. Default: False.

memory_format (torch.memory_format, optional): the desired memory format of

returned Tensor. Default: torch.preserve_format.

Example:

>>> input = torch.empty(2, 3)
>>> torch.zeros_like(input)
tensor([[ 0.,  0.,  0.],
        [ 0.,  0.,  0.]])

Tensorflow based computation backend.

geomstats._backend.tensorflow.array_from_sparse(indices, data, target_shape)[source]

Create an array of given shape, with values at specific indices.

The rest of the array will be filled with zeros.

Parameters
  • indices (iterable(tuple(int))) – Index of each element which will be assigned a specific value.

  • data (iterable(scalar)) – Value associated at each index.

  • target_shape (tuple(int)) – Shape of the output array.

Returns

a (array, shape=target_shape) – Array of zeros with specified values assigned to specified indices.

geomstats._backend.tensorflow.assignment(x, values, indices, axis=0)[source]

Add values at given indices of an array.

Parameters
  • x (array-like, shape=[dim]) – Initial array.

  • values ({float, list(float)}) – Value or list of values to be assigned.

  • indices ({)

  • int, tuple(int), array-like({int, tuple, boolean}) – Single index or array of indices where values are assigned. If the length of the tuples is shorter than ndim(x) by one, values are assigned to each copy along axis. If indices is a list of booleans and ndim(x) > 1, values are assigned across all dimensions.

  • axis (int, optional) – Axis along which values are assigned, if vectorized.

Returns

x_new (array-like, shape=[dim]) – Copy of x as the sum of x and the values at the given indices.

Notes

If a single value is provided, it is assigned at all the indices. If a single index is provided, and len(indices) == ndim(x) - 1, then values are assigned along axis.

Examples

Most examples translate as assignment(x, indices, values) <=> x[indices] = values Some special cases are given by vectorisation. (Beware that copies are always returned). if ndim(x) == 3, assignment(x, 1, (1, 0), 1) <=> x[1, :, 0] = 1 if ndim(x) == 2, assignment(x, [1, 2], [(0, 1), (2, 3)]) <=>

x[((0, 2), (1, 3))] = [1, 2]

geomstats._backend.tensorflow.assignment_by_sum(x, values, indices, axis=0)[source]

Add values at given indices of an array.

Parameters
  • x (array-like, shape=[dim]) – Initial array.

  • values ({float, list(float)}) – Value or list of values to be assigned.

  • indices ({)

  • int, tuple(int), array-like({int, tuple, boolean}) – Single index or array of indices where values are assigned. If the length of the tuples is shorter than ndim(x) by one, values are assigned to each copy along axis. If indices is a list of booleans and ndim(x) > 1, values are assigned across all dimensions.

  • axis (int, optional) – Axis along which values are assigned, if vectorized.

Returns

x_new (array-like, shape=[dim]) – Copy of x as the sum of x and the values at the given indices.

Notes

If a single value is provided, it is assigned at all the indices. If a single index is provided, and len(indices) == ndim(x) - 1, then values are assigned along axis.

Examples

Most examples translate as assignment_by_sum(x, indices, values) <=> x[indices] = x[indices] + values Some special cases are given by vectorisation. (Beware that copies are always returned). if ndim(x) == 3, assignment_by_sum(x, 1, (1, 0), 1) <=> x[1, :, 0] += 1 if ndim(x) == 2, assignment_by_sum(x, [1, 2], [(0, 1), (2, 3)]) <=>

x[((0, 2), (1, 3))] += [1, 2]

geomstats._backend.tensorflow.flatten(x)[source]

Collapse the tensor into 1-D.

Following https://www.tensorflow.org/api_docs/python/tf/reshape

geomstats._backend.tensorflow.get_mask_i_float(i, n)[source]

Create a 1D array of zeros with one element at one, with floating type.

Parameters
  • i (int) – Index of the non-zero element.

  • n (n) – Length of the created array.

Returns

mask_i_float (array-like, shape=[n,]) – 1D array of zeros except at index i, where it is one

geomstats._backend.tensorflow.get_slice(x, indices)[source]

Return a slice of an array, following Numpy’s style.

Parameters
  • x (array-like, shape=[dim]) – Initial array.

  • indices (iterable(iterable(int))) – Indices which are kept along each axis, starting from 0.

Returns

slice (array-like) – Slice of x given by indices.

Notes

This follows Numpy’s convention: indices are grouped by axis.

Examples

>>> a = tf.reshape(tf.convert_to_tensor(range(30)), (3,10))
>>> get_slice(a, ((0, 2), (8, 9)))
<tf.Tensor: id=41, shape=(2,), dtype=int32, numpy=array([ 8, 29])>
geomstats._backend.tensorflow.matmul(a, b)[source]

Matrix-matrix or matrix-vector product of two tensors.

This wraps both mathvec and matmul into a single function, to mimic the behavior of torch’s and numpy’s versions of matmul