
    bi4                        d Z ddlmZ ddlZddlmZmZmZ ddlZ	ddl
mZ ddlmZ ddlmZ  ed      Z G d	 d
e      Z G d de      Z G d de      Zy)aF  
Copyright 2013 Steven Diamond, 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.
    )annotationsN)ListTupleTypeVar)Cone)cvxtypes)scopes
Expressionc                       e Zd ZdZdd fdZddZddZed        Zedd       Z	d Z
ddZdd	Zddd
ZdddZddZedd       ZddZd Z xZS )ExpConeaX  A reformulated exponential cone constraint.

    Operates elementwise on :math:`x, y, z`.

    Original cone:

    .. math::

        K = \{(x,y,z) \mid y > 0, ye^{x/y} <= z\}
            \cup \{(x,y,z) \mid x \leq 0, y = 0, z \geq 0\}

    Reformulated cone:

    .. math::

        K = \{(x,y,z) \mid y, z > 0, y\log(y) + x \leq y\log(z)\}
             \cup \{(x,y,z) \mid x \leq 0, y = 0, z \geq 0\}

    Parameters
    ----------
    x : Expression
        x in the exponential cone.
    y : Expression
        y in the exponential cone.
    z : Expression
        z in the exponential cone.
    c                T   t        j                         }|j                  |      | _        |j                  |      | _        |j                  |      | _        | j                  | j                  | j
                  g}|D ]-  }|j                         r|j                         r$t        d       | j                  j                  | j                  j                  | j
                  j                  }
}	}||	k7  s||
k7  rdt        ||	|
f      z  }t        |      t        t        | 7  ||       y )Nz&All arguments must be affine and real.IAll arguments must have the same shapes. Provided arguments haveshapes %s)r   
expressioncastxyz	is_affineis_real
ValueErrorshapestrsuperr   __init__)selfr   r   r   	constr_idr
   argsvalxsyszsmsg	__class__s               X/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/constraints/exponential.pyr   zExpCone.__init__;   s    ((*
###' 	KCMMO !IJJ	K VV\\466<<B8rRx!$b"b\!23CS/!gt%dI6    c                V    d| j                   d| j                  d| j                  dS NzExpCone(z, )r   r   r   r   s    r$   __str__zExpCone.__str__K       (,??r%   c                V    d| j                   d| j                  d| j                  dS r'   r)   r*   s    r$   __repr__zExpCone.__repr__N   r,   r%   c           
     X   ddl m}m}m}m}m} | j                  j                  ,| j                  j                  | j                  j                  y  || j                  j                        } || j                  j                        } || j                  j                        }t        |||      g}	 | | ||||g       || j                  j                  | j                  j                  | j                  j                  g      z
              }
 ||
|	      }|j                         S Nr   )MinimizeProblemVariablehstacknorm2)cvxpyr1   r2   r3   r4   r5   r   valuer   r   r   r   solver   r1   r2   r3   r4   r5   r   r   r   constrobjproblems               r$   residualzExpCone.residualQ   s     	ED66<<466<<#7466<<;OTVV\\"TVV\\"TVV\\"!Q"#uVQ1I.#TVV\\466<<$NOP Q R#v&}}r%   c                (    d| j                         z  S z5The number of entries in the combined cones.
           	num_conesr*   s    r$   sizezExpCone.size`        4>>###r%   c                .    | j                   j                  S z)The number of elementwise cones.
        r   rC   r*   s    r$   rB   zExpCone.num_conesf        vv{{r%   c                ^    t        | j                  | j                  | j                   ||      S N)RelEntrConeQuadr   r   r   )r   mks      r$   as_quad_approxzExpCone.as_quad_approxk   s#    tvvtvvw1==r%   c                *    dg| j                         z  S zThe dimensions of the exponential cones.

        Returns
        -------
        list
            A list of the sizes of the elementwise cones.
        r@   rA   r*   s    r$   
cone_sizeszExpCone.cone_sizesn        s4>>###r%   c                    |r:t        j                         5  t        d | j                  D              cddd       S t        d | j                  D              S # 1 sw Y   %xY w)EAn exponential constraint is DCP if each argument is affine.
        c              3  <   K   | ]  }|j                           y wrJ   r   .0args     r$   	<genexpr>z!ExpCone.is_dcp.<locals>.<genexpr>}        @s3==?@   Nc              3  <   K   | ]  }|j                           y wrJ   rV   rW   s     r$   rZ   z!ExpCone.is_dcp.<locals>.<genexpr>~        8s3==?8r\   r	   	dpp_scopeallr   r   dpps     r$   is_dcpzExpCone.is_dcpx   Y     !!# A@dii@@A A8dii888A A   AA"c                     yNF rb   s     r$   is_dgpzExpCone.is_dgp       r%   c                "    | j                         S rJ   rd   r*   s    r$   is_dqcpzExpCone.is_dqcp       {{}r%   c                8    d| j                   j                  z   }|S N)r@   r   r   r   ss     r$   r   zExpCone.shape       466<<r%   c                
   t        j                  |d      }t        j                  |d d df   | j                  j                        }t        j                  |d d df   | j                  j                        }t        j                  |d d df   | j
                  j                        }| j                  d   j                  |       | j                  d   j                  |       | j                  d   j                  |       y )N)r@   r         )npreshaper   r   r   r   dual_variables
save_value)r   r7   dv0dv1dv2s        r$   save_dual_valuezExpCone.save_dual_value   s    

5'*jjq!tdffll3jjq!tdffll3jjq!tdffll3A))#.A))#.A))#.r%   c                   |dk(  rLt        | j                  d    | j                  d    t        j                  d      | j                  d   z        S |D cg c]  }|j                   }}| j
                  D cg c]  }|j                   }}t        |      t        | j
                        k(  sJ ||k(  sJ t        |d    |d    t        j                  d      |d   z        S c c}w c c}w )zwImplements the dual cone of the exponential cone
        See Pg 85 of the MOSEK modelling cookbook for more informationri   rx   r   ry   )r   r|   rz   expr   r   len)r   r   rY   args_shapesinstance_args_shapess        r$   
_dual_conezExpCone._dual_cone   s     2:D//22T5H5H5K4K66!9t':':1'==? ? 1553995K59=#C#CII#C #Ct9DII..."6666DG8d1gX66!9T!W,. .	 6#Cs   C&9C+rJ   )r   r
   r   r
   r   r
   returnNoner   r   r   int)rL   r   rM   r   r   rK   r   z	List[int]Frc   boolr   r   r   r   r   zTuple[int, ...]r   r   )__name__
__module____qualname____doc__r   r+   r.   propertyr=   rC   rB   rN   rQ   rd   rj   rn   r   r   r   __classcell__r#   s   @r$   r   r      s}    87 @@   $ $

>$9  /.r%   r   c                       e Zd ZdZ	 d	 	 	 	 	 d fdZd ZddZddZed        Z	edd       Z
d Zdd	Zddd
ZdddZddZedd       ZddZ xZS )rK   a  An approximation of the scalar relative entropy cone,

    .. math::

        K_{re}=\text{cl}\{(x,y,z)\in\mathbb{R}_{++}\times
                \mathbb{R}_{++}\times\mathbb{R}_{++}\:x\log(x/y)\leq z\},

    in terms of second order cones. The approximation uses a numerical quadrature scheme
    described in https://arxiv.org/abs/1705.00812.

    Parameters
    ----------
    x : Expression
        x in the (approximate) scalar relative entropy cone
    y : Expression
        y in the (approximate) scalar relative entropy cone
    z : Expression
        z in the (approximate) scalar relative entropy cone
    m : int
        Number of quadrature points in the approximation.
    k: int
        Number of scaling points in the approximation.
    c                   t        j                         }|j                  |      | _        |j                  |      | _        |j                  |      | _        | j                  | j                  | j
                  g}|D ]-  }	|	j                         r|	j                         r$t        d       || _	        || _
        | j                  j                  | j                  j                  | j
                  j                  }}}
|
|k7  s|
|k7  rdt        |
||f      z  }t        |      t        t        | ?  | j                  | j                  | j
                  g|       y )Nz1All Expression arguments must be affine and real.r   )r   r   r   r   r   r   r   r   r   rL   rM   r   r   r   rK   r   )r   r   r   r   rL   rM   r   r
   r   r   r   r    r!   r"   r#   s                 r$   r   zRelEntrConeQuad.__init__   s   ((*
###' 	VCMMO !TUU	V VV\\466<<B8rRx!$b"b\!23CS/!ot-tvvtvvtvv.F	Rr%   c                H    | j                   | j                  | j                  gS rJ   rL   rM   idr*   s    r$   get_datazRelEntrConeQuad.get_data       ((r%   c                ~    | j                   | j                  | j                  | j                  | j                  f}d|z  S )Nz#RelEntrConeQuad(%s, %s, %s, %s, %s))r   r   r   rL   rM   r   tups     r$   r+   zRelEntrConeQuad.__str__   s1    vvtvvtvvtvvtvv64s::r%   c                "    | j                         S rJ   r+   r*   s    r$   r.   zRelEntrConeQuad.__repr__       ||~r%   c           
        ddl m}m}m}m}m} | j                  j                  ,| j                  j                  | j                  j                  y t        j                           || j                  j                        } || j                  j                        } || j                  j                        }t        |||| j                  | j                        g}	 | | ||||g       || j                  j                  | j                  j                  | j                  j                  g      z
              }
 ||
|	      }|j!                         S r0   )r6   r1   r2   r3   r4   r5   r   r7   r   r   r   r   r   rK   rL   rM   r8   r9   s               r$   r=   zRelEntrConeQuad.residual   s     	ED66<<466<<#7466<<;OTVV\\"TVV\\"TVV\\"!!Q466466:;uVQ1I.#TVV\\466<<$NOP Q R#v&}}r%   c                (    d| j                         z  S r?   rA   r*   s    r$   rC   zRelEntrConeQuad.size   rD   r%   c                .    | j                   j                  S rF   rG   r*   s    r$   rB   zRelEntrConeQuad.num_cones   rH   r%   c                *    dg| j                         z  S rP   rA   r*   s    r$   rQ   zRelEntrConeQuad.cone_sizes   rR   r%   c                    |r:t        j                         5  t        d | j                  D              cddd       S t        d | j                  D              S # 1 sw Y   %xY w)rT   c              3  <   K   | ]  }|j                           y wrJ   rV   rW   s     r$   rZ   z)RelEntrConeQuad.is_dcp.<locals>.<genexpr>  r[   r\   Nc              3  <   K   | ]  }|j                           y wrJ   rV   rW   s     r$   rZ   z)RelEntrConeQuad.is_dcp.<locals>.<genexpr>  r^   r\   r_   rb   s     r$   rd   zRelEntrConeQuad.is_dcp   re   rf   c                     yrh   ri   rb   s     r$   rj   zRelEntrConeQuad.is_dgp  rk   r%   c                "    | j                         S rJ   rm   r*   s    r$   rn   zRelEntrConeQuad.is_dqcp  ro   r%   c                8    d| j                   j                  z   }|S rq   rr   rs   s     r$   r   zRelEntrConeQuad.shape  ru   r%   c                     y rJ   ri   r   r7   s     r$   r   zRelEntrConeQuad.save_dual_value      r%   rJ   )r   r
   r   r
   r   r
   rL   r   rM   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r+   r.   r   r=   rC   rB   rQ   rd   rj   rn   r   r   r   r   s   @r$   rK   rK      s    2 ,0SSS48S&);   $ $

$9  r%   rK   c                       e Zd ZdZ	 d	 	 	 	 	 d fdZd ZddZddZed        Z	edd       Z
d Zdd	Zddd
ZdddZddZedd       ZddZ xZS )OpRelEntrConeQuada  An approximate construction of the operator relative entropy cone,

    .. math::

        K_{re}^n = \text{cl}\{(X,Y,T)\in\mathbb{H}^n_{++}\times
                \mathbb{H}^n_{++}\times\mathbb{H}^n_{++}\:D_{\text{op}}(X,Y) \succeq T\}.

    Details on the approximation can be found in Theorem-3 on page-10 of
    https://arxiv.org/abs/1705.00812.

    Parameters
    ----------
    X : Expression
        x in the (approximate) operator relative entropy cone
    Y : Expression
        y in the (approximate) operator relative entropy cone
    Z : Expression
        Z in the (approximate) operator relative entropy cone
    m: int
        Must be positive. Controls the number of quadrature nodes used in a local
        approximation of the matrix logarithm. Increasing this value results in
        better local approximations, but does not significantly expand the region
        of inputs for which the approximation is effective.
    k: int
        Must be positive. Sets the number of scaling points about which the
        quadrature approximation is performed. Increasing this value will
        expand the region of inputs over which the approximation is effective.

    This approximation uses :math:`m + k` semidefinite constraints.
    c                   t        j                         }|j                  |      | _        |j                  |      | _        |j                  |      | _        |j                         r |j                         r|j                         sd}t        j                  |       || _	        || _
        | j                  j                  | j                  j                  | j
                  j                  }}
}	|	|
k7  s|	|k7  rdt        |	|
|f      z  }t        |      t        t        | C  | j                  | j                  | j
                  g|       y )Na  One of the input matrices has not explicitly been declared as symmetric orHermitian. If the inputs are Variable objects, try declaring them with thesymmetric=True or Hermitian=True properties. If the inputs are general Expression objects that are known to be symmetric or Hermitian, then youcan wrap them with the symmetric_wrap and hermitian_wrap atoms. Failure todo one of these things will cause this function to impose a symmetry orconjugate-symmetry constraint internally, in a way that is veryinefficient.r   )r   r   r   XYZis_hermitianwarningswarnrL   rM   r   r   r   r   r   r   )r   r   r   r   rL   rM   r   r
   r"   XsYsZsr#   s               r$   r   zOpRelEntrConeQuad.__init__8  s    ((*
### !..*:ANNDT"C MM#VV\\466<<B8rRx!$b"b\!23CS/!/0H)Tr%   c                H    | j                   | j                  | j                  gS rJ   r   r*   s    r$   r   zOpRelEntrConeQuad.get_dataQ  r   r%   c                ~    | j                   | j                  | j                  | j                  | j                  f}d|z  S )Nz%OpRelEntrConeQuad(%s, %s, %s, %s, %s))r   r   r   rL   rM   r   s     r$   r+   zOpRelEntrConeQuad.__str__T  s1    vvtvvtvvtvvtvv66<<r%   c                "    | j                         S rJ   r   r*   s    r$   r.   zOpRelEntrConeQuad.__repr__X  r   r%   c                    t               rJ   )NotImplementedErrorr*   s    r$   r=   zOpRelEntrConeQuad.residual[  s     "##r%   c                (    d| j                         z  S r?   rA   r*   s    r$   rC   zOpRelEntrConeQuad.size`  rD   r%   c                .    | j                   j                  S rF   )r   rC   r*   s    r$   rB   zOpRelEntrConeQuad.num_conesf  rH   r%   c                *    dg| j                         z  S rP   rA   r*   s    r$   rQ   zOpRelEntrConeQuad.cone_sizesk  rR   r%   c                    |r:t        j                         5  t        d | j                  D              cddd       S t        d | j                  D              S # 1 sw Y   %xY w)zNAn operator relative conic constraint is DCP when (A, b, C) is affine
        c              3  <   K   | ]  }|j                           y wrJ   rV   rW   s     r$   rZ   z+OpRelEntrConeQuad.is_dcp.<locals>.<genexpr>z  r[   r\   Nc              3  <   K   | ]  }|j                           y wrJ   rV   rW   s     r$   rZ   z+OpRelEntrConeQuad.is_dcp.<locals>.<genexpr>{  r^   r\   r_   rb   s     r$   rd   zOpRelEntrConeQuad.is_dcpu  re   rf   c                     yrh   ri   rb   s     r$   rj   zOpRelEntrConeQuad.is_dgp}  rk   r%   c                "    | j                         S rJ   rm   r*   s    r$   rn   zOpRelEntrConeQuad.is_dqcp  ro   r%   c                8    d| j                   j                  z   }|S rq   )r   r   rs   s     r$   r   zOpRelEntrConeQuad.shape  ru   r%   c                     y rJ   ri   r   s     r$   r   z!OpRelEntrConeQuad.save_dual_value  r   r%   rJ   )r   r
   r   r
   r   r
   rL   r   rM   r   r   r   r   r   r   r   r   r   r   r   r   r   s   @r$   r   r     s    @ ,0UUU48U2)= $ $ $ $

$9  r%   r   )r   
__future__r   r   typingr   r   r   numpyrz   cvxpy.constraints.conesr   cvxpy.expressionsr   cvxpy.utilitiesr	   r
   r   rK   r   ri   r%   r$   <module>r      sZ    #  ' '  ( & "\"
D.d D.Npd pfr rr%   