Page MenuHomec4science

scipy_tensorize.py
No OneTemporary

File Metadata

Created
Wed, Nov 13, 23:05

scipy_tensorize.py

# Copyright (C) 2018 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
import scipy.sparse as scsp
from rrompy.utilities.base.types import Np1D, Np2D, List
from rrompy.utilities.exception_manager import RROMPyException
__all__ = ['tensorizeLS', 'detensorizeLS']
def tensorizeLS(As : List[Np2D] = [], bs : List[Np1D] = [],
AFormat : str = "csr"):
if len(As) > 0:
A = scsp.block_diag(As, format = AFormat)
else:
A = None
if len(bs) > 0:
b = np.concatenate(bs, axis = None)
else:
b = None
return A, b
def detensorizeLS(x:Np1D, n : int = 0, sizes : List[int] = []):
if n > 0 and len(sizes) > 0 and n != len(sizes):
raise RROMPyException("Specified n and sizes are inconsistent.")
if n == 0 and len(sizes) == 0:
raise RROMPyException("Must specify either n or sizes.")
if len(sizes) == 0:
sizes = [len(x) // n] * n
if n * sizes[0] != len(x):
raise RROMPyException(("Number of chunks must divide vector "
"length."))
n = len(sizes)
sEnd = np.cumsum(sizes)
sStart = sEnd - sizes[0]
return [x[sStart[j] : sEnd[j]] for j in range(n)]

Event Timeline