Page MenuHomec4science

compress_matrix.py
No OneTemporary

File Metadata

Created
Sun, Apr 28, 01:54

compress_matrix.py

# Copyright (C) 2018-2020 by the RROMPy authors
#
# This file is part of RROMPy.
#
# RROMPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# RROMPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RROMPy. If not, see <http://www.gnu.org/licenses/>.
#
import numpy as np
from rrompy.utilities.numerical.tensor_la import dot
from rrompy.utilities.base.types import Np2D, Tuple, HFEng
__all__ = ["compressMatrix"]
def compressMatrix(A:Np2D, tol : float = 0., HFEngine : HFEng = None,
is_state : bool = True) -> Tuple[Np2D, Np2D, float]:
"""Compress matrix by SVD."""
if HFEngine is None:
U, s, _ = np.linalg.svd(A.T.conj().dot(A))
else:
U, s, _ = np.linalg.svd(HFEngine.innerProduct(A, A,
is_state = is_state))
remove = np.where(s < tol * s[0])[0]
ncut = len(s) if len(remove) == 0 else remove[0]
sums = np.sum(s)
s = s[: ncut] ** .5
R = (U[:, : ncut].conj() * s).T
U = dot(A, U[:, : ncut] * s ** -1.)
return U, R, 1. - np.linalg.norm(s) / sums

Event Timeline