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