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)
CodePudding user response:
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)
CodePudding user response:
It looks like there is an issue with the example provided on the geeksforgeeks website. The row_del() method of the Matrix class in SymPy does not return a new matrix with the specified row removed. Instead, it modifies the original matrix in place and returns None.
Here is an example of how you can use the row_del() method to delete a row from a matrix:
from sympy import Matrix
matrix = Matrix([[1, 2], [2, 3], [23, 45]])
matrix.row_del(0)
print(matrix)
This will output the matrix Matrix([[2, 3], [23, 45]]).
or you can use the del statement to delete a row from a matrix, like this:
from sympy import Matrix
matrix = Matrix([[1, 2], [2, 3], [23, 45]])
del matrix[0]
print(matrix)
This will also output the matrix Matrix([[2, 3], [23, 45]]).