
    bi?                         d Z ddlZddlmZmZmZ erddlmZ ddl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 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.
    N)TYPE_CHECKINGListTuple)
Constraint)	interface)	utilities)cvxtypes)Constant)
Expression)performance_utils)unique_listc                      e Zd ZdZdZd/dZdefdZd/dZe	j                  deedf   fd	       Zedeedf   fd
       Ze	j                  deeef   fd       Zej&                  defd       Zej&                  defd       Zej&                  defd       Zej&                  defd       Ze	j                  defd       Ze	j                  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!ej&                  defd       Z"ej&                  defd       Z#d0defdZ$ej&                  defd       Z%ej&                  defd       Z&ej&                  de'e   fd       Z(ej&                  defd        Z)ej&                  defd!       Z*ej&                  defd"       Z+d# Z,	 d1d$eedf   dee-j\                  e'd%   f   fd&Z/ed'        Z0d( Z1ed)        Z2e	j                  d*        Z3ede'd%   fd+       Z4de'd%   fd,Z5e6d-        Z7de'd    fd.Z8y)2Atomz  Abstract base class for atoms. FreturnNc                    t        j                         | _        t        |      dk(  r"t	        d| j
                  j                  z        |D cg c]  }t        j                  |       c}| _	        | j                          | j                         | _        t        j                  s$t        | j                        dkD  rt        d      y y c c}w )Nr   zNo arguments given to %s.   zAtoms must be at most 2D.)luget_ididlen	TypeError	__class____name__r   cast_to_constargsvalidate_argumentsshape_from_args_shapesALLOW_ND_EXPR
ValueError)selfr   args      K/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/atom.py__init__zAtom.__init__)   s    ))+t9>+dnn.E.EE  9==T'',=	!**,3t{{#3a#7899 $8 >s   Cc           	      0   | j                         g }n&| j                         D cg c]  }t        |       }}| j                  j                  ddj	                  | j
                  D cg c]  }|j                          c}|z         dS c c}w c c}w )z@Returns the string representation of the function call.
        (z, ))get_datastrr   r   joinr   name)r"   dataelemr#   s       r$   r,   z	Atom.name7   sy     ==?"D*.--/:$CI:D:>>2299DII%FSchhj%F%MNP 	P ;%Fs   B*Bc                     | j                   s?t        d | j                  D              r"t        d| j                  j
                  z        yy)z6Raises an error if the arguments are invalid.
        c              3   <   K   | ]  }|j                           y wN)
is_complex.0r#   s     r$   	<genexpr>z*Atom.validate_arguments.<locals>.<genexpr>D   s     *Q3>>+;*Q   z"Arguments to %s cannot be complex.N)_allow_complexanyr   r!   r   r   r"   s    r$   r   zAtom.validate_argumentsA   sF     ""s*Qtyy*Q'Q4t~~7N7NN  (R"    .c                     t               )z-Returns the shape of the expression.
        NotImplementedErrorr9   s    r$   r   zAtom.shape_from_argsI        "##r:   c                     | j                   S r1   )r   r9   s    r$   shapez
Atom.shapeO   s    {{r:   c                     t               )zCReturns sign (is positive, is negative) of the expression.
        r<   r9   s    r$   sign_from_argszAtom.sign_from_argsS   r>   r:   c                 (    | j                         d   S )z'Is the expression nonnegative?
        r   rB   r9   s    r$   	is_nonnegzAtom.is_nonnegY        ""$Q''r:   c                 (    | j                         d   S )z'Is the expression nonpositive?
           rD   r9   s    r$   	is_nonposzAtom.is_nonpos_   rF   r:   c                      y)z%Is the expression imaginary?
        F r9   s    r$   is_imagzAtom.is_image       
 r:   c                      y)z*Is the expression complex valued?
        FrK   r9   s    r$   r2   zAtom.is_complexl   rM   r:   c                     t               )zIs the atom convex?
        r<   r9   s    r$   is_atom_convexzAtom.is_atom_convexs   r>   r:   c                     t               )zIs the atom concave?
        r<   r9   s    r$   is_atom_concavezAtom.is_atom_concavey   r>   r:   c                 F    | j                         xr | j                         S )zIs the atom affine?
        )rR   rP   r9   s    r$   is_atom_affinezAtom.is_atom_affine   s!     ##%?$*=*=*??r:   c                      y)z$Is the atom log-log convex?
        FrK   r9   s    r$   is_atom_log_log_convexzAtom.is_atom_log_log_convex        r:   c                      y)z%Is the atom log-log concave?
        FrK   r9   s    r$   is_atom_log_log_concavezAtom.is_atom_log_log_concave   rW   r:   c                 "    | j                         S )z!Is the atom quasiconvex?
        )rP   r9   s    r$   is_atom_quasiconvexzAtom.is_atom_quasiconvex   s     ""$$r:   c                 "    | j                         S )z"Is the atom quasiconcave?
        )rR   r9   s    r$   is_atom_quasiconcavezAtom.is_atom_quasiconcave   s     ##%%r:   c                 F    | j                         xr | j                         S )z$Is the atom log-log affine?
        )rY   rV   r9   s    r$   is_atom_log_log_affinezAtom.is_atom_log_log_affine   s!     ++-O$2M2M2OOr:   c                     t               )z;Is the composition non-decreasing in argument idx?
        r<   r"   idxs     r$   is_incrzAtom.is_incr   r>   r:   c                     t               )z;Is the composition non-increasing in argument idx?
        r<   ra   s     r$   is_decrzAtom.is_decr   r>   r:   c                 .   | j                         ry| j                         rtt        | j                        D ][  \  }}|j	                         r|j                         r| j                  |      r9|j                         r| j                  |      r[ y yy)z"Is the expression convex?
        TF)	is_constantrP   	enumerater   	is_affine	is_convexrc   
is_concavere   r"   rb   r#   s      r$   rj   zAtom.is_convex   sx    
   "%dii0 !ST\\#->)dll3.? 	!
 r:   c                 .   | j                         ry| j                         rtt        | j                        D ][  \  }}|j	                         r|j                         r| j                  |      r9|j                         r| j                  |      r[ y yy)z#Is the expression concave?
        TF)	rg   rR   rh   r   ri   rk   rc   rj   re   rl   s      r$   rk   zAtom.is_concave   sx    
 !!#%dii0 !S)dll3.?T\\#-> 	!
 r:   c                     |j                         dk(  r| j                  d      S |j                         dk(  r| j                  d      S t        d|      )zBThe expression is a disciplined parameterized expression.
        dcpT)dppdgpzUnsupported context )loweris_dcpis_dgpr!   )r"   contexts     r$   is_dppzAtom.is_dpp   sP     ==?e#;;4;((]]_%;;4;((3W==r:   c                 .   | j                         ry| j                         rtt        | j                        D ][  \  }}|j	                         r|j                         r| j                  |      r9|j                         r| j                  |      r[ y yy)z*Is the expression log-log convex?
        TF)	is_log_log_constantrV   rh   r   is_log_log_affineis_log_log_convexrc   is_log_log_concavere   rl   s      r$   rz   zAtom.is_log_log_convex   s    
 ##%((*%dii0 !S--/..0T\\#5F//1dll36G 	!
 r:   c                 .   | j                         ry| j                         rtt        | j                        D ][  \  }}|j	                         r|j                         r| j                  |      r9|j                         r| j                  |      r[ y yy)z+Is the expression log-log concave?
        TF)	rx   rY   rh   r   ry   r{   rc   rz   re   rl   s      r$   r{   zAtom.is_log_log_concave   s    
 ##%))+%dii0 !S--///1dll36G..0T\\#5F 	!
 r:   c                     t        | j                        D cg c]  \  }}|j                         r| c}}S c c}}w r1   )rh   r   rg   )r"   ir#   s      r$   _non_const_idxzAtom._non_const_idx   s,     )$)) 4NfaCOO<MNNNs   ::c                     | j                         }| j                         xr0 t        |      dk(  xr  | j                  |d      j                         S )NrH   r   )r   	is_scalarr   r   )r"   	non_consts     r$   _is_realzAtom._is_real   sO     '')	  4S^q%8 4		)A,'113	5r:   c                    ddl m} | j                         ryt        |       t	        j
                         |fv rt        d | j                  D              S | j                         }| j                         r4| j                  |d         r | j                  |d      j                         S | j                         r4| j                  |d         r | j                  |d      j                         S | j                         rtt        | j                        D ][  \  }}|j!                         r|j                         r| j                  |      r9|j#                         r| j                  |      r[ y yy)z'Is the expression quaisconvex?
        r   )maxTc              3   <   K   | ]  }|j                           y wr1   )is_quasiconvexr3   s     r$   r5   z&Atom.is_quasiconvex.<locals>.<genexpr>  s     As))+Ar6   F)cvxpy.atoms.maxr   rj   typer	   maximumallr   r   r   rc   r   re   is_quasiconcaver[   rh   ri   rk   )r"   max_atomr   rb   r#   s        r$   r   zAtom.is_quasiconvex  s     	4 >>:(**,h77AtyyAAA'')	==?t||IaL999Yq\*99;;==?t||IaL999Yq\*::<<##%%dii0 !ST\\#->)dll3.? 	!
 r:   c                    ddl m} | j                         ryt        |       t	        j
                         |fv rt        d | j                  D              S | j                         }| j                         r4| j                  |d         r | j                  |d      j                         S | j                         r4| j                  |d         r | j                  |d      j                         S | j                         rtt        | j                        D ][  \  }}|j!                         r|j                         r| j                  |      r9|j#                         r| j                  |      r[ y yy)z(Is the expression quasiconcave?
        r   )minTc              3   <   K   | ]  }|j                           y wr1   )r   r3   s     r$   r5   z'Atom.is_quasiconcave.<locals>.<genexpr>(  s     Bs**,Br6   F)cvxpy.atoms.minr   rk   r   r	   minimumr   r   r   r   rc   r   re   r   r]   rh   ri   rj   )r"   min_atomr   rb   r#   s        r$   r   zAtom.is_quasiconcave  s     	4 ??:(**,h77B		BBB'')	==?t||IaL999Yq\*::<<==?t||IaL999Yq\*99;;$$&%dii0 !S)dll3.?T\\#-> 	!
 r:   c                 b   | j                         r/| j                         st        | j                        j                  S g }g }| j
                  D ]'  }|j                  \  }}|j                  |       ||z  }) | j                         }| j                  || j                  |      \  }}|||z   fS )zIRepresent the atom as an affine objective and conic constraints.
        )
rg   
parametersr
   valuecanonical_formr   appendr)   graph_implementationr@   )	r"   arg_objsconstraintsr#   objconstrr-   	graph_objgraph_constrs	            r$   canonicalizezAtom.canonicalize7  s     doo&7DJJ'666HKyy &!00V$v%&
 ==?D&*&?&?@D

@D'F#I| kL888r:   r@   r   c                     t               )a  Reduces the atom to an affine expression and list of constraints.

        Parameters
        ----------
        arg_objs : list
            LinExpr for each argument.
        shape : tuple
            The shape of the resulting expression.
        data :
            Additional data required by the atom.

        Returns
        -------
        tuple
            (LinOp for objective, list of constraints)
        r<   )r"   r   r@   r-   s       r$   r   zAtom.graph_implementationL  s    & "##r:   c                     t        | j                         D cg c]  }|j                  d u  c}      ry | j                         S c c}w r1   )r8   r   r   _value_impl)r"   ps     r$   r   z
Atom.valuea  s;    ):;A4;<!! <s   Ac                    d| j                   v rt        j                  g       }|S g }| j                  D ]7  }|j	                         }|| j                         s y |j                  |       9 | j                  |      }|S )Nr   )r@   nparrayr   r   rg   r   numeric)r"   result
arg_valuesr#   arg_vals        r$   r   zAtom._value_implg  s    

?XXb\F  Jyy 
/ //+?4+;+;+=%%g.
/ \\*-Fr:   c                    | j                         rt        j                  j                  |       S g }| j                  D ]J  }|j
                  !t        j                  j                  |       c S |j                  |j
                         L | j                  |      }i }t        | j                        D ]  \  }}|j                  }|D ]  }||   ||   d||<   t        j                  ||         st        j                  ||         r||   ||   z  }n||   ||   z  }t        j                  |      s|j                  dk(  r|d   }||v r||xx   |z  cc<   |||<     |S )a-  Gives the (sub/super)gradient of the expression w.r.t. each variable.

        Matrix expressions are vectorized, so the gradient is a matrix.
        None indicates variable values unknown or outside domain.

        Returns:
            A map of variable to SciPy CSC sparse matrix or None.
        N)rH   rH   )r   r   )rg   ugradconstant_gradr   r   
error_gradr   _gradrh   r   isscalarr@   )	r"   r   r#   	grad_selfr   rb   grad_argkeyDs	            r$   r   z	Atom.grad{  s^    66''-- 
99 	-Cyy vv((..!!#)),		- JJz*	!$)), 	(HC xxH (C=(IcN,B"&F3K{{8C=1R[[35P$SMIcN:$SMIcN:;;q>agg.?dGf}sq(&'s!(		(, r:   c                     t               )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<   )r"   valuess     r$   r   z
Atom._grad  s     "##r:   c                     | j                         | j                  D cg c]  }|j                  D ]  }|  c}}z   S c c}}w )znA list of constraints describing the closure of the region
           where the expression is finite.
        )_domainr   domain)r"   r#   cons      r$   r   zAtom.domain  s7    
 ||~		 PSZZ Pc P PPP Ps   ?c                     g S )z?Returns constraints describing the domain of the atom.
        rK   r9   s    r$   r   zAtom._domain  s	     	r:   c                       fd}|S )zWraps an atom's numeric function that requires numpy ndarrays as input.
           Ensures both inputs and outputs are the correct matrix types.
        c                     t         j                  }|D cg c]  }|j                  |d       }} | |      }t         j                  j                  |      S c c}w )NT)convert_scalars)intfDEFAULT_INTFconst_to_matrix)r"   r   r   vr   numeric_funcs        r$   new_numericz'Atom.numpy_numeric.<locals>.new_numeric  sc    ))I%'  //4/H 'F '!$/F$$44V<<'s   ArK   )r   r   s   ` r$   numpy_numericzAtom.numpy_numeric  s    	= r:   c                 ~    g }| j                   D ]  }||j                         z  } t        |t        |       gz         S )zHA list of the atom types present amongst this atom's arguments.
        )r   atomsr   r   )r"   	atom_listr#   s      r$   r   z
Atom.atoms  sB     	99 	%C$I	%9T
|344r:   )r   N)ro   r1   )9r   
__module____qualname____doc__r7   r%   r*   r,   r   abcabstractmethodr   intr   propertyr@   boolrB   perfcompute_oncerE   rI   rL   r2   rP   rR   rT   rV   rY   r[   r]   r_   rc   re   rj   rk   rv   rz   r{   r   r   r   r   r   r   loLinOpr   r   r   r   r   r   r   staticmethodr   r   rK   r:   r$   r   r   $   s   *N:Pc P 	$sCx $ $
 uS#X   	$dDj 1 $ $
 
(4 ( (
 
(4 ( (
 
   
D   	$ $ $
 	$ $ $
@ @
 
 
%T %
&d &
P P
 	$d $ $
 	$d $ $
 
4    
D   >t > 
4    
D    
OS	 O O 
5$ 5 5 
  0 
  09, 6:$$S#X$	rxxl++	,$* " "
( / /b 	$ $ Q\* Q Ql+   5tF| 5r:   r   )%r   r   typingr   r   r   cvxpy.constraints.constraintr   numpyr   cvxpy.lin_ops.lin_oplin_opslin_opr   cvxpy.lin_ops.lin_utils	lin_utilsr   cvxpy.settingssettingsr   cvxpyr   r   r   r   cvxpy.expressionsr	   cvxpy.expressions.constantsr
   cvxpy.expressions.expressionr   cvxpy.utilitiesr   r   cvxpy.utilities.deterministicr   r   rK   r:   r$   <module>r      sP     - -7  ! ! $ $  #   & 0 3 5 5x5: x5r:   