
    bi|                     r    d Z ddlmZ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  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)AffAtom)
Constraintc                        e Zd ZdZd fdZdeeef   fdZej                  d        Z
ddZdeed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deedf   deej&                  ee   f   fdZ xZS )tracezThe sum of the diagonal entries of a matrix.

    Parameters
    ----------
    expr : Expression
        The expression to sum the diagonal of.
    returnc                 ,    t         t        |   |       y N)superr   __init__)selfexpr	__class__s     S/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/affine/trace.pyr   ztrace.__init__#   s    eT#D)    c                     | j                   d   j                         xs | j                   d   j                         }| j                   d   j                         xs | j                   d   j	                         }||fS )zfTrace is nonneg (nonpos) if its argument is elementwise nonneg
        (nonpos) or psd (nsd).
        r   )args	is_nonnegis_psd	is_nonposis_nsd)r   r   r   s      r   sign_from_argsztrace.sign_from_args&   sg     IIaL**,E		!0C0C0E	IIaL**,E		!0C0C0E	)##r   c                 2    t        j                  |d         S )z#Sums the diagonal entries.
        r   )npr   )r   valuess     r   numericztrace.numeric/   s     xxq	""r   c                     | j                   d   j                  }| j                   d   j                  dk7  s|d   |d   k7  rt        d      y)z5Checks that the argument is a square matrix.
        r         z-Argument to trace must be a 2-d square array.N)r   shapendim
ValueError)r   r!   s     r   validate_argumentsztrace.validate_arguments5   sM     		!""99Q<!U1Xq%9LMM &:r   .c                     t               S )zAlways scalar.
        )tupler   s    r   shape_from_argsztrace.shape_from_args<   s     wr   c                 z    | j                   d   j                         xs | j                   d   j                         S )Nr   )r   is_realis_hermitianr'   s    r   r*   ztrace.is_realA   s/    yy|##%D1)B)B)DDr   c                 $    | j                          S r   )r*   r'   s    r   
is_complexztrace.is_complexD   s    <<>!!r   c                      y)z$Is the atom log-log convex?
        T r'   s    r   is_atom_log_log_convexztrace.is_atom_log_log_convexG   s     r   c                      y)z%Is the atom log-log concave?
        Fr/   r'   s    r   is_atom_log_log_concaveztrace.is_atom_log_log_concaveL   s     r   r!   c                 6    t        j                  |d         g fS )a  Sum the diagonal entries of the linear expression.

        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   )lur   )r   arg_objsr!   datas       r   graph_implementationztrace.graph_implementationQ   s    & !%r**r   )r	   Nr   )__name__
__module____qualname____doc__r   r   boolr   r   numpy_numericr   r$   intr(   r*   r-   r0   r2   loLinOpr   r   r7   __classcell__)r   s   @r   r   r      s    *$dDj 1 $ # #
NsCx 
E E"D " 
  6:+$S#X+	rxxj))	*+r   r   )r;   typingr   r   numpyr   cvxpy.lin_ops.lin_oplin_opslin_opr?   cvxpy.lin_ops.lin_utils	lin_utilsr4   cvxpy.atoms.affine.affine_atomr   cvxpy.constraints.constraintr   r   r/   r   r   <module>rK      s2      ! ! $ $ 2 3J+G J+r   