
    bi                          d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	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efdZ G d de      Zy)a/  
Copyright 2017 Robin Verschueren

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.
    )problems)settings)vec_to_upper_tri)PSDSOCEqualityOpRelEntrConeQuadZero)
Constraint)cvxtypes)	lin_utils)InverseDataSolution)CANON_METHODS)	Reductionreturnc                     | j                         | j                         z   | j                         z   }t        d |D              S )Nc              3   <   K   | ]  }|j                           y wN)
is_complex).0leafs     e/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/reductions/complex2real/complex2real.py	<genexpr>zaccepts.<locals>.<genexpr>%   s     4Tt 4s   )	variables
parameters	constantsany)problemleavess     r   acceptsr!   #   s=     7#5#5#77':K:K:MMF4V444    c                   8    e Zd ZdZeefZddZd Zd Z	d Z
d Zy)	Complex2Realz/Lifts complex numbers to a real representation.Nc                     t        |       y r   )r!   )selfr   s     r   r!   zComplex2Real.accepts-   s
    r"   c                    t        |      }|j                         D ci c]1  }|j                         r|j                  t	        j
                         3 }}|j                  D ci c]1  }|j                         r|j                  t	        j
                         3 }}|j                  |       ||_        i }| j                  |j                  |j                  |      \  }}	|	J g }
|j                  D ]  }| j                  ||j                  |      \  }}t        |t              r|
j                  |       n!t        |t              r|
j                  |       t        |t              r|
j                  |       t        |t              s|
j                  |        t         j"                  j%                  ||
      }||fS c c}w c c}w r   )r   r   r   idluget_idconstraintsupdate	real2imagcanonicalize_tree	objective
isinstancelistextendr   appendr   r   Problem)r&   r   inverse_datavarr-   consconstr_dictleaf_mapreal_objimag_objconstrs
constraintreal_constrsimag_constrsnew_problems                  r   applyzComplex2Real.apply0   s   "7+4;4E4E4G *S( VVRYY[( *	 *8?8K8K -//+ ww		+ - -%!*!33|55xA(!-- 	-J *.)?)?L22H*>&L,,-|,L*5|,,-|,L*5|,	- &&..x/68L((;*-s   6G$6Gc                 \   i }i }|j                   t        j                  v r|j                  j	                         D ]Q  \  }}|j                         r|j                  |   ||<   *|j                         r%|j                  |   }d|j                  |   z  ||<   _|j                         r|j                         rs|j                  |   ||<   |j                  |   }||j                  v s|j                  |   }t        |d      j                  }||j                  z  }||   d|z  z   ||<   |j                         s|j                  |   ||<   |j                  |   }||j                  v s5|j                  |   }||   d|z  z   ||<   T |j                  rc|j                  j	                         D ]E  \  }	}
|
j                         r|j                  |	   ||	<   *|
j                         r%|j                  |	   }d|j                  |   z  ||	<   _t!        |
t"        t$        f      rV|j                  |	   }||j                  v r&|j                  |	   d|j                  |   z  z   ||	<   |j                  |	   ||	<   t!        |
t&              rJ|
j(                  d   j*                  d   }|j                  |	   }|d |d |f   d||d d |f   z  z   ||	<   %t!        |
| j,                        r=t/        d       t1        |j                   |j2                  |||j4                        S )Ny              ?Tr   zUnknown constraint type.)statussSOLUTION_PRESENTid2varitemsis_realprimal_varsis_imagr-   r   is_hermitianr   valueT	dual_varsid2consr0   r   r
   r   argsshapeUNIMPLEMENTED_COMPLEX_DUALS	Exceptionr   opt_valattr)r&   solutionr5   pvarsdvarsvidr6   imag_idimag_valcidr7   nduals                r   invertzComplex2Real.invertQ   s   ??a000 )//557 >S;;=!)!5!5c!:E#J[[]*44S9G!#H$8$8$A!AE#J^^%#*:*:*<!)!5!5c!:E#J*44S9G("6"66#+#7#7#@#3Hd#C#I#I HJJ.%*3Z"X+%=c
^^%!)!5!5c!:E#J*44S9G("6"66#+#7#7#@%*3Z"X+%=c
/>0 !! ".!5!5!;!;!= &DIC||~%-%7%7%<c
"."8"8"=%'(:(:7(C%Cc
 $D8T*:;"."8"8"="h&8&88)1););C)@ "8#5#5g#> >*?E#J *2););C)@E#J#D#. !IIaL..q1'11#6%)"1"bqb&\BtABF|O%Cc
#D$*J*JK'(BCCM&DP )9)95% ' 	'r"   c                 (   t        |      t        j                         k(  r
t               g }g }|j                  D ]:  }| j                  |||      \  }}|j                  |       |j                  |       < | j                  |||||      \  }	}
|	|
fS r   )typer   partial_problemNotImplementedErrorrP   r.   r3   canonicalize_expr)r&   exprr-   r9   	real_args	imag_argsargreal_argimag_argreal_outimag_outs              r   r.   zComplex2Real.canonicalize_tree   s    :1133%''IIyy +%)%;%;CH%U"(  *  *+ "&!7!7i8A98@"BHh !!r"   c                 *   t        |      t        v rZt        |j                        dk(  r	||v r||   S t        t        |         ||||      }t        |j                        dk(  r|||<   |S t	        d |D              sJ |j                  |      }|d fS )Nr   c              3   $   K   | ]  }|d u  
 y wr    )r   vs     r   r   z1Complex2Real.canonicalize_expr.<locals>.<genexpr>   s     4QqDy4s   )ra   elim_cplx_methodslenrP   allcopy)r&   re   rf   rg   r-   r9   resultrk   s           r   rd   zComplex2Real.canonicalize_expr   s    :**499~"tx'7~%&tDz24IyYF499~"!'M4)4444yy+HT>!r"   )r   N)__name__
__module____qualname____doc__r   r	   rR   r!   rA   r_   r.   rd   ro   r"   r   r$   r$   (   s-    9#&(9":)BL'\" "r"   r$   N)ry   cvxpyr   r   rD   cvxpy.atoms.affine.upper_trir   cvxpy.constraintsr   r   r   r	   r
   cvxpy.constraints.constraintr   cvxpy.expressionsr   cvxpy.lin_opsr   r)   cvxpy.reductionsr   r   ,cvxpy.reductions.complex2real.canonicalizersr   rq   cvxpy.reductions.reductionr   boolr!   r$   ro   r"   r   <module>r      sM       9  4 & ) 2 [ 05 5
S"9 S"r"   