Hi Saul,

This is a consistent design decision across Sage: when you invert a
ring element, it automatically returns the inverse as an element of
the fraction field. E.g.

sage: a = ZZ(1)
sage: a.parent()
Integer Ring
sage: b = ~a
sage: b.parent()
Rational Field

Same with polynomials, power series, etc. Generally the idea is that
the parent of the result of an operation should depend only on the
parents of the operands (not on their actual values). I don't know
exactly what the arguments for and against this are, but if it's to be
changed we would certainly want to change it consistently across all
Sage parents (not just for matrices alone).

David



On 22 January 2014 16:47, Saul Schleimer <saul...@gmail.com> wrote:
> I sent the following to John Cremona - he suggested I post it here.
>
> Dear John -
>
>   I was computing Smith normal forms (over ZZ) and I ran into an issue.  I
> boiled it down to this:
>
> sage: version()
> 'Sage Version 5.12, Release Date: 2013-10-07'
> sage: M = matrix([[1,0],[0,1]])
> sage: M
> [1 0]
> [0 1]
> sage: M.parent()
> Full MatrixSpace of 2 by 2 dense matrices over Integer Ring
> sage: M.inverse()
> [1 0]
> [0 1]
> sage: M.inverse().parent()
> Full MatrixSpace of 2 by 2 dense matrices over Rational Field
>
> Note that the base ring has changed.  This caught me out: I was computing
> Smith normal forms in two ways and they were different.  Eventually I
> realized that (obviously) the Smith normal form is sensitive to the base
> ring!
>
> Does the base ring have to change here?  I guess that there is a subtle
> algorithm to quickly compute inverses, that requires extending the ring.
> But once we've got the answer, couldn't we do a fast check to see if the
> answer is actually in the original base ring?
>
> That is - if M is the original matrix and N is the computed inverse then add
> this to the very end:
>
> if N.parent() == M.parent():
>     return N
> try:
>     R = M.parent().base_ring()
>     P = N.change_ring(R)
>     assert M*P == P*M == 1
>     return P
> except:
>     return N
>
> all the best,
>
> saul
>
> [Edit]
> John pointed me to the code for M.__invert__() so I suppose that I am
> suggesting placing some version of the above code after the try and before
> the return, around line 11.
>
> "try:
>                         return (~self.lift()).change_ring(self.base_ring())"
>
> Of course, in this particular place in the code the first two lines of my
> suggestion are pointless.
>
> best,
>
> saul
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-support+unsubscr...@googlegroups.com.
> To post to this group, send email to sage-support@googlegroups.com.
> Visit this group at http://groups.google.com/group/sage-support.
> For more options, visit https://groups.google.com/groups/opt_out.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-support" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-support+unsubscr...@googlegroups.com.
To post to this group, send email to sage-support@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-support.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to