matrix_rank#

ivy.matrix_rank(x, /, *, atol=None, rtol=None, hermitian=False, out=None)[source]#

Return the rank (i.e., number of non-zero singular values) of a matrix (or a stack of matrices).

Parameters:
  • x (Union[Array, NativeArray]) – input array having shape (..., M, N) and whose innermost two dimensions form MxN matrices. Should have a floating-point data type.

  • atol (Optional[Union[float, Tuple[float]]], default: None) – absolute tolerance. When None it’s considered to be zero.

  • rtol (Optional[Union[float, Tuple[float]]], default: None) – relative tolerance for small singular values. Singular values approximately less than or equal to rtol * largest_singular_value are set to zero. If a float, the value is equivalent to a zero-dimensional array having a floating-point data type determined by type-promotion (as applied to x) and must be broadcast against each matrix. If an array, must have a floating-point data type and must be compatible with shape(x)[:-2] (see broadcasting). If None, the default value is max(M, N) * eps, where eps must be the machine epsilon associated with the floating-point data type determined by type-promotion (as applied to x). Default: None.

  • hermitian (Optional[bool], default: False) – indicates whether x is Hermitian. When hermitian=True, x is assumed to be Hermitian, enabling a more efficient method for finding eigenvalues, but x is not checked inside the function. Instead, We just use the lower triangular of the matrix to compute. Default: False.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – an array containing the ranks. The returned array must have a floating-point data type determined by type-promotion and must have shape (...) (i.e., must have a shape equal to shape(x)[:-2]).

This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.

Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts ivy.Container instances in place of any of the arguments.

Examples

With :code: ‘ivy.Array’ inputs:

  1. Full Matrix

>>> x = ivy.array([[1., 2.], [3., 4.]])
>>> ivy.matrix_rank(x)
ivy.array(2.)
  1. Rank Deficient Matrix

>>> x = ivy.array([[1., 0.], [0., 0.]])
>>> ivy.matrix_rank(x)
ivy.array(1.)
  1. 1 Dimension - rank 1 unless all 0

>>> x = ivy.array([[1., 1.])
>>> ivy.matrix_rank(x)
ivy.array(1.)
>>> x = ivy.array([[0., 0.])
>>> ivy.matrix_rank(x)
ivy.array(0)

With :code: ‘ivy.NativeArray’ inputs:

>>> x = ivy.native_array([[1., 2.], [3., 4.]], [[1., 0.], [0., 0.]])
>>> ivy.matrix_rank(x)
ivy.array([2., 1.])

With :code: ‘ivy.Container’ inputs: >>> x = ivy.Container(a = ivy.array([[1., 2.], [3., 4.]]), b = ivy.array([[1., 0.], [0., 0.]])) >>> ivy.matrix_rank(x) {

a:ivy.array(2.), b:ivy.array(1.)

}

Array.matrix_rank(self, /, *, atol=None, rtol=None, hermitian=False, out=None)[source]#

ivy.Array instance method variant of ivy.matrix_rank. This method returns the rank (i.e., number of non-zero singular values) of a matrix (or a stack of matrices).

Parameters:
  • self (Array) – input array having shape (..., M, N) and whose innermost two dimensions form MxN matrices. Should have a floating-point data type.

  • atol (Optional[Union[float, Tuple[float]]], default: None) – absolute tolerance. When None it’s considered to be zero.

  • rtol (Optional[Union[float, Tuple[float]]], default: None) – relative tolerance for small singular values. Singular values approximately less than or equal to rtol * largest_singular_value are set to zero. If a float, the value is equivalent to a zero-dimensional array having a floating-point data type determined by type-promotion (as applied to x) and must be broadcast against each matrix. If an array, must have a floating-point data type and must be compatible with shape(x)[:-2] (see broadcasting). If None, the default value is max(M, N) * eps, where eps must be the machine epsilon associated with the floating-point data type determined by type-promotion (as applied to x). Default: None.

  • hermitian (Optional[bool], default: False) – indicates whether x is Hermitian. When hermitian=True, x is assumed to be Hermitian, enabling a more efficient method for finding eigenvalues, but x is not checked inside the function. Instead, We just use the lower triangular of the matrix to compute. Default: False.

  • out (Optional[Array], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

ret – a container containing the ranks. The returned array must have a floating-point data type determined by type-promotion and must have shape (...) (i.e., must have a shape equal to shape(x)[:-2]).

Examples

1. Full Matrix >>> x = ivy.array([[1., 2.], [3., 4.]]) >>> ivy.matrix_rank(x) ivy.array(2.)

2. Rank Deficient Matrix >>> x = ivy.array([[1., 0.], [0., 0.]]) >>> ivy.matrix_rank(x) ivy.array(1.)

3. 1 Dimension - rank 1 unless all 0 >>> x = ivy.array([[1., 1.]) >>> ivy.matrix_rank(x) ivy.array(1.)

>>> x = ivy.array([[0., 0.])
>>> ivy.matrix_rank(x)
ivy.array(0)
Container.matrix_rank(self, /, *, atol=None, rtol=None, hermitian=False, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.matrix_rank. This method returns the rank (i.e., number of non-zero singular values) of a matrix (or a stack of matrices).

Parameters:
  • self (Container) – input container having shape (..., M, N) and whose innermost two dimensions form MxN matrices. Should have a floating-point data type.

  • atol (Optional[Union[float, Tuple[float], Container]], default: None) – absolute tolerance. When None it’s considered to be zero.

  • rtol (Optional[Union[float, Tuple[float], Container]], default: None) – relative tolerance for small singular values. Singular values approximately less than or equal to rtol * largest_singular_value are set to zero. If a float, the value is equivalent to a zero-dimensional array having a floating-point data type determined by type-promotion (as applied to x) and must be broadcast against each matrix. If an array, must have a floating-point data type and must be compatible with shape(x)[:-2] (see broadcasting). If None, the default value is max(M, N) * eps, where eps must be the machine epsilon associated with the floating-point data type determined by type-promotion (as applied to x). Default: None.

  • hermitian (Optional[Union[bool, Container]], default: False) – indicates whether x is Hermitian. When hermitian=True, x is assumed to be Hermitian, enabling a more efficient method for finding eigenvalues, but x is not checked inside the function. Instead, We just use the lower triangular of the matrix to compute. Default: False.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

  • out (Optional[Container], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Container

Returns:

ret – a container containing the ranks. The returned array must have a floating-point data type determined by type-promotion and must have shape (...) (i.e., must have a shape equal to shape(x)[:-2]).

Examples

With ivy.Container input: >>> x = ivy.Container(a=ivy.array([[1., 0.], [0., 1.]]), … b=ivy.array([[1., 0.], [0., 0.]])) >>> y = x.matrix_rank() >>> print(y) {

a: ivy.array(2), b: ivy.array(1)

}