
    bi                     l    d Z ddlZddlZddlmc mZ ddlm	Z
 ddlmZ  G d de
j                        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.
    N)cvxtypesc                       e Zd ZdZdd fdZd ZdefdZddZe	d        Z
e	defd       Ze	d	        Zdefd
ZdefdZdefdZej&                  ddedefd       Zej&                  ddedefd       ZddefdZe	ej&                  d               Zd Zd defdZe	d        Zej8                  d        Zd Zd Zd Ze	d        Z ddZ! xZ"S )!
ConstraintaY  The base class for constraints.

    A constraint is an equality, inequality, or more generally a generalized
    inequality that is imposed upon a mathematical expression or a list of
    thereof.

    Parameters
    ----------
    args : list
        A list of expression trees.
    constr_id : int
        A unique id for the constraint.
    returnc                     || _         |t        j                         | _        n|| _        | j	                  |       t
        t        |           y N)argsluget_id	constr_id_construct_dual_variablessuperr   __init__)selfr	   r   	__class__s      W/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/constraints/constraint.pyr   zConstraint.__init__)   sA     	YY[DN&DN&&t,j$(*    c                 "    | j                         S )z>Returns a string showing the mathematical constraint.
        )namer   s    r   __str__zConstraint.__str__4   s     yy{r   c                 f    | j                   j                  dt        | j                  d         dS )z@Returns a string with information about the constraint.
        (r   ))r   __name__reprr	   r   s    r   __repr__zConstraint.__repr__9   s+      >>22		!-/ 	/r   c                 |    |D cg c]&  } t        j                         |j                        ( c}| _        y c c}w r   )r   variableshapedual_variables)r   r	   args      r   r   z$Constraint._construct_dual_variables?   s-    IMN#2x002399=NNs   +9c                 4    | j                   d   j                  S )z@
        int : The shape of the constrained expression.
        r   )r	   r    r   s    r   r    zConstraint.shapeB   s    
 yy|!!!r   c                 F    t        | j                  d   j                        S )zW
        int : The maximum number of dimensions of the constrained expression.
        r   )lenr	   r    r   s    r   ndimzConstraint.ndimI   s    
 499Q<%%&&r   c                 4    | j                   d   j                  S )z-int : The size of the constrained expression.r   )r	   sizer   s    r   r(   zConstraint.sizeP   s     yy|   r   c                 $    | j                          S )z!Is the Leaf real valued?
        
is_complexr   s    r   is_realzConstraint.is_realU   s     ??$$$r   c                 :    t        d | j                  D              S )zIs the Leaf imaginary?
        c              3   <   K   | ]  }|j                           y wr   )is_imag.0r"   s     r   	<genexpr>z%Constraint.is_imag.<locals>.<genexpr>]   s     6S3;;=6   )allr	   r   s    r   r/   zConstraint.is_imagZ   s     6DII666r   c                 :    t        d | j                  D              S )z$Is the Leaf complex valued?
        c              3   <   K   | ]  }|j                           y wr   r*   r0   s     r   r2   z(Constraint.is_complex.<locals>.<genexpr>b   s     93>>#9r3   )anyr	   r   s    r   r+   zConstraint.is_complex_   s     9tyy999r   dppc                     t               )zChecks whether the constraint is DCP.

        Returns
        -------
        bool
            True if the constraint is DCP, False otherwise.
        NotImplementedErrorr   r8   s     r   is_dcpzConstraint.is_dcpd        "##r   c                     t               )zChecks whether the constraint is DGP.

        Returns
        -------
        bool
            True if the constraint is DGP, False otherwise.
        r:   r<   s     r   is_dgpzConstraint.is_dgpo   r>   r   c                     |j                         dk(  r| j                  d      S |j                         dk(  r| j                  d      S t        d|      )NdcpT)r8   dgpzUnsupported context )lowerr=   r@   
ValueError)r   contexts     r   is_dppzConstraint.is_dppz   sN    ==?e#;;4;((]]_%;;4;((3W==r   c                     t               )zThe residual of the constraint.

        Returns
        -------
        NumPy.ndarray
            The residual, or None if the constrained expression does not have
            a value.
        r:   r   s    r   residualzConstraint.residual   s     "##r   c                 8    | j                   }|t        d      |S )a  The numeric residual of the constraint.

        The violation is defined as the distance between the constrained
        expression's value and its projection onto the domain of the
        constraint:

        .. math::

            ||\Pi(v) - v||_2

        where :math:`v` is the value of the constrained expression and
        :math:`\Pi` is the projection operator onto the constraint's domain .

        Returns
        -------
        NumPy.ndarray
            The residual value.

        Raises
        ------
        ValueError
            If the constrained expression does not have a value associated
            with it.
        zNCannot compute the violation of an constraint whose expression is None-valued.)rI   rE   )r   rI   s     r   	violationzConstraint.violation   s-    2 == @ A Ar   	tolerancec                 d    | j                   }|t        d      t        j                  ||k        S )a  Checks whether the constraint violation is less than a tolerance.

        Parameters
        ----------
            tolerance : float
                The absolute tolerance to impose on the violation.

        Returns
        -------
            bool
                True if the violation is less than ``tolerance``, False
                otherwise.

        Raises
        ------
            ValueError
                If the constrained expression does not have a value associated
                with it.
        zJCannot compute the value of an constraint whose expression is None-valued.)rI   rE   npr4   )r   rL   rI   s      r   valuezConstraint.value   s<    ( == @ A Avvh)+,,r   c                     | j                   S )z2Wrapper for compatibility with variables.
        r   r   s    r   idzConstraint.id   s     ~~r   c                     || _         y r   rQ   r   rO   s     r   rR   zConstraint.id   s	    r   c                     | j                   gS )zData needed to copy.
        )rR   r   s    r   get_datazConstraint.get_data   s     yr   c                     t        d      )z4Raises an error due to chained constraints.
        zXCannot evaluate the truth value of a constraint or chain constraints, e.g., 1 >= x >= 0.)	Exceptionr   s    r   _chain_constraintszConstraint._chain_constraints   s     5
 	
r   c                 "    | j                         S )zRaises an exception when called.

        Python 3 version.

        Called when evaluating the truth value of the constraint.
        Raising an error here prevents writing chained constraints.
        )rY   r   s    r   __bool__zConstraint.__bool__   s     &&((r   c                 |    | j                   D cg c]  }|j                   }}t        |      dk(  r|d   S |S c c}w )z8NumPy.ndarray : The value of the dual variable.
           r   )r!   rO   r%   )r   dv	dual_valss      r   
dual_valuezConstraint.dual_value   sB     )-(;(;<"RXX<	<y>QQ<	 =s   9c                 @    | j                   d   j                  |       y)zSave the value of the dual variable for the constraint's parent.
        Args:
            value: The value of the dual variable.
        r   N)r!   
save_valuerT   s     r   save_dual_valuezConstraint.save_dual_value   s    
 	A))%0r   r   )r   N)F)rB   )g:0yE>)#r   
__module____qualname____doc__r   r   strr   r   propertyr    intr&   r(   boolr,   r/   r+   abcabstractmethodr=   r@   rG   rI   rK   floatrO   rR   setterrV   rY   r[   r`   rc   __classcell__)r   s   @r   r   r      sn   	+
/# /O " " 'c ' ' ! !% %
7 7
:D :
 	$$ $4 $ $ 	$$ $4 $ $>t > 	$  	$>-u -4  
 YY 

)  1r   r   )rf   rk   numpyrN   cvxpy.lin_ops.lin_utilslin_ops	lin_utilsr
   cvxpy.utilities	utilitiesucvxpy.expressionsr   	Canonicalr    r   r   <module>rz      s0       $ $  &`1 `1r   