
    bi*              	       ,   d Z ddlZddlZddlmZ ddlmZ	 ej                   e	j                         ej                   e	j                         ej                   e	j                         ej                    e	j                         iZeej                     Zeej                     Zeej                      Zd Zd Zd Zd Zd Zd	 Zd
efdZd Zd
efdZd
efdZd Z d Z!ddedefdZ"d Z#d Z$dde%d
efdZ&d Z'd
efdZ(d
efdZ)d ded
efdZ*d
ef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.
    N)numpy_interfacec                     t         |    S N)
INTERFACES)target_classs    [/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/cvxpy/interface/matrix_utilities.pyget_matrix_interfacer	   &   s    l##    c                  :    ddl mc mc m}  | j	                         S )z.Dynamic import of CVXOPT dense interface.
    r   N)3cvxpy.interface.cvxopt_interface.valuerix_interface	interfacecvxopt_interfacevaluerix_interfaceDenseMatrixInterface)dmis    r   get_cvxopt_dense_intfr   *   s     FE##%%r
   c                  :    ddl mc mc m}  | j	                         S )z/Dynamic import of CVXOPT sparse interface.
    r   N)8cvxpy.interface.cvxopt_interface.sparse_matrix_interfacer   r   sparse_matrix_interfaceSparseArrayInterface)smis    r   get_cvxopt_sparse_intfr   1   s     KJ##%%r
   c                    ddl }t        | t        j                  t        j                  f      r1|j                  |j	                  | j                  d            d      S t        j                  |       rx| j                         } |j                  | j                  j                         | j                  j                         | j                  j                         | j                  d      S y)zConverts a SciPy sparse matrix to a CVXOPT sparse matrix.

    Parameters
    ----------
    sparse_mat : SciPy sparse matrix
        The matrix to convert.

    Returns
    -------
    CVXOPT spmatrix
        The converted matrix.
    r   Nfloat64dtc)sizer   )cvxopt
isinstancenpndarraymatrixsparseastypespissparsetocoospmatrixdatatolistrowcolshapevaluer   s     r   sparse2cvxoptr1   :   s     %"**bii01}}V]]5<<	+BC}LL	U	uzz002EII4D4D4F$yy//1  M 	M 
r
   c                 0    ddl }|j                  | d      S )zConverts a NumPy matrix to a CVXOPT matrix.

    Parameters
    ----------
    value : NumPy matrix/ndarray
        The matrix to convert.

    Returns
    -------
    CVXOPT matrix
        The converted matrix.
    r   Nr   r   )r   r#   r/   s     r   dense2cvxoptr3   Q   s     ==3=''r
   c                 ,    t        j                  |       S )zConverts a CVXOPT matrix to a NumPy ndarray.

    Parameters
    ----------
    value : CVXOPT matrix
        The matrix to convert.

    Returns
    -------
    NumPy ndarray
        The converted matrix.
    )r!   array)r0   s    r   cvxopt2denser6   b   s     88E?r
   returnc                 ,    t        j                  |       S )z%Is the constant a sparse matrix?
    )r&   r'   constants    r   	is_sparser;   r   s     ;;x  r
   c                 .   t        | t        j                        st        j                  |       r
t               S t        | t              rQt        |       dk(  ryt        | d   t        j                        rt        |       fS t        | d         t        |       fS | j                  t        v r"t        | j                     j                  |       S t        |       r&t        t        j                     j                  |       S t        dt        |       z        )Nr   )r   ,%s is not a valid type for a Constant value.)r    numbersNumberr!   isscalartuplelistlen	__class__r   r.   r;   r&   	csc_array	TypeErrortyper9   s    r   r.   r.   z   s    (GNN+r{{8/Dw	Hd	#x=AW^^4M##$c(m44			z	)(,,-33H==	8	",,'--h77FhWXXr
   c                 $    t        |       d   dk(  S )N   r.   r9   s    r   	is_vectorrK      s    ?1""r
   c                     t        |       dk(  S )N)rI   rI   rJ   r9   s    r   	is_scalarrM      s    ?f$$r
   c                     t        | t        j                        r+| j                  dk(  rt        j                  |       dddf   S | S )z/Convert 2D Numpy matrices or arrays to 1D.
       Nr   )r    r!   r"   ndimasarrayr9   s    r   from_2D_to_1DrR      s:     (BJJ'HMMQ,>zz(#AqD))r
   c                     t        | t        j                        r.| j                  dk(  rt        j                  |       j
                  S | S )z)Convert 1D Numpy arrays to matrices.
    rI   )r    r!   r"   rP   asmatrixTr9   s    r   from_1D_to_2DrV      s7     (BJJ'HMMQ,>{{8$&&&r
   r$   convert_scalarsc                     t        | t        t        j                  t        j                  f      rt
        j                  | |      S |rt        j                  | |      S | S )z!Convert to appropriate type.
    )rW   )r    rB   r!   r"   r#   DEFAULT_INTFconst_to_matrixDEFAULT_SPARSE_INTF)r:   r$   rW   s      r   convertr\      se     (T2::ryy9:++H<K , M 	M	"228CR 3 T 	T r
   c                    t        | t        j                        st        j                  |       r| S t        | t
              r| d   S | j                  t        v r"t        | j                     j                  |       S t        |       r4t        t        j                     j                  | j                               S t        dt        |       z        )Nr   r=   )r    r>   r?   r!   r@   rB   rD   r   scalar_valuer;   r&   rE   tocscrF   rG   r9   s    r   r^   r^      s    (GNN+r{{8/D	Hd	#{			z	)(,,-::8DD	8	",,'44X^^5EFFFhWXXr
   c                 L   t        | t        j                        r| }| }n|t        j                  |       r!| j                         }| j                         }nFt        t        j                     j                  |       }|j                         }|j                         }|dk\  |dk  fS )zReturn (is positive, is negative).

    Parameters
    ----------
    constant : numeric type
        The numeric value to evaluate the sign of.

    Returns
    -------
    tuple
        The sign of the constant.
    r   )r    r>   r?   r&   r'   maxminr   r!   r"   rZ   )r:   max_valmin_valmats       r   signrf      s     (GNN+	X	,,.,,.$44X>'')'')qL'Q,''r
   tolc                    t        j                  |       }|syt        | t        j                        rQt        j
                  t        j                  |             }t        j
                  t        j                  |             }nt        j                  |       r[t        j
                  | j                        j                         }t        j
                  | j                        j                         }nt        t         j                     j                  |       } t        j
                  | j                        j                         }t        j
                  | j                        j                         }||k\  ||k\  fS )a#  Return (is real, is imaginary).

    Parameters
    ----------
    constant : numeric type
        The numeric value to evaluate the sign of.
    tol : float, optional
        The largest magnitude considered nonzero.

    Returns
    -------
    tuple
        The sign of the constant.
    )TF)r!   iscomplexobjr    r>   r?   absrealimagr&   r'   ra   r   r"   rZ   )r:   rg   complex_typereal_maximag_maxs        r   
is_complexrp      s     ??8,L(GNN+66"''(+,66"''(+,	X	66(--(,,.66(--(,,.bjj)99(C66(--(,,.66(--(,,.OX_--r
   c                    t        |       r| S | j                  t        v r#t        | j                     j                  | |      S t	        |       r:t        t
        j                     }|j                  |       } |j                  | |      S y r   )rM   rD   r   indexr;   r&   rE   rZ   )r:   keyr   s      r   rr   rr     sw    			z	)(,,-33HcBB	8	r||,	,,X6x-- 
r
   c                    t        j                  |       }|r_d}t        j                  |       rt	        | d      }||fS t        j
                  | t        j                  | j                              }||fS t        j                  |       rt	        | d      }n t        j
                  | | j                        }|}||fS )z5Check if a matrix is Hermitian and/or symmetric.
    FT)complex)r!   ri   r&   r'   is_sparse_symmetricallcloseconjrU   )r:   rm   is_symmis_herms       r   is_hermitianr{     s     ??8,L ;;x )(DAG G kk(BGGHJJ,?@G G ;;x )(EBGkk(HJJ7GGr
   c                     t        j                  |       }|ryt        j                  |       rt	        |       }|S t        j
                  | | j                  z   d      }|S )zIs the Fg        )r!   ri   r&   r'   is_sparse_skew_symmetricrw   rU   )r:   rm   is_skew_symms      r   is_skew_symmetricr   +  sU    ??8,L;;x 3H=L  ;;x(**'<cBLr
   ru   c                    | j                   d   | j                   d   k7  rt        d      t        | t        j                        st        j                  |       } | j
                  | j                  | j                  }}}||kD  }||kD  }|j                         |j                         k7  ry||   }||   }||   }	||   }
||   }||   }t        j                  ||f      }t        j                  |
|f      }|	|   }	||   }|r+t        j                  |	t        j                  |            }|S t        j                  |	|      }|S )zCheck if a sparse matrix is symmetric

    Parameters
    ----------
    m : array or sparse matrix
        A square matrix.

    Returns
    -------
    check : bool
        The check result.

    r   rI   m must be a square matrixF)r.   
ValueErrorr    r&   
coo_matrixr,   r-   r*   sumr!   lexsortrw   rx   )mru   rcvtril_no_diagtriu_no_diagrlclvlrucuvusortlsortuchecks                   r   rv   rv   8  s1    	wwqzQWWQZ455a'MM!eeQUUAFF!qAq5Lq5L\--//	
<B	
<B	
<B	
<B	
<B	
<BJJBx EJJBx E	EB	EBB, L B#Lr
   c                 6   | j                   d   | j                   d   k7  rt        d      t        | t        j                        st        j                  |       } | j
                  | j                  | j                  }}}||k\  }||k\  }|j                         |j                         k7  ry||   }||   }||   }||   }	||   }
||   }t        j                  ||f      }t        j                  |	|
f      }||   }||   }t        j                  ||z   d      }|S )zCheck if a real sparse matrix A satisfies A + A.T == 0.

    Parameters
    ----------
    A : array or sparse matrix
        A square matrix.

    Returns
    -------
    check : bool
        The check result.
    r   rI   r   F)r.   r   r    r&   r   r,   r-   r*   r   r!   r   rw   )Ar   r   r   triltriur   r   r   r   r   r   r   r   r   s                  r   r}   r}   h  s    	wwqzQWWQZ455a'MM!eeQUUAFF!qA6D6DxxzTXXZ	
4B	
4B	
4B	
4B	
4B	
4BJJBx EJJBx E	EB	EBKKR#ELr
   )FF)gh㈵>)F),__doc__r>   numpyr!   scipy.sparser$   r&   cvxpy.interfacer   np_intfr"   NDArrayInterfacer#   MatrixInterface
csc_matrixr   rE   r   DEFAULT_NP_INTFrY   r[   r	   r   r   r1   r3   r6   boolr;   r.   rK   rM   rR   rV   r\   r^   rf   floatrp   rr   r{   r   rv   r}    r
   r   <module>r      sd       6 jj2'224ii0002mm9W99;ll8G88:
 RZZ("**% . $&&M.(" !4 !Y*#4 #%4 %
d 
T 
Y (4.e .t .B	.d 4
4 
-D -T -`'4 'r
   