Esempio n. 1
0
    def __abstract_new__(cls, matrix, vector):
        data, N, M = flatten(matrix)
        if N != M:
            raise ValueError('expected an square matrix')

        M += 1
        for i, x in enumerate(vector):
            data.insert((i + 1) * M - 1, x)
        T = cls[N, dtype(data)]
        return T.from_flat(data)
Esempio n. 2
0
    def __abstract_new__(cls, matrix, vector):
        data, N, M = flatten(matrix)
        if N != M:
            raise ValueError("expected an square matrix")

        M += 1
        for i, x in enumerate(vector):
            data.insert((i + 1) * M - 1, x)
        T = cls[N, dtype(data)]
        return T.from_flat(data)
Esempio n. 3
0
    def from_rows(cls, rows, dtype=None):
        """
        Build matrix from a sequence of row vectors.
        """

        data, M, N = flatten(rows, 2)
        if (M, N) != cls.shape:
            if cls.size is None:
                return cls[M, N, cls.dtype].from_flat(data, dtype)
            msg = ('data shape %s is not consistent with matrix type %s' %
                   ((M, N), cls.shape))
            raise ValueError(msg)
        return cls.from_flat(data, dtype=dtype)
Esempio n. 4
0
    def from_rows(cls, rows, dtype=None):
        """
        Build matrix from a sequence of row vectors.
        """

        data, M, N = flatten(rows, 2)
        if (M, N) != cls.shape:
            if cls.size is None:
                return cls[M, N, cls.dtype].from_flat(data, dtype)
            msg = ('data shape %s is not consistent with matrix type %s' %
                   ((M, N), cls.shape))
            raise ValueError(msg)
        return cls.from_flat(data, dtype=dtype)
Esempio n. 5
0
    def from_cols(cls, cols, dtype=None):
        """
        Build matrix from a sequence of column vectors.
        """

        dataT, N, M = flatten(cols, 2)
        data = dataT[:]
        for i in range(M):
            data[i * N:i * N + N] = dataT[i::M]

        if (M, N) != cls.shape:
            if cls.size is None:
                return cls[M, N, cls.dtype].from_flat(data, dtype)
            msg = ('data shape %s is not consistent with matrix type %s' %
                   ((M, N), cls.shape))
            raise ValueError(msg)
        return cls.from_flat(data, dtype=dtype)
Esempio n. 6
0
    def from_cols(cls, cols, dtype=None):
        """
        Build matrix from a sequence of column vectors.
        """

        dataT, N, M = flatten(cols, 2)
        data = dataT[:]
        for i in range(M):
            data[i * N:i * N + N] = dataT[i::M]

        if (M, N) != cls.shape:
            if cls.size is None:
                return cls[M, N, cls.dtype].from_flat(data, dtype)
            msg = ('data shape %s is not consistent with matrix type %s' %
                   ((M, N), cls.shape))
            raise ValueError(msg)
        return cls.from_flat(data, dtype=dtype)
Esempio n. 7
0
 def __abstract_new__(cls, *args, dtype=None):
     flat, nrows, ncols = flatten(args, 2)
     return cls[nrows, ncols].from_flat(flat)
Esempio n. 8
0
 def __init__(self, *args):
     flat, N, M = flatten(args, 2)
     self.flat = self.__flat__(flat, copy=False)
     if N != self.nrows or M != self.ncols:
         raise ValueError('data has an invalid shape: %s' % repr((N, M)))
Esempio n. 9
0
 def __abstract_new__(cls, *args, dtype=None):
     flat, nrows, ncols = flatten(args, 2)
     return cls[nrows, ncols].from_flat(flat)
Esempio n. 10
0
 def __init__(self, *args):
     flat, N, M = flatten(args, 2)
     self.flat = self.__flat__(flat, copy=False)
     if N != self.nrows or M != self.ncols:
         raise ValueError('data has an invalid shape: %s' % repr((N, M)))
Esempio n. 11
0
def test_flatten():
    assert flatten([1, 2]) == ([1, 2], 2)
    assert flatten([[1, 2]]) == ([1, 2], 1, 2)
    assert flatten([[1], [2]]) == ([1, 2], 2, 1)
    assert flatten([[1, 2], [3, 4], [5, 6]]) == ([1, 2, 3, 4, 5, 6], 3, 2)