
    bi                     z    d Z ddlmZmZ ddlZddl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.
    )ListTupleN)Atom)
Constraint)is_param_freec                       e Zd ZdZdZd fdZej                  d        Zde	e
   fdZd Zdeedf   f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 fdZdefdZdefdZdefdZ xZS )quad_over_linz!:math:`(sum_{ij}X^2_{ij})/y`
    Treturnc                 .    t         t        |   ||       y )N)superr	   __init__)selfxy	__class__s      T/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/quad_over_lin.pyr   zquad_over_lin.__init__"   s    mT+Aq1    c                 F   | j                   d   j                         rYt        j                  |d   j                        t        j                  |d   j
                        z   j                         |d   z  S t        j                  |d         j                         |d   z  S )z<Returns the sum of the entries of x squared over y.
        r      )args
is_complexnpsquareimagrealsum)r   valuess     r   numericzquad_over_lin.numeric%   s     99Q<""$IIfQinn-		&)..0IINNPQWXYQZZZyy#'')&)33r   c                 (    | j                   d   dk\  gS )z?Returns constraints describing the domain of the node.
        r   r   )r   r   s    r   _domainzquad_over_lin._domain-   s     		!!""r   c                    |d   }|d   }|dk  rddgS | j                   d   j                         ret        j                  |j                        t        j                  |j
                        z   j                          t        j                  |      z  }n:t        j                  |      j                          t        j                  |      z  }t        j                  |gg      }d|z  |z  }t        j                  || j                   d   j                  df      }t        j                  j                  |      }||g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   r   Ng       @)r   r   r   r   r   r   r   sp	csc_arrayreshapesizescipysparse)r   r   Xr   DyDXs         r   _gradzquad_over_lin._grad3   s     1I1I6$< yy|&&(yy(299QVV+<<AACCBIIaLPiil&&((15tf%BQqBB1!2!2A 67B''+B8Or   .c                     t               S )z8Returns the (row, col) shape of the expression.
        )tupler    s    r   shape_from_argszquad_over_lin.shape_from_argsO   s     wr   c                      y)zCReturns sign (is positive, is negative) of the expression.
        )TF r    s    r   sign_from_argszquad_over_lin.sign_from_argsT   s     r   c                 p    t         j                  j                         rt        | j                  d         S y)zIs the atom convex?
        r   T)uscopesdpp_scope_activer   r   r    s    r   is_atom_convexzquad_over_lin.is_atom_convexZ   s*     88$$& 1..r   c                      y)zIs the atom concave?
        Fr1   r    s    r   is_atom_concavezquad_over_lin.is_atom_concavec        r   c                      y)z$Is the atom log-log convex?
        Tr1   r    s    r   is_atom_log_log_convexz$quad_over_lin.is_atom_log_log_convexh   s     r   c                      y)z%Is the atom log-log concave?
        Fr1   r    s    r   is_atom_log_log_concavez%quad_over_lin.is_atom_log_log_concavem   r:   r   c                 J    |dk(  xr | j                   |   j                         S )z;Is the composition non-decreasing in argument idx?
        r   )r   	is_nonnegr   idxs     r   is_incrzquad_over_lin.is_incrr   s$     q8diin6688r   c                 X    |dk(  xr | j                   |   j                         xs |dk(  S )z;Is the composition non-increasing in argument idx?
        r   r   )r   	is_nonposrA   s     r   is_decrzquad_over_lin.is_decrw   s-     9tyy~779HsaxHr   c                     | j                   d   j                         st        d      | j                   d   j                         rt        d      t        t
        |           y)z'Check dimensions of arguments.
        r   z6The second argument to quad_over_lin must be a scalar.z7The second argument to quad_over_lin cannot be complex.N)r   	is_scalar
ValueErrorr   r   r	   validate_arguments)r   r   s    r   rJ   z quad_over_lin.validate_arguments|   sR     yy|%%'UVV99Q<""$VWWmT57r   c                 z    | j                   d   j                         xr | j                   d   j                         S )z4Quadratic if x is affine and y is constant.
        r   r   )r   	is_affineis_constantr    s    r   is_quadraticzquad_over_lin.is_quadratic   s1     yy|%%'FDIIaL,D,D,FFr   c                 <    | j                   d   j                         S )z+A quadratic term if y is constant.
        r   )r   rM   r    s    r   has_quadratic_termz quad_over_lin.has_quadratic_term   s     yy|''))r   c                 z    | j                   d   j                         xr | j                   d   j                         S )zEQuadratic of piecewise affine if x is PWL and y is constant.
        r   r   )r   is_pwlrM   r    s    r   is_qpwazquad_over_lin.is_qpwa   s1     yy|""$C1)A)A)CCr   )r
   N)__name__
__module____qualname____doc___allow_complexr   r   numpy_numericr   r   r   r!   r,   r   intr/   boolr2   r7   r9   r<   r>   rC   rF   rJ   rN   rP   rS   __classcell__)r   s   @r   r	   r	      s    N2 
4 4#j) #8sCx 
dDj 1   
 
 
9d 9
Id I
8Gd G
*D *
D Dr   r	   )rW   typingr   r   numpyr   r'   scipy.sparser(   r#   cvxpy.utilities	utilitiesr4   cvxpy.atoms.atomr   cvxpy.constraints.constraintr   %cvxpy.expressions.constants.parameterr   r	   r1   r   r   <module>re      s4          ! 3 ?uDD uDr   