qr#

ivy.qr(x, /, *, mode='reduced', out=None)[source]#

Return the qr decomposition x = QR of a full column rank matrix (or a stack of matrices), where Q is an orthonormal matrix (or a stack of matrices) and R is an upper-triangular 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 of rank N. Should have a floating-point data type.

  • mode (str, default: 'reduced') –

    decomposition mode. Should be one of the following modes: - ‘reduced’: compute only the leading K columns of q, such that q and r have

    dimensions (…, M, K) and (…, K, N), respectively, and where K = min(M, N).

    • ’complete’: compute q and r with dimensions (…, M, M) and (…, M, N), respectively.

    Default: ‘reduced’.

  • out (Optional[Tuple[Array, Array]], default: None) – optional output tuple of arrays, for writing the result to. The arrays must have shapes that the inputs broadcast to.

Return type:

Tuple[Array, Array]

Returns:

ret – a namedtuple (Q, R) whose - first element must have the field name Q and must be an array whose shape

depends on the value of mode and contain matrices with orthonormal columns. If mode is ‘complete’, the array must have shape (…, M, M). If mode is ‘reduced’, the array must have shape (…, M, K), where K = min(M, N). The first x.ndim-2 dimensions must have the same size as those of the input array x.

  • second element must have the field name R and must be an array whose shape depends on the value of mode and contain upper-triangular matrices. If mode is ‘complete’, the array must have shape (…, M, N). If mode is ‘reduced’, the array must have shape (…, K, N), where K = min(M, N). The first x.ndim-2 dimensions must have the same size as those of the input x.

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 ivy.Array input:

>>> x = ivy.array([[1.,2.,3.],[4.,5.,6.],[7.,8.,9.]])
>>> q, r = ivy.qr(x)
>>> print(q)
ivy.array([[-0.12309149,  0.90453403,  0.40824829],
    [-0.49236596,  0.30151134, -0.81649658],
    [-0.86164044, -0.30151134,  0.40824829]])
>>> print(r)
ivy.array([[-8.12403841e+00,-9.60113630e+00, -1.10782342e+01],
    [ 0.00000000e+00,  9.04534034e-01,  1.80906807e+00],
    [ 0.00000000e+00,  0.00000000e+00, -8.88178420e-16]])

# Note: if int values are used in x the output for q, r vary >>> x = ivy.array([[1., 2.], [3., 4.]]) >>> q = ivy.zeros_like(x) >>> r = ivy.zeros_like(x) >>> ivy.qr(x, out=(q,r)) >>> print(q) ivy.array([[-0.31622776, -0.94868332],

[-0.94868332, 0.31622776]])

>>> print(r)
ivy.array([[-3.1622777 , -4.42718887],
       [ 0.        , -0.63245553]])

With ivy.Container input:

>>> x = ivy.Container(a = ivy.native_array([[1., 2.], [3., 4.]]),
...                   b = ivy.array([[2., 3.], [4. ,5.]]))
>>> q,r = ivy.qr(x, mode='complete')
>>> print(q)
{
    a: ivy.array([[-0.31622777, -0.9486833],
                [-0.9486833, 0.31622777]]),
    b: ivy.array([[-0.4472136, -0.89442719],
                [-0.89442719, 0.4472136]])
}
>>> print(r)
{
    a: ivy.array([[-3.16227766, -4.42718872],
                [0., -0.63245553]]),
    b: ivy.array([[-4.47213595, -5.81377674],
                [0., -0.4472136]])
}
Array.qr(self, /, *, mode='reduced', out=None)[source]#

ivy.Array instance method variant of ivy.qr. This method simply wraps the function, and so the docstring for ivy.qr also applies to this method with minimal changes.

Returns the qr decomposition x = QR of a full column rank matrix (or a stack of matrices), where Q is an orthonormal matrix (or a stack of matrices) and R is an upper-triangular matrix (or a stack of matrices).

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

  • mode (str, default: 'reduced') – decomposition mode. Should be one of the following modes: - ‘reduced’: compute only the leading K columns of q, such that q and r have dimensions (…, M, K) and (…, K, N), respectively, and where K = min(M, N). - ‘complete’: compute q and r with dimensions (…, M, M) and (…, M, N), respectively. Default: ‘reduced’.

  • out (Optional[Tuple[Array, Array]], default: None) – optional output tuple of arrays, for writing the result to. The arrays must have shapes that the inputs broadcast to.

Return type:

Tuple[Array, Array]

Returns:

ret – a namedtuple (Q, R) whose - first element must have the field name Q and must be an array whose shape depends on the value of mode and contain matrices with orthonormal columns. If mode is ‘complete’, the array must have shape (…, M, M). If mode is ‘reduced’, the array must have shape (…, M, K), where K = min(M, N). The first x.ndim-2 dimensions must have the same size as those of the input array x. - second element must have the field name R and must be an array whose shape depends on the value of mode and contain upper-triangular matrices. If mode is ‘complete’, the array must have shape (…, M, N). If mode is ‘reduced’, the array must have shape (…, K, N), where K = min(M, N). The first x.ndim-2 dimensions must have the same size as those of the input x.

Examples

>>> x = ivy.array([[1.,2.,3.],[4.,5.,6.],[7.,8.,9.]])
>>> q, r = x.qr(mode='reduced')
>>> print(q)
ivy.array([[-0.12309149,  0.90453403,  0.40824829],
    [-0.49236596,  0.30151134, -0.81649658],
    [-0.86164044, -0.30151134,  0.40824829]])
>>> print(r)
ivy.array([[-8.12403841e+00,-9.60113630e+00, -1.10782342e+01],
    [ 0.00000000e+00,  9.04534034e-01,  1.80906807e+00],
    [ 0.00000000e+00,  0.00000000e+00, -8.88178420e-16]])
Container.qr(self, /, *, mode='reduced', key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.qr. This method simply wraps the function, and so the docstring for ivy.qr also applies to this method with minimal changes.

Returns the qr decomposition x = QR of a full column rank matrix (or a stack of matrices), where Q is an orthonormal matrix (or a stack of matrices) and R is an upper-triangular matrix (or a stack of matrices).

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

  • mode (Union[str, Container], default: 'reduced') – decomposition mode. Should be one of the following modes: - ‘reduced’: compute only the leading K columns of q, such that q and r have dimensions (…, M, K) and (…, K, N), respectively, and where K = min(M, N). - ‘complete’: compute q and r with dimensions (…, M, M) and (…, M, N), respectively. Default: ‘reduced’.

  • 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[Tuple[Container, Container]], default: None) – optional output tuple of containers, for writing the result to. The arrays must have shapes that the inputs broadcast to.

Return type:

Tuple[Container, Container]

Returns:

ret – a namedtuple (Q, R) whose - first element must have the field name Q and must be an container whose shape depends on the value of mode and contain matrices with orthonormal columns. If mode is ‘complete’, the container must have shape (…, M, M). If mode is ‘reduced’, the container must have shape (…, M, K), where K = min(M, N). The first x.ndim-2 dimensions must have the same size as those of the input container x. - second element must have the field name R and must be an container whose shape depends on the value of mode and contain upper-triangular matrices. If mode is ‘complete’, the container must have shape (…, M, N). If mode is ‘reduced’, the container must have shape (…, K, N), where K = min(M, N). The first x.ndim-2 dimensions must have the same size as those of the input x.

Examples

>>> x = ivy.Container(a = ivy.native_array([[1., 2.], [3., 4.]]),
...                   b = ivy.array([[2., 3.], [4. ,5.]]))
>>> q,r = x.qr(mode='complete')
>>> print(q)
{
    a: ivy.array([[-0.31622777, -0.9486833],
                [-0.9486833, 0.31622777]]),
    b: ivy.array([[-0.4472136, -0.89442719],
                [-0.89442719, 0.4472136]])
}
>>> print(r)
{
    a: ivy.array([[-3.16227766, -4.42718872],
                [0., -0.63245553]]),
    b: ivy.array([[-4.47213595, -5.81377674],
                [0., -0.4472136]])
}