# 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.

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.mat_from_diag_triu_tril(diag, tri_upp, tri_low)[source]

Build matrix from given components.

Forms a matrix from diagonal, strictly upper triangular and strictly lower traingular parts.

Parameters
• diag (array_like, shape=[…, n])

• tri_upp (array_like, shape=[…, (n * (n - 1)) / 2])

• tri_low (array_like, shape=[…, (n * (n - 1)) / 2])

Returns

mat (array_like, shape=[…, n, n])

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.

geomstats._backend.numpy.vec_to_diag(vec)[source]

Convert vector to diagonal matrix.

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(input, *, out=None) Tensor

Alias for torch.acos().

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

Alias for torch.acosh().

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

Alias for torch.asin().

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.arctanh(input, *, out=None) Tensor

Alias for torch.atanh().

geomstats._backend.pytorch.argmin(input, dim=None, keepdim=False) LongTensor

Returns the indices of the minimum value(s) of the flattened tensor or along a dimension

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. 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)
tensor(13)
>>> torch.argmin(a, dim=1)
tensor([ 2,  1,  3,  1])
>>> torch.argmin(a, dim=1, keepdim=True)
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$
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(input, min=None, max=None, *, out=None) Tensor

Alias for torch.clamp().

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])


Note

When input is on the CPU, the implementation of torch.cosh may use the Sleef library, which rounds very large results to infinity or negative infinity. See here for details.

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(input, other, *, rounding_mode=None, out=None) Tensor

Alias for torch.div().

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

Alias for torch.special.erf().

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

Keyword arguments:

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 input by reshaping it into a one-dimensional tensor. If start_dim or end_dim are passed, only dimensions starting with start_dim and ending with end_dim are flattened. The order of elements in input is unchanged.

Unlike NumPy’s flatten, which always copies input’s data, this function may return the original object, a view, or copy. If no dimensions are flattened, then the original object input is returned. Otherwise, if input can be viewed as the flattened shape, then that view is returned. Finally, only if the input cannot be viewed as the flattened shape is input’s data copied. See torch.Tensor.view() for details on when a view will be returned.

Note

Flattening a zero-dimensional tensor will return a one-dimensional view.

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.])


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(input, other, *, out=None) Tensor

Alias for torch.gt().

geomstats._backend.pytorch.hstack(tensors, *, out=None) Tensor

Stack tensors in sequence horizontally (column wise).

This is equivalent to concatenation along the first axis for 1-D tensors, and along the second axis for all other tensors.

Args:

tensors (sequence of Tensors): sequence of tensors to concatenate

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.tensor([1, 2, 3])
>>> b = torch.tensor([4, 5, 6])
>>> torch.hstack((a,b))
tensor([1, 2, 3, 4, 5, 6])
>>> a = torch.tensor([[1],[2],[3]])
>>> b = torch.tensor([[4],[5],[6]])
>>> torch.hstack((a,b))
tensor([[1, 4],
[2, 5],
[3, 6]])

geomstats._backend.pytorch.imag(input) Tensor

Returns a new tensor containing imaginary values of the self tensor. The returned tensor and self share the same underlying storage.

Warning

imag() is only supported for tensors with complex dtypes.

Args:

input (Tensor): the input tensor.

Example:

>>> x=torch.randn(4, dtype=torch.cfloat)
>>> x
tensor([(0.3100+0.3553j), (-0.5445-0.7896j), (-1.6492-0.0633j), (-0.0638-0.8119j)])
>>> x.imag
tensor([ 0.3553, -0.7896, -0.0633, -0.8119])

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(input, other, *, out=None) Tensor

Alias for torch.lt().

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.logical_or(input, other, *, out=None) Tensor

Computes the element-wise logical OR of the given input tensors. Zeros are treated as False and nonzeros are treated as True.

Args:

input (Tensor): the input tensor. other (Tensor): the tensor to compute OR with

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> torch.logical_or(torch.tensor([True, False, True]), torch.tensor([True, False, False]))
tensor([ True, False,  True])
>>> a = torch.tensor([0, 1, 10, 0], dtype=torch.int8)
>>> b = torch.tensor([4, 0, 1, 0], dtype=torch.int8)
>>> torch.logical_or(a, b)
tensor([ True,  True,  True, False])
>>> torch.logical_or(a.double(), b.double())
tensor([ True,  True,  True, False])
>>> torch.logical_or(a.double(), b)
tensor([ True,  True,  True, False])
>>> torch.logical_or(a, b, out=torch.empty(4, dtype=torch.bool))
tensor([ True,  True,  True, False])

geomstats._backend.pytorch.mat_from_diag_triu_tril(diag, tri_upp, tri_low)[source]

Build matrix from given components.

Forms a matrix from diagonal, strictly upper triangular and strictly lower traingular parts.

Parameters
• diag (array_like, shape=[…, n])

• tri_upp (array_like, shape=[…, (n * (n - 1)) / 2])

• tri_low (array_like, shape=[…, (n * (n - 1)) / 2])

Returns

mat (array_like, shape=[…, n, n])

geomstats._backend.pytorch.mean(input, *, dtype=None) Tensor

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

Args:

input (Tensor): the input tensor.

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

If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.

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, *, dtype=None, 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:
dtype (torch.dtype, optional): the desired data type of returned tensor.

If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.

out (Tensor, optional): the output tensor.

torch.nanmean() computes the mean value of non-NaN elements.

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

Applies C++’s std::fmod for floating point tensors, and the modulus operation for integer tensors. The result has the same sign as the dividend input and its absolute value is less than that of other.

Supports broadcasting to a common shape, type promotion, and integer and float inputs.

Note

When the divisor is zero, returns NaN for floating point dtypes on both CPU and GPU; raises RuntimeError for integer division by zero on CPU; Integer division by zero on GPU may return any value.

Note

Complex inputs are not supported. In some cases, it is not mathematically possible to satisfy the definition of a modulo operation with complex numbers.

Args:

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

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])


torch.remainder() which is similar to torch.fmod() except that if the sign of the modulus is different than the sign of the divisor other then the divisor is added to the modulus.

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.

Keyword arguments:

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.

Keyword arguments:
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(input, vec2, *, out=None) Tensor

Outer product of input and vec2. If input is a vector of size $$n$$ and vec2 is a vector of size $$m$$, then out must be a matrix of size $$(n \times m)$$.

Note

This function does not broadcast.

Args:

input (Tensor): 1-D input vector vec2 (Tensor): 1-D input vector

Keyword args:

out (Tensor, optional): optional output matrix

Example:

>>> v1 = torch.arange(1., 5.)
>>> v2 = torch.arange(1., 4.)
>>> torch.outer(v1, v2)
tensor([[  1.,   2.,   3.],
[  2.,   4.,   6.],
[  3.,   6.,   9.],
[  4.,   8.,  12.]])

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

Alias for torch.special.polygamma().

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.real(input) Tensor

Returns a new tensor containing real values of the self tensor. The returned tensor and self share the same underlying storage.

Warning

real() is only supported for tensors with complex dtypes.

Args:

input (Tensor): the input tensor.

Example:

>>> x=torch.randn(4, dtype=torch.cfloat)
>>> x
tensor([(0.3100+0.3553j), (-0.5445-0.7896j), (-1.6492-0.0633j), (-0.0638-0.8119j)])
>>> x.real
tensor([ 0.3100, -0.5445, -1.6492, -0.0638])

geomstats._backend.pytorch.repeat()

repeat_interleave(input, repeats, dim=None, *, output_size=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.

Keyword args:
output_size (int, optional): Total output size for the given axis

( e.g. sum of repeats). If given, it will avoid stream syncronization needed to calculate output shape of the tensor.

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]])
>>> torch.repeat_interleave(y, torch.tensor([1, 2]), dim=0, output_size=3)
tensor([[1, 2],
[3, 4],
[3, 4]])

geomstats._backend.pytorch.repeat_interleave(repeats, *, output_size=None) 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])


Note

When input is on the CPU, the implementation of torch.sinh may use the Sleef library, which rounds very large results to infinity or negative infinity. See here for details.

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, dim, unbiased, keepdim=False, *, out=None) Tensor

If unbiased is True, Bessel’s correction will be used. Otherwise, the sample deviation is calculated, without any correction.

Args:

input (Tensor): the input tensor. dim (int or tuple of ints): the dimension or dimensions to reduce.

Keyword args:

unbiased (bool): whether to use Bessel’s correction ($$\delta N = 1$$). keepdim (bool): whether the output tensor has dim retained or not. out (Tensor, optional): the output tensor.

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

Calculates the standard deviation of all elements in the input tensor.

If unbiased is True, Bessel’s correction will be used. Otherwise, the sample deviation is calculated, without any correction.

Args:

input (Tensor): the input tensor. unbiased (bool): whether to use Bessel’s correction ($$\delta N = 1$$).

Example:

>>> a = torch.tensor([[-0.8166, -1.3802, -0.3560]])
>>> torch.std(a, unbiased=False)
tensor(0.4188)

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.vstack(tensors, *, out=None) Tensor

Stack tensors in sequence vertically (row wise).

This is equivalent to concatenation along the first axis after all 1-D tensors have been reshaped by torch.atleast_2d().

Args:

tensors (sequence of Tensors): sequence of tensors to concatenate

Keyword args:

out (Tensor, optional): the output tensor.

Example:

>>> a = torch.tensor([1, 2, 3])
>>> b = torch.tensor([4, 5, 6])
>>> torch.vstack((a,b))
tensor([[1, 2, 3],
[4, 5, 6]])
>>> a = torch.tensor([[1],[2],[3]])
>>> b = torch.tensor([[4],[5],[6]])
>>> torch.vstack((a,b))
tensor([[1],
[2],
[3],
[4],
[5],
[6]])

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.

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.mat_from_diag_triu_tril(diag, tri_upp, tri_low)[source]

Build matrix from given components.

Forms a matrix from diagonal, strictly upper triangular and strictly lower traingular parts.

Parameters
• diag (array_like, shape=[…, n])

• tri_upp (array_like, shape=[…, (n * (n - 1)) / 2])

• tri_low (array_like, shape=[…, (n * (n - 1)) / 2])

Returns

mat (array_like, shape=[…, n, n])

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

geomstats._backend.tensorflow.vec_to_tril(vec)[source]

Take vec and forms strictly lower triangular matrix.

Parameters

vec (array_like, shape=[…, n])

Returns

tril (array_like, shape=[…, k, k] where) – k is (1 + sqrt(1 + 8 * n)) / 2

geomstats._backend.tensorflow.vec_to_triu(vec)[source]

Take vec and forms strictly upper triangular matrix.

Parameters

vec (array_like, shape[…, n])

Returns

tril (array_like, shape=[…, k, k] where) – k is (1 + sqrt(1 + 8 * n)) / 2