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, 03 Jan 2017 23:32:50 +0100get short name for QQ, RDF, AA etchttps://ask.sagemath.org/question/36150/get-short-name-for-qq-rdf-aa-etc/ I wonder whether there is a way to get back the *short name of the function* of the various rings and fields like QQ, RDF, AA, RLF, RR, etc. as a string.
For example
sage: a=QQ
sage: str(a)
'Rational Field'
But I am looking for something like:
sage: function_I_want(a)
'QQ'
I guess I could predefine a dictionary like this:
shortnames={eval(name):name for name in ['QQ', 'RDF', 'AA', 'RLF', 'RR']}
and then have a function
def function_I_want(a):
return shortnames[a]
But this seems a bit clumsy. Is there a better way to do this; or is there somewhere in the sage code such a dictionary already defined?
The reason I am thinking about this is the following tiny bug:
https://trac.sagemath.org/ticket/22132
Tue, 03 Jan 2017 21:15:23 +0100https://ask.sagemath.org/question/36150/get-short-name-for-qq-rdf-aa-etc/Comment by mf for <p>I wonder whether there is a way to get back the <em>short name of the function</em> of the various rings and fields like QQ, RDF, AA, RLF, RR, etc. as a string.</p>
<p>For example</p>
<pre><code>sage: a=QQ
sage: str(a)
'Rational Field'
</code></pre>
<p>But I am looking for something like:</p>
<pre><code>sage: function_I_want(a)
'QQ'
</code></pre>
<p>I guess I could predefine a dictionary like this:</p>
<pre><code>shortnames={eval(name):name for name in ['QQ', 'RDF', 'AA', 'RLF', 'RR']}
</code></pre>
<p>and then have a function</p>
<pre><code>def function_I_want(a):
return shortnames[a]
</code></pre>
<p>But this seems a bit clumsy. Is there a better way to do this; or is there somewhere in the sage code such a dictionary already defined?</p>
<p>The reason I am thinking about this is the following tiny bug:
<a href="https://trac.sagemath.org/ticket/22132">https://trac.sagemath.org/ticket/22132</a></p>
https://ask.sagemath.org/question/36150/get-short-name-for-qq-rdf-aa-etc/?comment=36155#post-id-36155thanks for the explanation!Tue, 03 Jan 2017 23:32:50 +0100https://ask.sagemath.org/question/36150/get-short-name-for-qq-rdf-aa-etc/?comment=36155#post-id-36155Comment by nbruin for <p>I wonder whether there is a way to get back the <em>short name of the function</em> of the various rings and fields like QQ, RDF, AA, RLF, RR, etc. as a string.</p>
<p>For example</p>
<pre><code>sage: a=QQ
sage: str(a)
'Rational Field'
</code></pre>
<p>But I am looking for something like:</p>
<pre><code>sage: function_I_want(a)
'QQ'
</code></pre>
<p>I guess I could predefine a dictionary like this:</p>
<pre><code>shortnames={eval(name):name for name in ['QQ', 'RDF', 'AA', 'RLF', 'RR']}
</code></pre>
<p>and then have a function</p>
<pre><code>def function_I_want(a):
return shortnames[a]
</code></pre>
<p>But this seems a bit clumsy. Is there a better way to do this; or is there somewhere in the sage code such a dictionary already defined?</p>
<p>The reason I am thinking about this is the following tiny bug:
<a href="https://trac.sagemath.org/ticket/22132">https://trac.sagemath.org/ticket/22132</a></p>
https://ask.sagemath.org/question/36150/get-short-name-for-qq-rdf-aa-etc/?comment=36151#post-id-36151The name `QQ` is not an intrinsic property of the field of rational numbers. It's just that for convenience, the default toplevel environment has the binding `QQ=RationalField()`. You cannot really let the Rational Field depend on it. For the bug you're referring to: You cannot assume rings have a "short, globally defined name". There are infinitely many possible finite fields and number fields already, in different representations.Tue, 03 Jan 2017 21:30:51 +0100https://ask.sagemath.org/question/36150/get-short-name-for-qq-rdf-aa-etc/?comment=36151#post-id-36151Answer by tmonteil for <p>I wonder whether there is a way to get back the <em>short name of the function</em> of the various rings and fields like QQ, RDF, AA, RLF, RR, etc. as a string.</p>
<p>For example</p>
<pre><code>sage: a=QQ
sage: str(a)
'Rational Field'
</code></pre>
<p>But I am looking for something like:</p>
<pre><code>sage: function_I_want(a)
'QQ'
</code></pre>
<p>I guess I could predefine a dictionary like this:</p>
<pre><code>shortnames={eval(name):name for name in ['QQ', 'RDF', 'AA', 'RLF', 'RR']}
</code></pre>
<p>and then have a function</p>
<pre><code>def function_I_want(a):
return shortnames[a]
</code></pre>
<p>But this seems a bit clumsy. Is there a better way to do this; or is there somewhere in the sage code such a dictionary already defined?</p>
<p>The reason I am thinking about this is the following tiny bug:
<a href="https://trac.sagemath.org/ticket/22132">https://trac.sagemath.org/ticket/22132</a></p>
https://ask.sagemath.org/question/36150/get-short-name-for-qq-rdf-aa-etc/?answer=36152#post-id-36152You can use the `sage_input` function, which, luckily selects the short name among the possible constructions:
sage: a = RationalField()
sage: sage_input(a)
QQ
It seems however not to work with `RLF`:
sage: for obj in [QQ, RDF, AA, RR, ZZ, QQbar, RLF]:
....: print sage_input(obj)
QQ
RDF
AA
RR
ZZ
QQbar
ValueError: Can't convert Real Lazy Field to sage_input formTue, 03 Jan 2017 21:40:10 +0100https://ask.sagemath.org/question/36150/get-short-name-for-qq-rdf-aa-etc/?answer=36152#post-id-36152