
    bi                     t   d Z ddlmZ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mZ ddlmZ ddlmZ dd	lmZ dd
lmZ eej0                  heej2                  hiZd Zdej8                  dej8                  dej8                  fdZdedeeee   f   fdZdedeeee   f   fdZd Z d Z! G d de      Z"y)a2  
Copyright 2022 the CVXPY developers

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.
    )ListTupleN)	upper_tri)
Constraint)ExpConeOpRelEntrConeQuadRelEntrConeQuad)Zero)Variable)Canonicalization)von_neumann_entr_canonc           
      $   dt        j                  t        j                  | dz
        dt        j                  d| t              z  dz  z
        z  }t        j
                  |d      t        j
                  |d      z   }t         j                  j                  |      \  }}|}t        j                  |      t        j                  |      }}dt        j                  |D cg c]
  }|d   |    c}      dz  z  }|dz   dz  }|dz  }||fS c c}w )zt
    Helper function for returning the weights and nodes for an
    n-point Gauss-Legendre quadrature on [0, 1]
          ?      )dtyper   )npsqrtonesarangefloatdiaglinalgeighsortargsortarray)	nbetaTDVxikws	            d/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/reductions/cone2cone/approximations.pygauss_legendrer*   *   s    
 rwwrwwqs|QryyAU'C%Cr$JJKKD
a2774,,A99>>!DAq	A771:rzz!}qA	RXX*1qtAw*+a//A	
Q	A	!Aa4K +s   !D
Xyzc                 t   |j                   }|j                   |k(  sJ | j                  d   |k(  sJ t        | j                        dk  rt        j                  | |dfd      } t        j                  ||z
  |dfd      }t        j
                  |d| z  f      }||z   }t        j                  ||d      }|S )a  
    For each i, enforce a constraint that
        (X[i, :], y[i], z[i])
    belongs to the rotated quadratic cone
        { (x, y, z) : || x ||^2 <= y z, 0 <= (y, z) }
    This implementation doesn't enforce (x, y) >= 0! That should be imposed by the calling function.
    r   r   r   F)order)tr+   axis)sizeshapelencpreshapehstackSOC)r+   r,   r-   m
soc_X_col0soc_Xsoc_tcons           r)   rotated_quad_coner?   :   s     	
A66Q;;771:??
177|aJJq1a&, AEAq65JIIz1Q3'(EEE
&&5E
*CJ    r>   returnc                    | j                   | j                  }}| j                  | j                  }}|j                  }t        |dz   |f      }t        |      \  }}	t        ||f      }
t        ||
z  | j                  d|z  z  z         }t        |d   |z
        g}t        |      D ]N  }||ddf   }||dz   ddf   }t        |||      }|j                  |       |j                  |dk\  |dk\  g       P t        |      D ]  }|	|   dz   |
|ddf   z  }||ddf   |z
  |
|ddf   z
  }t        ||||	|   |
|ddf   z  z
        }|j                  |       |j                  |dk\  ||	|   |
|ddf   z  z
  dk\  g        ||fS )a`  
    Use linear and SOC constraints to approximately enforce
        con.x * log(con.x / con.y) <= con.z.

    We rely on an SOC characterization of 2-by-2 PSD matrices.
    Namely, a matrix
        [ a, b ]
        [ b, c ]
    is PSD if and only if (a, c) >= 0 and a*c >= b**2.
    That system of constraints can be expressed as
        a >= quad_over_lin(b, c).

    Note: constraint canonicalization in CVXPY uses a return format
    (lead_con, con_list) where lead_con is a Constraint that might be
    used in dual variable recovery and con_list consists of extra
    Constraint objects as needed.
    r   )r4   r   r   Nr   )r'   r:   r%   r,   r3   r   r*   r
   r-   ranger?   appendextend)r>   argsr'   r:   r%   r,   r    Zr(   r1   r"   lead_conconstrsr&   epistackedZconsoff_diags                     r)   RelEntrConeQuad_canonrN   U   s   $ 55#%%qA55#%%qA	A!Qx A!DAq1vAAECEE!Q$J&'HAaD1H~G1X + 1gQqS!V9 32tq!q&)*+ 1X 8qT3Y<!AqD') Aw{Qq!tW$ 3!A$qAw,?tq!AaD1a4L.A"5678 Wr@   c                 
   | j                   | j                  }}| j                  | j                  }}|j	                         sJ |j	                         sJ | j
                  j	                         sJ t        |dz         D ci c]  }|t        |j                  d       }}t        |dz         D ci c]  }|t        |j                  d       }}t        |d   |z
        g}	|j                         s4t        |      }
t        |j                        }|	j                  |
|k(         |j                         s4t        |      }
t        |j                        }|	j                  |
|k(         | j
                  j                         sHt        | j
                        }
t        | j
                  j                        }|	j                  |
|k(         t        |      \  }}t        t        j                   t        |      D cg c]  }||   ||   z   c}      | j
                  d|z  z  z         }t        |      D ]H  }|	j                  t        j"                  ||   ||dz      g||dz      j                  |gg      dz	         J t        |      D ]`  }||   dz   ||   z  }|	j                  t        j"                  ||   |z
  ||   z
  |g|j                  |||   ||   z  z
  gg      dz	         b ||	fS c c}w c c}w c c}w )Nr   T)r4   	symmetricr   r   r   )r'   r:   r+   Yis_realrG   rC   r   r4   r
   is_symmetricr   r"   rD   r*   r6   sumbmat)r>   rF   r'   r:   r+   rQ   r&   ZsTsrI   utltr(   r1   rH   rM   s                   r)   OpRelEntrConeQuad_canonrZ      s   55#%%qA55#%%qA99;;99;;55==??>CAaCj	I!XAGGt4
4	IB	I>CAaCj	I!XAGGt4
4	IB	IBqEAIG>>q\qss^rRx >>q\qss^rRx 55suusuuwwrRx !DAqBFF%(;QAaD2a5L;<suuQTzIJH1X I 	rwwA1Q3 02ac799a.ABaGHI
 1X bqT3Y<"Q%' 	rwwARU!2H =

AaPQdSUVWSXjL?YZ[_``ab W= 
J	I <s   K62K;>L c                 @   | j                   d   | j                   d   }}t        | |      \  }}g }|D ]g  }t        |t              rD|j	                  ||      }t        |d       \  }	}
|j                  |	       |j                  |
       W|j                  |       i ||fS )Nr   r   )quad_approxr   
isinstancer   as_quad_approxrN   rD   rE   )exprrF   r:   r'   rJ   initial_consrL   r>   qa_conqa_con_canon_leadqa_con_canons              r)   von_neumann_entr_QuadApproxrd      s    A 0 0 3qA.tT:CD c7#''1-F.C/+|KK)*KK%KK 9r@   c                 J    | j                   rt        | |      S t        | |      S N)r\   rd   r   )r_   rF   s     r)   von_neumann_entr_canon_dispatchrg      s%    *466%dD11r@   c                   .     e Zd ZeeeeiZdd fdZ xZ	S )
QuadApproxc                 L    t         t        |   |t        j                         y )N)problemcanon_methods)superri   __init__CANON_METHODS)selfrk   	__class__s     r)   rn   zQuadApprox.__init__   s#    j$(:+C+C 	) 	Er@   rf   )rA   N)
__name__
__module____qualname__r	   rN   r   rZ   ro   rn   __classcell__)rq   s   @r)   ri   ri      s"    .2M
E Er@   ri   )#__doc__typingr   r   numpyr   cvxpyr6   cvxpy.atoms.affine.upper_trir   cvxpy.constraints.constraintr   cvxpy.constraints.exponentialr   r   r	   cvxpy.constraints.zeror
   cvxpy.expressions.variabler   !cvxpy.reductions.canonicalizationr   ?cvxpy.reductions.dcp2cone.canonicalizers.von_neumann_entr_canonr   r9   PSDAPPROX_CONESr*   
Expressionr?   rN   rZ   rd   rg   ri    r@   r)   <module>r      s        2 3 
 ( / >
 bffXx  2== R]] 64 4z4PZK[?[9\ 4n$!2 $U:tT^O_C_=` $N 2E! Er@   