var() definition in finite fields

373 views
Skip to first unread message

Kim Schoener

unread,
Sep 30, 2014, 10:14:03 AM9/30/14
to sage-s...@googlegroups.com
Heya!

I want to do something relatively easy in Sage but can't figure out how. Hopefully you can help me. I want to do some symbolic operations (matrix/vector) in the GF(2). Let's start out with real numbers first:

(m1, m2, m3, m4) = (var("m1"), var("m2"), var("m3"), var("m4"))
q = Matrix([
    [m1, m2],
    [m3, m4],
])
print(q)
print(q * q)

Works pefectly:

[m1 m2]
[m3 m4]
[ m1^2 + m2*m3 m1*m2 + m2*m4]
[m1*m3 + m3*m4  m2*m3 + m4^2]

But when I try the same thing in GF(2) by definiing

q = Matrix(GF(2), [
    [m1, m2],
    [m3, m4],
])

I get:

[...]
  File "parent.pyx", line 1069, in sage.structure.parent.Parent.__call__ (sage/structure/parent.c:8546)
  File "coerce_maps.pyx", line 156, in sage.structure.coerce_maps.NamedConvertMap._call_ (sage/structure/coerce_maps.c:4930)
  File "expression.pyx", line 857, in sage.symbolic.expression.Expression._integer_ (sage/symbolic/expression.cpp:5877)
TypeError: unable to convert x (=m1) to an integer

However, the matrix definition seems to be okay, when I do

q = Matrix(GF(2), [
    [1, 1 ],
    [1, 0],
])
print(q * q)

I get

[0 1]
[1 1]

which is what I'd expect. Why does it not work with variables when working in GF(2) and how can I get this to work the way I want it to?

Thank you so much,
Regards,
Kim

Volker Braun

unread,
Sep 30, 2014, 11:04:10 AM9/30/14
to sage-s...@googlegroups.com
Anything symbolic is in the symbolic ring SR, finite field elements are in GF(2). You can wrap finite field elements in the symbolic ring if you want to do symbolic computations with finite field coefficients:

sage: SR(GF(5)(3)) * x
3*x
sage: _ * 2
x

though the symbolic elemnts still don't know anything about finite fields, they just carry the coefficients along.

Kim Schoener

unread,
Sep 30, 2014, 11:46:42 AM9/30/14
to sage-s...@googlegroups.com
I'm not sure I understand fully what you're saying. I did

m1 = SR(GF(2)(1)) * var("m1")
m2 = SR(GF(2)(1)) * var("m2")
m3 = SR(GF(2)(1)) * var("m3")
m4 = SR(GF(2)(1)) * var("m4")

but the Matrix definition


q = Matrix(GF(2), [
    [m1, m2],
    [m3, m4],
])

still results in the same error: "unable to convert x (=x1) to an integer".

How do I define a variable in the SR that I can work with? I can't seem to figure it out from the example you gave me.

Thank you,
Kim

Martin Albrecht

unread,
Sep 30, 2014, 12:51:45 PM9/30/14
to sage-s...@googlegroups.com
Your matrix is over GF(2) not over the symbolic ring SR:

sage: m1 = SR(GF(2)(1)) * var("m1")
sage: m2 = SR(GF(2)(1)) * var("m2")
sage: m3 = SR(GF(2)(1)) * var("m3")
sage: m4 = SR(GF(2)(1)) * var("m4")

sage: q = Matrix(SR, [
[m1, m2],
[m3, m4],
])

sage: q^2
[ m1^2 + m2*m3 m1*m2 + m2*m4]
[m1*m3 + m3*m4 m2*m3 + m4^2]


signature.asc

Peter Bruin

unread,
Sep 30, 2014, 2:45:07 PM9/30/14
to sage-s...@googlegroups.com
Hello,


I want to do some symbolic operations (matrix/vector) in the GF(2).

Here is an alternative approach (assuming all your expressions are polynomials in m1, m2, m3 and m4):

sage: R.<m1,m2,m3,m4> = PolynomialRing(GF(2))
sage: q = Matrix(R, [[m1, m2], [m3, m4]])
sage: q
[m1 m2]
[m3 m4]
sage: q*q

[ m1^2 + m2*m3 m1*m2 + m2*m4]
[m1*m3 + m3*m4  m2*m3 + m4^2]

Note that the coefficients are elements of R = GF(2)[m1, m2, m3, m4], not of GF(2); cf. the other answers where they are in SR.

Peter

Kim Schoener

unread,
Oct 1, 2014, 6:30:16 PM10/1/14
to sage-s...@googlegroups.com
Hi Peter, hi Martin,

somehow both approaches I think don't work for me. For example, the square (m1^2) is carried in both approaches, even though it can be simplified to m1 in GF(2). I would like sage to account for the GF(2) in order to simplify terms. For example I would expect that x * (x + 1) is simplified to 0 if x is a variable in GF(2).

Is there a way to do this or does sage lack that funcitonality?

Thank you,
Kim

Nils Bruin

unread,
Oct 1, 2014, 7:29:56 PM10/1/14
to sage-s...@googlegroups.com
On Wednesday, October 1, 2014 3:30:16 PM UTC-7, Kim Schoener wrote:
Hi Peter, hi Martin,

somehow both approaches I think don't work for me. For example, the square (m1^2) is carried in both approaches, even though it can be simplified to m1 in GF(2). I would like sage to account for the GF(2) in order to simplify terms. For example I would expect that x * (x + 1) is simplified to 0 if x is a variable in GF(2).

It means that you want to work modulo the ideal (m1^2-m1,m2^2-m2,m3^2-m3,m4^2-m4). You can use

sage: P.<m1,m2,m3,m4>=BooleanPolynomialRing()
sage: m1^2+m1
0
sage: q=matrix(2,2,[m1,m2,m3,m4])
sage: q^2
[   m1 + m2*m3 m1*m2 + m2*m4]
[m1*m3 + m3*m4    m2*m3 + m4]

 
Reply all
Reply to author
Forward
0 new messages