# Ticket #10425: trac_10425-matrix-augment-subdivide.patch

File trac_10425-matrix-augment-subdivide.patch, 5.5 KB (added by rbeezer, 10 years ago)
• ## sage/matrix/matrix1.pyx

```# HG changeset patch
# User Rob Beezer <beezer@ups.edu>
# Date 1291410713 28800
# Node ID b5a0bc9c6c6bda9d5ab3ee2fdc9f1c863eb525e8
# Parent  bb8bfd4d2eff6242af4a5de13e9133212f16ae30
10425: manage matrix subdivisions when augment'ing

diff -r bb8bfd4d2eff -r b5a0bc9c6c6b sage/matrix/matrix1.pyx```
 a return self.matrix_space(nrows, ncols, sparse=sparse)(entries=entries, coerce=coerce, copy=copy) def augment(self, other): def augment(self, other, subdivide=False): r""" Returns a new matrix formed by appending the matrix (or vector) ``other`` on the right side of ``self``. - ``other`` - a matrix, vector or free module element, whose dimensions are compatible with ``self``. - ``subdivide`` - default: ``False`` - request the resulting matrix to have a new subdivision, separating ``self`` from ``other``. OUTPUT: A new matrix formed by appending ``other`` onto the right side of ``self``. If ``other`` is a vector (or free module element) then in this context it is apprpriate to consider it as a column vector.  (The code first it is appropriate to consider it as a column vector.  (The code first converts a vector to a 1-column matrix.) If ``subdivide`` is ``True`` then any column subdivisions for the two matrices are preserved, and a new subdivision is added between ``self`` and ``other``.  If the row divisions are identical, then they are preserved, otherwise they are discarded. When ``subdivide`` is ``False`` there is no subdivision information in the result. .. warning:: If ``subdivide`` is ``True`` then unequal row subdivisions will be discarded, since it would be ambiguous how to interpret them.  If the subdivision behavior is not what you need, you can manage subdivisions yourself with methods like :meth:`~sage.matrix.matrix2.Matrix.get_subdivisions` and :meth:`~sage.matrix.matrix2.Matrix.subdivide`. You might also find :func:`~sage.matrix.constructor.block_matrix` or :func:`~sage.matrix.constructor.block_diagonal_matrix` useful and simpler in some instances. EXAMPLES: Augmenting with a matrix. :: ... TypeError: number of rows must be the same Setting ``subdivide`` to ``True`` will, in its simplest form, add a subdivision between ``self`` and ``other``. :: sage: A = matrix(3, range(12)) sage: B = matrix(3, range(15)) sage: A.augment(B, subdivide=True) [ 0  1  2  3| 0  1  2  3  4] [ 4  5  6  7| 5  6  7  8  9] [ 8  9 10 11|10 11 12 13 14] Column subdivisions are preserved by augmentation, and enriched, if subdivisions are requested.  (So multiple augmentations can be recorded.) :: sage: A = matrix(3, range(6)) sage: A.subdivide(None,) sage: B = matrix(3, range(9)) sage: B.subdivide(None,) sage: A.augment(B, subdivide=True) [0|1|0 1|2] [2|3|3 4|5] [4|5|6 7|8] Row subdivisions can be preserved, but only if they are identical. Otherwise, this information is discarded and must be managed separately. :: sage: A = matrix(3, range(6)) sage: A.subdivide([1,3],None) sage: B = matrix(3, range(9)) sage: B.subdivide([1,3],None) sage: A.augment(B, subdivide=True) [0 1|0 1 2] [---+-----] [2 3|3 4 5] [4 5|6 7 8] [---+-----] sage: A.subdivide([1,2],None) sage: A.augment(B, subdivide=True) [0 1|0 1 2] [2 3|3 4 5] [4 5|6 7 8] The result retains the base ring of ``self`` by coercing the elements of ``other`` into the base ring of ``self``. :: other = matrix(other.degree(), other.list()) if not isinstance(other, sage.matrix.matrix1.Matrix): raise TypeError("a matrix must be augmented with another matrix, or a vector") return self._augment(other) return self._augment(other, subdivide) def _augment(self, Matrix other): def _augment(self, Matrix other, subdivide): """ Return the augmented matrix of the form:: for c from 0 <= c < other._ncols: Z.set_unsafe(r, c+nc, other.get_unsafe(r,c)) if subdivide: self_subs_rows, self_subs_cols = self.get_subdivisions() other_subs_rows, other_subs_cols = other.get_subdivisions() if self_subs_rows == other_subs_rows: z_subs_rows = self_subs_rows else: z_subs_rows = None z_subs_cols = self_subs_cols + [nc] for col in other_subs_cols: z_subs_cols.append(col+nc) Z.subdivide(z_subs_rows, z_subs_cols) return Z def block_sum(self, Matrix other):