
    bi                     Z    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y)	a,  
Copyright 2013 Steven Diamond

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OptionalN)Cone)cvxtypes)scopesc                        e Zd ZdZddeddf fdZdefdZede	e
j                     fd       Z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 )SOCaN  A second-order cone constraint for each row/column.

    Assumes ``t`` is a vector the same length as ``X``'s columns (rows) for
    ``axis == 0`` (``1``).

    Attributes:
        t: The scalar part of the second-order constraint.
        X: A matrix whose rows/columns are each a cone.
        axis: Slice by column 0 or row 1.
    Naxisreturnc                    t        j                         j                  |      }t        |j                        dk\  s|j                         st        d      t        |j                        dk  r|j                  dkD  sTt        |j                        dk(  r|j                  |j                  d|z
     k7  st        |j                        dk(  r*|dk(  r%t        d|j                  |j                  |fz        || _        t        |j                        dk(  r|j                  d      }t        t        | 3  ||g|       y )N   zInvalid first argument.   z>Argument dimensions %s and %s, with axis=%i, are incompatible.r   F)order)r   
expressioncast_to_constlenshapeis_real
ValueErrorsizer
   flattensuperr	   __init__)selftXr
   	constr_id	__class__s        Y/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/constraints/second_order.pyr   zSOC.__init__&   s    !//2qww<1AIIK677LA!&&1*LA!&&AGGAdFO";LA$!)P77AGGT*+  	qww<1			$Ac4!1a&)4    c                 H    d| j                   d   d| j                   d   dS )NzSOC(r   z, r   ))argsr   s    r    __str__zSOC.__str__7   s     $		!diil;;r!   c                 6   | j                   d   j                  }| j                   d   j                  }||y| j                  dk(  r|j                  }|j                  dk(  }t        j                  |      }t        j                  |j                        }t        j                  |j                        }t
        j                  j                  |dd      }||k\  }||   ||<   ||   ||<   t        j                  |      |k  }dd||z  z   z  }	|	|df   ||   z  ||<   |	|   ||   z  ||<   t        j                  ||dddf   gd      }
t        j                  ||dddf   gd      }t
        j                  j                  |
|z
  dd      }|r|d   S |S )a  
        For each cone, returns:

        ||(t,X) - proj(t,X)||
        with
        proj(t,X) = (t,X)                       if t >= ||x||
                    0.5*(t/||x|| + 1)(||x||,x)  if -||x|| < t < ||x||
                    0                           if t <= -||x||

        References:
             https://docs.mosek.com/modeling-cookbook/practical.html#distance-to-a-cone
             https://math.stackexchange.com/questions/2509986/projection-onto-the-second-order-cone
        r   r   Nr   )ordr
   g      ?)r
   )r$   valuer
   Tndimnp
atleast_2dzerosr   linalgnormabsconcatenate)r   r   r   promotedt_projX_projnormst_geq_x_normabs_t_less_x_norm	avg_coeffXtXt_projresids                r    residualzSOC.residual:   s     IIaLIIaL9	 99>A66Q;MM! !''"!''"		qaa0 Ez | | FF1I-1qw;'	$-.?.E$FK\I]$] !$-.?$@5IZC[$[ !^^Q!T'
O!4..&&D/!:C		rG|; 8OLr!   c                 2    | j                   | j                  gS )zvReturns info needed to reconstruct the object besides the args.

        Returns
        -------
        list
        )r
   idr%   s    r    get_datazSOC.get_dataq   s     		477##r!   c                 4    | j                   d   j                  S )z)The number of elementwise cones.
        r   )r$   r   r%   s    r    	num_coneszSOC.num_conesz   s     yy|   r!   c                 z    d| j                   d   j                  | j                     z   }|| j                         z  S )z5The number of entries in the combined cones.
        r   r$   r   r
   rB   r   	cone_sizes     r    r   zSOC.size   s7     		!**49955	4>>+++r!   c                 |    d| j                   d   j                  | j                     z   }|g| j                         z  S )zThe dimensions of the second-order cones.

        Returns
        -------
        list
            A list of the sizes of the elementwise cones.
        r   rD   rE   s     r    
cone_sizeszSOC.cone_sizes   s9     		!**49955	{T^^---r!   dppc                     |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)zEAn SOC constraint is DCP if each of its arguments is affine.
        c              3   <   K   | ]  }|j                           y wN	is_affine.0args     r    	<genexpr>zSOC.is_dcp.<locals>.<genexpr>   s     @s3==?@   Nc              3   <   K   | ]  }|j                           y wrL   rM   rO   s     r    rR   zSOC.is_dcp.<locals>.<genexpr>   s     8s3==?8rS   )r   	dpp_scopeallr$   r   rI   s     r    is_dcpz
SOC.is_dcp   sY     !!# A@dii@@A A8dii888A As   AA"c                      y)NF rW   s     r    is_dgpz
SOC.is_dgp   s    r!   c                 "    | j                         S rL   )rX   r%   s    r    is_dqcpzSOC.is_dqcp   s    {{}r!   c                 \   d| j                   d   j                  | j                     z   }t        j                  |d|f      }|d d df   }|d d dd f   }| j                  dk(  r|j
                  }| j                  d   j                  |       | j                  d   j                  |       y )Nr   r   )r$   r   r
   r,   reshaper*   dual_variables
save_value)r   r)   rF   r   r   s        r    save_dual_valuezSOC.save_dual_value   s    		!**49955	

52y/2!Q$K!QR%L99>AA))!,A))!,r!   c                    |1t        | j                  d   | j                  d   | j                        S |D cg c]  }|j                   }}| j                  D cg c]  }|j                   }}t        |      t        | j                        k(  sJ ||k(  sJ t        |d   |d   | j                        S c c}w c c}w )zxImplements the dual cone of the second-order cone
        See Pg 85 of the MOSEK modelling cookbook for more informationr   r   )r	   ra   r
   r   r$   r   )r   r$   rQ   args_shapesinstance_args_shapess        r    
_dual_conezSOC._dual_cone   s     <t**1-t/B/B1/EtyyQQ 1553995K59=#C#CII#C #Ct9DII..."6666tAwQ33	 6#Cs   B7B<)r   N)F)r   N)__name__
__module____qualname____doc__intr   strr&   propertyr   r,   ndarrayr=   r@   rB   r   r   rH   boolrX   r[   r]   rc   rg   __classcell__)r   s   @r    r	   r	      s    	53 5t 5"< < 4(2::. 4 4l$!
 ,c , ,	.DI 	.9$ 94 9$ 4  -4r!   r	   )rk   typingr   r   numpyr,   cvxpy.constraints.conesr   cvxpy.expressionsr   cvxpy.utilitiesr   r	   rZ   r!   r    <module>rw      s)     "  ( & "Z4$ Z4r!   