
    bi                     f    d Z ddlmZm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  G d d	e      Zy)
a0  
Copyright 2022, the CVXPY authors

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)linalg)entr)Atom)
Constraintc                        e Zd ZdZddeedf   ddf fdZd ZddZdee	e	f   fd	Z
de	f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 Zd Zdee   fdZ xZS )von_neumann_entraE  
    Represents the von Neumann Entropy of the positive-definite matrix :math:`X,`

        .. math::
            -\operatorname{tr}(X \log X).

    Mathematically, this is equivalent to

        .. math::
            \texttt{von_neumann_entr}(X) = -\textstyle\sum_{i=1}^n \lambda_i \log \lambda_i

    where :math:`\lambda_{i}` are the eigenvalues of :math:`X.`

    Parameters
    ----------
    X : Expression or numeric
        A PSD matrix

    quad_approx : Tuple[int,...]
        This is either an empty tuple (default) or a 2-tuple. If a 2-tuple, then this atom
        is approximately canonicalized. The approximations replace ExpCone constraints with SOC
        constraints based on a quadrature scheme from https://arxiv.org/abs/1705.00812;
        quad_approx[0] is the number of quadrature nodes and quad_approx[1] is the number of
        scaling points in the quadrature scheme.

    Notes
    -----
    This function does not assume :math:`\operatorname{tr}(X)=1,` which would be required
    for most uses of this function in the context of quantum information theory. 
    quad_approx.returnNc                 :    || _         t        t        |   |       y N)r   superr
   __init__)selfXr   	__class__s      W/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/von_neumann_entr.pyr   zvon_neumann_entr.__init__:   s    &.q1    c                     |d   }t        |d      r|j                  }t        j                  |      }t	        j
                  t        |            }|S )Nr   value)hasattrr   LAeigvalshnpsumr   )r   valuesNwvals        r   numericzvon_neumann_entr.numeric>   sA    1I1gAKKNffT!Wo
r   c                     | j                   d   j                         s*t        d| j                   d   j                          d      y)z&Verify that the argument is Hermitian.r   z
                The argument z to von_neumann_entr must be a Hermitian matrix.
                If you know for a fact that the input is Hermitian, wrap it with the hermitian_wrap
                atom before calling von_neumann_entr.
                N)argsis_hermitian
ValueErrornamer   s    r   validate_argumentsz#von_neumann_entr.validate_argumentsF   sN    yy|((*"iil//12 3  +r   c                      y)zCReturns sign (is positive, is negative) of the expression.
        )FF r'   s    r   sign_from_argszvon_neumann_entr.sign_from_argsQ   s     r   c                      y)zIs the atom convex?
        Fr*   r'   s    r   is_atom_convexzvon_neumann_entr.is_atom_convexV        r   c                     t               S )z-Returns the shape of the expression.
        )tupler'   s    r   shape_from_argsz von_neumann_entr.shape_from_args[   s     wr   c                      y)zIs the atom concave?
        Tr*   r'   s    r   is_atom_concavez von_neumann_entr.is_atom_concave`   s     r   c                      y)z;Is the composition non-decreasing in argument idx?
        Fr*   r   idxs     r   is_incrzvon_neumann_entr.is_incre   r.   r   c                      y)z;Is the composition non-increasing in argument idx?
        Fr*   r5   s     r   is_decrzvon_neumann_entr.is_decrj   r.   r   c                     | j                   gS r   )r   r'   s    r   get_datazvon_neumann_entr.get_datao   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.
        )NotImplementedError)r   r   s     r   _gradzvon_neumann_entr._gradr   s      "##r   c                 (    | j                   d   dz	  gS )z?Returns constraints describing the domain of the node.
        r   )r#   r'   s    r   _domainzvon_neumann_entr._domain   s     		!!""r   )r*   )r   N)__name__
__module____qualname____doc__r   intr   r!   r(   boolr+   r-   r1   r3   r7   r9   r;   r>   r   r   r@   __classcell__)r   s   @r   r
   r
      s    >2uS#X 2 2	dDj 1 
 
sCx 
 
d 
d 
"$$#j) #r   r
   )rD   typingr   r   numpyr   scipyr   r   scipy.specialr   cvxpy.atoms.atomr   cvxpy.constraints.constraintr   r
   r*   r   r   <module>rN      s,        ! 3m#t m#r   