Containment and general/special linear

30 views
Skip to first unread message

Greg Laun

unread,
Sep 26, 2012, 4:07:31 PM9/26/12
to sage-...@googlegroups.com
Hi Everyone,

I'm sorry if this has been discussed, but I can't find a discussion.  I noticed the following behavior:

sage: matrix(GL(2),2,[1,0,0,1]) in GL(2,CC)
True

so 'in' ignores base ring.  The problem is that __contains__ for general_linear.pyx and special_linear.pyx only check whether a matrix can be coerced:       
 try:
            x = self(x)
        except TypeError:
            return False
        return True

Greg Laun

unread,
Sep 26, 2012, 4:19:31 PM9/26/12
to sage-...@googlegroups.com
Whoops,  gmail sent the email before I was finished.  

 I think a more appropriate way to do the comparison would be to check if self(x) == x.  This is how contains is implemented in parent.pyx.

Any thoughts?

Thanks,

Greg

John H Palmieri

unread,
Sep 26, 2012, 4:21:07 PM9/26/12
to sage-...@googlegroups.com

So should it be this instead:

        try:

            self(x)
        except TypeError:
            return False
        return self.base_ring().has_coerce_map_from(x.base_ring())

??

--
John
 

Simon King

unread,
Sep 26, 2012, 5:02:54 PM9/26/12
to sage-...@googlegroups.com
Hi Greg,

On 2012-09-26, Greg Laun <greg...@gmail.com> wrote:
>
> sage: matrix(GL(2),2,[1,0,0,1]) in GL(2,CC)
> True

Do you mean "GF(2)" on the left hand side?

>
> so 'in' ignores base ring. The problem is that __contains__ for
> general_linear.pyx and special_linear.pyx only check whether a matrix can
> be coerced:
> try:
> x = self(x)
> except TypeError:
> return False
> return True

That's not coercion, that's conversion. It should better rely on
coercion.

Why is the default implementation for parents not used? The default
implementation would be something like
try:
return x==self(x)
except TypeError:
return False

Hence, it would check whether conversion of x into self works, but
*in addition* the equality test involves coercion.

In the present case, self is GL(2,CC), and x is a matrix over GF(2), if
I didn't misinterprete your statement. But then, the default
implementation would give exactly what you want:
sage: m = matrix(GF(2),2,[1,0,0,1])
sage: n = GL(2,CC)(m)
sage: m==n
False

Best regards,
Simon

Simon King

unread,
Sep 26, 2012, 5:05:32 PM9/26/12
to sage-...@googlegroups.com
On 2012-09-26, Greg Laun <greg...@gmail.com> wrote:
> ------=_Part_757_16956430.1348690771123
> Content-Type: text/plain; charset=ISO-8859-1
>
> Whoops, gmail sent the email before I was finished.
>
> I think a more appropriate way to do the comparison would be to check
> if self(x) == x. This is how contains is implemented in parent.pyx.
>
> Any thoughts?

Oops, and I replied before reading your second posting.

Yes, I totally agree that the custom implementation of __contains__ for
matrix groups should comply with the default implementation.

Best regards,
Simon

John H Palmieri

unread,
Sep 26, 2012, 5:55:17 PM9/26/12
to sage-...@googlegroups.com


On Wednesday, September 26, 2012 1:19:31 PM UTC-7, Greg Laun wrote:
Whoops,  gmail sent the email before I was finished.  

 I think a more appropriate way to do the comparison would be to check if self(x) == x.  This is how contains is implemented in parent.pyx.

In theory this looks okay. In practice, I think there is work to be done:

    sage: m = identity_matrix(QQ, 2)
    sage: G = GL(2, QQ)
    sage: m == G(m)
    False 

Oh, maybe this is the right thing:

    sage: m == G(m).matrix()
    True

--
John

Greg Laun

unread,
Sep 26, 2012, 6:11:15 PM9/26/12
to sage-...@googlegroups.com


On Wednesday, September 26, 2012 5:03:15 PM UTC-4, Simon King wrote:
Hi Greg,

On 2012-09-26, Greg Laun <greg...@gmail.com> wrote:
>
> sage: matrix(GL(2),2,[1,0,0,1]) in GL(2,CC)
> True

Do you mean "GF(2)" on the left hand side?

Ahh, yes I did mean that.
 

>
> so 'in' ignores base ring.  The problem is that __contains__ for
> general_linear.pyx and special_linear.pyx only check whether a matrix can
> be coerced:      
>  try:
>             x = self(x)
>         except TypeError:
>             return False
>         return True

That's not coercion, that's conversion. It should better rely on
coercion.

You're right, thanks.
 

Why is the default implementation for parents not used? The default
implementation would be something like
  try:
      return x==self(x)
  except TypeError:
      return False

Hence, it would check whether conversion of x into self works, but
*in addition* the equality test involves coercion.

In the present case, self is GL(2,CC), and x is a matrix over GF(2), if
I didn't misinterprete your statement. But then, the default
implementation would give exactly what you want:
  sage: m = matrix(GF(2),2,[1,0,0,1])
  sage: n = GL(2,CC)(m)
  sage: m==n
  False

Best regards,
Simon

John's suggestion above  of using  m == self(m).matrix() gives the correct behavior for the few examples I was able to cook up. 

Greg
 
Reply all
Reply to author
Forward
0 new messages