o
    #j+                     @   s   d dl Zd dlZd dlmZ ddlmZmZ ddlmZm	Z	 ddl
mZ ddlmZmZ dd	lmZmZ g Zd
d Zdd Zdd Zdd Zdd ZdddZdddZdS )    N)_C_ops   )
check_typecheck_variable_and_dtype)in_dynamic_or_pir_modein_pir_mode)Variable)LayerHelpercore   )_complex_to_real_dtypeassignc                 C   s4   t | dttjjfd t| j}tt	|d}|S )a%  

    Returns the number of dimensions for a tensor, which is a 0-D int32 Tensor.

    Args:
        input (Tensor): The input Tensor with shape of :math:`[N_1, N_2, ..., N_k]`, the data type is arbitrary.

    Returns:
        Tensor, the output data type is int32.: The 0-D tensor with the dimensions of the input Tensor.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> input = paddle.rand((3, 100, 100))
            >>> rank = paddle.rank(input)
            >>> print(rank.numpy())
            3
    inputint32)
r   r   paddlepirValuelenshaper   nparray)r   Zndimsout r   X/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/tensor/attribute.pyrank   s   
r   c                 C   sl   t  rt| }d|_|S t| dg dd tdi t }|jdd}|jdd| id|idd	 d|_|S )a  
    Get the shape of the input.

    .. code-block:: text

        Case1:
            Given N-D Tensor:
                input = [ [1, 2, 3, 4], [5, 6, 7, 8] ]

            Then:
                input.shape = [2, 4]

        Case2:
            Given SelectedRows:
                input.rows = [0, 4, 19]
                input.height = 20
                input.value = [ [1, 2], [3, 4], [5, 6] ]  # inner tensor
            Then:
                input.shape = [3, 2]

    Args:
        input (Variable): The input can be N-D Tensor or SelectedRows with data type bool, bfloat16, float16, float32, float64, int32, int64.
                          If input variable is type of SelectedRows, returns the shape of it's inner tensor.

    Returns:
        Variable (Tensor): The shape of the input variable.

    Examples:
        .. code-block:: python

            >>> import numpy as np
            >>> import paddle
            >>> paddle.enable_static()

            >>> inputs = paddle.static.data(name="x", shape=[3, 100, 100], dtype="float32")
            >>> output = paddle.shape(inputs)

            >>> exe = paddle.static.Executor(paddle.CPUPlace())
            >>> exe.run(paddle.static.default_startup_program())

            >>> img = np.ones((3, 100, 100)).astype(np.float32)

            >>> res = exe.run(paddle.static.default_main_program(), feed={'x':img}, fetch_list=[output])
            >>> print(res)
            [array([  3, 100, 100], dtype=int32)]
    Tr   )
booluint16Zfloat16Zfloat32Zfloat64r   Zint64	complex64
complex128r   r   r   dtypeZInputOut)typeinputsoutputsstop_gradientN)r   )	r   r   r   r%   r   r	   locals"create_variable_for_type_inference	append_op)r   r   helperr   r   r   r   ;   s(   /
r   c                 C   sj   t | tjtjjtjjfstdt|  | j	}|t
jjjkp2|t
jjjkp2|t
jjkp2|t
jjk}|S )a  Return whether x is a tensor of complex data type(complex64 or complex128).

    Args:
        x (Tensor): The input tensor.

    Returns:
        bool: True if the data type of the input is complex data type, otherwise false.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> x = paddle.to_tensor([1 + 2j, 3 + 4j])
            >>> print(paddle.is_complex(x))
            True

            >>> x = paddle.to_tensor([1.1, 1.2])
            >>> print(paddle.is_complex(x))
            False

            >>> x = paddle.to_tensor([1, 2, 3])
            >>> print(paddle.is_complex(x))
            False
    )Expected Tensor, but received type of x: )
isinstancer   Tensorstaticr   r   r   	TypeErrorr"   r    r
   VarDescVarTypeZ	COMPLEX64Z
COMPLEX128DataType)xr    Zis_complex_dtyper   r   r   
is_complex   s   

r3   c                 C   sh   t | tjtjjfstdt|  | j}|tj	j
jkp1|tj	j
jkp1|tj	j
jkp1|tj	j
jk}|S )aK  
    Returns whether the dtype of `x` is one of paddle.float64, paddle.float32, paddle.float16, and paddle.bfloat16.

    Args:
        x (Tensor): The input tensor.

    Returns:
        bool: True if the dtype of `x` is floating type, otherwise false.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> x = paddle.arange(1., 5., dtype='float32')
            >>> y = paddle.arange(1, 5, dtype='int32')
            >>> print(paddle.is_floating_point(x))
            True
            >>> print(paddle.is_floating_point(y))
            False
    r*   )r+   r   r,   r-   r   r.   r"   r    r
   r/   r0   ZFP32ZFP64ZFP16ZBF16)r2   r    Zis_fp_dtyper   r   r   is_floating_point   s   r4   c                 C   s   t | tjtjjtjjfstdt|  | j	}d}t
 sC|tjjjkp@|tjjjkp@|tjjjkp@|tjjjkp@|tjjjk}|S |tjjkp`|tjjkp`|tjjkp`|tjjkp`|tjjk}|S )aq  Return whether x is a tensor of integeral data type.

    Args:
        x (Tensor): The input tensor.

    Returns:
        bool: True if the data type of the input is integer data type, otherwise false.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> x = paddle.to_tensor([1 + 2j, 3 + 4j])
            >>> print(paddle.is_integer(x))
            False

            >>> x = paddle.to_tensor([1.1, 1.2])
            >>> print(paddle.is_integer(x))
            False

            >>> x = paddle.to_tensor([1, 2, 3])
            >>> print(paddle.is_integer(x))
            True
    r*   F)r+   r   r,   r-   r   r   ZOpResultr.   r"   r    r   r
   r/   r0   ZUINT8ZINT8ZINT16ZINT32ZINT64r1   )r2   r    Zis_int_dtyper   r   r   
is_integer   s6   



r5   c                 C   b   t  rt| S t| dddgd td
i t }|jt| d}|j	dd| id|id |S )a  
    Returns a new Tensor containing real values of the input Tensor.

    Args:
        x (Tensor): the input Tensor, its data type could be complex64 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:
        Tensor: a Tensor containing real values of the input Tensor.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> x = paddle.to_tensor(
            ...     [[1 + 6j, 2 + 5j, 3 + 4j], [4 + 3j, 5 + 2j, 6 + 1j]])
            >>> print(x)
            Tensor(shape=[2, 3], dtype=complex64, place=Place(cpu), stop_gradient=True,
            [[(1+6j), (2+5j), (3+4j)],
             [(4+3j), (5+2j), (6+1j)]])

            >>> real_res = paddle.real(x)
            >>> print(real_res)
            Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[1., 2., 3.],
             [4., 5., 6.]])

            >>> real_t = x.real()
            >>> print(real_t)
            Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[1., 2., 3.],
             [4., 5., 6.]])
    r2   r   r   realr   Xr!   r"   r#   r$   N)r7   )
r   r   r7   r   r	   r&   r'   r   input_dtyper(   r2   namer)   r   r   r   r   r7        $

r7   c                 C   r6   )a  
    Returns a new tensor containing imaginary values of input tensor.

    Args:
        x (Tensor): the input tensor, its data type could be complex64 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:
        Tensor: a tensor containing imaginary values of the input tensor.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> x = paddle.to_tensor(
            ...     [[1 + 6j, 2 + 5j, 3 + 4j], [4 + 3j, 5 + 2j, 6 + 1j]])
            >>> print(x)
            Tensor(shape=[2, 3], dtype=complex64, place=Place(cpu), stop_gradient=True,
            [[(1+6j), (2+5j), (3+4j)],
             [(4+3j), (5+2j), (6+1j)]])

            >>> imag_res = paddle.imag(x)
            >>> print(imag_res)
            Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[6., 5., 4.],
             [3., 2., 1.]])

            >>> imag_t = x.imag()
            >>> print(imag_t)
            Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[6., 5., 4.],
             [3., 2., 1.]])
    r2   r   r   imagr   r8   r!   r9   N)r>   )
r   r   r>   r   r	   r&   r'   r   r:   r(   r;   r   r   r   r>   ;  r=   r>   )N)numpyr   r   r   Zbase.data_feederr   r   Zbase.frameworkr   r   Zcommon_ops_importr   Z	frameworkr	   r
   Zcreationr   r   __all__r   r   r3   r4   r5   r7   r>   r   r   r   r   <module>   s    Q("
50