o
    Qe                    @   s  d dl ZddlmZ ddlmZmZmZmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZ d dlZd dlZd dlmZ d dlmZ d dlmZmZ g Z dZ!dZddZ"d[ddZ#d\ddZ$d]ddZ%d^ddZ&dZddZ'd_d d!Z(dZd"d#Z)d`d$d%Z*dad&d'Z+dbd(d)Z,dZd*d+Z-dcd-d.Z.ddd/d0Z/dZd1d2Z0dZd3d4Z1dZd5d6Z2dad7d8Z3dZd9d:Z4ded<d=Z5dfd>d?Z6dgd@dAZ7dZdBdCZ8dZdDdEZ9dZdFdGZ:dhdIdJZ;didLdMZ<dZdNdOZ=	djdPdQZ>dadRdSZ?dhdTdUZ@dkdVdWZAdldXdYZBdS )m    N   )LayerHelper)_varbase_creator_dygraph_tracerin_dygraph_mode_non_static_mode)check_variable_and_dtype
check_typecheck_dtype)Variable)_in_legacy_dygraph   )cast)multiplyadd)logical_not)full)core)VarDesc)_C_ops_legacy_C_ops	   c           	      C   s  t  r	t| |S t rt| d|\}}|S t| dg dd t|dtt	fd t
|t	r2t|}t|t| jkrHtdt| jt|f t|D ]\}}|t| jkretd||| t| jf qLtdi t }|| j}|| j}|jdd	| gi|g|gd
d|id |S )a  
    Permute the data dimensions of `input` according to `perm`.

    The `i`-th dimension  of the returned tensor will correspond to the
    perm[i]-th dimension of `input`.

    Args:
        x (Tensor): The input Tensor. It is a N-D Tensor of data types bool, float32, float64, int32.
        perm (list|tuple): Permute the input according to the data of perm.
        name (str): The name of this layer. It is optional.

    Returns:
        Tensor: A transposed n-D Tensor, with data type being bool, float32, float64, int32, int64.

    For Example:

        .. code-block:: text

         x = [[[ 1  2  3  4] [ 5  6  7  8] [ 9 10 11 12]]
             [[13 14 15 16] [17 18 19 20] [21 22 23 24]]]
         shape(x) =  [2,3,4]

         # Example 1
         perm0 = [1,0,2]
         y_perm0 = [[[ 1  2  3  4] [13 14 15 16]]
                   [[ 5  6  7  8]  [17 18 19 20]]
                   [[ 9 10 11 12]  [21 22 23 24]]]
         shape(y_perm0) = [3,2,4]

         # Example 2
         perm1 = [2,1,0]
         y_perm1 = [[[ 1 13] [ 5 17] [ 9 21]]
                   [[ 2 14] [ 6 18] [10 22]]
                   [[ 3 15]  [ 7 19]  [11 23]]
                   [[ 4 16]  [ 8 20]  [12 24]]]
         shape(y_perm1) = [4,3,2]

    Examples:

        .. code-block:: python

            import paddle

            x = paddle.randn([2, 3, 4])
            x_transposed = paddle.transpose(x, perm=[1, 0, 2])
            print(x_transposed.shape)
            # [3L, 2L, 4L]

    axisx)boolfloat16float32float64int32int64	complex64
complex128	transposepermzInput(perm) is the permutation of dimensions of Input(x), its length should be equal to dimensions of Input(x), but received dimension of Input(x) is %s, the length of Input(perm) is %s.zEach element in Input(perm) should be less than Input(x)'s dimension, but %d-th element in Input(perm) is %d which exceeds Input(x)'s dimension %d.
transpose2XOutZXShapetypeinputsoutputsattrsN)r"   )r   r   r"   r   r   r$   r   r	   listtuple
isinstancelenshape
ValueError	enumerater   locals"create_variable_for_type_inferencedtype	append_op)	r   r#   nameout_idxdimhelperx_shape r?   DD:\Projects\ConvertPro\env\Lib\site-packages\paddle/tensor/linalg.pyr"   /   sL   2

r"   Fc                 C   s   t  rt| |||S t rd}tt|}|| |d|d|S ||d}dd }|| | tdi t }	|	j| j	d}
|	j
d| |dd	|
i|d
 |
S )a1  
    Applies matrix multiplication to two tensors. `matmul` follows
    the complete broadcast rules,
    and its behavior is consistent with `np.matmul`.

    Currently, the input tensors' number of dimensions can be any, `matmul` can be used to
    achieve the `dot`, `matmul` and `batchmatmul`.

    The actual behavior depends on the shapes of :math:`x`, :math:`y` and the
    flag values of :attr:`transpose_x`, :attr:`transpose_y`. Specifically:

    - If a transpose flag is specified, the last two dimensions of the tensor
      are transposed. If the tensor is ndim-1 of shape, the transpose is invalid. If the tensor
      is ndim-1 of shape :math:`[D]`, then for :math:`x` it is treated as :math:`[1, D]`, whereas
      for :math:`y` it is the opposite: It is treated as :math:`[D, 1]`.

    The multiplication behavior depends on the dimensions of `x` and `y`. Specifically:

    - If both tensors are 1-dimensional, the dot product result is obtained.

    - If both tensors are 2-dimensional, the matrix-matrix product is obtained.

    - If the `x` is 1-dimensional and the `y` is 2-dimensional,
      a `1` is prepended to its dimension in order to conduct the matrix multiply.
      After the matrix multiply, the prepended dimension is removed.

    - If the `x` is 2-dimensional and `y` is 1-dimensional,
      the matrix-vector product is obtained.

    - If both arguments are at least 1-dimensional and at least one argument
      is N-dimensional (where N > 2), then a batched matrix multiply is obtained.
      If the first argument is 1-dimensional, a 1 is prepended to its dimension
      in order to conduct the batched matrix multiply and removed after.
      If the second argument is 1-dimensional, a 1 is appended to its
      dimension for the purpose of the batched matrix multiple and removed after.
      The non-matrix (exclude the last two dimensions) dimensions are
      broadcasted according the broadcast rule.
      For example, if input is a (j, 1, n, m) tensor and the other is a (k, m, p) tensor,
      out will be a (j, k, n, p) tensor.

    Args:
        x (Tensor): The input tensor which is a Tensor.
        y (Tensor): The input tensor which is a Tensor.
        transpose_x (bool): Whether to transpose :math:`x` before multiplication.
        transpose_y (bool): Whether to transpose :math:`y` before multiplication.
        name(str|None): A name for this layer(optional). If set None, the layer
            will be named automatically.

    Returns:
        Tensor: The output Tensor.

    Examples:

        .. code-block:: python

            import paddle

            # vector * vector
            x = paddle.rand([10])
            y = paddle.rand([10])
            z = paddle.matmul(x, y)
            print(z.shape)
            # [1]

            # matrix * vector
            x = paddle.rand([10, 5])
            y = paddle.rand([5])
            z = paddle.matmul(x, y)
            print(z.shape)
            # [10]

            # batched matrix * broadcasted vector
            x = paddle.rand([10, 5, 2])
            y = paddle.rand([2])
            z = paddle.matmul(x, y)
            print(z.shape)
            # [10, 5]

            # batched matrix * batched matrix
            x = paddle.rand([10, 5, 2])
            y = paddle.rand([10, 2, 5])
            z = paddle.matmul(x, y)
            print(z.shape)
            # [10, 5, 5]

            # batched matrix * broadcasted matrix
            x = paddle.rand([10, 1, 5, 2])
            y = paddle.rand([1, 3, 2, 5])
            z = paddle.matmul(x, y)
            print(z.shape)
            # [10, 3, 5, 5]

    	matmul_v2trans_xtrans_yrB   rC   c                 S   s2   | |d}|  D ]\}}t||g dd q	d S )N)r   y)r   r   r   r    r!   matmul)itemsr   )r   rE   	var_namesr8   valr?   r?   r@   __check_input   s   
zmatmul.<locals>.__check_inputr6   r%   Yr'   r(   N)rA   )r   r   rF   r   getattrr   r   r4   r5   r6   r7   )r   rE   Ztranspose_xZtranspose_yr8   op_typeopr,   rJ   r=   r9   r?   r?   r@   rF      s(   ^


rF   froc           	      C   s  ddd}	ddd}d|dddfdd}d	|ddfd
d}|du rX|durXt |tr>|dkr7|| |||dS td|t |ttfrO|| |||d|dS tdt|t |trat|}t |trpt	|dkrp|d }t |trt |tr|dkr|| d||d|dS td|t |ttfr|| |||d|dS td|t |trt	|dkr|dkr|| |||dS |t
jks|t
j kr|| ||||dS |dkrtd||| ||||dS td|)a5  

    Returns the matrix norm (Frobenius) or vector norm (the 1-norm, the Euclidean
    or 2-norm, and in general the p-norm for p > 0) of a given tensor.

    Note:
        This norm API is different from `numpy.linalg.norm`.
        This api supports high-order input tensors (rank >= 3), and certain axis need to be pointed out to calculate the norm.
        But `numpy.linalg.norm` only supports 1-D vector or 2-D matrix as input tensor.
        For p-order matrix norm, this api actually treats matrix as a flattened vector to calculate the vector norm, NOT REAL MATRIX NORM.

    Args:
        x (Tensor): The input tensor could be N-D tensor, and the input data
            type could be float32 or float64.
        p (float|string, optional): Order of the norm. Supported values are `fro`, `0`, `1`, `2`,
            `inf`, `-inf` and any positive real number yielding the corresponding p-norm. Not supported: ord < 0 and nuclear norm.
            Default value is `fro`.
        axis (int|list|tuple, optional): The axis on which to apply norm operation. If axis is int
            or list(int)/tuple(int)  with only one element, the vector norm is computed over the axis.
            If `axis < 0`, the dimension to norm operation is rank(input) + axis.
            If axis is a list(int)/tuple(int) with two elements, the matrix norm is computed over the axis.
            Default value is `None`.
        keepdim (bool, optional): Whether to reserve the reduced dimension in the
            output Tensor. The result tensor will have fewer dimension
            than the :attr:`input` unless :attr:`keepdim` is true, default
            value is False.
        name (str, optional): The default value is None. Normally there is no need for
            user to set this property. For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tensor: results of norm operation on the specified axis of input tensor,
        it's data type is the same as input's Tensor.

    Examples:
        .. code-block:: python

            import paddle
            x = paddle.arange(24, dtype="float32").reshape([2, 3, 4]) - 12
            # x: Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
            #          [[[-12., -11., -10., -9. ],
            #            [-8. , -7. , -6. , -5. ],
            #            [-4. , -3. , -2. , -1. ]],

            #           [[ 0. ,  1. ,  2. ,  3. ],
            #            [ 4. ,  5. ,  6. ,  7. ],
            #            [ 8. ,  9. ,  10.,  11.]]])

            # compute frobenius norm along last two dimensions.
            out_fro = paddle.linalg.norm(x, p='fro', axis=[0,1])
            # out_fro: Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
            #                 [17.43559647, 16.91153526, 16.73320007, 16.91153526])

            # compute 2-order vector norm along last dimension.
            out_pnorm = paddle.linalg.norm(x, p=2, axis=-1)
            # out_pnorm: Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            #                [[21.11871147, 13.19090557, 5.47722578 ],
            #                 [3.74165750 , 11.22497177, 19.13112640]])

            # compute 2-order  norm along [0,1] dimension.
            out_pnorm = paddle.linalg.norm(x, p=2, axis=[0,1])
            # out_pnorm: Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
            #                  [17.43559647, 16.91153526, 16.73320007, 16.91153526])

            # compute inf-order  norm
            out_pnorm = paddle.linalg.norm(x, p=float("inf"))
            # out_pnorm  = Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            #                    [12.])

            out_pnorm = paddle.linalg.norm(x, p=float("inf"), axis=0)
            # out_pnorm: Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
            #                 [[12., 11., 10., 9. ],
            #                  [8. , 7. , 6. , 7. ],
            #                  [8. , 9. , 10., 11.]])

            # compute -inf-order  norm
            out_pnorm = paddle.linalg.norm(x, p=-float("inf"))
            # out_pnorm: Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
            #                  [0.])

            out_pnorm = paddle.linalg.norm(x, p=-float("inf"), axis=0)
            # out_pnorm: Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
            #                  [[0., 1., 2., 3.],
            #                  [4., 5., 6., 5.],
            #                  [4., 3., 2., 1.]])
    NFc              	   S   s   |durt |trt|dkstdt r*|du r"t| g |dS t| ||dS t rE|du r:t| d|ddS t| d|d|ddS ||dd	}|du rSd|d< t	| d
ddgd t
di t }|j| d}|jdd| id|i|d |S )ab  
        The frobenius norm OP is to calculate the frobenius norm of certain two dimensions of Tensor `input`.
        Args:
          input (Variable): Tensor, data type float32, float64.
          dim (list, optional): None for last two dimensions.
          keepdim (bool, optional): Whether keep the dimensions as the `input`, Default False.
        Nr   zAThe dim of frobenius norm op should be None or two elements list!TFkeep_dim
reduce_allr<   r<   rR   rS   inputr   r   frobenius_normrK   r%   r'   r(   )rV   )r/   r-   r0   r2   r   r   rV   r   r   r   r   r4   r5   input_dtyper7   )rU   r<   keepdimr8   r,   r=   r9   r?   r?   r@   rV   n  sB   
znorm.<locals>.frobenius_normc           	      S   s   t  r|du r	d}t| ||d||S t r)|du rd}t| d|d|d|d|	S |dur6t|dttfd |durAt|dtd t| d	d
dgd |durP|nd|durYt|nd||dd}t	di t
 }|j| d}|jdd| id|i|d |S )a|  
        Calculate the p-order vector norm for certain  dimension of Tensor `input`.
        Args:
          input (Variable): Tensor, data type float32, float64.
          porder (float, optional): None for porder=2.0.
          axis (int, optional): None for last dimension.
          keepdim (bool, optional): Whether keep the dimensions as the `input`, Default False.
        Ng-q=porderr   rX   asvectorp_normrU   r   r   g       @)r   rZ   rX   r[   epsilonrK   r%   r'   r(   )r\   )r   r   r\   r   r   r	   floatintr   r   r4   r5   rW   r7   )	rU   rZ   r   rX   r[   r8   r,   r=   r9   r?   r?   r@   vector_norm  sT   znorm.<locals>.vector_normc                 S   sJ  t  rFt| }|d ks|g ks|dkrdnd}|d kr"|g kr"|ndg}|r1|g ks1|d u s1J |tdkr?t|||S t|||S tdi t }|j	|
 d}|jdd| id	|id
 |j	|
 d}	|d ksv|g ksv|dkrxdnd}|d kr|g kr|ndg}|tdkrdnd}
|j|
d|id	|	i|||dd |	S )NTFr   infinf_normrK   absr%   r'   r)   r*   r+   
reduce_max
reduce_minrT   r(   )rb   )r   r   rc   npr   maxminr   r4   r5   rW   r7   )rU   rZ   r   rX   r[   r8   r9   rS   r=   Z
reduce_outZreduce_typer?   r?   r@   rb     s@   

znorm.<locals>.inf_norm      ?c           
      S   s"  t  r"t| }t||}t||d|}t|td| }|S tdi t }	|	j|		 d}|	j|		 d}|	j
dd| id|id |	j|		 d}|	j
d	d|id|id
|id |	j|		 d}|	j
dd|id|i|||du rvdnddd |	j
d	d|id|id
td| id |S )z
        NOTE:
            This function actually treats the matrix as flattened vector to calculate vector norm instead of matrix norm.
        Nrj   normrK   rc   r%   r'   rd   powfactorr(   
reduce_sumTFrT   rk   )r   r   rc   rl   sumr^   r   r4   r5   rW   r7   )
rU   rZ   r   rX   r8   abs_outpow_outsum_outr9   blockr?   r?   r@   p_matrix_norm  sX   

znorm.<locals>.p_matrix_normrQ   )r<   rX   r8   z,only valid string values are 'fro', found {}T)rZ   r   rX   r[   r8   z.only valid p type is string or float, found {}r   r   r   )r   rZ   rX   r[   r8   z:unspport p for p-order vector norm. except float, found {})rZ   r   rX   r8   zIjust suport axis type int or list (length of list <=1) if p = 0, found {}z;except axis type int or list (length of list <=2), found {}NFN)NNFFN)r/   strr2   formatr_   r^   r)   r.   r-   r0   rg   ra   )	r   pr   rX   r8   rV   r`   rb   ru   r?   r?   r@   rk     s   
W.

<
,5
	



	
rk   c           	      C   s   t  r
t| ||S t| dddgd t|dddgd t|dttfd td
i t }|	| j
}| g|gd}d|gi}dt|i}|jd|d|i|d |S )a
  

    This OP returns the p-norm of (x - y). It is not a norm in a strict sense, only as a measure
    of distance. The shapes of x and y must be broadcastable. The definition is as follows, for
    details, please refer to the `numpy's broadcasting <https://docs.scipy.org/doc/numpy/user/basics.broadcasting.html>`_:

    - Each input has at least one dimension.
    - Match the two input dimensions from back to front, the dimension sizes must either be equal, one of them is 1, or one of them does not exist.

    Where, z = x - y, the shapes of x and y are broadcastable, then the shape of z can be
    obtained as follows:

    1. If the number of dimensions of x and y are not equal, prepend 1 to the dimensions of the
    tensor with fewer dimensions.

    For example, The shape of x is [8, 1, 6, 1], the shape of y is [7, 1, 5], prepend 1 to the
    dimension of y.

    x (4-D Tensor):  8 x 1 x 6 x 1

    y (4-D Tensor):  1 x 7 x 1 x 5

    2. Determine the size of each dimension of the output z: choose the maximum value from the
    two input dimensions.

    z (4-D Tensor):  8 x 7 x 6 x 5

    If the number of dimensions of the two inputs are the same, the size of the output can be
    directly determined in step 2. When p takes different values, the norm formula is as follows:

    When p = 0, defining $0^0=0$, the zero-norm of z is simply the number of non-zero elements of z.

    .. math::

        ||z||_{0}=\lim_{p \\rightarrow 0}\sum_{i=1}^{m}|z_i|^{p}

    When p = inf, the inf-norm of z is the maximum element of the absolute value of z.

    .. math::

        ||z||_\infty=\max_i |z_i|

    When p = -inf, the negative-inf-norm of z is the minimum element of the absolute value of z.

    .. math::

        ||z||_{-\infty}=\min_i |z_i|

    Otherwise, the p-norm of z follows the formula,

    .. math::

        ||z||_{p}=(\sum_{i=1}^{m}|z_i|^p)^{\\frac{1}{p}}

    Args:
        x (Tensor): 1-D to 6-D Tensor, its data type is float32 or float64.
        y (Tensor): 1-D to 6-D Tensor, its data type is float32 or float64.
        p (float, optional): The norm to be computed, its data type is float32 or float64. Default: 2.

    Returns:
        Tensor: Tensor that is the p-norm of (x - y).

    Examples:
        .. code-block:: python

            import paddle

            x = paddle.to_tensor([[3, 3],[3, 3]], dtype="float32")
            y = paddle.to_tensor([[3, 3],[3, 1]], dtype="float32")
            out = paddle.dist(x, y, 0)
            print(out) # out = [1.]

            out = paddle.dist(x, y, 2)
            print(out) # out = [2.]

            out = paddle.dist(x, y, float("inf"))
            print(out) # out = [2.]

            out = paddle.dist(x, y, float("-inf"))
            print(out) # out = [0.]
    r6   r   r   distry   rL   r'   r(   N)rz   )r   r   rz   r   r	   r^   r_   r   r4   r5   r6   r7   )	r   rE   ry   r8   r=   r9   r*   r+   r,   r?   r?   r@   rz     s   R
rz   c           
      C   s  ddd}ddgfdd}dgfd	d
}dd }t | j}t|dks.tddt| |dkr4d}d|v r:dnd}|ddddtjtj fv r|t|d  |t|d  kr|dkrf|| |dd S |  }	|dkrv|| ||	 S |dkr|| |||	| S |dv r|| |dgd||	|dgd S |tjtj fv r|| |dgd||	|dgd S dS td|d |dv r|dkr|| |dd S || |dS td|d )a  

    Computes the condition number of a matrix or batches of matrices with respect to a matrix norm ``p``.

    Args:
        x (Tensor): The input tensor could be tensor of shape ``(*, m, n)`` where ``*`` is zero or more batch dimensions
            for ``p`` in ``(2, -2)``, or of shape ``(*, n, n)`` where every matrix is invertible for any supported ``p``.
            And the input data type could be ``float32`` or ``float64``.
        p (float|string, optional): Order of the norm. Supported values are `fro`, `nuc`, `1`, `-1`, `2`, `-2`,
            `inf`, `-inf`. Default value is `None`, meaning that the order of the norm is `2`.
        name (str, optional): The default value is `None`. Normally there is no need for
            user to set this property. For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tensor: computing results of condition number, its data type is the same as input Tensor ``x``.

    Examples:
        .. code-block:: python

            import paddle

            x = paddle.to_tensor([[1., 0, -1], [0, 1, 0], [1, 0, 1]])

            # compute conditional number when p is None
            out = paddle.linalg.cond(x)
            # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [1.41421342])

            # compute conditional number when order of the norm is 'fro'
            out_fro = paddle.linalg.cond(x, p='fro')
            # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [3.16227770])

            # compute conditional number when order of the norm is 'nuc'
            out_nuc = paddle.linalg.cond(x, p='nuc')
            # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [9.24263859])

            # compute conditional number when order of the norm is 1
            out_1 = paddle.linalg.cond(x, p=1)
            # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [2.])

            # compute conditional number when order of the norm is -1
            out_minus_1 = paddle.linalg.cond(x, p=-1)
            # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [1.])

            # compute conditional number when order of the norm is 2
            out_2 = paddle.linalg.cond(x, p=2)
            # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [1.41421342])

            # compute conditional number when order of the norm is -1
            out_minus_2 = paddle.linalg.cond(x, p=-2)
            # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [0.70710683])

            # compute conditional number when order of the norm is inf
            out_inf = paddle.linalg.cond(x, p=float("inf"))
            # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [2.])

            # compute conditional number when order of the norm is -inf
            out_minus_inf = paddle.linalg.cond(x, p=-float("inf"))
            # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [1.])

            a = paddle.randn([2, 4, 4])
            # Tensor(shape=[2, 4, 4], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [[[-0.06784091, -0.07095790,  1.31792855, -0.58959651],
            #          [ 0.20818676, -0.85640615, -0.89998871, -1.47439921],
            #          [-0.49132481,  0.42250812, -0.77383220, -2.19794774],
            #          [-0.33551720, -1.70003879, -1.09795380, -0.63737559]],

            #         [[ 1.12026262, -0.16119350, -1.21157813,  2.74383283],
            #          [-0.15999718,  0.18798758, -0.69392562,  1.35720372],
            #          [-0.53013402, -2.26304483,  1.40843511, -1.02288902],
            #          [ 0.69533503,  2.05261683, -0.02251151, -1.43127477]]])

            a_cond_fro = paddle.linalg.cond(a, p='fro')
            # Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [8.86691189 , 75.23817444])

            b = paddle.randn([2, 3, 4])
            # Tensor(shape=[2, 3, 4], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [[[-0.43754861,  1.80796063, -0.78729683, -1.82264030],
            #          [-0.27670753,  0.06620564,  0.29072434, -0.31155765],
            #          [ 0.34123746, -0.05444612,  0.05001324, -1.46877074]],

            #         [[-0.64331555, -1.51103854, -1.26277697, -0.68024760],
            #          [ 2.59375715, -1.06665540,  0.96575671, -0.73330832],
            #          [-0.47064447, -0.23945692, -0.95150250, -1.07125998]]])
            b_cond_2 = paddle.linalg.cond(b, p=2)
            # Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [6.64228773, 3.89068866])

    rj   Nc           	   	   S   s  |du s|g kr
dnd}|dkr|g kr|ndg}d}t  rPt| }t||d|}|dks4|tjkr<t|dg|S |dksF|tj krNt|dg|S dS t rt	| }t	
|d|d|d	|}|dksl|tjkrxt	|ddgd|d	|S |dks|tj krt	|ddgd|d	|S dS tdi t }|j| d}|j| d}|j| d}|jdd| id|id |jdd|id|i|||dd |dks|tjkr|jdd|id|idg||dd |dks|tj kr|jdd|id|idg||dd |S )z
        NOTE:
            Calculate the matrix norm of a square matrix or batches of square matrices,
            when porder is in (1, -1, inf, -inf)
        NTFr   r   rY   r<   rX   rS   rk   rK   rc   r%   r'   rd   rn   rT   r(   re   rf   ro   )r   r   rc   rp   rg   ra   rh   ri   r   r   rn   re   rf   r   r4   r5   rW   r7   )	rU   rZ   r   rS   rX   rq   rs   rt   r9   r?   r?   r@   mat_normR  s   

		


zcond.<locals>.mat_normr   rY   c           
   	   S   s  |du s|g kr
dnd}d}t  r1t| |}t||d|}t||d|}t|td| S t r]t| d|}t|d|d|d|}t|d|d|d|}t|dtd| S t	di t
 }|j| d
}|j| d
}|j| d
}|j| d
}	|jdd| id|id|id |jdd|id|i|||dd |jdd|id|i|||dd |jdd|id|	idtd| id |	S )zr
        NOTE:
            Calculate the frobenius norm of a square matrix or batches of square matrices.
        NTFrj   rm   r<   rX   rS   rk   rK   rl   r%   r'   r(   rn   rT   ro   )r   r   rl   rp   r^   paddlein_dynamic_moder   rn   r   r4   r5   rW   r7   )
rU   rZ   r   rS   rX   rr   Z	sum_out_1Z	sum_out_2rt   r9   r?   r?   r@   fro_norm  s   		

zcond.<locals>.fro_normc              	   S   s,  |du s|g kr
dnd}d}t | dd\}}}t r|dkr4t r)t||d|S t|d|d|d|S t rZt|||}t|||}	|d	krOt	||	S |d
krYt	|	|S n2t
|d|d|d|}t|d|d|d|}	|d	kr~t||	d|ddS |d
krt|	|d|ddS tdi t }
|
j|
 d}|dkr|
jdd|id|i|||dd |S |
j|
 d}|
j|
 d}	|
jdd|id|i|||dd |
jdd|id|	i|||dd |d	kr|
jd||	dd|i|ddd |S |d
kr|
jd|	|dd|i|ddd |S dS )z
        NOTE:
            Calculate the matrix norm, which is related to singular values, of a matrix
            or batches of matrices, including nuclear norm, 2-norm and (-2)-norm.
        NTF)full_matricesnucr<   rX   rS   r   aixs
use_mkldnnrk   rK   rn   r%   r'   rT   r(   re   rf   elementwise_divrL   )r   r   ro   )svdr   r   r   rp   r   rn   rh   ri   dividere   rf   r   r   r4   r5   rW   r7   )rU   rZ   r   rS   rX   usvhZmax_outZmin_outrt   r9   r?   r?   r@   svd_norm  s   	



zcond.<locals>.svd_normc                 S   s   t  r	| |S td)Nz0only support x is nonempty tensor in static mode)r|   r}   reshaper2   )rU   r1   r?   r?   r@   empty_tensorb  s   
zcond.<locals>.empty_tensorz1input should be a matrix or batches of matrices, z)but the dimention of received input is {}r   r   rQ   r   r   r   rY   )rZ   r   z%only support p is {} when input is a z+square matrix or batches of square matrices)r   r   )rZ   z5unsupported {} for p, only supporting ('fro', 'nuc', z 1, -1, 2, -2, inf, -inf) or none)rj   N)r-   r1   r0   r2   rx   rg   ra   Zinverse)
r   ry   r8   r{   r~   r   r   r>   Zx_sizeZx_invr?   r?   r@   cond  s\   
dbJd
 r   c                 C   s   t  r	t| |S t rt| |S d}| dusJ d||dus*J d|t| dg d| t|dg d| t|fi t }|du rQ|j	| j
d}n	|j|| j
d	d
}|jd| |di d|id |S )af  
    This operator calculates inner product for vectors.

    Note:
       Support 1-d and 2-d Tensor. When it is 2d, the first dimension of this matrix
       is the batch dimension, which means that the vectors of multiple batches are dotted.

    Parameters:
        x(Tensor): 1-D or 2-D ``Tensor``. Its dtype should be ``float32``, ``float64``, ``int32``, ``int64``
        y(Tensor): 1-D or 2-D ``Tensor``. Its dtype soulde be ``float32``, ``float64``, ``int32``, ``int64``
        name(str, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name`

    Returns:
        Tensor: the calculated result Tensor.

    Examples:

    .. code-block:: python

        import paddle

        # 1-D Tensor * 1-D Tensor
        x = paddle.to_tensor([1, 2, 3])
        y = paddle.to_tensor([4, 5, 6])
        z = paddle.dot(x, y)
        print(z)  # [32]

        # 2-D Tensor * 2-D Tensor
        x = paddle.to_tensor([[1, 2, 3], [2, 4, 6]])
        y = paddle.to_tensor([[4, 5, 6], [4, 5, 6]])
        z = paddle.dot(x, y)
        print(z)  # [[32], [64]]

    dotNzx cannot be None in {}zy cannot be None in {}r   )r   r   r   r   rE   rK   F)r8   r6   ZpersistablerL   r'   r)   r*   r,   r+   )r   r   r   r   r   rx   r   r   r4   r5   r6   Zcreate_variabler7   )r   rE   r8   rO   r=   r9   r?   r?   r@   r     s.   #r   Tc                 C   s  d}t | jdkst | jdk rtdt | j t| dddgd | }t | jdkr0| d}|s=|jd	 dkr=| }d
}|jd }	|d
ur||j}t |jdkr^tdt |j |jd	 |	krptd|	|jd	 |	 d	k rtd|	 t
|t
|dkstd|d
ur||j}
t |
jdkrtdt |j t|dddgd |jd	 |	krtd|	|jd	 |	 d	k rtd|	 |d
ur||
 }n|
}t
j|	|jd}|d
us|d
ur| }| d	krtd|d
ur|| }|jdd| }n
|jdd| }|}|d
ur3|d
ur3|dkr3|||  |  }n|| }|d	krDt
jd	|jd}||d }t
||  }t
|| }|S )u
  
    Estimate the covariance matrix of the input variables, given data and weights.

    A covariance matrix is a square matrix, indicate the covariance of each pair variables in the input matrix.
    For example, for an N-dimensional samples X=[x1,x2,…xN]T, then the covariance matrix
    element Cij is the covariance of xi and xj. The element Cii is the variance of xi itself.

    Parameters:
        x(Tensor): A N-D(N<=2) Tensor containing multiple variables and observations. By default, each row of x represents a variable. Also see rowvar below.
        rowvar(Bool, optional): If rowvar is True (default), then each row represents a variable, with observations in the columns. Default: True
        ddof(Bool, optional): If ddof=True will return the unbiased estimate, and ddof=False will return the simple average. Default: True
        fweights(Tensor, optional): 1-D Tensor of integer frequency weights; The number of times each observation vector should be repeated. Default: None
        aweights(Tensor, optional): 1-D Tensor of observation vector weights. How important of the observation vector, larger data means this element is more important. Default: None
        name(str, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name`

    Returns:
        Tensor: The covariance matrix Tensor of the variables.

    Examples:

    .. code-block:: python

        import paddle

        xt = paddle.rand((3,4))
        paddle.linalg.cov(xt)

        '''
        Tensor(shape=[3, 3], dtype=float64, place=CUDAPlace(0), stop_gradient=True,
            [[0.07918842, 0.06127326, 0.01493049],
                [0.06127326, 0.06166256, 0.00302668],
                [0.01493049, 0.00302668, 0.01632146]])
        '''
    covr   r   z]Input(x) only support N-D (1<=N<=2) tensor in cov, but received length of Input(input) is %s.r6   r   r   r   r   Nz`Input(fweights) only support N-D (N<=1) tensor in cov, but received shape of Input(input) is %s.ziThe number of Input(fweights) should equal to x's dim[1]: {}, but received size of Input(fweights) is {}.zZThe value of Input(fweights) cannot be negtive, but received min of Input(fweights) is {}.z Input(fweights) must be integer zaInput(aweights) only support N-D (N<=1) tensor in cov, but received length of Input(input) is %s.ziThe number of Input(aweights) should equal to x's dim[1]: {}, but received size of Input(aweights) is {}.zZThe value of Input(aweights) cannot be negtive, but received min of Input(aweights) is {}.rK   z0The sum of weights is zero, can't be normalized.)r   T)r0   r1   r2   r   r   tZastyper6   rx   ri   r|   allround	to_tensorrp   item	unsqueezemmconjr   Zsqueeze)r   rowvarZddofZfweightsZaweightsr8   rO   nxwZobservation_numZawZw_sumZnx_wZavgZnorm_factorZxxtr   r?   r?   r@   r     s   #








r   c                 C   s  t | jdkrtdt | j t r(t | jdkr| S ddg}t| |}|S t rCt | jdkr4| S ddg}t| d|\}}|S t	| dg dd t
di t }|| j}|| j}t | jdkrk| }|S |jd
d| gi|g|gddddgid |S )al  
    Transpose <=2-D tensor.
    0-D and 1-D tensors are returned as it is and 2-D tensor is equal to
    the paddle.transpose function which perm dimensions set 0 and 1.

    Args:
        input (Tensor): The input Tensor. It is a N-D (N<=2) Tensor of data types float32, float64, int32, int64.
        name(str, optional): The default value is None.  Normally there is no need for
            user to set this property.  For more information, please refer to :ref:`api_guide_Name`
    Returns:
        Tensor: A transposed n-D Tensor, with data type being float16, float32, float64, int32, int64.

    Examples:

        .. code-block:: python
           :name: code-example
             import paddle

             # Example 1 (0-D tensor)
             x = paddle.to_tensor([0.79])
             paddle.t(x) # [0.79]

             # Example 2 (1-D tensor)
             x = paddle.to_tensor([0.79, 0.84, 0.32])
             paddle.t(x) # [0.79000002, 0.83999997, 0.31999999]
             paddle.t(x).shape # [3]

             # Example 3 (2-D tensor)
             x = paddle.to_tensor([[0.79, 0.84, 0.32],
                                  [0.64, 0.14, 0.57]])
             x.shape # [2, 3]
             paddle.t(x)
             # [[0.79000002, 0.63999999],
             #  [0.83999997, 0.14000000],
             #  [0.31999999, 0.56999999]]
             paddle.t(x).shape # [3, 2]

    r   zInput(input) only support N-D (N<=2) tensor, but received length of Input(input) is %s. Perhaps you can use paddle.tensor.transpose() instead.r   r   r   rU   )r   r   r   r   r   r"   r   r$   r%   r&   r(   N)r   )r0   r1   r2   r   r   r"   r   r   r$   r   r   r4   r5   r6   r7   )rU   r8   r#   r9   r:   r=   input_shaper?   r?   r@   r   M  sJ   '
r   c                 C   s   t  r|du r	tn|}t| ||S t r'|dur!t| |d|S t| |S tdi t }|| j	}t
 }||d< |jd| |dd|i|d |S )aC  
    Computes the cross product between two tensors along an axis.

    Inputs must have the same shape, and the length of their axes should be equal to 3.
    If `axis` is not given, it defaults to the first axis found with the length 3.

    Args:
        x (Tensor): The first input tensor.
        y (Tensor): The second input tensor.
        axis (int, optional): The axis along which to compute the cross product. It defaults to be 9 which indicates using the first axis found with the length 3.
        name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tensor. A Tensor with same data type as `x`.

    Examples:
        .. code-block:: python

            import paddle

            x = paddle.to_tensor([[1.0, 1.0, 1.0],
                                  [2.0, 2.0, 2.0],
                                  [3.0, 3.0, 3.0]])
            y = paddle.to_tensor([[1.0, 1.0, 1.0],
                                  [1.0, 1.0, 1.0],
                                  [1.0, 1.0, 1.0]])

            z1 = paddle.cross(x, y)
            # [[-1. -1. -1.]
            #  [ 2.  2.  2.]
            #  [-1. -1. -1.]]

            z2 = paddle.cross(x, y, axis=1)
            # [[0. 0. 0.]
            #  [0. 0. 0.]
            #  [0. 0. 0.]]
    Nr<   crossrL   r'   r(   )r   )r   K_DEFAULT_DIMr   r   r   r   r   r4   r5   r6   dictr7   )r   rE   r   r8   r=   r9   r,   r?   r?   r@   r     s$   &r   c                 C   s   t  r	t| |S t rt| d|S t| dddgd t|dtd tdi t	 }|j
| jd}|jdd| gid|id|id	 |S )a  
    Computes the Cholesky decomposition of one symmetric positive-definite
    matrix or batches of symmetric positive-definite matrice.

    If `upper` is `True`, the decomposition has the form :math:`A = U^{T}U` ,
    and the returned matrix :math:`U` is upper-triangular. Otherwise, the
    decomposition has the form  :math:`A = LL^{T}` , and the returned matrix
    :math:`L` is lower-triangular.

    Args:
        x (Tensor): The input tensor. Its shape should be `[*, M, M]`,
            where * is zero or more batch dimensions, and matrices on the
            inner-most 2 dimensions all should be symmetric positive-definite.
            Its data type should be float32 or float64.
        upper (bool): The flag indicating whether to return upper or lower
            triangular matrices. Default: False.
        name (str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tensor, A Tensor with same shape and data type as `x`. It represents
        triangular matrices generated by Cholesky decomposition.

    Examples:
        .. code-block:: python

            import paddle

            a = paddle.rand([3, 3], dtype="float32")
            a_t = paddle.transpose(a, [1, 0])
            x = paddle.matmul(a, a_t) + 1e-03

            out = paddle.linalg.cholesky(x, upper=False)
            print(out)
    upperr6   r   r   choleskyrK   r%   r'   r(   N)r   )r   r   r   r   r   r   r	   r   r   r4   r5   r6   r7   )r   r   r8   r=   r9   r?   r?   r@   r     s   $r   c              
   C   s  t  r8t|tr!|j| jkrt|| j}n|}d}t| |||S |du r*d}d}nt|}d}t| |||S t	 rs|du rFd}d}d}n!t|tr_|j| jkrXt|| j}n|}d}d}nd}t|}d}t
| |d|d|d|S i }i }t| dd	d
gd | |d< |du rd|d< n,t|trd|d< |j| jkrt|| j|d< n||d< nt|dtd d|d< ||d< t|dtd ||d< tdi t }	|	jdd}
|	jd|d|
i|d |
S )a  
    Computes the rank of a matrix.

    The rank of a matrix is the number of singular values that are greater than the specified `tol` threshold when hermitian=False,
    or the number of eigenvalues in absolute value that are greater than the specified `tol` threshold when hermitian=True.

    Args:
        x (Tensor): The input tensor. Its shape should be `[..., m, n]`, where `...` is zero or more batch dimensions. If `x` is a batch
            of matrices then the output has the same batch dimensions. The data type of `x` should be float32 or float64.
        tol (float,Tensor,optional): the tolerance value. Default: None. If `tol` is not specified, and `sigma` is the largest
            singular value (or eigenvalues in absolute value), and `eps` is the epsilon value for the dtype of `x`, then `tol` is computed
            with formula `tol=sigma * max(m,n) * eps`. Note that if `x` is a batch of matrices, `tol` is computed this way for every batch.
        hermitian (bool,optional): indicates whether `x` is Hermitian. Default: False. 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.
        name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tensor: Rank of tensor x.

    Examples:
        .. code-block:: python

            import paddle

            a = paddle.eye(10)
            b = paddle.linalg.matrix_rank(a)
            print(b)
            # b = [10]

            c = paddle.ones(shape=[3, 4, 5, 5])
            d = paddle.linalg.matrix_rank(c, tol=0.01, hermitian=True)
            print(d)
            # d = [[1, 1, 1, 1],
            #      [1, 1, 1, 1],
            #      [1, 1, 1, 1]]

    FNg        Ttol	hermitianuse_default_tolr   r   r   matrix_rankr%   Z	TolTensorr   rK   r'   r(   )r   )r   r/   r   r6   r   r   Zmatrix_rank_tolr^   r   r   r   r   r	   r   r   r4   r5   r7   )r   r   r   r8   Z
tol_tensorr   Ztol_attrr*   r,   r=   r9   r?   r?   r@   r     sz   '




r   c                 C   s   | j }|j }t|t|  krdksn td|||d |d kr-td|||d |d kr=td||t rFt| |S t rPt	| |S t
di t }|j| jd	}|jd| |d
d|id |S )a   
    Applies batched matrix multiplication to two tensors.

    Both of the two input tensors must be three-dementional and share the same batch size.

    if x is a (b, m, k) tensor, y is a (b, k, n) tensor, the output will be a (b, m, n) tensor.

    Args:
        x (Tensor): The input Tensor.
        y (Tensor): The input Tensor.
        name(str|None): A name for this layer(optional). If set None, the layer
            will be named automatically.

    Returns:
        Tensor: The product Tensor.

    Examples:
        .. code-block:: python

            import paddle

            # In imperative mode:
            # size x: (2, 2, 3) and y: (2, 3, 2)
            x = paddle.to_tensor([[[1.0, 1.0, 1.0],
                                [2.0, 2.0, 2.0]],
                                [[3.0, 3.0, 3.0],
                                [4.0, 4.0, 4.0]]])
            y = paddle.to_tensor([[[1.0, 1.0],[2.0, 2.0],[3.0, 3.0]],
                                [[4.0, 4.0],[5.0, 5.0],[6.0, 6.0]]])
            out = paddle.bmm(x, y)
            # Tensor(shape=[2, 2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
            #        [[[6. , 6. ],
            #          [12., 12.]],

            #         [[45., 45.],
            #          [60., 60.]]])

       zRx and y should be 3-dimensional. But received x's dimention: {}, y's dimention: {}r   r   zRx's width must be equal with y's height. But received x's shape: {}, y's shape: {}r   zgx's batch (shape[0]) must be equal with y's batch (shape[0]). But received x's shape: {}, y's shape: {}bmmrK   rL   r'   rd   N)r   )r1   r0   r2   rx   r   r   r   r|   r}   r   r   r4   r5   r6   r7   )r   rE   r8   r>   Zy_shaper=   r9   r?   r?   r@   r     s8   'r   d   c              	   C   s   t  rt| |||S t rt| d|d|d|S tdi t }t| dg dd |t	j
j}|jdd| id|i|||dd	 |S )a  
    Computes the histogram of a tensor. The elements are sorted into equal width bins between min and max.
    If min and max are both zero, the minimum and maximum values of the data are used.

    Args:
        input (Tensor): A Tensor(or LoDTensor) with shape :math:`[N_1, N_2,..., N_k]` . The data type of the input Tensor
            should be float32, float64, int32, int64.
        bins (int, optional): number of histogram bins.
        min (int, optional): lower end of the range (inclusive).
        max (int, optional): upper end of the range (inclusive).
        name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.

    Returns:
        Tensor: data type is int64, shape is (nbins,).

    Examples:
        .. code-block:: python

            import paddle

            inputs = paddle.to_tensor([1, 2, 1])
            result = paddle.histogram(inputs, bins=4, min=0, max=3)
            print(result) # [0, 2, 1, 0]
    binsri   rh   	histogramr%   r   r   r   r   r'   )r   ri   rh   r(   N)r   )r   r   r   r   r   r   r4   r   r5   r   ZVarTypeZINT64r7   )rU   r   ri   rh   r8   r=   r9   r?   r?   r@   r     s$   
r   c                 C   s   | j tjtjfvrtdt rt| |d|S tdi t	 }t
| dddgd |dur>t
|dg d	d |j|j d
}n|j| j d
}|jd| |dd|id|id |S )a  
    Computes frequency of each value in the input tensor.

    Args:
        x (Tensor): A Tensor with non-negative integer. Should be 1-D tensor.
        weights (Tensor, optional): Weight for each value in the input tensor. Should have the same shape as input. Default is None.
        minlength (int, optional): Minimum number of bins. Should be non-negative integer. Default is 0.
        name(str, optional): The default value is None.  Normally there is no need for user to set this
            property.  For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tensor: The tensor of frequency.

    Examples:
        .. code-block:: python

            import paddle

            x = paddle.to_tensor([1, 2, 1, 4, 5])
            result1 = paddle.bincount(x)
            print(result1) # [0, 2, 1, 0, 1, 1]

            w = paddle.to_tensor([2.1, 0.4, 0.1, 0.5, 0.5])
            result2 = paddle.bincount(x, weights=w)
            print(result2) # [0., 2.19999981, 0.40000001, 0., 0.50000000, 0.50000000]
    z+Elements in Input(x) should all be integers	minlengthbincountr%   r   r   NWeightsr   rK   )r%   r   r'   r(   )r   )r6   r|   r   r   	TypeErrorr   r   r   r   r4   r   r5   r7   )r   weightsr   r8   r=   r9   r?   r?   r@   r     s,   r   c                 C   sv   t  r	t| |S t rt| |}|S dd }|| | td	i t }|j| jd}|j	d| |dd|id |S )
aK  
    Performs a matrix-vector product of the matrix x and the vector vec.

    Args:
        x (Tensor): A tensor with shape :math:`[M, N]` , The data type of the input Tensor x
            should be one of float32, float64.
        vec (Tensor): A tensor with shape :math:`[N]` , The data type of the input Tensor x
            should be one of float32, float64.
        name(str, optional): The default value is None.  Normally there is no need for user to set this
            property.  For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tensor: The tensor which is producted by x and vec.

    Examples:
        .. code-block:: python

            # x: [M, N], vec: [N]
            # paddle.mv(x, vec)  # out: [M]

            import paddle

            x = paddle.to_tensor([[2, 1, 3], [3, 0, 1]]).astype("float64")
            vec = paddle.to_tensor([3, 5, 1]).astype("float64")
            out = paddle.mv(x, vec)
            print(out)
            # Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True,
            #        [14., 10.])
    c                 S   sz   | |d}|  D ]\}}t||ddgd q	t| j}t|j}t|dkr.td|t|dkr;td|d S )	N)r   vecr   r   mvr   z9x should be 2-dimensional. But received x's dimention: {}r   z=vec should be 1-dimensional. But received vec's dimention: {})rG   r   r-   r1   r0   r2   rx   )r   r   rH   r8   rI   r>   Z	vec_shaper?   r?   r@   rJ   Z  s(   


zmv.<locals>.__check_inputr   rK   )r%   ZVecr'   rd   N)r   )
r   r   r   r   r   r   r4   r5   r6   r7   )r   r   r8   r9   rJ   r=   r?   r?   r@   r   4  s   
r   c                 C   s   t  rt| S t rt| S t| jdddgd t| j	}t
|dks-J dt
| |d |d ksAJ d	|d |d f tdi t }|j| jd}|jd
d| gid|gid |S )af  
    Calculates determinant value of a square matrix or batches of square matrices.

    Args:
        x (Tensor): input (Tensor): the input matrix of size `(n, n)` or the
            batch of matrices of size `(*, n, n)` where `*` is one or more
            batch dimensions.

    Returns:
        Tensor, the determinant value of a square matrix or batches of square matrices.

    Examples:
        .. code-block:: python

            import paddle

            x =  paddle.randn([3,3,3])

            A = paddle.linalg.det(x)

            print(A)

            # [ 0.02547996,  2.52317095, -6.15900707])


    Inputr   r   detr   NThe x must be at least 2-dimensional, but received Input x's dimensional: %s.
rY   r   3Expect squared input,but received %s by %s matrix.
determinantrK   r'   rd   N)r   )r   r   r   r   r   r   r
   r6   r-   r1   r0   r   r4   r5   r7   r   r8   r   r=   r9   r?   r?   r@   r   y  s.   


r   c                 C   s   t  rt| S t rt| S t| jdddgd t	| j
}t|dks.J dt| |d |d ksBJ d	|d |d f tdi t }|j| jd}|jd
d| gid|gid |S )a  
    Calculates the sign and natural logarithm of the absolute value of a square matrix's or batches square matrices' determinant.
    The determinant can be computed with ``sign * exp(logabsdet)

    Supports input of float, double

    Note that for matrices that have zero determinant, this returns ``(0, -inf)``
    Args:
        x (Tensor): the batch of matrices of size :math:`(*, n, n)`
            where math:`*` is one or more batch dimensions.

    Returns:
        y (Tensor): A tensor containing the sign of the determinant and the natural logarithm
        of the absolute value of determinant, respectively.

    Examples:
        .. code-block:: python

            import paddle

            x =  paddle.randn([3,3,3])

            A = paddle.linalg.slogdet(x)

            print(A)

            # [[ 1.        ,  1.        , -1.        ],
            # [-0.98610914, -0.43010661, -0.10872950]])

    r   r   r   slogdetr   r   rY   r   r   slogdeterminantrK   r'   rd   N)r   )r   r   r   r|   r}   r   r   r
   r6   r-   r1   r0   r   r4   r5   r7   r   r?   r?   r@   r     s.   


r   c                 C   s   t  r	t| |S t rt| d|S t| dddgd t|dtd tdi t	 }|j
| jd}|j
| jd}|j
| jd}t }||d< |jdd| gi|||d|d	 |||fS )a  
    Computes the singular value decomposition of one matrix or a batch of regular matrices.

    Let :math:`X` be the input matrix or a batch of input matrices, the output should satisfies:

    .. math::
        X = U * diag(S) * VT

    Args:
        x (Tensor): The input tensor. Its shape should be `[..., N, M]`,
            where `...` is zero or more batch dimensions. N and M can be arbitraty
            positive number. Note that if x is sigular matrices, the grad is numerical
            instable. The data type of x should be float32 or float64.
        full_matrices (bool): A flag to control the behavor of svd.
            If full_matrices = True, svd op will compute full U and V matrics,
            which means shape of U is `[..., N, N]`, shape of V is `[..., M, M]`. K = min(M, N).
            If full_matrices = False, svd op will use a economic method to store U and V.
            which means shape of U is `[..., N, K]`, shape of V is `[..., M, K]`. K = min(M, N).
        name (str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tuple of 3 tensors: (U, S, VH). VH is the conjugate transpose of V. S is the singlar value vectors of matrics with shape `[..., K]`

    Examples:
        .. code-block:: python

            import paddle

            x = paddle.to_tensor([[1.0, 2.0], [1.0, 3.0], [4.0, 6.0]]).astype('float64')
            x = x.reshape([3, 2])
            u, s, vh = paddle.linalg.svd(x)
            print (u)
            #U = [[ 0.27364809, -0.21695147  ],
            #      [ 0.37892198, -0.87112408 ],
            #      [ 0.8840446 ,  0.44053933 ]]

            print (s)
            #S = [8.14753743, 0.78589688]
            print (vh)
            #VT= [[ 0.51411221,  0.85772294],
            #     [ 0.85772294, -0.51411221]]

            # one can verify : U * S * VT == X
            #                  U * UH == I
            #                  V * VH == I
    r   r6   r   r   r   rK   r%   UZVHSr(   N)r   )r   r   r   r   r   r   r	   r   r   r4   r5   r6   r   r7   )r   r   r8   r=   r   r   r   r,   r?   r?   r@   r     s&   0

r   c                 C   s   t  r	t| |S t rt| d|S t| dddgd t|dtd tdi t	 }|j
| jd}|jdd| id|id|id	 |S )a  
    Computes the n-th power of a square matrix or a batch of square matrices.

    Let :math:`X` be a sqaure matrix or a batch of square matrices, :math:`n` be
    an exponent, the equation should be:

    .. math::
        Out = X ^ {n}

    Specifically,

    - If `n > 0`, it returns the matrix or a batch of matrices raised to the power of `n`.

    - If `n = 0`, it returns the identity matrix or a batch of identity matrices.

    - If `n < 0`, it returns the inverse of each matrix (if invertible) raised to the power of `abs(n)`.

    Args:
        x (Tensor): A square matrix or a batch of square matrices to be raised
            to power `n`. Its shape should be `[*, M, M]`, where `*` is zero or
            more batch dimensions. Its data type should be float32 or float64.
        n (int): The exponent. It can be any positive, negative integer or zero.
        name (str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tensor: The n-th power of the matrix (or the batch of matrices) `x`. Its
            data type should be the same as that of `x`.

    Examples:
        .. code-block:: python

            import paddle

            x = paddle.to_tensor([[1, 2, 3],
                                  [1, 4, 9],
                                  [1, 8, 27]], dtype='float64')
            print(paddle.linalg.matrix_power(x, 2))
            # [[6.  , 34. , 102.],
            #  [14. , 90. , 282.],
            #  [36. , 250., 804.]]

            print(paddle.linalg.matrix_power(x, 0))
            # [[1., 0., 0.],
            #  [0., 1., 0.],
            #  [0., 0., 1.]]

            print(paddle.linalg.matrix_power(x, -2))
            # [[ 12.91666667, -12.75000000,  2.83333333 ],
            #  [-7.66666667 ,  8.         , -1.83333333 ],
            #  [ 1.80555556 , -1.91666667 ,  0.44444444 ]]
    nr6   r   r   matrix_powerrK   r%   r'   r(   N)r   )r   r   r   r   r   r   r	   r_   r   r4   r5   r6   r7   )r   r   r8   r=   r9   r?   r?   r@   r   2  s   5r   reducedc                 C   s   t  rt| |\}}|dkr|S ||fS t r+t| d|\}}|dkr'|S ||fS t| dddgd t|dtd tdi t	 }|j
| jd}|j
| jd}t }||d< |jdd| gi||d	|d
 |dkrm|S ||fS )a.  
    Computes the QR decomposition of one matrix or batches of matrice (backward is unsupported now).

    Args:
        x (Tensor): The input tensor. Its shape should be `[..., M, N]`,
            where ... is zero or more batch dimensions. M and N can be arbitrary
            positive number. The data type of x should be float32 or float64.
        mode (str, optional): A flag to control the behavior of qr, the default is "reduced".
            Suppose x's shape is `[..., M, N]` and denoting `K = min(M, N)`:
            If mode = "reduced", qr op will return reduced Q and R matrices,
            which means Q's shape is `[..., M, K]` and R's shape is `[..., K, N]`.
            If mode = "complete", qr op will return complete Q and R matrices,
            which means Q's shape is `[..., M, M]` and R's shape is `[..., M, N]`.
            If mode = "r", qr op will only return reduced R matrix, which means
            R's shape is `[..., K, N]`.
        name (str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        If mode = "reduced" or mode = "complete", qr will return a two tensor-tuple, which represents Q and R.
        If mode = "r", qr will return a tensor which represents R.

    Examples:
        .. code-block:: python

            import paddle

            x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]).astype('float64')
            q, r = paddle.linalg.qr(x)
            print (q)
            print (r)

            # Q = [[-0.16903085,  0.89708523],
            #      [-0.50709255,  0.27602622],
            #      [-0.84515425, -0.34503278]])

            # R = [[-5.91607978, -7.43735744],
            #      [ 0.        ,  0.82807867]])

            # one can verify : X = Q * R ;
    rmoder6   r   r   qrrK   r%   )QRr(   N)r   )r   r   r   r   r   r   r	   rw   r   r4   r5   r6   r   r7   )r   r   r8   qr   r=   r,   r?   r?   r@   r   z  s.   *r   c           	      C   s   t  rt| |\}}}nIt rt| d|\}}}n:t| dddgd tdi t }|j	| j
d}|j	dd}|j	dd}t }||d< |jdd| i|||d	|d
 |r]|||fS ||fS )uF  
    Computes the LU factorization of an N-D(N>=2) matrix x.

    Returns the LU factorization(inplace x) and Pivots. low triangular matrix L and
    upper triangular matrix U are combined to a single LU matrix.

    Pivoting is done if pivot is set to True.
    P mat can be get by pivots:

    .. code-block:: text
        ones = eye(rows) #eye matrix of rank rows
        for i in range(cols):
            swap(ones[i], ones[pivots[i]])
        return ones

    Args:

        X (Tensor): the tensor to factor of N-dimensions(N>=2).

        pivot (bool, optional): controls whether pivoting is done. Default: True.

        get_infos (bool, optional): if set to True, returns an info IntTensor. Default: False.

        name (str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        factorization (Tensor), LU matrix, the factorization of input X.

        pivots (IntTensor), the pivots of size(∗(N-2), min(m,n)). `pivots` stores all the
        intermediate transpositions of rows. The final permutation `perm` could be
        reconstructed by this, details refer to upper example.

        infos (IntTensor, optional), if `get_infos` is `True`, this is a tensor of size (∗(N-2))
        where non-zero values indicate whether factorization for the matrix or each minibatch
        has succeeded or failed.


    Examples:
        .. code-block:: python

            import paddle

            x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]).astype('float64')
            lu,p,info = paddle.linalg.lu(x, get_infos=True)

            # >>> lu:
            # Tensor(shape=[3, 2], dtype=float64, place=CUDAPlace(0), stop_gradient=True,
            #    [[5.        , 6.        ],
            #        [0.20000000, 0.80000000],
            #        [0.60000000, 0.50000000]])
            # >>> p
            # Tensor(shape=[2], dtype=int32, place=CUDAPlace(0), stop_gradient=True,
            #    [3, 3])
            # >>> info
            # Tensor(shape=[], dtype=int32, place=CUDAPlace(0), stop_gradient=True,
            #    0)

            P,L,U = paddle.linalg.lu_unpack(lu,p)

            # >>> P
            # (Tensor(shape=[3, 3], dtype=float64, place=CUDAPlace(0), stop_gradient=True,
            # [[0., 1., 0.],
            # [0., 0., 1.],
            # [1., 0., 0.]]),
            # >>> L
            # Tensor(shape=[3, 2], dtype=float64, place=CUDAPlace(0), stop_gradient=True,
            # [[1.        , 0.        ],
            # [0.20000000, 1.        ],
            # [0.60000000, 0.50000000]]),
            # >>> U
            # Tensor(shape=[2, 2], dtype=float64, place=CUDAPlace(0), stop_gradient=True,
            # [[5.        , 6.        ],
            # [0.        , 0.80000000]]))


            # one can verify : X = P @ L @ U ;
    pivotr6   r   r   lurK   r_   r%   )r'   PivotsZInfosr(   N)r   )r   r   r   r|   r}   r   r   r   r4   r5   r6   r   r7   )	r   r   	get_infosr8   r   ry   infor=   r,   r?   r?   r@   r     s(   P

r   c                 C   s   t  rt| |||\}}}|||fS t r)t| |d|d|\}}}|||fS t| dddgd tdi t }|j	| j
d}	|j	| j
d}
|j	| j
d}t }||d< ||d< |jd| |d|	|
|d	|d
 |	|
|fS )a	  
    Unpack L U and P to single matrix tensor .
    unpack L and U matrix from LU, unpack permutation matrix P from Pivtos .

    P mat can be get by pivots:

    .. code-block:: text
        ones = eye(rows) #eye matrix of rank rows
        for i in range(cols):
            swap(ones[i], ones[pivots[i]])


    Args:
        x (Tensor): The LU tensor get from paddle.linalg.lu, which is combined by L and U.

        y (Tensor): Pivots get from paddle.linalg.lu.

        unpack_ludata (bool,optional): whether to unpack L and U from x. Default: True.

        unpack_pivots (bool, optional): whether to unpack permutation matrix P from Pivtos. Default: True.

        name (str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        P (Tensor), Permutation matrix P of lu factorization.

        L (Tensor), The lower triangular matrix tensor of lu factorization.

        U (Tensor), The upper triangular matrix tensor of lu factorization.


    Examples:
        .. code-block:: python

            import paddle

            x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]).astype('float64')
            lu,p,info = paddle.linalg.lu(x, get_infos=True)

            # >>> lu:
            # Tensor(shape=[3, 2], dtype=float64, place=CUDAPlace(0), stop_gradient=True,
            #    [[5.        , 6.        ],
            #        [0.20000000, 0.80000000],
            #        [0.60000000, 0.50000000]])
            # >>> p
            # Tensor(shape=[2], dtype=int32, place=CUDAPlace(0), stop_gradient=True,
            #    [3, 3])
            # >>> info
            # Tensor(shape=[], dtype=int32, place=CUDAPlace(0), stop_gradient=True,
            #    0)

            P,L,U = paddle.linalg.lu_unpack(lu,p)

            # >>> P
            # (Tensor(shape=[3, 3], dtype=float64, place=CUDAPlace(0), stop_gradient=True,
            # [[0., 1., 0.],
            # [0., 0., 1.],
            # [1., 0., 0.]]),
            # >>> L
            # Tensor(shape=[3, 2], dtype=float64, place=CUDAPlace(0), stop_gradient=True,
            # [[1.        , 0.        ],
            # [0.20000000, 1.        ],
            # [0.60000000, 0.50000000]]),
            # >>> U
            # Tensor(shape=[2, 2], dtype=float64, place=CUDAPlace(0), stop_gradient=True,
            # [[5.        , 6.        ],
            # [0.        , 0.80000000]]))

            # one can verify : X = P @ L @ U ;
    unpack_ludataunpack_pivotsr6   r   r   	lu_unpackrK   )r%   r   )ZPmatLr   r(   N)r   )r   r   r   r|   r}   r   r   r   r4   r5   r6   r   r7   )r   rE   r   r   r8   Pr   r   r=   ry   lr   r,   r?   r?   r@   r   (	  s.   I




r   c                 C   s   t  rt| S t rt| \}}||fS t| dg dd tdi t }|	| j
}|	| j
}d| i}||d}|jd||d ||fS )a  
    Performs the eigenvalue decomposition of a square matrix or a batch of square matrices.

    Note:
        - If the matrix is a Hermitian or a real symmetric matrix, please use :ref:`paddle.linalg.eigh` instead, which is much faster.
        - If only eigenvalues is needed, please use :ref:`paddle.linalg.eigvals` instead.
        - If the matrix is of any shape, please use :ref:`paddle.linalg.svd`.
        - This API is only supported on CPU device.
        - The output datatype is always complex for both real and complex input.

    Args:
        x (Tensor): A tensor with shape math:`[*, N, N]`, The data type of the x should be one of ``float32``,
            ``float64``, ``compplex64`` or ``complex128``.
        name (str, optional): The default value is `None`. Normally there is no need for user to set
            this property. For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Eigenvalues(Tensors): A tensor with shape math:`[*, N]` refers to the eigen values.
        Eigenvectors(Tensors): A tensor with shape math:`[*, N, N]` refers to the eigen vectors.

    Examples:
        .. code-block:: python

            import paddle

            paddle.device.set_device("cpu")

            x = paddle.to_tensor([[1.6707249, 7.2249975, 6.5045543],
                               [9.956216,  8.749598,  6.066444 ],
                               [4.4251957, 1.7983172, 0.370647 ]])
            w, v = paddle.linalg.eig(x)
            print(v)
            # Tensor(shape=[3, 3], dtype=complex128, place=CPUPlace, stop_gradient=False,
            #       [[(-0.5061363550800655+0j) , (-0.7971760990842826+0j) ,
            #         (0.18518077798279986+0j)],
            #        [(-0.8308237755993192+0j) ,  (0.3463813401919749+0j) ,
            #         (-0.6837005269141947+0j) ],
            #        [(-0.23142567697893396+0j),  (0.4944999840400175+0j) ,
            #         (0.7058765252952796+0j) ]])

            print(w)
            # Tensor(shape=[3], dtype=complex128, place=CPUPlace, stop_gradient=False,
            #       [ (16.50471283351188+0j)  , (-5.5034820550763515+0j) ,
            #         (-0.21026087843552282+0j)])
    r%   r   r   r    r!   eigZEigenvaluesZEigenvectorsrd   N)r   )r   r   r   r|   r}   r   r   r   r4   r5   r6   r7   )r   r8   r   vr=   r*   r+   r?   r?   r@   r   	  s   .

r   c                 C   s   t | dg dd t| j}t|dk rtdt|||d |d kr-td|t r5t| S t	
 r>t| S tdi t }|j| jd	}|jdd
| id|id |S )aj  
    Compute the eigenvalues of one or more general matrices.

    Warning:
        The gradient kernel of this operator does not yet developed.
        If you need back propagation through this operator, please replace it with paddle.linalg.eig.

    Args:
        x (Tensor): A square matrix or a batch of square matrices whose eigenvalues will be computed.
            Its shape should be `[*, M, M]`, where `*` is zero or more batch dimensions.
            Its data type should be float32, float64, complex64, or complex128.
        name (str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tensor, A tensor containing the unsorted eigenvalues which has the same batch
        dimensions with `x`. The eigenvalues are complex-valued even when `x` is real.

    Examples:
        .. code-block:: python

            import paddle

            paddle.set_device("cpu")
            paddle.seed(1234)

            x = paddle.rand(shape=[3, 3], dtype='float64')
            # [[0.02773777, 0.93004224, 0.06911496],
            #  [0.24831591, 0.45733623, 0.07717843],
            #  [0.48016702, 0.14235102, 0.42620817]])

            print(paddle.linalg.eigvals(x))
            # [(-0.27078833542132674+0j), (0.29962280156230725+0j), (0.8824477020120244+0j)] #complex128
    r6   r   eigvalsr   z_The dimension of Input(x) should be at least 2, but received x's dimention = {}, x's shape = {}rY   r   zPThe last two dimensions of Input(x) should be equal, but received x's shape = {}rK   r%   r'   rd   N)r   )r   r-   r1   r0   r2   rx   r   r   r   r|   r}   r   r   r4   r5   r6   r7   )r   r8   r>   r=   r9   r?   r?   r@   r   	  s0   $


r   c                 C   s   t  rt| S t rt| S t| dttfd t| D ]\}}t	|dt
| d g dd |j| d jkr<tdqtdi t }|jdd}||}|jdd	| id
|id |S )aK  
    Multi_dot is an operator that calculates multiple matrix multiplications.

    Supports inputs of float16(only GPU support), float32 and float64 dtypes. This function does not
    support batched inputs.

    The input tensor in [x] must be 2-D except for the first and last can be 1-D.
    If the first tensor is a 1-D vector of shape(n, ) it is treated as row vector
    of shape(1, n), similarly if the last tensor is a 1D vector of shape(n, ), it
    is treated as a column vector of shape(n, 1).

    If the first and last tensor are 2-D matrix, then the output is also 2-D matrix,
    otherwise the output is a 1-D vector.

    Multi_dot will select the lowest cost multiplication order for calculation. The
    cost of multiplying two matrices with shapes (a, b) and (b, c) is a * b * c.
    Given matrices A, B, C with shapes (20, 5), (5, 100), (100, 10) respectively,
    we can calculate the cost of different multiplication orders as follows:
    - Cost((AB)C) = 20x5x100 + 20x100x10 = 30000
    - Cost(A(BC)) = 5x100x10 + 20x5x10 = 6000

    In this case, multiplying B and C first, then multiply A, which is 5 times faster
    than sequential calculation.

    Args:
        x ([Tensor]): The input tensors which is a list Tensor.
        name(str|None): A name for this layer(optional). If set None, the layer
            will be named automatically.

    Returns:
        Tensor: The output Tensor.


    Examples:

    .. code-block:: python

        import paddle
        # A * B
        A = paddle.rand([3, 4])
        B = paddle.rand([4, 5])
        out = paddle.linalg.multi_dot([A, B])
        print(out.shape)
        # [3, 5]
        # A * B * C
        A = paddle.rand([10, 5])
        B = paddle.rand([5, 8])
        C = paddle.rand([8, 7])
        out = paddle.linalg.multi_dot([A, B, C])
        print(out.shape)
        # [10, 7]
    r   	multi_dotzx[])r   r   r   r   z:All the Tensors in the input must have the same data type.)Zinput_param_namer%   r'   rd   N)r   )r   r   r   r   r   r	   r-   r.   r3   r   rw   r6   r   r   r4   rW   r5   r7   )r   r8   idr   r=   r6   r9   r?   r?   r@   r   
  s,   5


r   r   c                 C   s   t  r	t| |S t rt| d|S dd }|| | tdi t }t| dg dd |j| j	d}|j| j	d}|j
dd| i||d	d|id
 ||fS )au  
    Compute the eigenvalues and eigenvectors of a
    complex Hermitian (conjugate symmetric) or a real symmetric matrix.

    Args:
        x (Tensor): A tensor with shape :math:`[*, N, N]` , The data type of the input Tensor x
            should be one of float32, float64, complex64, complex128.
        UPLO(str, optional): (string, default 'L'), 'L' represents the lower triangular matrix,
                        "'U' represents the upper triangular matrix.".
        name(str, optional): The default value is None.  Normally there is no need for user to set this
            property.  For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        - out_value(Tensor):  A Tensor with shape [*, N] and data type of float32 and float64.
            The eigenvalues of eigh op.
        - out_vector(Tensor): A Tensor with shape [*, N, N] and data type of float32,float64,
            complex64 and complex128. The eigenvectors of eigh op.

    Examples:
        .. code-block:: python

            import paddle

            x = paddle.to_tensor([[1, -2j], [2j, 5]])
            out_value, out_vector = paddle.linalg.eigh(x, UPLO='L')
            print(out_value)
            #[0.17157288, 5.82842712]
            print(out_vector)
            #[(-0.9238795325112867+0j), (-0.3826834323650898+0j)],
            #[ 0.3826834323650898j    , -0.9238795325112867j    ]]

    UPLOc                 S   n   t | j}t| jdk rtdt| j |d |d kr$td||dkr3|dkr5td|d S d S 	Nr   zPInput(input) only support >=2 tensor, but received length of Input(input) is %s.rY   r   zSThe input matrix must be batches of square matrices. But received x's dimention: {}r   r   z-UPLO must be L or U. But received UPLO is: {}r-   r1   r0   r2   rx   r   r   r>   r?   r?   r@   rJ   
  $   
zeigh.<locals>.__check_inputeighr6   r   rK   r%   r   r(   N)r   )r   r   r   r   r   r   r4   r   r5   r6   r7   )r   r   r8   rJ   r=   	out_value
out_vectorr?   r?   r@   r   `
  s&   !
r   V瞯<c              	   C   sd  t  r|st| d\}}}t|dgd}tj|| jd}|| }td}	tj|	| jd}	||k}
t|
|j}tt	|
|j}t
d| |}t
d|	 |}t||}t|dg}ttt|j}|dd |d g |d g }t||}|| }t||dd}|S t| d	\}}t|}t|dgd}tj||jd}|| }td}	tj|	|jd}	||k}
t|
|j}tt	|
|j}t
d| |}t
d|	 |}t||}t|dg}|| }t|}t||dd}|S t r|st| d
d\}}}t|ddgdddd}tj|| jd}|| }td}	tj|	| jd}	||k}
t|
|j}tt	|
|j}t
d| |}t
d|	 |}t||}t|ddg\}}ttt|j}|dd |d g |d g }t|d|\}}|| }t  rt||dd}|S t||dddd}|S t| d	d\}}t|}t|ddgdddd}tj||jd}|| }td}	tj|	|jd}	||k}
t|
|j}tt	|
|j}t
d| |}t
d|	 |}t||}t|ddg\}}|| }t|}t  rt||dd}|S t||dddd}|S |s+td/i t }| j}t| dddgd ||}||}||}|j dd| gi|||dd
did ||}|j dd|id|idgdddd t!dg||d}|| }td}	t!dg|	|d}	||k}
t|
|}tt	|
|}t
d| |}t
d|	 |}t||}|j|d}|j|d}|j dd|iddgi||d d! ttt|j}|dd |d g |d g }||}||}|j d"d|gi|g|gd d|id ||}|j d#||d$d|iddd%d |"|}||}|j d&||d$d|iddd'd |S td/i t }| j}t| d(g d)d |tj#krHd}n|tj$krQd}n|}||}||}|j d*d| i||d+d	did ||}|j d,d|id|id- ||}|j dd|id|idgdddd t!dg||d}|| }td}	t!dg|	|d}	||k}
t|
|}tt	|
|}t
d| |}t
d|	 |}t||}|j|d}|j|d}|j dd|iddgi||d d! ||}|j d#||d$d|iddd%d |"|}||}|j d.d|id|gid- ||}|j d&||d$d|iddd'd |S )0aF  
    Calculate pseudo inverse via SVD(singular value decomposition)
    of one matrix or batches of regular matrix.

    .. math::

        if hermitian == False:
            x = u * s * vt  (SVD)
            out = v * 1/s * ut
        else:
            x = u * s * ut  (eigh)
            out = u * 1/s * u.conj().transpose(-2,-1)

    If x is hermitian or symmetric matrix, svd will be replaced with eigh.

    Args:
        x(Tensor): The input tensor. Its shape should be (*, m, n)
            where * is zero or more batch dimensions. m and n can be
            arbitraty positive number. The data type of x should be
            float32 or float64 or complex64 or complex128. When data
            type is complex64 or cpmplex128, hermitian should be set
            True.

        rcond(Tensor, optional): the tolerance value to determine
            when is a singular value zero. Default:1e-15.

        hermitian(bool, optional): indicates whether x is Hermitian
            if complex or symmetric if real. Default: False.

        name(str|None): A name for this layer(optional). If set None,
            the layer will be named automatically.

    Returns:
        Tensor: The tensor with same data type with x. it represents
        pseudo inverse of x. Its shape should be (*, n, m).

    Examples:
        .. code-block:: python

            import paddle

            x = paddle.arange(15).reshape((3, 5)).astype('float64')
            input = paddle.to_tensor(x)
            out = paddle.linalg.pinv(input)
            print(input)
            print(out)

            # input:
            # [[0. , 1. , 2. , 3. , 4. ],
            # [5. , 6. , 7. , 8. , 9. ],
            # [10., 11., 12., 13., 14.]]

            # out:
            # [[-0.22666667, -0.06666667,  0.09333333],
            # [-0.12333333, -0.03333333,  0.05666667],
            # [-0.02000000,  0.00000000,  0.02000000],
            # [ 0.08333333,  0.03333333, -0.01666667],
            # [ 0.18666667,  0.06666667, -0.05333333]]

            # one can verify : x * out * x = x ;
            # or              out * x * out = x ;
    FrY   TrK   ra   r   r   Nr   r   r<   rR   rS   Zaxesr   rB   rC   r   pinvr   r   r   r   r%   r   r(   re   r'   rT   )r1   Z
fill_valuer6   
unsqueeze2r&   r   r$   Zelementwise_mulrL   )r   r   rA   rD   r6   r   r   r   rc   rd   r   )r   )%r   r   r   rh   r|   r   r6   r^   r   r   r   r   r   r-   ranger0   r1   r"   rF   r   rc   r   r   r   re   r   r$   rA   r   r4   r   r5   r7   r   Zappend_activationr!   r    )r   rcondr   r8   r   r   vtZmax_singular_valcutoffrE   	conditionZcond_intZcond_not_intZout1Zout2Zsingularstdimsr#   r   Zout_1Zout_2Zs_absZu_conjr:   r=   r6   Zst_shapeZv_shapeZs_typer?   r?   r@   r   
  s  ?
 



 









 














r   c                 C   s   t  r	t| |S t rt| |S | g|gd}tdi t }t| dddgd t|dddgd |j| j	d}|j
d| |dd|id	 |S )a#  
    Computes the solution of a square system of linear equations with a unique solution for input 'X' and 'Y'.
    Let :math: `X` be a sqaure matrix or a batch of square matrices, :math:`Y` be
    a vector/matrix or a batch of vectors/matrices, the equation should be:

    .. math::
        Out = X^-1 * Y

    Specifically, this system of linear equations has one solution if and only if input 'X' is invertible.

    Args:
        x (Tensor): A square matrix or a batch of square matrices. Its shape should be `[*, M, M]`, where `*` is zero or
            more batch dimensions. Its data type should be float32 or float64.
        y (Tensor): A vector/matrix or a batch of vectors/matrices. Its shape should be `[*, M, K]`, where `*` is zero or
            more batch dimensions. Its data type should be float32 or float64.
        name(str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tensor: The solution of a square system of linear equations with a unique solution for input 'x' and 'y'.
        Its data type should be the same as that of `x`.

    Examples:

        .. code-block:: python

            # a square system of linear equations:
            # 2*X0 + X1 = 9
            # X0 + 2*X1 = 8

            import paddle

            x = paddle.to_tensor([[3, 1],[1, 2]], dtype="float64")
            y = paddle.to_tensor([9, 8], dtype="float64")
            out = paddle.linalg.solve(x, y)

            print(out)
            # [2., 3.])
    rL   solver   r   r   rE   rK   r'   rd   N)r   )r   r   r   r   r   r   r4   r   r5   r6   r7   )r   rE   r8   r*   r=   r9   r?   r?   r@   r     s   (r   c           	   
   C   s   t  rt| ||||S t rt| |d|d|d|S | g|gd}tdi t }t| dddgd t|d	ddgd |j	| j
d
}|jd| |dd|i|||dd |S )a  
        Computes the solution of a system of equations with a triangular coefficient matrix `x` and
        multiple right-hand sides `y` .

        Input `x` and `y` is 2D matrices or batches of 2D matrices. If the inputs are batches, the outputs
        is also batches.

    Args:
        x (Tensor): The input triangular coefficient matrix. Its shape should be `[*, M, M]`, where `*` is zero or
            more batch dimensions. Its data type should be float32 or float64.
        y (Tensor): Multiple right-hand sides of system of equations. Its shape should be `[*, M, K]`, where `*` is
            zero or more batch dimensions. Its data type should be float32 or float64.
        upper (bool, optional): Whether to solve the upper-triangular system of equations (default) or the lower-triangular
            system of equations. Default: True.
        transpose (bool, optional): whether `x` should be transposed before calculation. Default: False.
        unitriangular (bool, optional): whether `x` is unit triangular. If True, the diagonal elements of `x` are assumed
            to be 1 and not referenced from `x` . Default: False.
        name(str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tensor: The solution of the system of equations. Its data type should be the same as that of `x`.

    Examples:
        .. code-block:: python

            # a square system of linear equations:
            # x1 +   x2  +   x3 = 0
            #      2*x2  +   x3 = -9
            #               -x3 = 5

            import paddle

            x = paddle.to_tensor([[1, 1, 1],
                                  [0, 2, 1],
                                  [0, 0,-1]], dtype="float64")
            y = paddle.to_tensor([[0], [-9], [5]], dtype="float64")
            out = paddle.linalg.triangular_solve(x, y, upper=True)

            print(out)
            # [7, -2, -5]
    r   r"   unitriangularrL   triangular_solver   r   r   rE   rK   r'   )r   r"   r   r(   N)r   )r   r   r   r|   r}   r   r   r4   r   r5   r6   r7   )	r   rE   r   r"   r   r8   r*   r=   r9   r?   r?   r@   r   -  s8   -
r   c                 C   s   t  r
t| ||S t rt| |d|S tdi t }t| dddgd t|dddgd |j| j	d}|j
d| |dd	|id|id
 |S )a  
    Solves a linear system of equations A @ X = B, given A's Cholesky factor matrix u and  matrix B.

    Input `x` and `y` is 2D matrices or batches of 2D matrices. If the inputs are batches, the outputs
    is also batches.

    Args:
        x (Tensor): The input matrix which is upper or lower triangular Cholesky factor of square matrix A. Its shape should be `[*, M, M]`, where `*` is zero or
            more batch dimensions. Its data type should be float32 or float64.
        y (Tensor): Multiple right-hand sides of system of equations. Its shape should be `[*, M, K]`, where `*` is
            zero or more batch dimensions. Its data type should be float32 or float64.
        upper (bool, optional): whether to consider the Cholesky factor as a lower or upper triangular matrix. Default: False.
        name(str, optional): Name for the operation (optional, default is None).
            For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tensor: The solution of the system of equations. Its data type is the same as that of `x`.

    Examples:
        .. code-block:: python

            import paddle

            u = paddle.to_tensor([[1, 1, 1],
                                    [0, 2, 1],
                                    [0, 0,-1]], dtype="float64")
            b = paddle.to_tensor([[0], [-9], [5]], dtype="float64")
            out = paddle.linalg.cholesky_solve(b, u, upper=True)

            print(out)
            # [-2.5, -7, 9.5]
    r   cholesky_solver   r   r   rE   rK   rL   r'   r(   N)r   )r   r   r   r   r   r   r4   r   r5   r6   r7   )r   rE   r   r8   r=   r9   r?   r?   r@   r   |  s   !r   c           
      C   s   t  rt| || j\}}|S t r#| j}t| d|d|\}}|S dd }|| | tdi t }t	| dg dd |j
| jd}|j
| jd}	| j}|jdd	| i||	d
||dd |S )u  
    Computes the eigenvalues of a
    complex Hermitian (conjugate symmetric) or a real symmetric matrix.

    Args:
        x (Tensor): A tensor with shape :math:`[_, M, M]` , The data type of the input Tensor x
            should be one of float32, float64, complex64, complex128.
        UPLO(str, optional): Lower triangular part of a (‘L’, default) or the upper triangular part (‘U’).
        name(str, optional): The default value is None.  Normally there is no need for user to set this
            property.  For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tensor: The tensor eigenvalues in ascending order.

    Examples:
        .. code-block:: python

            import paddle

            x = paddle.to_tensor([[1, -2j], [2j, 5]])
            out_value = paddle.eigvalsh(x, UPLO='L')
            print(out_value)
            # Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
            #        [0.17157286, 5.82842731])
    r   is_testc                 S   r   r   r   r   r?   r?   r@   rJ     r   zeigvalsh.<locals>.__check_inputeigvalshr6   r   rK   r%   r   )r   r   r(   N)r   )r   r   r   Zstop_gradientr|   r}   r   r   r4   r   r5   r6   r7   )
r   r   r8   valuesr:   r   rJ   r=   r   r   r?   r?   r@   r     s4   
r   c                 C   sp  t  }|dkr|dvrtd||du rdn|}nd|v r4|dvr+td||du r1d	n|}ntd
| j|jkrH| jt jt jfv rHntd|du rw| jt jkrddt| j	d | j	d  }n| jt jkrwdt| j	d | j	d  }t
 rt rt| |||\}}}}	nt| |d|d|\}}}}	|d	krt jdgt jd}t jdg| jd}	n|dkrt jdg| jd}	||||	fS tdi t }
t| dg dd t|dg dd |
j| jd}|
j| jd}|
jt jd}|
j| jd}	|
jd| |d||||	d||dd |d	kr$t jjddgd}t jjddgd}	n|dkr2t jjddgd}	||||	fS ) uK  
    Computes a solution to
    the least squares problem of a system of linear equations.

    Args:
        x (Tensor): A tensor with shape ``(*, M, N)`` , the data type of the input Tensor ``x``
            should be one of float32, float64.
        y (Tensor): A tensor with shape ``(*, M, K)`` , the data type of the input Tensor ``y``
            should be one of float32, float64.
        rcond(float, optional): The default value is None. A float pointing number used to determine
            the effective rank of ``x``. If ``rcond`` is None, it will be set to max(M, N) times the
            machine precision of x_dtype.
        driver(str, optional): The default value is None. The name of LAPACK method to be used. For
            CPU inputs the valid values are ‘gels’, ‘gelsy’, ‘gelsd, ‘gelss’. For CUDA input, the only
            valid driver is ‘gels’. If ``driver`` is None, ‘gelsy’ is used for CPU inputs and ‘gels’
            for CUDA inputs.
        name(str, optional): The default value is None. Normally there is no need for user to set
            this property. For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tuple: A tuple of 4 Tensors which is (``solution``, ``residuals``, ``rank``, ``singular_values``).
        ``solution`` is a tensor with shape ``(*, N, K)``, meaning the least squares solution. ``residuals``
        is a tensor with shape ``(*, K)``, meaning the squared residuals of the solutions, which is computed
        when M > N and every matrix in ``x`` is full-rank, otherwise return an empty tensor. ``rank`` is a tensor
        with shape ``(*)``, meaning the ranks of the matrices in ``x``, which is computed when ``driver`` in
        (‘gelsy’, ‘gelsd’, ‘gelss’), otherwise return an empty tensor. ``singular_values`` is a tensor with
        shape ``(*, min(M, N))``, meaning singular values of the matrices in ``x``, which is computed when
        ``driver`` in (‘gelsd’, ‘gelss’), otherwise return an empty tensor.

    Examples:
        .. code-block:: python

            import paddle

            paddle.set_device("cpu")
            x = paddle.to_tensor([[1, 3], [3, 2], [5, 6.]])
            y = paddle.to_tensor([[3, 4, 6], [5, 3, 4], [1, 2, 1.]])
            results = paddle.linalg.lstsq(x, y, driver="gelsd")
            print(results[0])
            # [[ 0.78350395, -0.22165027, -0.62371236],
            # [-0.11340097,  0.78866047,  1.14948535]]
            print(results[1])
            # [19.81443405, 10.43814468, 30.56185532])
            print(results[2])
            # 2
            print(results[3])
            # [9.03455734, 1.54167950]

            x = paddle.to_tensor([[10, 2, 3], [3, 10, 5], [5, 6, 12.]])
            y = paddle.to_tensor([[4, 2, 9], [2, 0, 3], [2, 5, 3.]])
            results = paddle.linalg.lstsq(x, y, driver="gels")
            print(results[0])
            # [[ 0.39386186,  0.10230173,  0.93606132],
            # [ 0.10741687, -0.29028133,  0.11892585],
            # [-0.05115091,  0.51918161, -0.19948854]]
            print(results[1])
            # []
    cpu)NgelsZgelssZgelsdgelsyzaOnly support valid driver is 'gels', 'gelss', 'gelsd', 'gelsy' or None for CPU inputs. But got {}Nr   Zgpu)Nr   zGOnly support valid driver is 'gels' or None for CUDA inputs. But got {}r   z.Only support lstsq api for CPU or CUDA device.zIOnly support x and y have the same dtype such as 'float32' and 'float64'.gHz>r   rY   r   r   driverr   )r1   r6   lstsqr6   r   rK   rL   )ZSolutionZ	ResidualsZRankZSingularValues)r   r   r(   rank)r8   r1   singular_values)r   )r|   Z
get_devicer2   rx   RuntimeErrorr6   r   r   rh   r1   r   r   r   r   r   emptyr   r   r4   r   r5   r7   staticdata)r   rE   r   r   r8   ZdeviceZsolutionZ	residualsr   r   r=   r?   r?   r@   r     s   ;

r   c                 C   s   t | jdkst | jdk rtdt | j t| dddgd t| |}|jdkr.|| S t|}t|r<|	 }t
|}||d	d	d	f  }||d	d	d	f  }t|rntt|	 d
dt| d
dS t|d
d}|S )u  

    A correlation coefficient matrix indicate the correlation of each pair variables in the input matrix.
    For example, for an N-dimensional samples X=[x1,x2,…xN]T, then the correlation coefficient matrix
    element Rij is the correlation of xi and xj. The element Rii is the covariance of xi itself.

    The relationship between the correlation coefficient matrix `R` and the
    covariance matrix `C`, is

    .. math:: R_{ij} = \frac{ C_{ij} } { \sqrt{ C_{ii} * C_{jj} } }

    The values of `R` are between -1 and 1.

    Parameters:

        x(Tensor): A N-D(N<=2) Tensor containing multiple variables and observations. By default, each row of x represents a variable. Also see rowvar below.
        rowvar(Bool, optional): If rowvar is True (default), then each row represents a variable, with observations in the columns. Default: True.
        name(str, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name`.

    Returns:

        The correlation coefficient matrix of the variables.

    Examples:
        .. code-block:: python

            import paddle

            xt = paddle.rand((3,4))
            print(paddle.linalg.corrcoef(xt))

            # Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            # [[ 1.        , -0.73702252,  0.66228950],
            # [-0.73702258,  1.        , -0.77104872],
            # [ 0.66228974, -0.77104825,  1.        ]])

    r   r   zbInput(x) only support N-D (1<=N<=2) tensor in corrcoef, but received length of Input(input) is %s.r6   r   r   corrcoefr   NrY   )r0   r1   r2   r   r   ndimr|   ZdiagZ
is_complexrealsqrtcomplexZclipimag)r   r   r8   cdZstddevr?   r?   r@   r     s,   &





 r   )N)FFN)rQ   NFN)r   N)NN)TTNNN)r   N)FNrv   )r   r   r   N)Nr   N)r   N)TFN)TTN)r   N)r   FN)TFFN)NNN)TN)Cnumpyrg   Z	frameworkr   r   r   r   r   Zfluid.data_feederr   r	   r
   r   r   Zfluid.frameworkr   Zmanipulationr   mathr   r   Zlogicr   Zcreationr   r|   warningsZpaddle.common_ops_importr   r   r   r   __all__r   r"   rF   rk   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r?   r?   r?   r@   <module>   sx   

f 
  
u
d   
&
A
{
S
>
7
p
H
/
8
E
8
<
E
H
F
h
e
C
B
N
L  
I;

O
5
L 