
    bi,                         d Z ddlmZmZmZ ddlmZ ddlm	c m
Z ddlm	c mZ ddlmZ ddlm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.
    )AnyListTupleN)Atom)canonInterface)Constant)performance_utilsc                      e Zd ZdZdZdeeef   fdZdefdZdefdZ	defdZ
defdZdefd	Zdefd
ZdefdZdefdZdefdZdefdZej&                  defd       Zej&                  defd       Zdee   fdZy)AffAtomz' Abstract base class for affine atoms. Treturnc                 z    t         j                  j                  | j                  D cg c]  }| c}      S c c}w )zLBy default, the sign is the most general of all the argument signs.
        )usign	sum_signsargs)selfargs     Y/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/affine/affine_atom.pysign_from_argszAffAtom.sign_from_args!   s+     vv		 : :;; :s   	8c                 :    t        d | j                  D              S )z%Is the expression imaginary?
        c              3   <   K   | ]  }|j                           y wN)is_imag.0r   s     r   	<genexpr>z"AffAtom.is_imag.<locals>.<genexpr>*        6S3;;=6   allr   r   s    r   r   zAffAtom.is_imag&   s     6DII666    c                 :    t        d | j                  D              S )z*Is the expression complex valued?
        c              3   <   K   | ]  }|j                           y wr   )
is_complexr   s     r   r   z%AffAtom.is_complex.<locals>.<genexpr>0   s     93>>#9r   anyr   r!   s    r   r%   zAffAtom.is_complex,   s     9tyy999r"   c                      y)zIs the atom convex?
        T r!   s    r   is_atom_convexzAffAtom.is_atom_convex2        r"   c                      y)zIs the atom concave?
        Tr)   r!   s    r   is_atom_concavezAffAtom.is_atom_concave7   r+   r"   c                      y)z;Is the composition non-decreasing in argument idx?
        Tr)   r   idxs     r   is_incrzAffAtom.is_incr<   s     r"   c                      y)z;Is the composition non-increasing in argument idx?
        Fr)   r/   s     r   is_decrzAffAtom.is_decrB   s     r"   c                 :    t        d | j                  D              S )Nc              3   <   K   | ]  }|j                           y wr   )is_quadraticr   s     r   r   z'AffAtom.is_quadratic.<locals>.<genexpr>I   s     ;#3##%;r   r   r!   s    r   r6   zAffAtom.is_quadraticH   s    ;;;;r"   c                 :    t        d | j                  D              S )a  Does the affine head of the expression contain a quadratic term?

        The affine head is all nodes with a path to the root node
        that does not pass through any non-affine atom. If the root node
        is non-affine, then the affine head is the root alone.
        c              3   <   K   | ]  }|j                           y wr   )has_quadratic_termr   s     r   r   z-AffAtom.has_quadratic_term.<locals>.<genexpr>R   s     A3))+Ar   r&   r!   s    r   r9   zAffAtom.has_quadratic_termK   s     AtyyAAAr"   c                 :    t        d | j                  D              S )Nc              3   <   K   | ]  }|j                           y wr   )is_qpwar   s     r   r   z"AffAtom.is_qpwa.<locals>.<genexpr>U   r   r   r   r!   s    r   r<   zAffAtom.is_qpwaT   s    6DII666r"   c                 :    t        d | j                  D              S )Nc              3   <   K   | ]  }|j                           y wr   )is_pwlr   s     r   r   z!AffAtom.is_pwl.<locals>.<genexpr>X   s     5C3::<5r   r   r!   s    r   r?   zAffAtom.is_pwlW   s    5499555r"   c                     t        | j                        D ]J  \  }}| j                  |      r|j                         r(| j	                  |      r|j                         rJ y yz:Is the expression a positive semidefinite matrix?
        FT)	enumerater   r1   is_psdr3   is_nsdr   r0   r   s      r   rC   zAffAtom.is_psd[   P     "$)), 	HC\\#&3::<\\#&3::<	 r"   c                     t        | j                        D ]J  \  }}| j                  |      r|j                         r(| j	                  |      r|j                         rJ y yrA   )rB   r   r3   rC   r1   rD   rE   s      r   rD   zAffAtom.is_nsde   rF   r"   c                    g }i }d}t        | j                        D ]t  \  }}|j                         r'|t        |j                        j
                  d   gz  }=|t        j                  |j                  |      gz  }|||<   ||j                  z  }v |}| j                  || j                  | j                               \  }}	t        j                  di}
t        j                  di}t        j                  |g|||
|| j                        }|dz   | j                  f}|j!                  |      j#                         ddddf   }g }d}| j                  D ]k  }|j                         r9|j                  |d   f}|dk(  r|dgz  }0|t%        j&                  |d      gz  }L||j                  z   }||||ddf   gz  }|}m |S )a+  Gives the (sub/super)gradient of the atom w.r.t. each argument.

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

        Args:
            values: A list of numeric values for the arguments.

        Returns:
            A list of SciPy CSC sparse matrices or None.
        r      N)rI   rI   float64)dtype)rB   r   is_constantr   valuecanonical_formlu
create_varshapesizegraph_implementationget_dataloCONSTANT_IDr   get_problem_matrixreshapetocscsp
coo_matrix)r   values	fake_argsvar_offsetsoffsetr0   r   
var_length	fake_expr_param_to_sizeparam_to_col	canon_matrR   stacked_grad	grad_liststart
grad_shapestops                      r   _gradzAffAtom._grado   s    	!$)), 	#HC hsyy1@@CDD	bmmCIIs;<<	#)C #(("	# 
00DJJ15B	1+*"55KII
	 a+ ((/557Q?	99 
	C !hha1
'!$I"--
)"L!MMIsxx'l5:q=9::	
	 r"   N)__name__
__module____qualname____doc___allow_complexr   boolr   r   r%   r*   r-   r1   r3   r6   r9   r<   r?   perfcompute_oncerC   rD   r   r   rl   r)   r"   r   r   r      s    1N<dDj 1 <
7 7:D : 
 
d d <d <BD B7 76 6 
   
  7tCy 7r"   r   )rp   typingr   r   r   scipy.sparsesparser[   cvxpy.lin_ops.lin_oplin_opslin_oprV   cvxpy.lin_ops.lin_utils	lin_utilsrP   cvxpy.utilities	utilitiesr   cvxpy.atoms.atomr   cvxpy.cvxcore.pythonr   cvxpy.expressions.constantsr   r	   rs   r   r)   r"   r   <module>r      s>    $ #  ! ! $ $  ! / 0 5Id Ir"   