def eig(tensor, name=None): """Computes the eigen decomposition of a batch of matrices. The eigenvalues and eigenvectors for a non-Hermitian matrix in general are complex. The eigenvectors are not guaranteed to be linearly independent. Computes the eigenvalues and right eigenvectors of the innermost N-by-N matrices in `tensor` such that `tensor[...,:,:] * v[..., :,i] = e[..., i] * v[...,:,i]`, for i=0...N-1. Args: tensor: `Tensor` of shape `[..., N, N]`. Only the lower triangular part of each inner inner matrix is referenced. name: string, optional name of the operation. Returns: e: Eigenvalues. Shape is `[..., N]`. Sorted in non-decreasing order. v: Eigenvectors. Shape is `[..., N, N]`. The columns of the inner most matrices contain eigenvectors of the corresponding matrices in `tensor` """ if tensor.dtype == dtypes.float32 or tensor.dtype == dtypes.complex64: out_dtype = dtypes.complex64 elif tensor.dtype == dtypes.float64 or tensor.dtype == dtypes.complex128: out_dtype = dtypes.complex128 e, v = gen_linalg_ops.eig(tensor, Tout=out_dtype, compute_v=True, name=name) return e, v
def eigvals(tensor, name=None): """Computes the eigenvalues of one or more matrices. Note: If your program backpropagates through this function, you should replace it with a call to tf.linalg.eig (possibly ignoring the second output) to avoid computing the eigen decomposition twice. This is because the eigenvectors are used to compute the gradient w.r.t. the eigenvalues. See _SelfAdjointEigV2Grad in linalg_grad.py. Args: tensor: `Tensor` of shape `[..., N, N]`. name: string, optional name of the operation. Returns: e: Eigenvalues. Shape is `[..., N]`. The vector `e[..., :]` contains the `N` eigenvalues of `tensor[..., :, :]`. """ if tensor.dtype == dtypes.float32 or tensor.dtype == dtypes.complex64: out_dtype = dtypes.complex64 elif tensor.dtype == dtypes.float64 or tensor.dtype == dtypes.complex128: out_dtype = dtypes.complex128 e, _ = gen_linalg_ops.eig(tensor, Tout=out_dtype, compute_v=False, name=name) return e
def testMismatchedDtypes(self): tensor = constant_op.constant([[0, 1], [2, 3]], dtype=dtypes_lib.float32) with self.assertRaisesRegex((ValueError, errors.InvalidArgumentError), "Invalid output dtype"): self.evaluate( gen_linalg_ops.eig( input=tensor, Tout=dtypes_lib.complex128, # Expected dtype: complex64. compute_v=True))