
    biN                     r    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 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)Atom)
Constraintc                        e Zd ZdZd fdZd Zd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 Zdee   fdZedefd       Z xZS )tr_invz\
    :math:`\mathrm{tr}\left(X^{-1} \right),`
    where :math:`X` is positive definite.
    returnc                 ,    t         t        |   |       y )N)superr	   __init__)selfX	__class__s     M/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/tr_inv.pyr   ztr_inv.__init__"   s    fd$Q'    c                 Z   t        j                  |d   |d   j                  j                         z
        dk\  rt        j
                  S |d   |d   j                  z   dz  }t        j                  |      }t        |      dk  rt        j
                  S t	        j                  |dz        S )zReturns the trinv of positive definite matrix X.

        For positive definite matrix X, this is the trace of inverse of X.
        r   g:0yE>   )	LAnormTconjnpinfeigvalshminsum)r   valuessymmeigVals       r   numericztr_inv.numeric%   s     GGF1Iq	 0 0 223t;66Mq	F1IKK'*T"v;!66Mvvfbj!!r   c                    | j                   d   j                  dk(  r9| j                   d   j                  d   | j                   d   j                  d   k7  r*t        d| j                   d   j	                          d      y)z,Verify that the argument is a square matrix.r   r      zThe argument z& to tr_inv must be a 2-d square array.N)argsndimshape
ValueErrornamer   s    r   validate_argumentsztr_inv.validate_arguments4   sx    yy|  A%1););A)>$))A,BTBTUVBW)W		! 1 1 344Z[  *Xr   .c                     t               S )z8Returns the (row, col) shape of the expression.
        )tupler*   s    r   shape_from_argsztr_inv.shape_from_args;   s     wr   c                      y)zCReturns sign (is positive, is negative) of the expression.
        )TF r*   s    r   sign_from_argsztr_inv.sign_from_args@   s     r   c                      y)zIs the atom convex?
        Tr0   r*   s    r   is_atom_convexztr_inv.is_atom_convexE   s     r   c                      y)zIs the atom concave?
        Fr0   r*   s    r   is_atom_concaveztr_inv.is_atom_concaveJ        r   c                      y)z;Is the composition non-decreasing in argument idx?
        Fr0   r   idxs     r   is_incrztr_inv.is_incrO   r6   r   c                      y)z;Is the composition non-increasing in argument idx?
        Fr0   r8   s     r   is_decrztr_inv.is_decrT   r6   r   c                 *   |d   }t        j                  |      }t        j                  |      dkD  r_t        j                  j                  |      j                  }| |z  }t        j                  |j                  d            j                  gS d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   F)orderN)
r   r   r   r   r   invr   sp	csc_arrayravel)r   r   r   	eigen_valDs        r   _gradztr_inv._gradY   sz     1IKKN	66)q 		a ""AaALLs!346677 6Mr   c                 (    | j                   d   dz	  gS )z?Returns constraints describing the domain of the node.
        r   )r%   r*   s    r   _domainztr_inv._domaino   s     		!!""r   c                    t        j                  | j                  d   j                  | j                  d   j                  j                  j                         t        j                  t        j                        st        d      t        | j                         D cg c]  }|j                  d u  c}      ry | j                         S c c}w )Nr   )rtolatolz2Input array to tr_inv was not Hermitian/symmetric.)r   allcloser%   valuer   r   sATOM_EVAL_TOLr(   any
parameters_value_impl)r   ps     r   rM   ztr_inv.valuet   s    {{499Q<--99Q<--//446 ! !1 QRR):;A4;<!! <s   C)r
   N)__name__
__module____qualname____doc__r   r"   r+   r   intr.   boolr1   r3   r5   r:   r<   rF   r   r   rH   propertyfloatrM   __classcell__)r   s   @r   r	   r	      s    
("sCx 
dDj 1 
 
 
d 
d 
,#j) #
 "u " "r   r	   )rW   typingr   r   numpyr   scipy.sparsesparserA   r   r   cvxpy.settingssettingsrN   cvxpy.atoms.atomr   cvxpy.constraints.constraintr   r	   r0   r   r   <module>re      s/          ! 3a"T a"r   