
    bi                         d Z ddlZddl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 ddlmZ dd	lmZ  G d
 de      Z G d de      Z G d de      ZddefdZddefdZy)aD  
Copyright 2013 Steven Diamond, 2017 Robin Verschueren

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.
    N)Tuple)linalg)psd_wrap)Atom)
Expression)	is_sparse)sparse_choleskyc                       e Zd Zy)CvxPyDomainErrorN)__name__
__module____qualname__     P/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/atoms/quad_form.pyr   r      s    r   r   c                        e Zd ZdZd fdZd Zd 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defdZdefdZdefdZd Zdeedf   fdZ xZS )QuadFormTreturnc                 .    t         t        |   ||       y)z"Atom representing :math:`x^T P x`.N)superr   __init__)selfxP	__class__s      r   r   zQuadForm.__init__&   s    h&q!,r   c                 Z   |d   j                  |d         }| j                  d   j                         r7t        j                   t        j                  |d         j
                  |      }n,t        j                   t        j                  |d         |      }t        j                  |      S )N   r   )dotargs
is_complexnpconjT	transposereal)r   valuesprodquads       r   numericzQuadForm.numeric*   sz    ay}}VAY'99Q<""$66"''&),..5D66",,vay148Dwwt}r   c                 F   t         t        |           | j                  d   j                  d   }| j                  d   j                  d   |k7  s | j                  d   j                  |df|ffvrt        d      | j                  d   j                         st        d      y )Nr   r   .Invalid dimensions for arguments to quad_form.z4Quadratic form matrices must be symmetric/Hermitian.)r   r   validate_argumentsr   shape
ValueErroris_hermitian)r   nr   s     r   r,   zQuadForm.validate_arguments2   s    h02IIaLq!99Q<a A%1););QFQD>)QMNNyy|((*STT +r   c                 B    | j                         | j                         fS )zCReturns sign (is positive, is negative) of the expression.
        )is_atom_convexis_atom_concaver   s    r   sign_from_argszQuadForm.sign_from_args:   s!     ##%t';';'=>>r   c                 d    | j                   d   }|j                         xr |j                         S )zIs the atom convex?
        r   )r   is_constantis_psdr   r   s     r   r2   zQuadForm.is_atom_convex?   (     IIaL}}-188:-r   c                 d    | j                   d   }|j                         xr |j                         S )zIs the atom concave?
        r   )r   r7   is_nsdr9   s     r   r3   zQuadForm.is_atom_concaveE   r:   r   c                      y)z$Is the atom log-log convex?
        Tr   r4   s    r   is_atom_log_log_convexzQuadForm.is_atom_log_log_convexK        r   c                      y)z%Is the atom log-log concave?
        Fr   r4   s    r   is_atom_log_log_concavez QuadForm.is_atom_log_log_concaveP        r   c                     | j                   d   j                         xr | j                   d   j                         xs< | j                   d   j                         xr | j                   d   j                         S )z;Is the composition non-decreasing in argument idx?
        r   r   r   	is_nonneg	is_nonposr   idxs     r   is_incrzQuadForm.is_incrU   d     		!&&(ETYYq\-C-C-E G		!&&(ETYYq\-C-C-E	Gr   c                     | j                   d   j                         xr | j                   d   j                         xs< | j                   d   j                         xr | j                   d   j                         S )z;Is the composition non-increasing in argument idx?
        r   r   rD   rG   s     r   is_decrzQuadForm.is_decr[   rJ   r   c                      y)zIs the atom quadratic?
        Tr   r4   s    r   is_quadraticzQuadForm.is_quadratica   r?   r   c                      y)z!Always a quadratic term.
        Tr   r4   s    r   has_quadratic_termzQuadForm.has_quadratic_termf   r?   r   c                      y)z&Is the atom piecewise linear?
        Fr   r4   s    r   is_pwlzQuadForm.is_pwlk   rB   r   c                 t    | j                   j                  d| j                  d   d| j                  d   dS )N(r   z, r   ))r   r   r   r4   s    r   namezQuadForm.namep   s/    #~~66#yy|#yy|- 	-r   c                    t        j                  |d         }t        j                  |d         }|t        j                  |j                        z   |z  }t	        j
                  |j                  d      g      j                  gS )Nr   r   F)order)r!   arrayr"   r#   sp	csc_arrayravel)r   r&   r   r   Ds        r   _gradzQuadForm._gradu   sg    HHVAYHHVAY"aggCg0124455r   .c                     t               S N)tupler4   s    r   shape_from_argszQuadForm.shape_from_args{   s	    wr   r   N)r   r   r   _allow_complexr   r)   r,   r   boolr5   r2   r3   r>   rA   rI   rL   rN   rP   rR   strrV   r_   intrc   __classcell__r   s   @r   r   r   #   s    N-U?dDj 1 ?
. .. . 
 
Gd GGd Gd 
D 
 
-c -
6sCx r   r   c                        e Zd ZdZd fdZd Zd ZdefdZdefdZ	defdZ
defd	Zdeed
f   fdZdeeef   fdZdefdZ xZS )SymbolicQuadFormzM
    Symbolic form of QuadForm when quadratic matrix is not known (yet).
    r   c                 d    || _         t        t        |   ||       | j                  d   | _        y )Nr   )original_expressionr   rl   r   r   r   )r   r   r   exprr   s       r   r   zSymbolicQuadForm.__init__   s,    #' .q!41r   c                     | j                   gS ra   )rn   r4   s    r   get_datazSymbolicQuadForm.get_data   s    (())r   c                     t               ra   )NotImplementedError)r   r&   s     r   r_   zSymbolicQuadForm._grad   s    !##r   c                 6    | j                   j                         S ra   )rn   r3   r4   s    r   r3   z SymbolicQuadForm.is_atom_concave       ''7799r   c                 6    | j                   j                         S ra   )rn   r2   r4   s    r   r2   zSymbolicQuadForm.is_atom_convex       ''6688r   c                 8    | j                   j                  |      S ra   )rn   rL   rG   s     r   rL   zSymbolicQuadForm.is_decr       ''//44r   c                 8    | j                   j                  |      S ra   )rn   rI   rG   s     r   rI   zSymbolicQuadForm.is_incr   ry   r   .c                 6    | j                   j                         S ra   )rn   rc   r4   s    r   rc   z SymbolicQuadForm.shape_from_args   ru   r   c                 6    | j                   j                         S ra   )rn   r5   r4   s    r   r5   zSymbolicQuadForm.sign_from_args   rw   r   c                      y)NTr   r4   s    r   rN   zSymbolicQuadForm.is_quadratic   s    r   rd   )r   r   r   __doc__r   rq   r_   rf   r3   r2   rL   rI   r   rh   rc   r5   rN   ri   rj   s   @r   rl   rl      s|    
*$: :9 95d 55d 5:sCx :9dDj 1 9d r   rl   check_finitec                    t        |       rS	 t        |       \  }}}|dkD  rd||ddf   t        j                  d      fS dt        j                  d      |dd|f   fS t        j                  | ||      \  }}	||}|dv rN|	j                  j                  j                         }
ddd	}||
   t        j                  |
      j                  z  }t        t        j                   |            }|dk(  r|}n||z  }||kD  }|| k  }t        j"                  |      r*t        j"                  |      rt%        j&                  d
       |	dd|f   t        j(                  ||         z  }|	dd|f   t        j(                  ||          z  }|||fS # t        t
        f$ r' t        j                  | j                               } Y ew xY w)a  
    Compute a matrix decomposition.

    Compute sgn, scale, M such that P = sgn * scale * dot(M, M.T).
    The strategy of determination of eigenvalue negligibility follows
    the pinvh contributions from the scikit-learn project to scipy.

    Parameters
    ----------
    P : matrix or ndarray
        A real symmetric positive or negative (semi)definite input matrix
    cond, rcond : float, optional
        Cutoff for small eigenvalues.
        Singular values smaller than rcond * largest_eigenvalue
        are considered negligible.
        If None or -1, suitable machine precision is used (default).
    lower : bool, optional
        Whether the array data is taken from the lower or upper triangle of P.
        The default is to take it from the lower triangle.
    check_finite : bool, optional
        Whether to check that the input matrix contains only finite numbers.
        The default is True; disabling may give a performance gain
        but may result in problems (crashes, non-termination) if the inputs
        contain infinities or NaNs.

    Returns
    -------
    scale : float
        induced matrix 2-norm of P
    M1, M2 : 2d ndarray
        A rectangular ndarray such that P = scale * (dot(M1, M1.T) - dot(M2, M2.T))

    r   g      ?N)r   r   )lowerr   )Ng     @@g    .A)fdz8Forming a nonconvex expression quad_form(x, indefinite).)r   r	   r!   emptyr.   ModuleNotFoundErrorrZ   todenseLAeighdtypecharr   finfoepsmaxabsoluteanywarningswarnsqrt)r   condrcondr   r   signLpwVtfactorscalew_scaledmaskpmasknM1M2s                     r   decomp_quadr      s   D |	&(+JD!QaxAadGRXXf%555BHHV,a1g55 771E=DAqzGGLL %ay288A;??*AEzu9tOEuE	vve}PQ	
1e8rwwx/	/B	
1e8rww/0	0B"b=/ /0 	&%A	&s   2F  F 2GG
assume_PSDc                    t        t        j                  | |f      \  } }|j                  dk(  rH|j                  d   |j                  d   k7  s)t        | j                  d      d   |j                  d   k7  rt        d      | j                         r | j                  j                         |z  | z  S |j                         r|rt        |      }t        | |      S t        d      )z Alias for :math:`x^T P x`.

    Parameters
    ----------
    x : vector argument.
    P : matrix argument.
    assume_PSD : P is assumed to be PSD without checking.
       r   r   )r   r+   z8At least one argument to quad_form must be non-variable.)mapr   cast_to_constndimr-   r   	Exceptionr7   r#   	conjugater   r   )r   r   r   s      r   	quad_formr      s     z''!Q0DAq66Q;!''!*
2c!''46H6KqwwWXz6YHII}}ss}}"Q&&	
A1~F
 	
r   )NNTT)F)r~   r   typingr   numpyr!   scipy.sparsesparser[   scipyr   r   cvxpy.atoms.affine.wrapsr   cvxpy.atoms.atomr   cvxpy.expressions.expressionr    cvxpy.interface.matrix_utilitiesr   cvxpy.utilities.linalgr	   r   r   r   rl   rf   r   r   r   r   r   <module>r      sm          - ! 3 6 2	y 	Yt Yx"t "JAD AH
 
r   