
    bi                         d Z ddlm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 ee      d        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.
    )wraps)ListTupleN)linalg)Atom)QuadForm)
Constraintc                        e Zd ZdZdZd fdZd Zdee   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efdZdefdZdefdZ xZS )
MatrixFracz tr X.T*P^-1*X Treturnc                 .    t         t        |   ||       y )N)superr   __init__)selfXP	__class__s      R/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/matrix_frac.pyr   zMatrixFrac.__init__!   s    j$(A.    c                    |d   }|d   }| j                   d   j                         rQt        j                  |      j                  j                  t        j                  |            j                  |      }n=|j                  j                  t        j                  |            j                  |      }t        |j                        dk(  r|j                         S |S )zReturns tr X.T*P^-1*X.
        r         )args
is_complexnpconjTdotLAinvlenshapetrace)r   valuesr   r   products        r   numericzMatrixFrac.numeric$   s     1I1I99Q<""$ggajll&&rvvay155a8GccggbffQi(,,Q/G"%gmm"4"9w}}FwFr   c                 (    | j                   d   dz	  gS )z?Returns constraints describing the domain of the node.
        r   r   )r   r   s    r   _domainzMatrixFrac._domain0   s     		!!""r   c                    t        j                  |d         }|j                  dk(  r	|dddf   }t        j                  |d         }	 t        j                  |      }t        j
                  |t        j                  |      z   |      }|j                  j                  d      }t        j                  |g      j                  }t        j
                  ||      }t        j
                  ||j                        }t        j
                  ||      }|j                   }t        j                  |j                  j                  d      g      j                  }||gS # t        j                  $ r ddgcY S w xY w)a4  
        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   NF)order)r   arrayndimr   r    r   	transposer   ravelsp	csc_arrayLinAlgError)r   r$   r   r   P_invDXDPs          r   _gradzMatrixFrac._grad5   s(    HHVAY66Q;!T'
AHHVAY	FF1IE bll51115B#&Brd#%%Bq!BACCBE"B$$Brttzzz45688B8O ~~ 	 $<	 s   
E E87E8c                    | j                   d   }| j                   d   }|j                  dk7  s|j                  d   |j                  d   k7  rt        d      |j                  d   |j                  d   k7  rt        d      y)z5Checks that the dimensions of x and P match.
        r   r   r   z;The second argument to matrix_frac must be a square matrix.z:The arguments to matrix_frac have incompatible dimensions.N)r   r.   r"   
ValueError)r   r   r   s      r   validate_argumentszMatrixFrac.validate_argumentsW   s     IIaLIIaL66Q;!''!*
2M  WWQZ1771:%L  &r   .c                     t               S )z8Returns the (row, col) shape of the expression.
        )tupler(   s    r   shape_from_argszMatrixFrac.shape_from_argse   s     wr   c                      y)zCReturns sign (is positive, is negative) of the expression.
        )TF r(   s    r   sign_from_argszMatrixFrac.sign_from_argsj   s     r   c                      y)zIs the atom convex?
        Tr?   r(   s    r   is_atom_convexzMatrixFrac.is_atom_convexo   s     r   c                      y)zIs the atom concave?
        Fr?   r(   s    r   is_atom_concavezMatrixFrac.is_atom_concavet        r   c                      y)z;Is the composition non-decreasing in argument idx?
        Fr?   r   idxs     r   is_incrzMatrixFrac.is_incry   rE   r   c                      y)z;Is the composition non-increasing in argument idx?
        Fr?   rG   s     r   is_decrzMatrixFrac.is_decr~   rE   r   c                 z    | j                   d   j                         xr | j                   d   j                         S )z4Quadratic if x is affine and P is constant.
        r   r   )r   	is_affineis_constantr(   s    r   is_quadraticzMatrixFrac.is_quadratic   s1     yy|%%'FDIIaL,D,D,FFr   c                 <    | j                   d   j                         S )z)Quadratic term if P is constant.
        r   )r   rN   r(   s    r   has_quadratic_termzMatrixFrac.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 P is constant.
        r   r   )r   is_pwlrN   r(   s    r   is_qpwazMatrixFrac.is_qpwa   s1     yy|""$C1)A)A)CCr   )r   N)__name__
__module____qualname____doc___allow_complexr   r&   r   r	   r)   r7   r:   r   intr=   boolr@   rB   rD   rI   rK   rO   rQ   rT   __classcell__)r   s   @r   r   r      s    N/
G#j) #
 DsCx 
dDj 1 
 
 
d 
d 
Gd G
*D *
D Dr   r   c                     t        |t        j                        rDt        j                  |      }t        | |t        j                  |      j                  z   dz        S t        | |      S )Ng       @)	
isinstancer   ndarrayr   r    r   r   r   r   )r   r   invPs      r   matrix_fracra      sM    !RZZ vvayD2774=??2c9::!Qr   )rX   	functoolsr   typingr   r   numpyr   scipy.sparsesparser1   r   r   cvxpy.atoms.atomr   cvxpy.atoms.quad_formr   cvxpy.constraints.constraintr	   r   ra   r?   r   r   <module>rj      sN          ! * 3sD sDl z   r   