ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 27 Sep 2016 04:26:58 +0200character table of a finite fieldhttps://ask.sagemath.org/question/34899/character-table-of-a-finite-field/I am trying to calculate the character table of a finite field. The following is my code:
def character_table(q,n):
k=GF(q^n)
tr(x)=sum(x^(q^i) for i in [0..n-1])
ksi = e^(2*pi*I/n)
chi(x)=ksi^(tr(x))
for a in k:
L_a = [chi(a*x) for x in k]
print L_a
And the TypeError is "free variable 'x' referenced before assignment in enclosing scope". Can somebody help me how to fix the code?
Thanks
> BlockquoteThu, 22 Sep 2016 23:32:34 +0200https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/Comment by slelievre for <p>I am trying to calculate the character table of a finite field. The following is my code:</p>
<pre><code>def character_table(q,n):
k=GF(q^n)
tr(x)=sum(x^(q^i) for i in [0..n-1])
ksi = e^(2*pi*I/n)
chi(x)=ksi^(tr(x))
for a in k:
L_a = [chi(a*x) for x in k]
print L_a
</code></pre>
<p>And the TypeError is "free variable 'x' referenced before assignment in enclosing scope". Can somebody help me how to fix the code?</p>
<p>Thanks</p>
<blockquote>
<p>Blockquote</p>
</blockquote>
https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?comment=34902#post-id-34902Beyond the error that you are getting, here are some comments.
1. The function definition's body (everything below the first line) should be indented four spaces.
2. You define `L` but never use it.
3. Are `k` and `K` supposed to be the same?Fri, 23 Sep 2016 09:12:11 +0200https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?comment=34902#post-id-34902Comment by kcrisman for <p>I am trying to calculate the character table of a finite field. The following is my code:</p>
<pre><code>def character_table(q,n):
k=GF(q^n)
tr(x)=sum(x^(q^i) for i in [0..n-1])
ksi = e^(2*pi*I/n)
chi(x)=ksi^(tr(x))
for a in k:
L_a = [chi(a*x) for x in k]
print L_a
</code></pre>
<p>And the TypeError is "free variable 'x' referenced before assignment in enclosing scope". Can somebody help me how to fix the code?</p>
<p>Thanks</p>
<blockquote>
<p>Blockquote</p>
</blockquote>
https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?comment=34904#post-id-34904You are aware that Sage has finite group character tables via GAP as well, correct?Fri, 23 Sep 2016 14:11:36 +0200https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?comment=34904#post-id-34904Comment by xhimi for <p>I am trying to calculate the character table of a finite field. The following is my code:</p>
<pre><code>def character_table(q,n):
k=GF(q^n)
tr(x)=sum(x^(q^i) for i in [0..n-1])
ksi = e^(2*pi*I/n)
chi(x)=ksi^(tr(x))
for a in k:
L_a = [chi(a*x) for x in k]
print L_a
</code></pre>
<p>And the TypeError is "free variable 'x' referenced before assignment in enclosing scope". Can somebody help me how to fix the code?</p>
<p>Thanks</p>
<blockquote>
<p>Blockquote</p>
</blockquote>
https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?comment=34909#post-id-34909@slelievre I fixed all your suggestions. Now I get "free variable 'x' referenced before assignment in enclosing scope".
@kcrisman I tried that but I couldn't get the right syntax. Could you please help? It seems that character_table() does not have an attribute for abelian groups. Also, since I am new in sage, I thought it was worth it to try and define a function from scratch. I also having difficulties on using only the additive group of a finite field.Fri, 23 Sep 2016 17:39:15 +0200https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?comment=34909#post-id-34909Comment by kcrisman for <p>I am trying to calculate the character table of a finite field. The following is my code:</p>
<pre><code>def character_table(q,n):
k=GF(q^n)
tr(x)=sum(x^(q^i) for i in [0..n-1])
ksi = e^(2*pi*I/n)
chi(x)=ksi^(tr(x))
for a in k:
L_a = [chi(a*x) for x in k]
print L_a
</code></pre>
<p>And the TypeError is "free variable 'x' referenced before assignment in enclosing scope". Can somebody help me how to fix the code?</p>
<p>Thanks</p>
<blockquote>
<p>Blockquote</p>
</blockquote>
https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?comment=34921#post-id-34921Ah, then see my answer (which is just a link).Sat, 24 Sep 2016 22:53:02 +0200https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?comment=34921#post-id-34921Comment by xhimi for <p>I am trying to calculate the character table of a finite field. The following is my code:</p>
<pre><code>def character_table(q,n):
k=GF(q^n)
tr(x)=sum(x^(q^i) for i in [0..n-1])
ksi = e^(2*pi*I/n)
chi(x)=ksi^(tr(x))
for a in k:
L_a = [chi(a*x) for x in k]
print L_a
</code></pre>
<p>And the TypeError is "free variable 'x' referenced before assignment in enclosing scope". Can somebody help me how to fix the code?</p>
<p>Thanks</p>
<blockquote>
<p>Blockquote</p>
</blockquote>
https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?comment=34953#post-id-34953@slelievre Please see the edited question.Mon, 26 Sep 2016 18:17:01 +0200https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?comment=34953#post-id-34953Answer by slelievre for <p>I am trying to calculate the character table of a finite field. The following is my code:</p>
<pre><code>def character_table(q,n):
k=GF(q^n)
tr(x)=sum(x^(q^i) for i in [0..n-1])
ksi = e^(2*pi*I/n)
chi(x)=ksi^(tr(x))
for a in k:
L_a = [chi(a*x) for x in k]
print L_a
</code></pre>
<p>And the TypeError is "free variable 'x' referenced before assignment in enclosing scope". Can somebody help me how to fix the code?</p>
<p>Thanks</p>
<blockquote>
<p>Blockquote</p>
</blockquote>
https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?answer=34901#post-id-34901Some tips:
- Instead of `sum[...]` you want `sum(...)`.
- Instead of `tr(x) = ...`, use `def tr(x): return ...`
This might be what you were trying to write:
def character_table(q, n):
k = GF(q^n)
def tr(x):
return sum(x^(q^i) for i in [0..n-1])
ksi = e^(2*pi*I/n)
def chi(x):
return ksi^(tr(x))
for a in k:
L_a = [chi(a*x) for x in k]
print L_a
Calling this function prints something:
sage: character_table(3, 2)
[1, 1, 1, 1, 1, 1, 1, 1, 1]
[1, 1, -1, -1, 1, 1, 1, 1, -1]
[1, -1, -1, 1, 1, 1, 1, -1, 1]
[1, -1, 1, 1, 1, 1, -1, 1, -1]
[1, 1, 1, 1, 1, -1, 1, -1, -1]
[1, 1, 1, 1, -1, 1, -1, -1, 1]
[1, 1, 1, -1, 1, -1, -1, 1, 1]
[1, 1, -1, 1, -1, -1, 1, 1, 1]
[1, -1, 1, -1, -1, 1, 1, 1, 1]Fri, 23 Sep 2016 09:05:08 +0200https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?answer=34901#post-id-34901Answer by kcrisman for <p>I am trying to calculate the character table of a finite field. The following is my code:</p>
<pre><code>def character_table(q,n):
k=GF(q^n)
tr(x)=sum(x^(q^i) for i in [0..n-1])
ksi = e^(2*pi*I/n)
chi(x)=ksi^(tr(x))
for a in k:
L_a = [chi(a*x) for x in k]
print L_a
</code></pre>
<p>And the TypeError is "free variable 'x' referenced before assignment in enclosing scope". Can somebody help me how to fix the code?</p>
<p>Thanks</p>
<blockquote>
<p>Blockquote</p>
</blockquote>
https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?answer=34922#post-id-34922Alternately, see the solution at [this question](https://ask.sagemath.org/question/30501/how-do-i-create-the-character-table-of-the-group-of-units-mod-n/).Sat, 24 Sep 2016 22:53:38 +0200https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?answer=34922#post-id-34922Comment by xhimi for <p>Alternately, see the solution at <a href="https://ask.sagemath.org/question/30501/how-do-i-create-the-character-table-of-the-group-of-units-mod-n/">this question</a>.</p>
https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?comment=34957#post-id-34957@kcrisman Could you explain why the very same code does not work is I change the group $g$, say with, AdditiveAbelianGroup([2,2,2])? ThanksMon, 26 Sep 2016 18:59:12 +0200https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?comment=34957#post-id-34957Comment by kcrisman for <p>Alternately, see the solution at <a href="https://ask.sagemath.org/question/30501/how-do-i-create-the-character-table-of-the-group-of-units-mod-n/">this question</a>.</p>
https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?comment=34964#post-id-34964Hmm, not sure.Tue, 27 Sep 2016 04:26:58 +0200https://ask.sagemath.org/question/34899/character-table-of-a-finite-field/?comment=34964#post-id-34964