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.Sat, 07 Mar 2020 08:30:56 +0100retrieving word for free group with custom set of generatorshttps://ask.sagemath.org/question/50178/retrieving-word-for-free-group-with-custom-set-of-generators/I would like to define a free group on a custom ordered list of symbols, like say
Frgp=Groups().free([f'a{j}' for j in range (5,10)])
i.e. `a5` up to `a9` (say). It's important that the list be defined programmatically as above, as the number of generators changes throughout the program run (their labels also need to change).
I would then like to recover elements of the group by evaluating at numbered lists, as in `Frgp([1,2])` to get back `a5*a6`, say, as suggested in the [docs](http://doc.sagemath.org/html/en/reference/groups/sage/groups/free_group.html). This syntax of `Frgp(<numerical list>)`, however, only works when I define the group via one of the methods
Frgp.<x,y,z> = Groups().free()
or
Frgp=Groups().free(3,'t')
which are not flexible enough to allow me to interpolate strings in variable names (or are they? I have tried all sorts of `eval`/`exec` trickery to no avail).
If I define the group in my preferred style (first code display above), then
sage: Frgp=Groups().free([f'a{j}' for j in range (5,10)])
sage: Frgp([1,2])
results in an error:
TypeError: 'sage.rings.integer.Integer' object is not iterable
---
**Edit:**
To clarify, I am aware that the goal I alluded to above can be achieved by other means. For instance, I can do
sage: Frgp=Groups().free(5,'t')
sage: Frgp([1,2])([var(f'a{j}') for j in range (5,10)])
a5*a6
So in other words, I can create a list of symbolic variables and apply a free-group word to that. But this seems like a clumsy workaround. It would be preferable to work in the free group on the desired set of symbolic variables to begin with.Fri, 06 Mar 2020 23:36:47 +0100https://ask.sagemath.org/question/50178/retrieving-word-for-free-group-with-custom-set-of-generators/Answer by grobber for <p>I would like to define a free group on a custom ordered list of symbols, like say </p>
<pre><code>Frgp=Groups().free([f'a{j}' for j in range (5,10)])
</code></pre>
<p>i.e. <code>a5</code> up to <code>a9</code> (say). It's important that the list be defined programmatically as above, as the number of generators changes throughout the program run (their labels also need to change). </p>
<p>I would then like to recover elements of the group by evaluating at numbered lists, as in <code>Frgp([1,2])</code> to get back <code>a5*a6</code>, say, as suggested in the <a href="http://doc.sagemath.org/html/en/reference/groups/sage/groups/free_group.html">docs</a>. This syntax of <code>Frgp(<numerical list>)</code>, however, only works when I define the group via one of the methods</p>
<pre><code>Frgp.<x,y,z> = Groups().free()
</code></pre>
<p>or </p>
<pre><code>Frgp=Groups().free(3,'t')
</code></pre>
<p>which are not flexible enough to allow me to interpolate strings in variable names (or are they? I have tried all sorts of <code>eval</code>/<code>exec</code> trickery to no avail).</p>
<p>If I define the group in my preferred style (first code display above), then </p>
<pre><code>sage: Frgp=Groups().free([f'a{j}' for j in range (5,10)])
sage: Frgp([1,2])
</code></pre>
<p>results in an error: </p>
<pre><code>TypeError: 'sage.rings.integer.Integer' object is not iterable
</code></pre>
<hr>
<p><strong>Edit:</strong></p>
<p>To clarify, I am aware that the goal I alluded to above can be achieved by other means. For instance, I can do </p>
<pre><code>sage: Frgp=Groups().free(5,'t')
sage: Frgp([1,2])([var(f'a{j}') for j in range (5,10)])
a5*a6
</code></pre>
<p>So in other words, I can create a list of symbolic variables and apply a free-group word to that. But this seems like a clumsy workaround. It would be preferable to work in the free group on the desired set of symbolic variables to begin with.</p>
https://ask.sagemath.org/question/50178/retrieving-word-for-free-group-with-custom-set-of-generators/?answer=50180#post-id-50180What ended up working: once I have the list of symbols, as in
sage: lst=tuple(f'a{j}' for j in range(5,10))
the free group can be constructed by passing the list via the key word argument `names` rather than directly:
sage: Frgp=Groups().free(names=lst)
Then:
sage: Frgp([1,2])
a5*a6
`tuple()` isn't crucial in the definition of `lst`: `list()` works just as well.
The point is there's a difference between `Groups().free(names=lst)` and `Groups().free(lst)` that I don't quite fully grok at the moment, but the former will do what I want whereas the latter wasn't:
sage: Groups().free([var('a')])
Free group indexed by {a}
sage: Groups().free(names=[var('a')])
Free Group on generators {a}
As you can see, `sage` reports slightly different output depending on whether the list is passed via `names=` or directly.Sat, 07 Mar 2020 08:30:56 +0100https://ask.sagemath.org/question/50178/retrieving-word-for-free-group-with-custom-set-of-generators/?answer=50180#post-id-50180