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.

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