Matrix row_del and row_insert operation with out of bound index

42 views
Skip to first unread message

Gaurav Dhingra

unread,
May 31, 2015, 8:13:14 AM5/31/15
to sy...@googlegroups.com
Hi all

>>> from sympy import *
>>> M = Matrix( [ [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6] ] )
>>> M.row_del ( 8 )                                            # any index >= M.rows to be used
>>> print( M )
Matrix( [ [ 1, 2, 3], [2, 3, 4], [3, 4, 5] ] )               # deletes the last row of Matrix for Index >= M.rows

>>> N = Matrix( [ [ 1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6] ] )
>>> N.row_del( -10 )
>>> print( N )                                                     # deletes the last row of Matrix with any `Negative Index`
Matrix( [ [ 1, 2, 3], [2, 3, 4], [3, 4, 5] ] )

But the output for .row_insert() shows different behavior

>>> M = Matrix( [ [1, 2, 3], [2, 3, 4], [3, 4, 5] ] )
>>> M.row_insert ( 10, Matrix( [ [ 5, 5, 5 ] ] ) )
Matrix( [ [1, 2, 3], [2, 3, 4], [3, 4, 5], [5, 5, 5] ] )   # inserts the row at the very end for Index >= M.rows

>>> N = Matrix( [ [ 1, 2, 3], [2, 3, 4], [3, 4, 5] ] )
>>> N.row_insert( -10, Matrix( [ [5, 5, 5 ] ] ) )
Matrix( [ [5, 5, 5], [1, 2, 3], [2, 3, 4], [3, 4, 5] ] )     # inserts the row at the very beginning for `Negative Index`


First of all i  expected that an IndexError be raised in all cases. Because i think it should follow something like  a = [1, 2, 3] and del a[ 10 ] which raises an error. 
But even if not error then it should be more consistent with the indices. Is it the way the things are expected to work ?

Chris Smith

unread,
Jun 1, 2015, 10:32:33 AM6/1/15
to sy...@googlegroups.com
The question is how you would append a row using the insert method if you disallowed indices that are out of range. I agree that the negative indice behavior is wrong for deletion: it should have deleted the first row as you indicate.

Gaurav Dhingra

unread,
Jun 1, 2015, 3:17:01 PM6/1/15
to sy...@googlegroups.com
I agree with your statement that: "These all operations be allowed". For the M.row_del( ) for negative indices, should I Open an Issue and Fix it ?
Reply all
Reply to author
Forward
0 new messages