o
    ?e#                     @   sl  d 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	 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 eejdd Zeejdd Zee
jdd Zeejdd Zeejdd Zeejdd Zeejdd Zeejdd Z ee	j!dd  Z"eej#d!d" Z$d#S )$z)Registrations for LinearOperator.inverse.    )math_ops)linear_operator)linear_operator_addition)linear_operator_algebra)linear_operator_block_diag)&linear_operator_block_lower_triangular)linear_operator_circulant)linear_operator_diag)linear_operator_full_matrix)linear_operator_householder)linear_operator_identity)linear_operator_inversion)linear_operator_kroneckerc                 C   s   t j| | j| j| j| jdS )Nis_non_singularis_self_adjointis_positive_definite	is_square)r   LinearOperatorInversionr   r   r   r   )Zlinop r   s/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/ops/linalg/inverse_registrations.py_inverse_linear_operator"   s   r   c                 C   s   | j S N)operator)Zlinop_inversionr   r   r    _inverse_inverse_linear_operator,   s   r   c                 C   s    t jd| j | j| j| jddS )N      ?Tr   )r	   LinearOperatorDiagZdiagr   r   r   )Zdiag_operatorr   r   r   _inverse_diag2   s   r   c                 C      | S r   r   Zidentity_operatorr   r   r   _inverse_identity=      r    c                 C   s"   t j| jd| j | jd| jddS )Nr   T)num_rows
multiplierr   r   r   r   )r   LinearOperatorScaledIdentityZ	_num_rowsr#   r   r   r   r   r   r   _inverse_scaled_identityC   s   r%   c                 C   &   t jdd | jD | j| j| jddS )Nc                 S      g | ]}|  qS r   inverse.0r   r   r   r   
<listcomp>T       z'_inverse_block_diag.<locals>.<listcomp>T	operatorsr   r   r   r   )r   LinearOperatorBlockDiagr/   r   r   r   )Zblock_diag_operatorr   r   r   _inverse_block_diagO   s   r1   c                    sn  t | jdkrtj| jd d  gg| j| j| jddS t | j}t| jdd  }| jd }|d  }g }t|d D ]_}g }t||d D ]&}|| 	|j| |  t
 fddtjD smt   |  qLt|}	t |	dksJ |	d }
|	|
}
tj| tjd|
jd	d
	|
}
||
 qA|| tj|j|g | j| j| jddS )a  Inverse of LinearOperatorBlockLowerTriangular.

  We recursively apply the identity:

  ```none
  |A 0|'  =  |    A'  0|
  |B C|      |-C'BA' C'|
  ```

  where `A` is n-by-n, `B` is m-by-n, `C` is m-by-m, and `'` denotes inverse.

  This identity can be verified through multiplication:

  ```none
  |A 0||    A'  0|
  |B C||-C'BA' C'|

    = |       AA'   0|
      |BA'-CC'BA' CC'|

    = |I 0|
      |0 I|
  ```

  Args:
    block_lower_triangular_operator: Instance of
      `LinearOperatorBlockLowerTriangular`.

  Returns:
    block_lower_triangular_operator_inverse: Instance of
      `LinearOperatorBlockLowerTriangular`, the inverse of
      `block_lower_triangular_operator`.
     r   Tr   Nc                 3   s    | ]}t  |V  qd S r   )
isinstance)r+   Zop_typeresultr   r   	<genexpr>   s    z2_inverse_block_lower_triangular.<locals>.<genexpr>)dtype)r"   r#   )lenr/   r   "LinearOperatorBlockLowerTriangularr)   r   r   r   rangematmulanyr   ZSUPPORTED_OPERATORSr
   ZLinearOperatorFullMatrixZto_denseappendZadd_operatorsr   r$   Zdomain_dimension_tensorr   castr8   )Zblock_lower_triangular_operatorZblockwise_dimZupper_left_inverseZ
bottom_rowZbottom_right_inverseZinverse_bottom_rowiblocksjZsummed_blocksblockr   r5   r   _inverse_block_lower_triangular\   sf   $
	




rD   c                 C   r&   )Nc                 S   r'   r   r(   r*   r   r   r   r,      r-   z&_inverse_kronecker.<locals>.<listcomp>Tr.   )r   LinearOperatorKroneckerr/   r   r   r   )Zkronecker_operatorr   r   r   _inverse_kronecker   s   rF   c                 C   s$   | j d| j | j| j| jd| jdS )Nr   T)spectrumr   r   r   r   Zinput_output_dtype)	__class__rG   r   r   r   r8   )Zcirculant_operatorr   r   r   _inverse_circulant   s   rI   c                 C   r   r   r   )Zhouseholder_operatorr   r   r   _inverse_householder   r!   rJ   N)%__doc__Ztensorflow.python.opsr   Ztensorflow.python.ops.linalgr   r   r   r   r   r   r	   r
   r   r   r   r   ZRegisterInverseZLinearOperatorr   r   r   r   r   ZLinearOperatorIdentityr    r$   r%   r0   r1   r:   rD   rE   rF   Z_BaseLinearOperatorCirculantrI   ZLinearOperatorHouseholderrJ   r   r   r   r   <module>   sh   

	

	




a

