예제 #1
0
    def _build(self, num_rows, num_cols):
        """
        Allocate the matrix.

        Parameters
        ----------
        num_rows : int
            number of rows in the matrix.
        num_cols : int
            number of cols in the matrix.
        """
        self._matrix = np.zeros((num_rows, num_cols))
        submats = self._submats
        metadata = self._metadata

        for key in submats:
            info, loc, src_indices, shape, factor = submats[key]
            irow, icol = loc
            rows = info['rows']

            if rows is None:
                val = info['value']
                if val is None or isinstance(val, np.ndarray):
                    nrows, ncols = shape
                    irow2 = irow + nrows
                    if src_indices is None:
                        icol2 = icol + ncols
                        metadata[key] = (slice(irow,
                                               irow2), slice(icol, icol2),
                                         np.ndarray, factor)
                    else:
                        metadata[key] = (slice(irow,
                                               irow2), src_indices + icol,
                                         np.ndarray, factor)
                else:  # sparse
                    jac = val.tocoo()
                    if src_indices is None:
                        irows = irow + jac.row
                        icols = icol + jac.col
                    else:
                        irows, icols, idxs = _compute_index_map(
                            jac.row, jac.col, irow, icol, src_indices)
                        revidxs = np.argsort(idxs)
                        irows, icols = irows[revidxs], icols[revidxs]

                    metadata[key] = (irows, icols, type(val), factor)
            else:  # list format [data, rows, cols]
                if src_indices is None:
                    irows = rows + irow
                    icols = info['cols'] + icol
                else:
                    irows, icols, idxs = _compute_index_map(
                        rows, info['cols'], irow, icol, src_indices)
                    revidxs = np.argsort(idxs)
                    irows, icols = irows[revidxs], icols[revidxs]

                metadata[key] = (irows, icols, list, factor)
예제 #2
0
    def _build(self, num_rows, num_cols):
        """
        Allocate the matrix.

        Parameters
        ----------
        num_rows : int
            number of rows in the matrix.
        num_cols : int
            number of cols in the matrix.
        """
        self._matrix = np.zeros((num_rows, num_cols))
        submats = self._submats
        metadata = self._metadata

        for key in submats:
            info, irow, icol, src_indices, shape, factor = submats[key]
            rows = info['rows']

            if rows is None:
                val = info['value']
                if val is None or isinstance(val, np.ndarray):
                    nrows, ncols = shape
                    irow2 = irow + nrows
                    if src_indices is None:
                        icol2 = icol + ncols
                        metadata[key] = (slice(irow, irow2),
                                         slice(icol, icol2), np.ndarray, factor)
                    else:
                        metadata[key] = (slice(irow, irow2),
                                         src_indices + icol, np.ndarray, factor)
                else:  # sparse
                    jac = val.tocoo()
                    if src_indices is None:
                        irows = irow + jac.row
                        icols = icol + jac.col
                    else:
                        irows, icols, idxs = _compute_index_map(jac.row,
                                                                jac.col,
                                                                irow, icol,
                                                                src_indices)
                        revidxs = np.argsort(idxs)
                        irows, icols = irows[revidxs], icols[revidxs]

                    metadata[key] = (irows, icols, type(val), factor)
            else:  # list format [data, rows, cols]
                if src_indices is None:
                    irows = rows + irow
                    icols = info['cols'] + icol
                else:
                    irows, icols, idxs = _compute_index_map(rows, info['cols'],
                                                            irow, icol,
                                                            src_indices)
                    revidxs = np.argsort(idxs)
                    irows, icols = irows[revidxs], icols[revidxs]

                metadata[key] = (irows, icols, list, factor)
예제 #3
0
    def _build_coo(self, system):
        """
        Allocate the data, rows, and cols for the COO matrix.

        Parameters
        ----------
        system : <System>
            Parent system of this matrix.

        Returns
        -------
        (ndarray, ndarray, ndarray)
            data, rows, cols that can be used to construct a COO matrix.
        """
        submats = self._submats
        metadata = self._metadata
        pre_metadata = self._key_ranges = OrderedDict()
        if system is None:
            owns = None
            iproc = 0
            abs2meta = None
            use_owned = False
        else:
            owns = system._owning_rank
            iproc = system.comm.rank
            abs2meta = system._var_allprocs_abs2meta
            use_owned = system._use_owned_sizes()

        start = end = 0
        for key, (info, loc, src_indices, shape, factor) in iteritems(submats):
            wrt_dist = abs2meta[
                key[1]]['distributed'] if abs2meta and owns else False
            if owns and not (owns[key[1]] == iproc or wrt_dist
                             or abs2meta[key[0]]['distributed']):
                continue  # only keep stuff that this rank owns

            val = info['value']
            rows = info['rows']
            dense = (rows is None
                     and (val is None or isinstance(val, ndarray)))

            full_size = np.prod(shape)
            if dense:
                if src_indices is None:
                    if wrt_dist:
                        delta = np.prod(info['shape'])
                    else:
                        delta = full_size
                else:
                    if wrt_dist:
                        delta = info['shape'][0] * len(src_indices)
                    else:
                        delta = shape[0] * len(src_indices)
            elif rows is None:  # sparse matrix
                delta = val.data.size
            else:  # list sparse format
                delta = len(rows)

            end += delta
            pre_metadata[key] = (start, end, dense, rows)
            start = end

        data = np.zeros(end)
        rows = np.empty(end, dtype=int)
        cols = np.empty(end, dtype=int)

        for key, (start, end, dense, jrows) in iteritems(pre_metadata):
            info, loc, src_indices, shape, factor = submats[key]
            irow, icol = loc
            val = info['value']
            idxs = None

            col_offset = row_offset = 0
            if use_owned and self._is_internal:
                shape = info['shape']
                if abs2meta[key[1]]['distributed']:
                    col_offset = np.sum(
                        system.
                        _owned_sizes[:iproc, system.
                                     _var_allprocs_abs2idx['linear'][key[1]]])
                if abs2meta[key[0]]['distributed']:
                    row_offset = np.sum(
                        system.
                        _owned_sizes[:iproc, system.
                                     _var_allprocs_abs2idx['linear'][key[0]]])

            if dense:

                jac_type = ndarray

                if src_indices is None:
                    colrange = np.arange(shape[1], dtype=int) + col_offset
                else:
                    colrange = src_indices

                ncols = colrange.size
                subrows = rows[start:end]
                subcols = cols[start:end]

                for i in range(shape[0]):
                    subrows[i * ncols:(i + 1) * ncols] = i + row_offset
                    subcols[i * ncols:(i + 1) * ncols] = colrange

                subrows += irow
                subcols += icol

            else:  # sparse
                if jrows is None:
                    jac_type = type(val)
                    jac = val.tocoo()
                    jrows = jac.row
                    jcols = jac.col
                else:
                    jac_type = list
                    jcols = info['cols']

                if src_indices is None:
                    rows[start:end] = jrows + (irow + row_offset)
                    cols[start:end] = jcols + (icol + col_offset)
                else:
                    irows, icols, idxs = _compute_index_map(
                        jrows, jcols, irow, icol, src_indices)
                    rows[start:end] = irows
                    cols[start:end] = icols

            metadata[key] = (start, end, idxs, jac_type, factor)

        return data, rows, cols
예제 #4
0
    def _build_sparse(self, num_rows, num_cols):
        """
        Allocate the data, rows, and cols for the sparse matrix.

        Parameters
        ----------
        num_rows : int
            number of rows in the matrix.
        num_cols : int
            number of cols in the matrix.

        Returns
        -------
        (ndarray, ndarray, ndarray)
            data, rows, cols that can be used to construct a sparse matrix.
        """
        submats = self._submats
        metadata = self._metadata
        pre_metadata = self._key_ranges = OrderedDict()

        start = end = 0
        for key, (info, loc, src_indices, shape, factor) in iteritems(submats):
            val = info['value']
            rows = info['rows']
            dense = (rows is None
                     and (val is None or isinstance(val, ndarray)))

            full_size = np.prod(shape)
            if dense:
                if src_indices is None:
                    delta = full_size
                else:
                    delta = shape[0] * len(src_indices)
            elif rows is None:  # sparse matrix
                delta = val.data.size
            else:  # list sparse format
                delta = len(rows)

            end += delta
            pre_metadata[key] = (start, end, dense, rows)
            start = end

        data = np.zeros(end)
        rows = np.empty(end, dtype=int)
        cols = np.empty(end, dtype=int)

        for key, (start, end, dense, jrows) in iteritems(pre_metadata):
            info, loc, src_indices, shape, factor = submats[key]
            irow, icol = loc
            val = info['value']
            idxs = None

            if dense:
                jac_type = ndarray

                if src_indices is None:
                    colrange = np.arange(shape[1], dtype=int)
                else:
                    colrange = src_indices

                ncols = colrange.size
                subrows = rows[start:end]
                subcols = cols[start:end]

                for i in range(shape[0]):
                    subrows[i * ncols:(i + 1) * ncols] = i
                    subcols[i * ncols:(i + 1) * ncols] = colrange

                subrows += irow
                subcols += icol

            else:  # sparse
                if jrows is None:
                    jac_type = type(val)
                    jac = val.tocoo()
                    jrows = jac.row
                    jcols = jac.col
                else:
                    jac_type = list
                    jcols = info['cols']

                if src_indices is None:
                    rows[start:end] = jrows + irow
                    cols[start:end] = jcols + icol
                else:
                    irows, icols, idxs = _compute_index_map(
                        jrows, jcols, irow, icol, src_indices)
                    rows[start:end] = irows
                    cols[start:end] = icols

            metadata[key] = (start, end, idxs, jac_type, factor)

        return data, rows, cols
예제 #5
0
    def _build_coo(self, system):
        """
        Allocate the data, rows, and cols for the COO matrix.

        Parameters
        ----------
        system : <System>
            Parent system of this matrix.

        Returns
        -------
        (ndarray, ndarray, ndarray)
            data, rows, cols that can be used to construct a COO matrix.
        """
        submats = self._submats
        metadata = self._metadata
        key_ranges = self._key_ranges = OrderedDict()

        start = end = 0
        for key, (info, loc, src_indices, shape, factor) in submats.items():

            val = info['value']
            rows = info['rows']
            dense = (rows is None and (val is None or isinstance(val, ndarray)))

            shape = val.shape
            full_size = np.prod(shape)
            if dense:
                if src_indices is None:
                    end += full_size
                else:
                    end += shape[0] * len(src_indices)

            elif rows is None:  # sparse matrix
                end += val.data.size
            else:  # list sparse format
                end += len(rows)

            key_ranges[key] = (start, end, dense, rows)
            start = end

        data = np.zeros(end)
        rows = np.empty(end, dtype=INT_DTYPE)
        cols = np.empty(end, dtype=INT_DTYPE)

        for key, (start, end, dense, jrows) in key_ranges.items():
            info, loc, src_indices, shape, factor = submats[key]
            irow, icol = loc
            val = info['value']
            # _shape = val.shape
            # assert _shape == shape
            idxs = None

            col_offset = row_offset = 0

            if dense:
                jac_type = ndarray

                if src_indices is None:
                    colrange = np.arange(shape[1], dtype=INT_DTYPE) + col_offset
                else:
                    colrange = src_indices

                ncols = colrange.size
                subrows = rows[start:end]
                subcols = cols[start:end]

                for i in range(shape[0]):
                    subrows[i * ncols: (i + 1) * ncols] = i + row_offset
                    subcols[i * ncols: (i + 1) * ncols] = colrange

                subrows += irow
                subcols += icol

            else:  # sparse
                if jrows is None:
                    jac_type = type(val)
                    jac = val.tocoo()
                    jrows = jac.row
                    jcols = jac.col
                else:
                    jac_type = list
                    jcols = info['cols']

                if src_indices is None:
                    rows[start:end] = jrows + (irow + row_offset)
                    cols[start:end] = jcols + (icol + col_offset)
                else:
                    irows, icols, idxs = _compute_index_map(jrows, jcols,
                                                            irow, icol,
                                                            src_indices)
                    rows[start:end] = irows
                    cols[start:end] = icols

            metadata[key] = (start, end, idxs, jac_type, factor)

        return data, rows, cols
예제 #6
0
    def _build_sparse(self, num_rows, num_cols):
        """
        Allocate the data, rows, and cols for the sparse matrix.

        Parameters
        ----------
        num_rows : int
            number of rows in the matrix.
        num_cols : int
            number of cols in the matrix.

        Returns
        -------
        (ndarray, ndarray, ndarray)
            data, rows, cols that can be used to construct a sparse matrix.
        """
        counter = 0

        submats = self._submats
        metadata = self._metadata
        pre_metadata = self._key_ranges = OrderedDict()
        locations = {}

        for key, (info, loc, src_indices, shape, factor) in iteritems(submats):
            val = info['value']
            rows = info['rows']
            dense = (rows is None
                     and (val is None or isinstance(val, ndarray)))

            full_size = np.prod(shape)
            if dense:
                if src_indices is None:
                    delta = full_size
                else:
                    delta = shape[0] * len(src_indices)
            elif rows is None:  # sparse matrix
                delta = val.data.size
            else:  # list sparse format
                delta = len(rows)

            if loc in locations:
                ind1, ind2, otherkey = locations[loc]
                if not (src_indices is None and
                        (ind2 - ind1) == delta == full_size):
                    raise RuntimeError(
                        "Keys %s map to the same sub-jacobian of a CSC or "
                        "CSR partial jacobian and at least one of them is either "
                        "not dense or uses src_indices.  This can occur when "
                        "multiple inputs on the same "
                        "component are connected to the same output." % sorted(
                            (key, otherkey)))
            else:
                ind1 = counter
                counter += delta
                ind2 = counter
                locations[loc] = (ind1, ind2, key)

            pre_metadata[key] = (ind1, ind2, dense)

        data = np.zeros(counter)
        rows = np.empty(counter, dtype=int)
        cols = np.empty(counter, dtype=int)

        for key, (ind1, ind2, dense) in iteritems(pre_metadata):
            info, loc, src_indices, shape, factor = submats[key]
            irow, icol = loc
            val = info['value']
            idxs = None

            if dense:
                jac_type = ndarray

                if src_indices is None:
                    colrange = np.arange(shape[1], dtype=int)
                else:
                    colrange = src_indices

                ncols = colrange.size
                subrows = rows[ind1:ind2]
                subcols = cols[ind1:ind2]

                for i in range(shape[0]):
                    subrows[i * ncols:(i + 1) * ncols] = i
                    subcols[i * ncols:(i + 1) * ncols] = colrange

                subrows += irow
                subcols += icol

            else:  # sparse
                if isinstance(val, sparse_types):
                    jac_type = type(val)
                    jac = val.tocoo()
                    jrows = jac.row
                    jcols = jac.col
                else:
                    jac_type = list
                    jrows = info['rows']
                    jcols = info['cols']

                if src_indices is None:
                    rows[ind1:ind2] = jrows + irow
                    cols[ind1:ind2] = jcols + icol
                else:
                    irows, icols, idxs = _compute_index_map(
                        jrows, jcols, irow, icol, src_indices)
                    rows[ind1:ind2] = irows
                    cols[ind1:ind2] = icols

            metadata[key] = (ind1, ind2, idxs, jac_type, factor)

        return data, rows, cols
예제 #7
0
    def _build_sparse(self, num_rows, num_cols):
        """
        Allocate the data, rows, and cols for the sparse matrix.

        Parameters
        ----------
        num_rows : int
            number of rows in the matrix.
        num_cols : int
            number of cols in the matrix.

        Returns
        -------
        (ndarray, ndarray, ndarray)
            data, rows, cols that can be used to construct a sparse matrix.
        """
        counter = 0

        submats = self._submats
        metadata = self._metadata
        pre_metadata = {}
        for key, (info, irow, icol, src_indices, shape, factor) in iteritems(submats):
            if not info['dependent']:
                continue
            val = info['value']
            rows = info['rows']
            dense = (rows is None and (val is None or
                     isinstance(val, ndarray)))
            ind1 = counter
            if dense:
                counter += np.prod(shape)
            elif rows is None:
                counter += val.data.size
            else:
                counter += len(rows)
            ind2 = counter
            pre_metadata[key] = (ind1, ind2, None)

        data = np.zeros(counter)
        rows = -np.ones(counter, int)
        cols = -np.ones(counter, int)

        for key, (ind1, ind2, idxs) in iteritems(pre_metadata):
            info, irow, icol, src_indices, shape, factor = submats[key]
            val = info['value']
            dense = (info['rows'] is None and (val is None or
                     isinstance(val, ndarray)))

            if dense:
                jac_type = ndarray
                rowrange = np.arange(shape[0], dtype=int)

                if src_indices is None:
                    colrange = np.arange(shape[1], dtype=int)
                else:
                    colrange = src_indices

                ncols = colrange.size
                subrows = rows[ind1:ind2]
                subcols = cols[ind1:ind2]

                for i, row in enumerate(rowrange):
                    subrows[i * ncols: (i + 1) * ncols] = row
                    subcols[i * ncols: (i + 1) * ncols] = colrange

                rows[ind1:ind2] += irow
                cols[ind1:ind2] += icol

            else:  # sparse
                if isinstance(val, sparse_types):
                    jac_type = type(val)
                    jac = val.tocoo()
                    jrows = jac.row
                    jcols = jac.col
                else:
                    jac_type = list
                    jrows = info['rows']
                    jcols = info['cols']

                if src_indices is None:
                    rows[ind1:ind2] = jrows + irow
                    cols[ind1:ind2] = jcols + icol
                else:
                    irows, icols, idxs = _compute_index_map(jrows, jcols,
                                                            irow, icol,
                                                            src_indices)
                    rows[ind1:ind2] = irows
                    cols[ind1:ind2] = icols

            metadata[key] = (ind1, ind2, idxs, jac_type, factor)

        return data, rows, cols
예제 #8
0
    def _build_sparse(self, num_rows, num_cols):
        """
        Allocate the data, rows, and cols for the sparse matrix.

        Parameters
        ----------
        num_rows : int
            number of rows in the matrix.
        num_cols : int
            number of cols in the matrix.

        Returns
        -------
        (ndarray, ndarray, ndarray)
            data, rows, cols that can be used to construct a sparse matrix.
        """
        counter = 0

        submats = self._submats
        metadata = self._metadata
        pre_metadata = {}
        for key, (info, irow, icol, src_indices, shape, factor) in iteritems(submats):
            if not info['dependent']:
                continue
            val = info['value']
            rows = info['rows']
            dense = (rows is None and (val is None or
                     isinstance(val, ndarray)))
            ind1 = counter
            if dense:
                counter += np.prod(shape)
            elif rows is None:
                counter += val.data.size
            else:
                counter += len(rows)
            ind2 = counter
            pre_metadata[key] = (ind1, ind2, None)

        data = np.zeros(counter)
        rows = -np.ones(counter, int)
        cols = -np.ones(counter, int)

        for key, (ind1, ind2, idxs) in iteritems(pre_metadata):
            info, irow, icol, src_indices, shape, factor = submats[key]
            val = info['value']
            dense = (info['rows'] is None and (val is None or
                     isinstance(val, ndarray)))

            if dense:
                jac_type = ndarray
                rowrange = np.arange(shape[0], dtype=int)

                if src_indices is None:
                    colrange = np.arange(shape[1], dtype=int)
                else:
                    colrange = src_indices

                ncols = colrange.size
                subrows = rows[ind1:ind2]
                subcols = cols[ind1:ind2]

                for i, row in enumerate(rowrange):
                    subrows[i * ncols: (i + 1) * ncols] = row
                    subcols[i * ncols: (i + 1) * ncols] = colrange

                rows[ind1:ind2] += irow
                cols[ind1:ind2] += icol

            else:  # sparse
                if isinstance(val, sparse_types):
                    jac_type = type(val)
                    jac = val.tocoo()
                    jrows = jac.row
                    jcols = jac.col
                else:
                    jac_type = list
                    jrows = info['rows']
                    jcols = info['cols']

                if src_indices is None:
                    rows[ind1:ind2] = jrows + irow
                    cols[ind1:ind2] = jcols + icol
                else:
                    irows, icols, idxs = _compute_index_map(jrows, jcols,
                                                            irow, icol,
                                                            src_indices)
                    rows[ind1:ind2] = irows
                    cols[ind1:ind2] = icols

            metadata[key] = (ind1, ind2, idxs, jac_type, factor)

        return data, rows, cols