
    IR-e&                     ~    d Z ddlZddlmZmZmZ ddlmZ ddgZ	d Z
d	 Zd
 Zd Zd Zd Zd Zd ZeeeeeeedZdS )a  
Functions to determine if a model is separable, i.e.
if the model outputs are independent.

It analyzes ``n_inputs``, ``n_outputs`` and the operators
in a compound model by stepping through the transforms
and creating a ``coord_matrix`` of shape (``n_outputs``, ``n_inputs``).


Each modeling operator is represented by a function which
takes two simple models (or two ``coord_matrix`` arrays) and
returns an array of shape (``n_outputs``, ``n_inputs``).

    N   )CompoundModelModelModelDefinitionError)Mappingis_separableseparability_matrixc                     | j         dk    r/| j        dk    r$t          j        dg| j        z            j        }|S t          |           }|                    d          }t          j        |dk    dd          }|S )a  
    A separability test for the outputs of a transform.

    Parameters
    ----------
    transform : `~astropy.modeling.core.Model`
        A (compound) model.

    Returns
    -------
    is_separable : ndarray
        A boolean array with size ``transform.n_outputs`` where
        each element indicates whether the output is independent
        and the result of a separable transform.

    Examples
    --------
    >>> from astropy.modeling.models import Shift, Scale, Rotation2D, Polynomial2D
    >>> is_separable(Shift(1) & Shift(2) | Scale(1) & Scale(2))
        array([ True,  True]...)
    >>> is_separable(Shift(1) & Shift(2) | Rotation2D(2))
        array([False, False]...)
    >>> is_separable(Shift(1) & Shift(2) | Mapping([0, 1, 0, 1]) |         Polynomial2D(1) & Polynomial2D(2))
        array([False, False]...)
    >>> is_separable(Shift(1) & Shift(2) | Mapping([0, 1, 0, 1]))
        array([ True,  True,  True,  True]...)

    r   FT)n_inputs	n_outputsnparrayT
_separablesumwhere)	transformr   separable_matrixs      :lib/python3.11/site-packages/astropy/modeling/separable.pyr   r      s    < Q9#6#:#:x)*= =>>@!),,#''**L8LA-ud;;L    c                     | j         dk    r7| j        dk    r,t          j        | j        | j         ft          j                  S t          |           }t          j        |dk    dd          }|S )at  
    Compute the correlation between outputs and inputs.

    Parameters
    ----------
    transform : `~astropy.modeling.core.Model`
        A (compound) model.

    Returns
    -------
    separable_matrix : ndarray
        A boolean correlation matrix of shape (n_outputs, n_inputs).
        Indicates the dependence of outputs on inputs. For completely
        independent outputs, the diagonal elements are True and
        off-diagonal elements are False.

    Examples
    --------
    >>> from astropy.modeling.models import Shift, Scale, Rotation2D, Polynomial2D
    >>> separability_matrix(Shift(1) & Shift(2) | Scale(1) & Scale(2))
        array([[ True, False], [False,  True]]...)
    >>> separability_matrix(Shift(1) & Shift(2) | Rotation2D(2))
        array([[ True,  True], [ True,  True]]...)
    >>> separability_matrix(Shift(1) & Shift(2) | Mapping([0, 1, 0, 1]) |         Polynomial2D(1) & Polynomial2D(2))
        array([[ True,  True], [ True,  True]]...)
    >>> separability_matrix(Shift(1) & Shift(2) | Mapping([0, 1, 0, 1]))
        array([[ True, False], [False,  True], [ True, False], [False,  True]]...)

    r   )dtyper   TF)r   r   r   onesbool_r   r   )r   r   s     r   r	   r	   A   so    > Q9#6#:#:w	+Y-?@QQQQ!),,x 0A 5tUCCr   c                     t          | t                    r| j        }n| j        d         }t          |t                    r|j        }n|j        d         }||z   }|S )aB  
    Compute the number of outputs of two models.

    The two models are the left and right model to an operation in
    the expression tree of a compound model.

    Parameters
    ----------
    left, right : `astropy.modeling.Model` or ndarray
        If input is of an array, it is the output of `coord_matrix`.

    r   )
isinstancer   r   shape)leftrightlnoutrnoutnoutps        r   _compute_n_outputsr#   g   s^     $ 
1% AEMELr   c                     d } ||           \  }} ||          \  }}||k    s||k    rt          d| d| d| d| d	          t          j        ||f          }|S )a  
    Function corresponding to one of the arithmetic operators
    ['+', '-'. '*', '/', '**'].

    This always returns a nonseparable output.

    Parameters
    ----------
    left, right : `astropy.modeling.Model` or ndarray
        If input is of an array, it is the output of `coord_matrix`.

    Returns
    -------
    result : ndarray
        Result from this operation.
    c                 f    t          | t                    r| j        | j        }}n
| j        \  }}||fS )N)r   r   r   r   r   )inputr   r   s      r   _n_inputs_outputsz&_arith_oper.<locals>._n_inputs_outputs   s:    eU## 	."'/5>xII"'+Ix""r   z=Unsupported operands for arithmetic operator: left (n_inputs=z, n_outputs=z) and right (n_inputs=zO); models must have the same n_inputs and the same n_outputs for this operator.)r   r   r   )r   r   r'   left_inputsleft_outputsright_inputsright_outputsresults           r   _arith_operr-      s    $# # # !2 1$ 7 7K"3"3E":":L-l""lm&C&C"K%K K3?K K&K K4AK K K
 
 	
 WlK011FMr   c                    t          | t                    rg }| j        D ]6}t          j        | j        f          }d||<   |                    |           7t          j        |          }t          j        || j        f          }|dk    r||d| j        d| j        f<   n||| j         d| j         df<   |S | j	        sOt          j        || j        f          }|dk    rd|d| j        d| j        f<   ntd|| j         d| j         df<   n\t          j        || j        f          }t          | j                  D ]	}d|||f<   
|dk    rt          j        ||| j        z
            }|S )a  
    Create an array representing inputs and outputs of a simple model.

    The array has a shape (noutp, model.n_inputs).

    Parameters
    ----------
    model : `astropy.modeling.Model`
        model
    pos : str
        Position of this model in the expression tree.
        One of ['left', 'right'].
    noutp : int
        Number of outputs of the compound model of which the input model
        is a left or right child.

    r   r   Nr   )r   r   mappingr   zerosr   appendvstackr   	separablerangeroll)modelposr"   axesiaxismmats           r   _coord_matrixr=      s   $ %!!  	 	A8U^-..DDGKKIdOOhu~.//&==78C!%/!#3U^#33449:C ""U^O$5$556
? :hu~.//&==78C!%/!#3U^#33449:C ""U^O$5$5566hu~.//u~&& 	 	AC1II'>>'# 799CJr   c                    t          | |          }t          | t                    rt          | d|          }nBt	          j        || j        d         f          }| |d| j        d         d| j        d         f<   t          |t                    rt          |d|          }nDt	          j        ||j        d         f          }|||j        d          d|j        d          df<   t	          j        ||g          S )a  
    Function corresponding to '&' operation.

    Parameters
    ----------
    left, right : `astropy.modeling.Model` or ndarray
        If input is of an array, it is the output of `coord_matrix`.

    Returns
    -------
    result : ndarray
        Result from this operation.

    r   r   Nr   r   )r#   r   r   r=   r   r0   r   hstack)r   r   r"   cleftcrights        r   _cstackrB      s     tU++E$ 7dFE22%A/0026o
1oA./% =ugu555%+a.1227<A  5;q>/"3"3349eV_%%%r   c                     || }} d } || d          } ||d          }	 t          j        ||          }n## t          $ r t          d| d|           w xY w|S )a  
    Function corresponding to "|" operation.

    Parameters
    ----------
    left, right : `astropy.modeling.Model` or ndarray
        If input is of an array, it is the output of `coord_matrix`.

    Returns
    -------
    result : ndarray
        Result from this operation.
    c                 b    t          | t                    rt          | || j                  }n| }|S )zQ
        Return ``n_inputs``, ``n_outputs`` for a model or coord_matrix.
        )r   r   r=   r   )r&   positioncoordss      r   r'   z _cdot.<locals>._n_inputs_outputs  s5     eU## 	"5(EODDFFFr   r   r   zFModels cannot be combined with the "|" operator; left coord_matrix is z, right coord_matrix is )r   dot
ValueErrorr   )r   r   r'   r@   rA   r,   s         r   _cdotrI      s     %D   dF++Eug..F
v&& 
 
 
"L$*L LDIL L
 
 	


 Ms	   7  Ac                 J   |                                  x}t          ur|S t          | t                    rCt	          | j                  }t	          | j                  }t          | j                 ||          S t          | t                    rt          | d| j                  S dS )aq  
    Calculate the separability of outputs.

    Parameters
    ----------
    transform : `astropy.modeling.Model`
        A transform (usually a compound model).

    Returns :
    is_separable : ndarray of dtype np.bool
        An array of shape (transform.n_outputs,) of boolean type
        Each element represents the separablity of the corresponding output.
    r   N)_calculate_separability_matrixNotImplementedr   r   r   r   r   
_operatorsopr   r=   r   )r   transform_matrixsepleftseprights       r   r   r     s     &DDFFF   	I}	-	- EY^,,io..),':::	Iu	%	% EY	0CDDDE Er   )&|+-*/z**)__doc__numpyr   corer   r   r   mappingsr   __all__r   r	   r#   r-   r=   rB   rI   r   rM    r   r   <module>r^      s         < < < < < < < < < <      0
1$ $ $N# # #L  2& & &R- - -`& & &>$ $ $NE E E: 
					
 


r   