Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matrix determinant and simplify #21262

Open
proy87 opened this issue Apr 8, 2021 · 3 comments
Open

Matrix determinant and simplify #21262

proy87 opened this issue Apr 8, 2021 · 3 comments
Labels

Comments

@proy87
Copy link

proy87 commented Apr 8, 2021

I want to calculate eigenvalues "manually", so I form the following matrix:
m=Matrix([[Rational(1,2)-x, sqrt(3)/2, 0,0], [1, sqrt(3)-x,0,0],[-sqrt(3)/2, Rational(1,2), -x, 0],[-2,0,0,-x]])
m.det() gives (4*x**5 - 8*sqrt(3)*x**4 - 4*x**4 + 4*sqrt(3)*x**3 + 13*x**3)/(4*x - 4*sqrt(3) - 2), when, in fact, it should be a polynomial and I can't find a way to simplify it: factor and cancel don't work.

@oscarbenjamin
Copy link
Contributor

You can use extension=True:

In [3]: m.det()
Out[3]: 
   5         4      4         3       3
4⋅x  - 8⋅√3⋅x  - 4⋅x  + 4⋅√3⋅x  + 13⋅x 
───────────────────────────────────────
             4⋅x - 4⋅√3 - 2            

In [4]: factor(m.det(), extension=True)
Out[4]: 
 3               
x ⋅(x - √3 - 1/2)

In [5]: cancel(m.det(), extension=True)
Out[5]: 
      3            
 4   x ⋅(-4⋅√3 - 2)
x  + ──────────────
           4   

The algorithm for charpoly is different and does not use division:

In [6]: m.charpoly()
Out[6]: 
PurePoly(lambda**4 + (4*x - sqrt(3) - 1/2)*lambda**3 + 3*x*(4*x - 2*sqrt(3) - 1)/2*lambda**2 + x**2*(8*x - 6*sqrt(3) - 3)/2*lamb
da + x**3*(x - sqrt(3) - 1/2), lambda, domain='EX')

@proy87
Copy link
Author

proy87 commented Apr 8, 2021

@oscarbenjamin , I use det(method='berkowitz') which works. Can it give division? Is there any reason that the default algorithm for det is bareiss, not berkowitz?

@oscarbenjamin
Copy link
Contributor

The bareiss algorithm is supposed to use exact division i.e. divisions that always cancel. I guess that it isn't using cancel or isn't using extension=True so the cancellation doesn't happen. It would make more sense to use the bareiss algorithm with DomainMatrix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants