sympy submatrices

704 views
Skip to first unread message

dxdydz

unread,
Jan 11, 2011, 1:17:01 PM1/11/11
to sympy
Hi all,
is it possible to extract a submatrix from a Sympy Matrix
object by specifying a list of rows and columns ? For example, with
Matlab, we could do

> A([1,3],[1,3])

to get the first and third rows/columns. I know you can extract a
submatrix with Sympy by providing a range like A(1:3,1:3), but what if
we only want some particular indices ?

Thanks a lot!

Aaron S. Meurer

unread,
Jan 11, 2011, 4:43:05 PM1/11/11
to sy...@googlegroups.com
I don't think there is a single function that does what you want (you can look through help(Matrix) to see if I am wrong). Probably it should be implemented.

I think you could achieve it by getting the contiguous parts and appending them together. So if you wanted the first and third columns of a Matrix, you could do

In [21]: m
Out[21]:
⎡0 1 2 3⎤
⎢ ⎥
⎢1 2 3 4⎥
⎢ ⎥
⎢2 3 4 5⎥
⎢ ⎥
⎣3 4 5 6⎦

In [22]: m[:,0].row_join(m[:, 2])
Out[22]:
⎡0 2⎤
⎢ ⎥
⎢1 3⎥
⎢ ⎥
⎢2 4⎥
⎢ ⎥
⎣3 5⎦

Is this what you wanted?

Aaron Meurer

> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>

Alexey U. Gudchenko

unread,
Jan 11, 2011, 4:58:29 PM1/11/11
to sy...@googlegroups.com
11.01.2011 21:17, dxdydz пишет:

It can be done by definition of new method in Matrix class, for example
(without checking input data):

In [1]: def extract(self, rowsList, colsList):
...: return Matrix(len(rowsList), len(colsList), lambda i,j:
self.mat[rowsList[i]*self.cols + colsList[j]])

In [5]: Matrix.extract = extract

Then we can use modification:

In [6]: m = Matrix(4, 3, lambda i, j: i*3 + j)
In [7]: m
Out[7]:
⎡0 1 2 ⎤
⎢ ⎥
⎢3 4 5 ⎥
⎢ ⎥
⎢6 7 8 ⎥
⎢ ⎥
⎣9 10 11⎦

In [8]: m.extract([0,1,3],[0,1])
Out[8]:
⎡0 1 ⎤
⎢ ⎥
⎢3 4 ⎥
⎢ ⎥
⎣9 10⎦

In [9]: m.extract([0,3],[0,2])
Out[9]:
⎡0 2 ⎤
⎢ ⎥
⎣9 11⎦

Aaron S. Meurer

unread,
Jan 11, 2011, 5:37:51 PM1/11/11
to sy...@googlegroups.com
Excellent. Can you send in a patch that implements this in Matrix?

Aaron Meurer

Alexey U. Gudchenko

unread,
Jan 11, 2011, 7:47:52 PM1/11/11
to sy...@googlegroups.com

It is in attachment now.


Some technical notes:

I have done it relative to "git://github.com/sympy/sympy.git" with the
aim of "git format-patch" command in this way:

> git clone git://github.com/sympy/sympy.git
> cd sympy
> git checkout -b matrix_extract

then I have implemented extract method in "sympy/matrices/matrices.py"

> ./bin/test

achieved that test_code_quality.py was not angry

> ./bin/doctest

There are many fails in doctest reported by the red, but not in matrices.py.

> git status
> git commit -a -m "implementation of extract method of matrices"
> git format-patch master --stdout >
extract_method_of_matrices_implemetation.patch

And send you this path created.

Is it right way?


12.01.2011 01:37, Aaron S. Meurer пишет:

extract_method_of_matrices_implemetation.patch

dxdydz

unread,
Jan 11, 2011, 8:50:21 PM1/11/11
to sympy
Thank you so much!
>  extract_method_of_matrices_implemetation.patch
> 1KViewDownload

Aaron S. Meurer

unread,
Jan 11, 2011, 10:23:13 PM1/11/11
to sy...@googlegroups.com
Excellent. Yes, you created the patch exactly correctly.

Do not worry about the doctest failures. Those are because of issue 2041 (http://code.google.com/p/sympy/issues/detail?id=2041), which will be fixed *very* soon anyway (you can get around it currently by running the doctests in Python 2.5).

This is great. It even works if you give the rows or columns out of order, or if you duplicate them:

In [3]: m = Matrix(5, 6, lambda i, j: i + j)

In [4]: m
Out[4]:
⎡0 1 2 3 4 5⎤
⎢ ⎥
⎢1 2 3 4 5 6⎥
⎢ ⎥
⎢2 3 4 5 6 7⎥


⎢ ⎥
⎢3 4 5 6 7 8⎥
⎢ ⎥

⎣4 5 6 7 8 9⎦

In [6]: m.extract([0, 4, 3], [2, 3])
Out[6]:
⎡2 3⎤
⎢ ⎥
⎢6 7⎥
⎢ ⎥
⎣5 6⎦

In [7]: m.extract([0, 0, 3], [2, 3])
Out[7]:
⎡2 3⎤
⎢ ⎥
⎢2 3⎥
⎢ ⎥
⎣5 6⎦

One last thing: can you write a regular test for this? Just create a function test_extract() in test_matrices.py with assert statements like the others there, and test the functionality (you can test the exceptions by using the raises() function). ./bin/test matrices will run the test. Go ahead and just make it as another patch.

I am otherwise +1 on your patch, so once you add tests I can push it in.

Aaron Meurer

> <extract_method_of_matrices_implemetation.patch>

Alexey U. Gudchenko

unread,
Jan 12, 2011, 2:07:10 AM1/12/11
to sy...@googlegroups.com

A few lines of code.

I have made the patch of the realization of function "test_extract()"
not separately. It's merged with the previous one ("extract()" method
patch), so two files are affected by it now.

12.01.2011 06:23, Aaron S. Meurer пишет:

extract_method_of_matrices_with_test.patch

Aaron S. Meurer

unread,
Jan 12, 2011, 2:46:25 AM1/12/11
to sy...@googlegroups.com
Thank you a lot! It is in. See https://github.com/sympy/sympy/commit/2c80cf09b85a74cabaebdf94502a32643cb565d5. This will also be in the next release, which is planned for the next week or so.

Aaron Meurer

> <extract_method_of_matrices_with_test.patch>

Reply all
Reply to author
Forward
0 new messages