
    bi                     F    d Z ddlmZ ddlZddl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.
    )TupleN)Elementwisec                        e Zd ZdZddeddf fdZ fdZej                  de	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 Zd fdZd Z xZS )hubera  The Huber function

    .. math::

        \operatorname{Huber}(x, M) =
            \begin{cases}
                2M|x|-M^2 & \text{for } |x| \geq |M| \\
                      |x|^2 & \text{for } |x| \leq |M|.
            \end{cases}

    :math:`M` defaults to 1.

    Parameters
    ----------
    x : Expression
        The expression to which the huber function will be applied.
    M : Constant or Parameter
        A scalar constant.
    MreturnNc                 X    | j                  |      | _        t        t        |   |       y )N)cast_to_constr   superr   __init__)selfxr   	__class__s      X/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/elementwise/huber.pyr   zhuber.__init__0   s$    ##A&eT#A&    c                 V    t         |          | j                  j                         z   S )z9If M is a Parameter, include it in the list of Parameters)r   
parametersr   r   r   s    r   r   zhuber.parameters4   s#    w!#dff&7&7&999r   c                 v    dt         j                  j                  | j                  j                  |d         z  S )z4Returns the huber function applied elementwise to x.   r   )scipyspecialr   r   value)r   valuess     r   numericzhuber.numeric8   s,     5==&&tvv||VAY???r   c                      y)z:Returns sign (is positive, is negative) of the expression.)TF r   s    r   sign_from_argszhuber.sign_from_args=   s     r   c                      y)zIs the atom convex?Tr   r   s    r   is_atom_convexzhuber.is_atom_convexB       r   c                      y)zIs the atom concave?Fr   r   s    r   is_atom_concavezhuber.is_atom_concaveF   s    r   c                 <    | j                   |   j                         S )z2Is the composition non-decreasing in argument idx?)args	is_nonnegr   idxs     r   is_incrzhuber.is_incrJ       yy~''))r   c                 <    | j                   |   j                         S )z2Is the composition non-increasing in argument idx?)r&   	is_nonposr(   s     r   is_decrzhuber.is_decrN   r+   r   c                 <    | j                   d   j                         S )zQuadratic if x is affine.r   )r&   	is_affiner   s    r   is_quadraticzhuber.is_quadraticR   s    yy|%%''r   c                      y)z"Always generates a quadratic term.Tr   r   s    r   has_quadratic_termzhuber.has_quadratic_termV   r"   r   c                     | j                   gS )zReturns the parameter M.)r   r   s    r   get_datazhuber.get_dataZ   s    xr   c                     | j                   j                         r4| j                   j                         r| j                   j                         st	        d      t
        t        |           y)z2Checks that M >= 0 and is a constant or Parameter.z6M must be a non-negative scalar constant or Parameter.N)r   r'   	is_scalaris_constant
ValueErrorr   r   validate_argumentsr   s    r   r:   zhuber.validate_arguments^   sJ      "tvv'7'7'9dff>P>P>RUVVeT-/r   c                 Z   | j                   d   j                  }| j                  }t        j                  t        j                  |d         | j
                  j                        }dt        j                  t        j                  |d         |      z  }t        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   )r&   sizenpminimumabsr   r   multiplysignr   elemwise_grad_to_diag)r   r   rowscolsmin_val	grad_valss         r   _gradzhuber._gradd   s     yy|  yy**RVVF1I.=BGGF1I$6@@	++ItTBCCr   )   )r   N)__name__
__module____qualname____doc__intr   r   r   numpy_numericfloatr   r   boolr   r!   r$   r*   r.   r1   r3   r5   r:   rG   __classcell__)r   s   @r   r   r      s    ('S ' ': @ @ @dDj 1 
  *d **d *(d (D 0Dr   r   )
rL   typingr   numpyr=   scipy.specialr   #cvxpy.atoms.elementwise.elementwiser   r   r   r   r   <module>rV      s(        ;
XDK XDr   