Elementary operations on rows (or columns) in Sympy

861 views
Skip to first unread message

A L

unread,
Apr 14, 2014, 7:35:14 PM4/14/14
to sy...@googlegroups.com
Hi all.
I am quite new to Sympy. I am studying several tutorials, starting from those on the official site and some presentation in Ipython format.
Concerning the various methods about matrices, I still did not find the commands to perform the elementary operations on rows (or columns). For example in Sage there are the following functions:
  • swap_rows() - interchange two rows
  • rescale_row() - scale a row by using a scale factor
  • add_multiple_of_row() - add a multiple of one row to another row, replacing the row

I did not find the equivalent commands in Sympy. Am I missing something? Could you please give me some indications?

Thanks.


-

Aaron Meurer

unread,
Apr 14, 2014, 8:27:28 PM4/14/14
to sy...@googlegroups.com
I'm not sure about the other two, to swap rows you can use Matrix.row_swap.

Aaron Meurer


--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/0b81c32b-2da9-48ef-84d1-9f0253d6e239%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

A L

unread,
Apr 15, 2014, 9:31:34 AM4/15/14
to sy...@googlegroups.com
Thanks for the answer Aaron.
At present I am having some difficulties, because the documentation of Sympy about the functions operating on matrices is a bit scattered. I also noticed that it varies quite a lot from the variuos versions of the package, for example the page of the 0.72 version http://docs.sympy.org/0.7.2/modules/matrices/matrices.html) is much more detailed that that of the 0.75 (http://docs.sympy.org/latest/tutorial/matrices.html).
I was hoping for some sort of Quick Reference, but up to now I did not find it. Do you have some other link about the argument that I could have missed?
Thanks.

Aaron Meurer

unread,
Apr 15, 2014, 4:53:23 PM4/15/14
to sy...@googlegroups.com
Those are different documents. The latest version of the full Matrix documentation is at http://docs.sympy.org/latest/modules/matrices/matrices.html. The other page you saw was just the matrices section of the tutorial, which only has the basics of matrices, for introductory purposes.

Aaron Meurer


--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.

A L

unread,
Apr 16, 2014, 8:56:26 AM4/16/14
to sy...@googlegroups.com
Thanks Aaron.
By carefully reading the documentation, it seems that actually the elementary operaration directly implemented in Sympy is row_swap, so I am trying to find the simplest way to perform the others.

If I understood correctly, a possibility is to use lambda functors. I was able to multiply a single row (e.g. the second from top) for a scalar (e.g. 5) in this manner:

M.row(1,lambda i,j: i*5)

At this point the only remaining difficulty for me is how to sum two rows in a similar manner. Could someone help?
Thanks

Aaron Meurer

unread,
Apr 16, 2014, 4:15:24 PM4/16/14
to sy...@googlegroups.com
Note that row() is deprecated. You should use row_op() instead.

The example in the documentation for row_op adds a multiple of a row to another row:

>>> from sympy.matrices import eye
>>> M = eye(3)
>>> M.row_op(1, lambda v, j: v + 2*M[0, j]); M
Matrix([
[1, 0, 0],
[2, 1, 0],
[0, 0, 1]])

Aaron Meurer


--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.

A L

unread,
Apr 16, 2014, 4:55:15 PM4/16/14
to sy...@googlegroups.com
Thanks a lot.
I did not know that row() is deprecated, and your example works really well, it solves my problem.
Maybe it would be useful, also to other users, if all the methods for the three elementary operations on rows (or columns) would be collected clearly, together with some examples, in the Sympy documentation about matrices. I can prepare a related Ipython notebook, if you think it can be useful.
Thanks.

Aaron Meurer

unread,
Apr 16, 2014, 5:04:28 PM4/16/14
to sy...@googlegroups.com
I think it would be useful to have the elementary row operations as actual methods, so that they are easier to apply. If you want to submit a pull request implementing this, that would be great.

Aaron Meurer


Reply all
Reply to author
Forward
0 new messages