
    biW!                         d 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c mZ ddlmZ ddlmc mZ ddlmZ ddlmZ d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.
    N)ListOptionalTuple)Leaf)performance_utilszgInitializing a Constant with a nested list is undefined behavior. Consider using a numpy array instead.c                       e Zd ZdZddee   ddf fdZdefdZded    fdZ	de
fdZed	        Zde
fd
Zed        Zedeedf   fd       Zd ZdefdZde
fdZde
fdZde
fdZej2                  de
fd       Zej2                  de
fd       Zej2                  de
fd       ZddZddZde
fdZej2                  de
fd       Z ej2                  de
fd       Z! xZ"S )ConstantaC  
    A constant value.

    Raw numerical constants such as Python primitive types or NumPy ndarrays
    are implicitly cast to constants via Expression operator overloading.
    For example, if ``x`` is an expression and``c`` is a raw constant,
    then ``x + c`` creates an expression by casting ``c`` to a Constant.
    Nnamereturnc                 D   t        j                  |      r.t         j                  j                  |d      | _        d| _        nft        |t              r+t        d |D              rt        j                  t               t         j                  j                  |      | _        d| _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        || _        t.        t0        | g  t        j4                  | j6                               y )NT)convert_scalarsc              3   <   K   | ]  }t        |t                y wN)
isinstancelist).0is     _/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/expressions/constants/constant.py	<genexpr>z$Constant.__init__.<locals>.<genexpr>3   s     .Rqz!T/B.Rs   F)intf	is_sparseDEFAULT_SPARSE_INTFconst_to_matrix_value_sparser   r   anywarningswarnNESTED_LIST_WARNINGDEFAULT_INTF_imag_nonneg_nonpos_symm_herm	_psd_test	_nsd_test_cached_is_pos
_skew_symm_namesuperr	   __init__shapevalue)selfr.   r
   	__class__s      r   r,   zConstant.__init__,   s    >>% 22BBt C -DKDL%&3.RE.R+R12++;;EBDK DL%)
'+'+%)
%)
)-)-"
h&tzz$**'=>    c                 >   | j                   t        | j                        dk(  rYdt        | j                        v rBt        j                  | j                  t        j                  t        j                  dd i      S t        | j                        S | j                   S )z9
         The value of the constant as a string.
           
floatc                 
    | dS )Nz.2f )xs    r   <lambda>zConstant.name.<locals>.<lambda>M   s
    #w r1   )	edgeitems	threshold	formatter)
r*   lenr-   strr.   nparray2stringsPRINT_EDGEITEMSPRINT_THRESHOLDr/   s    r   r
   zConstant.nameD   s{     ::4::!#DJJ(?tzz121B1B121B1B29;O1PR R tzz?"::r1   c                     | gS )z$Returns self as a constant.
        r7   rD   s    r   	constantszConstant.constantsR   s     vr1   c                      y)NTr7   rD   s    r   is_constantzConstant.is_constantW   s    r1   c                     | j                   S )zBNumPy.ndarray or None: The numeric value of the constant.
        )r   rD   s    r   r.   zConstant.valueZ        {{r1   c                     | j                   Yt        j                  | j                        rd| _         | j                   S t	        j
                  | j                  dkD        | _         | j                   S )z>Returns whether the constant is elementwise positive.
        Fr   )r(   spissparser   r?   allrD   s    r   is_poszConstant.is_pos`   s_     &{{4;;' ',# """ ')ffT[[1_&=#"""r1   c                     i S )zGives the (sub/super)gradient of the expression w.r.t. each variable.

        Matrix expressions are vectorized, so the gradient is a matrix.

        Returns:
            A map of variable to SciPy CSC sparse matrix or None.
        r7   rD   s    r   gradzConstant.gradl   s	     	r1   .c                     | j                   S )z=Returns the (row, col) dimensions of the expression.
        )_shaperD   s    r   r-   zConstant.shapew   rJ   r1   c                 t    t        j                  | j                  | j                  | j                        }|g fS )zReturns the graph implementation of the object.

        Returns:
            A tuple of (affine expression, [constraints]).
        )lucreate_constr.   r-   r   )r/   objs     r   canonicalizezConstant.canonicalize}   s,     oodjj$**dllCRyr1   c                 V    d| j                   d| j                  d| j                  dS )z@Returns a string with information about the expression.
        z	Constant(z, ))	curvaturesignr-   rD   s    r   __repr__zConstant.__repr__   s#     *.)-)-5 	5r1   c                 R    | j                   | j                          | j                   S )z'Is the expression nonnegative?
        )r"   _compute_attrrD   s    r   	is_nonnegzConstant.is_nonneg   $     << ||r1   c                 R    | j                   | j                          | j                   S )z'Is the expression nonpositive?
        )r#   r_   rD   s    r   	is_nonposzConstant.is_nonpos   ra   r1   c                 R    | j                   | j                          | j                   S )zIs the Leaf imaginary?
        )r!   r_   rD   s    r   is_imagzConstant.is_imag   s$     :: zzr1   c                 @    t        j                  | j                        S )z$Is the Leaf complex valued?
        )r?   iscomplexobjr.   rD   s    r   
is_complexzConstant.is_complex   s     tzz**r1   c                     | j                         ry| j                  dk(  rG| j                  d   | j                  d   k(  r(| j                  | j	                          | j                  S y)z%Is the expression symmetric?
        Tr3   r      F)	is_scalarndimr-   r$   _compute_symm_attrrD   s    r   is_symmetriczConstant.is_symmetric   sV     >>YY!^

1A >zz!'')::r1   c                     | j                         r| j                         ry| j                  dk(  rG| j                  d   | j                  d   k(  r(| j                  | j                          | j                  S y)z.Is the expression a Hermitian matrix?
        Tr3   r   rj   F)rk   is_realrl   r-   r%   rm   rD   s    r   is_hermitianzConstant.is_hermitian   s^     >>YY!^

1A >zz!'')::r1   c                     t        j                  | j                        \  }}| j                         rdx}}n"t        j                  | j                        \  }}|xr | | _        || _        || _        y)zNCompute the attributes of the constant related to complex/real, sign.
        FN)r   rh   r.   r\   r!   r#   r"   )r/   rp   re   r`   rc   s        r   r_   zConstant._compute_attr   sb      ??4::6??$))I	#'99TZZ#8 Iy-g+
  r1   c                 d    t        j                  | j                        \  }}|| _        || _        y)z?Determine whether the constant is symmetric/Hermitian.
        N)r   rq   r.   r$   r%   )r/   is_symmis_herms      r   rm   zConstant._compute_symm_attr   s+      ,,TZZ8

r1   c                 z    | j                   $t        j                  | j                        | _         | j                   S r   )r)   r   is_skew_symmetricr.   rD   s    r   rw   zConstant.is_skew_symmetric   s,    ??""44TZZ@DOr1   c                    | j                         r| j                         ry| j                         ry| j                  dk(  ry| j                  dk(  r | j                  d   | j                  d   k7  ry| j	                         sy| j
                  3t        j                  | j                  t        j                        | _        | j
                  S )z:Is the expression a positive semidefinite matrix?
        TFrj   r3   r   )rk   r`   rl   r-   rq   r&   eig_utilis_psd_within_tolr.   rA   
EIGVAL_TOLrD   s    r   is_psdzConstant.is_psd   s    
 >> 0^^YY!^YY!^

1A >""$ >>!%77

ALLQDN~~r1   c                    | j                         r| j                         ry| j                         ry| j                  dk(  ry| j                  dk(  r | j                  d   | j                  d   k7  ry| j	                         sy| j
                  4t        j                  | j                   t        j                        | _        | j
                  S )z:Is the expression a negative semidefinite matrix?
        TFrj   r3   r   )rk   rc   rl   r-   rq   r'   ry   rz   r.   rA   r{   rD   s    r   is_nsdzConstant.is_nsd   s    
 >> 0^^YY!^YY!^

1A >""$ >>!%77Q\\RDN~~r1   r   )r   N)#__name__
__module____qualname____doc__r   r>   r,   r
   r   rF   boolrH   propertyr.   rO   rQ   r   intr-   rX   r]   r`   rc   re   perfcompute_oncerh   rn   rq   r_   rm   rw   r|   r~   __classcell__)r0   s   @r   r	   r	   "   s   ?HSM ?T ?0c 4
+ 
T   

# 
#   uS#X  
5# 54 4   
+D + +
 

d 
 
 

d 
 
!4 
 
  * 
  r1   r	   ) r   r   typingr   r   r   numpyr?   scipy.sparsesparserL   cvxpy.interface	interfacer   cvxpy.lin_ops.lin_utilslin_ops	lin_utilsrU   cvxpy.settingssettingsrA   cvxpy.utilities.linalg	utilitieslinalgry   cvxpy.expressions.leafr   cvxpy.utilitiesr   r   r   r	   r7   r1   r   <module>r      sJ      ( (    $ $  ) ) ' 5R dt dr1   