Why doesn't Sympy's row_del() method work (example from geeksforgeeeks website)?

Question:

TL;DR There is an inconsistency when using Sympy’s [Matrix].row_del() method and websites seem to be reporting its use wrongly.

Note from the following link from the geeksforgeeks website for an example of deleting matrix rows using sympy:

from sympy import *
  
# use the row_del() method for matrix
gfg_val = Matrix([[1, 2], [2, 3], [23, 45]]).row_del(0)
  
print(gfg_val)

The geeksforgeeks website gives the expected but false output as:

" Matrix([[2, 3], [23, 45]]) "

HOWEVER, the actual output can be found by running the code and getting:

None

What is going on here? Is the sympy program not working? I doubt the geeksforgeeks website has forged the output.

Furthermore, I’ve sometimes gotten the None output in my code to when trying to do the row_del() operation… anyone have an idea of what’s going on here? Example code below:

from sympy import Matrix
from sympy import *

A = Matrix([(1, 2, 3), (4, 5, 6)])
display(A)

A = A.row_del(0)
display(A)
Asked By: Hendrix13

||

Answers:

The discrepancy is probably related to the SymPy version used by that website.

In SymPy there are two main matrix types:

  • ImmutableDenseMatrix: as the name suggests, you cannot modify in place this matrix. Every time you perform a matrix modification, the operation will return a new matrix.
  • MutableDenseMatrix: Every time you perform a modification, the matrix will be modified in-place. No new matrix is created… As of SymPy 1.11.1, Matrix is an alias of this type.

Based on your findings, it might be that some Sympy version ago, Matrix was an alias of ImmutableDenseMatrix.

Anyway, this is an example that clarifies the concept.

A = Matrix([(1, 2, 3), (4, 5, 6)])
B = ImmutableDenseMatrix([(1, 2, 3), (4, 5, 6)])
display(A, B)

A.row_del(0)
B = B.row_del(0)
display(A, B)
Answered By: Davide_sd
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.