
    bi-                     p    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l
mZ  G d de      Z G d	 d
e      Zy)a2  
Copyright 2021 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)Cone)cvxtypes)scopesc                        e Zd ZdZdd fdZdefdZed        Zd Z	de
fdZde
fdZedefd	       Zd
 Zdee   fdZdde
de
fdZdde
de
fdZde
fdZedeedf   fd       ZddZd Z xZS )	PowCone3DaK  
    An object representing a collection of 3D power cone constraints

        x[i]**alpha[i] * y[i]**(1-alpha[i]) >= |z[i]|  for all i
        x >= 0, y >= 0

    If the parameter alpha is a scalar, it will be promoted to
    a vector matching the (common) sizes of x, y, z. The numeric
    value of alpha (or its components, in the vector case) must
    be a number in the open interval (0, 1).

    We store flattened representations of the arguments (x, y, z,
    and alpha) as Expression objects. We construct dual variables
    with respect to these flattened representations.
    returnc                 P  	 t        j                         }|j                  |      | _        |j                  |      | _        |j                  |      | _        | j                  | j                  | j
                  fD ]-  }|j                         r|j                         r$t        d       |j                  |      }|j                         ra| j                  j                  r0 t        j                         || j                  j                        }n t        j                         |d      }|| _        t        j                  | j                  j                  dk        s,t        j                  | j                  j                  dk\        rd}t        |      |j                  dk(  rC| j                  j                  | j                  j                  | j
                  j                  dg	nV| j                  j                  | j                  j                  | j
                  j                  | j                  j                  g	t        	fd	dd  D              rdt!        	      z  }t        |      t"        t$        | O  | j                  | j                  | j
                  g|       y )	Nz&All arguments must be affine and real.)   r   r   z=Argument alpha must have entries in the open interval (0, 1). c              3   .   K   | ]  }d    |k7    ywr   Nr   ).0s
arg_shapess     R/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/constraints/power.py	<genexpr>z%PowCone3D.__init__.<locals>.<genexpr>D   s     :az!}!:s   zIAll arguments must have the same shapes. Provided arguments haveshapes %s)r   
expressioncast_to_constxyz	is_affineis_real
ValueError	is_scalarshapepromotealphanpanyvaluestrsuperr	   __init__)selfr   r   r   r    	constr_id
Expressionvalmsgr   	__class__s            @r   r&   zPowCone3D.__init__+   s   ((*
))!,))!,))!,FFDFFDFF+ 	KCMMO !IJJ	K ((/??vv||*((*5$&&,,?
 +((*5$7
66$**""a'(BFF4::3C3Cq3H,IQCS/!;;$&&,,dffllBGJ&&,,dffllDJJDTDTUJ::ab>::!$Z1CS/!i'(@(1	3    c           	      p    d| j                   d| j                  d| j                  d| j                  d	S )NzPow3D(, ; ))r   r   r   r    r'   s    r   __str__zPowCone3D.__str__J   s!    *.&&$&&$&&$**MMr-   c           
      t   ddl m}m}m}m}m} | j                  j                  ,| j                  j                  | j                  j                  y  || j                  j                        } || j                  j                        } || j                  j                        }t        |||| j                        g}	 | | ||||g       || j                  j                  | j                  j                  | j                  j                  g      z
              }
 ||
|	      }|j                  dd      S )Nr   MinimizeProblemVariablehstacknorm2SCS:0yE>solvereps)cvxpyr6   r7   r8   r9   r:   r   r#   r   r   r   r	   r    solve)r'   r6   r7   r8   r9   r:   r   r   r   constrobjproblems               r   residualzPowCone3D.residualM   s     	ED66<<466<<#7466<<;OTVV\\"TVV\\"TVV\\"Aq!TZZ01uVQ1I.#TVV\\466<<$NOP Q R#v&}}Et}44r-   c                 2    | j                   | j                  gS N)r    idr2   s    r   get_datazPowCone3D.get_data\   s    

DGG$$r-   c                      yNFr   r2   s    r   is_imagzPowCone3D.is_imag_       r-   c                      yrK   r   r2   s    r   
is_complexzPowCone3D.is_complexb   rM   r-   c                 (    d| j                         z  S N   	num_conesr2   s    r   sizezPowCone3D.sizee   s    4>>###r-   c                 .    | j                   j                  S rG   )r   rU   r2   s    r   rT   zPowCone3D.num_conesi       vv{{r-   c                 *    dg| j                         z  S rQ   rS   r2   s    r   
cone_sizeszPowCone3D.cone_sizesl   s    s4>>###r-   dppc                 (   |rit        j                         5  t        d | j                  D              }t	        | j
                  t        j                                }|xr |cd d d        S t        d | j                  D              S # 1 sw Y   %xY w)Nc              3   <   K   | ]  }|j                           y wrG   r   r   args     r   r   z#PowCone3D.is_dcp.<locals>.<genexpr>r   s     C#cmmoC   c              3   <   K   | ]  }|j                           y wrG   r]   r^   s     r   r   z#PowCone3D.is_dcp.<locals>.<genexpr>u   s     8s3==?8r`   )r   	dpp_scopeallargs
isinstancer    r   	parameterr'   rZ   args_okexps_oks       r   is_dcpzPowCone3D.is_dcpo   sz    !!# +CCC(X5G5G5IJJ*7+ + 8dii888	+ +s   ABBc                      yrK   r   r'   rZ   s     r   is_dgpzPowCone3D.is_dgpw   rM   r-   c                 "    | j                         S rG   rj   r2   s    r   is_dqcpzPowCone3D.is_dqcpz       {{}r-   .c                 8    d| j                   j                  z   }|S )N)rR   )r   r   )r'   r   s     r   r   zPowCone3D.shape}   s    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)rR   r   r      )r!   reshaper   r   r   r   dual_variables
save_value)r'   r#   dv0dv1dv2s        r   save_dual_valuezPowCone3D.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                    |]t        | j                  d   | j                  z  | j                  d   d| j                  z
  z  | j                  d   | j                         y|D cg c]  }|j                   }}| j                  D cg c]  }|j                   }}t        |      t        | j                        k(  sJ ||k(  sJ t        |d   | j                  z  |d   d| j                  z
  z  |d   | j                        S c c}w c c}w )zlImplements the dual cone of PowCone3D See Pg 85
        of the MOSEK modelling cookbook for more informationNr   r   ru   )r	   rw   r    r   rd   len)r'   rd   r_   args_shapesinstance_args_shapess        r   
_dual_conezPowCone3D._dual_cone   s     <d))!,TZZ79L9LQ9OQRSWS]S]Q]9^))!,djj: 1553995K59=#C#CII#C #Ct9DII..."6666T!WTZZ/a!DJJ,1G!!Wdjj2 2	 6#Cs   $DD	rG   r
   NF)__name__
__module____qualname____doc__r&   r$   r3   propertyrE   rI   boolrL   rO   intrU   rT   r   rY   rj   rm   rp   r   r   r|   r   __classcell__r,   s   @r   r	   r	      s     3>N N 5 5% D  $c $ $$DI $9$ 94 9$ 4   uS#X  
/2r-   r	   c                        e Zd ZdZdZddeddf fdZdefdZde	fdZ
de	fd	Zd
 Zed        Zd Zedefd       Zdee   fdZdde	de	fdZdde	de	fdZde	fdZddZd Z xZS )	PowConeNDa  
    Represents a collection of N-dimensional power cone constraints
    that is *mathematically* equivalent to the following code
    snippet (which makes incorrect use of numpy functions on cvxpy
    objects):

        np.prod(np.power(W, alpha), axis=axis) >= np.abs(z),
        W >= 0

    All arguments must be Expression-like, and z must satisfy
    z.ndim <= 1. The columns (rows) of alpha must sum to 1 when
    axis=0 (axis=1).

    Note: unlike PowCone3D, we make no attempt to promote
    alpha to the appropriate shape. The dimensions of W and
    alpha must match exactly.

    Note: Dual variables are not currently implemented for this type
    of constraint.
    gư>Naxisr
   c           	         t        j                         }|j                  |      }|j                         r|j	                         sd}t        |      |j                  |      }|j                  dkD  s |j                         r|j	                         sd}t        |      |j                  dk  r|j                  dkD  sB|j                  dk(  r|j                  |j                  d|z
     k7  s|j                  dk(  r*|dk(  r%t        d|j                  |j                  |fz        |j                  dk(  r|j                  |   dk  rd}t        |      |j                  |      }|j                  |j                  k7  r&t        d|j                  d|j                  d	      t        j                  |j                  d
k        rt        d      t        j                  t        j                  dt        j                  |j                  |      z
        t        j                  kD        rt        d|z        || _        || _        || _        || _        |j                  d
k(  r|j)                  d      }t*        t        | [  ||g|       y )Nz2Invalid first argument; W must be affine and real.r   zRInvalid second argument. z must be affine, real, and have at most one z.ndim <= 1.ru   z>Argument dimensions %s and %s, with axis=%i, are incompatible.z=PowConeND requires left-hand-side to have at least two terms.zArgument dimensions z and z are not equal.r   z+Argument alpha must be entry-wise positive.r   z+Argument alpha must sum to 1 along axis %s.Forder)r   r   r   r   r   r   ndimrU   r   r!   r"   r#   abssumr   _TOL_Wr   r    r   flattenr%   r&   )	r'   r   r   r    r   r(   r)   r+   r,   s	           r   r&   zPowConeND.__init__   s   ((*
$$Q'		FCS/!$$Q'66A:aiikakkm7CS/!FFaKAFFQJFFaKAFFaggafo5FFaKDAIP77AGGT*+, , 66Q;1774=A-QCS/!((/;;!''! !6 7 766%++"#JKK66"&&RVVEKKd;;<yNOJTQRR
	66Q;			$Ai'A	:r-   c                 V    d| j                   d| j                  d| j                  dS )NzPowND(r/   r0   r1   )r   r   r    r2   s    r   r3   zPowConeND.__str__   s    &*ffdffdjjAAr-   c                      yrK   r   r2   s    r   rL   zPowConeND.is_imag   rM   r-   c                      yrK   r   r2   s    r   rO   zPowConeND.is_complex   rM   r-   c                 H    | j                   | j                  | j                  gS rG   )r    r   rH   r2   s    r   rI   zPowConeND.get_data   s    

DIItww//r-   c                 z   ddl m}m}m}m}m} | j                  j                  | j                  j                  y  || j                  j                        } || j                  j                        }t        ||| j                  | j                        g} | | ||j                  d      |j                  d      g       || j                  j                  d      j                  | j                  j                  d      j                  g      z
              }	 ||	|      }
|
j                  dd      S )	Nr   r5   r   r   r   r;   r<   r=   )r@   r6   r7   r8   r9   r:   r   r#   r   r   r   r    r   r   rA   )r'   r6   r7   r8   r9   r:   r   r   rB   rC   rD   s              r   rE   zPowConeND.residual   s    	ED66<<466<<#7TVV\\"TVV\\"Aq$**499=>uVQYYSY%919939;O$PQ#TVV^^#^%>%D%D%)VV^^#^%>%D%D%F GG H I #v&}}Et}44r-   c                 .    | j                   j                  S rG   )r   rU   r2   s    r   rT   zPowConeND.num_cones   rW   r-   c                 z    d| j                   d   j                  | j                     z   }|| j                         z  S Nr   r   rd   r   r   rT   r'   	cone_sizes     r   rU   zPowConeND.size   s5    		!**49955	4>>+++r-   c                 |    d| j                   d   j                  | j                     z   }|g| j                         z  S r   r   r   s     r   rY   zPowConeND.cone_sizes   s7    		!**49955	{T^^---r-   rZ   c                 2   |rt        j                         5  | j                  d   j                         xr | j                  d   j                         }t	        | j
                  t        j                                }|xr |cddd       S y# 1 sw Y   yxY w)zCA power cone constraint is DCP if each argument is affine.
        r   r   NT)r   rb   rd   r   re   r    r   rf   rg   s       r   rj   zPowConeND.is_dcp  s     !!# +))A,002Otyy|7M7M7O(X5G5G5IJJ*7+ + 	+ s   A+BBc                      yrK   r   rl   s     r   rm   zPowConeND.is_dgp  rM   r-   c                 "    | j                         S rG   ro   r2   s    r   rp   zPowConeND.is_dqcp  rq   r-   c                 @   |d d d df   }|d d df   }| j                   dk(  r|j                  }|j                  }|j                  d   dk(  rt        j                  |      }| j
                  d   j                  |       | j
                  d   j                  |       y )Nrt   r   r   )r   Tr   r!   squeezerw   rx   )r'   r#   dWdzs       r   r|   zPowConeND.save_dual_value  s    1crc6]1b5\99>BB88A;! BBA))"-A))"-r-   c                 l   ||dk(  rL| j                   d   | j                  z  }t        || j                   d   | j                  | j                        S |D cg c]  }|j                   }}| j
                  D cg c]  }|j                   }}t        |      t        | j
                        k(  sJ ||k(  sJ |d   j                  j                  | j                  j                  j                  k(  sJ t        |d   | j                  z  |d   | j                  | j                        S c c}w c c}w )zlImplements the dual cone of PowConeND See Pg 85
        of the MOSEK modelling cookbook for more informationr   r   r   r   )rw   r    r   r   r   rd   r~   r#   )r'   rd   scaled_dualsr_   r   r   s         r   r   zPowConeND._dual_cone   s    <42:..q1$**<L\4+>+>q+A4::TXT]T]^^ 1553995K59=#C#CII#C #Ct9DII..."66667==&&$***:*:*@*@@@@T!WTZZ/a$**499UU 6#Cs   D,;D1r   r   r   )r   r   r   r   r   r   r&   r$   r3   r   rL   rO   rI   r   rE   rT   rU   r   rY   rj   rm   rp   r|   r   r   r   s   @r   r   r      s    * E#;# #;d #;JB B D 0 5 5 ,c , ,.DI .$ 4 $ 4  .Vr-   r   )r   typingr   r   numpyr!   cvxpy.constraints.conesr   cvxpy.expressionsr   cvxpy.utilitiesr   r	   r   r   r-   r   <module>r      s:       ( & "A2 A2HOV OVr-   