
    bi                     r    d Z ddlZddlZddlmZ ddlmZ  G d de      Z G d de      Z	 G d	 d
e      Z
y)a+  
Copyright, 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.
    N)
Constraint)scopesc                   |     e Zd ZdZdZdd fdZd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
        Zd Z xZS )NonPosa  An inequality constraint of the form :math:`x \leq 0`.

    The preferred way of creating an inequality constraint is through
    operator overloading. To constrain an expression ``x`` to be nonpositive,
    write ``x <= 0``; to constrain ``x`` to be nonnegative, write ``x >= 0``.

    Dual variables associated with this constraint are nonnegative, rather
    than nonpositive. As such, dual variables to this constraint belong to the
    polar cone rather than the dual cone.

    Note: strict inequalities are not supported, as they do not make sense in
    a numerical setting.

    Parameters
    ----------
    expr : Expression
        The expression to constrain.
    constr_id : int
        A unique id for the constraint.
    a  
    Explicitly invoking "NonPos(expr)" to a create a constraint is deprecated.
    Please use operator overloading or "NonNeg(-expr)" instead.
    
    Sign conventions on dual variables associated with NonPos constraints may
    change in the future.
    returnc                     t        j                  t        j                  t               t
        t        |   |g|       | j                  d   j                         st        d      y )Nr   zInput to NonPos must be real.)
warningswarnr   DEPRECATION_MESSAGEDeprecationWarningsuper__init__argsis_real
ValueErrorselfexpr	constr_id	__class__s      S/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/constraints/nonpos.pyr   zNonPos.__init__8   sP    f002DEfd$dVY7yy|##%<== &    c                 &    d| j                   d   z  S )Nz%s <= 0r   r   r   s    r   namezNonPos.name>       499Q<''r   dppc                     |r;t        j                         5  | j                  d   j                         cddd       S | j                  d   j                         S # 1 sw Y   &xY w)z5A NonPos constraint is DCP if its argument is convex.r   N)r   	dpp_scoper   	is_convexr   r   s     r   is_dcpzNonPos.is_dcpA   sV    !!# 0yy|--/0 0yy|%%''0 0   AA$c                      yNF r"   s     r   is_dgpzNonPos.is_dgpH       r   c                 <    | j                   d   j                         S Nr   )r   is_quasiconvexr   s    r   is_dqcpzNonPos.is_dqcpK   s    yy|**,,r   c                     | j                   j                  yt        j                  | j                   j                  d      S zaThe residual of the constraint.

        Returns
        ---------
        NumPy.ndarray
        Nr   r   valuenpmaximumr   s    r   residualzNonPos.residualN   .     99??"zz$))//1--r   c                 z    | j                   }|t        d      t        j                  j	                  |d      }|S NzNCannot compute the violation of an constraint whose expression is None-valued.   )ordr4   r   r2   linalgnormr   resviols      r   	violationzNonPos.violationZ   @    mm; @ A Ayy~~cq~)r   Nr   NF)__name__
__module____qualname____doc__r   r   strr   boolr#   r(   r-   propertyr4   r@   __classcell__r   s   @r   r   r      sl    *>(c (($ (4 ($ 4 - - 	. 	.r   r   c                   x     e Zd ZdZdd fdZd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	        Zd
 Z xZS )NonNega8  A constraint of the form :math:`x \geq 0`.

    The preferred way of creating an inequality constraint is through
    operator overloading. To constrain an expression ``x`` to be nonnegative,
    write ``x >= 0``; to constrain ``x`` to be nonpositive, write ``x <= 0``.

    Dual variables for these constraints are nonnegative. As such, they
    actually belong to this constraint class' corresponding dual cone.

    Parameters
    ----------
    expr : Expression
        The expression to constrain.
    constr_id : int
        A unique id for the constraint.
    r   c                     t         t        |   |g|       | j                  d   j	                         st        d      y )Nr   zInput to NonNeg must be real.)r   rO   r   r   r   r   r   s      r   r   zNonNeg.__init__t   s;    fd$dVY7yy|##%<== &r   c                 &    d| j                   d   z  S )Nz%s >= 0r   r   r   s    r   r   zNonNeg.namey   r   r   r   c                     |r;t        j                         5  | j                  d   j                         cddd       S | j                  d   j                         S # 1 sw Y   &xY w)z<A non-negative constraint is DCP if its argument is concave.r   N)r   r    r   
is_concaver"   s     r   r#   zNonNeg.is_dcp|   sV    !!# 1yy|..01 1yy|&&((1 1r$   c                      yr&   r'   r"   s     r   r(   zNonNeg.is_dgp   r)   r   c                 <    | j                   d   j                         S r+   )r   is_quasiconcaver   s    r   r-   zNonNeg.is_dqcp   s    yy|++--r   c                     | j                   j                  yt        j                  t        j                  | j                   j                  d            S r/   )r   r1   r2   absminimumr   s    r   r4   zNonNeg.residual   s7     99??"vvbjj!455r   c                 z    | j                   }|t        d      t        j                  j	                  |d      }|S r7   r:   r=   s      r   r@   zNonNeg.violation   rA   r   rB   rC   rD   )rE   rF   rG   rH   r   rI   r   rJ   r#   r(   r-   rK   r4   r@   rL   rM   s   @r   rO   rO   c   sd     >
(c ()$ )4 )$ 4 . . 	6 	6r   rO   c                        e Zd ZdZdd fdZd fdZed        ZdefdZ	ed        Z
ed        Zdd	edefd
Zdd	edefdZddefdZdefdZed        Z xZS )
Inequalitya  A constraint of the form :math:`x \leq y`.

    Dual variables to these constraints are always nonnegative.
    A constraint of this type affects the Lagrangian :math:`L` of a
    minimization problem by

        :math:`L += (x - y)^{T}(\texttt{con.dual\_value})`.

    The preferred way of creating one of these constraints is via
    operator overloading. The expression ``x <= y`` evaluates to
    ``Inequality(x, y)``, and the expression ``x >= y`` evaluates
    to ``Inequality(y, x)``.

    Parameters
    ----------
    lhs : Expression
        The expression to be upper-bounded by rhs
    rhs : Expression
        The expression to be lower-bounded by lhs
    constr_id : int
        A unique id for the constraint.
    r   c                     ||z
  | _         | j                   j                         rt        d      t        t        |   ||g|       y )Nz)Inequality constraints cannot be complex.)_expr
is_complexr   r   r\   r   )r   lhsrhsr   r   s       r   r   zInequality.__init__   s?    3Y
::  "HIIj$(#sY?r   c                 B    t         t        |   | j                  g       y rB   )r   r\   _construct_dual_variablesr^   )r   r   r   s     r   rc   z$Inequality._construct_dual_variables   s    j$94::,Gr   c                     | j                   S rB   )r^   r   s    r   r   zInequality.expr   s    zzr   c                 D    | j                   d   d| j                   d   S )Nr   z <=    r   r   s    r   r   zInequality.name   s    !YYq\499Q<88r   c                 .    | j                   j                  S )z.int : The shape of the constrained expression.)r   shaper   s    r   rh   zInequality.shape   s     yyr   c                 .    | j                   j                  S )z-int : The size of the constrained expression.)r   sizer   s    r   rj   zInequality.size   s     yy~~r   r   c                     |r8t        j                         5  | j                  j                         cddd       S | j                  j                         S # 1 sw Y   #xY w)z;A non-positive constraint is DCP if its argument is convex.N)r   r    r   r!   r"   s     r   r#   zInequality.is_dcp   sN    !!# -yy**,- -yy""$$- -s   AAc                 J   |rZt        j                         5  | j                  d   j                         xr | j                  d   j	                         cd d d        S | j                  d   j                         xr | j                  d   j	                         S # 1 sw Y   ExY wNr   rf   )r   r    r   is_log_log_convexis_log_log_concaver"   s     r   r(   zInequality.is_dgp   s    !!# ;		!668 :		!779; ; 		!..0 2		!//1	3; ;s   <BB"c                     |j                         dk(  r| j                  d      S |j                         dk(  r| j                  d      S t        d|      )NdcpT)r   dgpzUnsupported context )lowerr#   r(   r   )r   contexts     r   is_dppzInequality.is_dpp   sN    ==?e#;;4;((]]_%;;4;((3W==r   c                    | j                         xsz | j                  d   j                         xr | j                  d   j                         xs< | j                  d   j                         xr | j                  d   j	                         S rm   )r#   r   r,   is_constantrV   r   s    r   r-   zInequality.is_dqcp   sq    KKM LYYq\((*Ityy|/G/G/ILYYq\%%'JDIIaL,H,H,J	Mr   c                     | j                   j                  yt        j                  | j                   j                  d      S r/   r0   r   s    r   r4   zInequality.residual   r5   r   rB   rC   rD   )rq   )rE   rF   rG   rH   r   rc   rK   r   rI   r   rh   rj   rJ   r#   r(   ru   r-   r4   rL   rM   s   @r   r\   r\      s    ,@H  9c 9    %$ %4 %3$ 34 3>t >M M 	. 	.r   r\   )rH   r	   numpyr2   cvxpy.constraints.constraintr   cvxpy.utilitiesr   r   rO   r\   r'   r   r   <module>r|      sD       4 "FZ FR8Z 8vX. X.r   