
    biD              
           d Z ddlmZmZ ddlZddlmZ ddl	m
Z
 ddededee   dee   fd	Zddeed
f   dee   fdZy)a1  
Copyright 2022, the CVXPY authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    )OptionalTupleN)Atomijdimsaxisc                 >   t        j                  dgdgdgff      }t        |      D ]l  \  }}||k(  r7t        j                  dg|g|gff||f      }t        j                  ||      }Bt        j                  |      }	t        j                  ||	      }n || z  |z  S )a  Helper function for partial transpose.

    Parameters
    ----------
    expr : :class:`~cvxpy.expressions.expression.Expression`
        The 2D expression to take the partial transpose of.
    i : int
        Term in the partial transpose sum.
    j : int
        Term in the partial transpose sum.
    dims : tuple of ints.
        A tuple of integers encoding the dimensions of each subsystem.
    axis : int
        The index of the subsystem to be transposed
        from the tensor product that defines expr.
    g      ?r      )shape)sp
coo_matrix	enumeratekron	eye_array)
exprr   r   r   r	   ai_axisdimveye_mats
             _/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/affine/partial_transpose.py_termr      s    * 	usQCj)*A"4 $T>saS1#J/SzBA1All3'G7#A$ t8a<    .c                    t        j                  |       } | j                  dk  s| j                  d   | j                  d   k7  rt	        d      |dk  s|t        |      k\  rt	        dt        |       d| d      | j                  d   t        j                  |      k7  rt	        d      t        t        ||         D cg c]$  }t        ||         D ]  }t        | ||||       & c}}      S c c}}w )	a&  
    Assumes :math:`\texttt{expr} = X_1 \otimes ... \otimes X_n` is a 2D Kronecker
    product composed of :math:`n = \texttt{len(dims)}` implicit subsystems.
    Letting :math:`k = \texttt{axis}`, the returned expression is a
    *partial transpose* of :math:`\texttt{expr}`, with the transpose applied to its
    :math:`k^{\text{th}}` implicit subsystem:

    .. math::
        X_1 \otimes ... \otimes X_k^T \otimes ... \otimes X_n.

    Parameters
    ----------
    expr : :class:`~cvxpy.expressions.expression.Expression`
        The 2D expression to take the partial transpose of.
    dims : tuple of ints.
        A tuple of integers encoding the dimensions of each subsystem.
    axis : int
        The index of the subsystem to be transposed
        from the tensor product that defines expr.
       r   r   z2partial_transpose only supports 2-d square arrays.z/Invalid axis argument, should be between 0 and z, got .zBDimension of system doesn't correspond to dimension of subsystems.)r   cast_to_constndimr   
ValueErrorlennpprodsumranger   )r   r   r	   r   r   s        r   partial_transposer&   ;   s    * d#Dyy1}

1A6MNNax43t9$=c$i[tfTUV
 	
 zz!}%]^^/4T$Z/@*+%PTUYPZJ[EFdAq$%%   s   )C4
)r   )__doc__typingr   r   numpyr"   scipy.sparsesparser   cvxpy.atoms.atomr   intr   r&    r   r   <module>r/      sa   $ #   !3 3 eCj  @ %S/  #  r   