Hi, I’m trying to translate this Sage syntax to Python syntax (i.e. using sage as a Python library.) But I got stuck even on the first command.
In Sage,
>>> R.<x> = QQ[]
>>> type(R)
<class 'sage.rings.polynomial.polynomial_ring.PolynomialRing_field_with_category'>
Then I thought I can import it in Python like this:
import sage.rings
# OK
sage.rings.polynomial.polynomial_ring.PolynomialRing_field
# AttributeError
sage.rings.polynomial.polynomial_ring.PolynomialRing_field_with_category
How would you write the same program in Python using sage as a library? And in general I see many unfamiliar syntax (from Python’s point of view) like R.<x>
, QQ[]
, (0..20)
, etc. Do you think it is realistic to use sage as a Python library and completely not using sage (as an interpreter) itself?
c.f. Documentation on using sage as a library?
Thanks.
--
Thank you both for the answers. However, I’m still stuck:
Focusing on just translating the first line: R.<x> = QQ[]
In sage,
>>> preparse("R.<x> = QQ[]")
"R = QQ['x']; (x,) = R._first_ngens(1)"
>>> import_statements(QQ)
# ** Warning **: several names for that object: Q, QQ
from sage.rings.rational_field import Q
>>> import_statements(R)
from sage.rings.qqbar import QQx
Immediately I have a question: how come the import_statements of QQ is ... import Q
, and the import statement of R is ... import QQx
? In either case the namespace in question is not imported. Does it mean ... import Q as QQ
and ... import QQx as R
respectively?
Then I’ve a problem: In sage, I can import them using these import statements. But when entering these 2 import statements in Python,
>>> from sage.rings.rational_field import Q
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-0fb4c40d1a13> in <module>()
----> 1 from sage.rings.rational_field import Q
/usr/lib/python2.7/site-packages/sage/rings/rational_field.py in <module>()
62 _long_type = int
63
---> 64 from .rational import Rational
65 from .integer import Integer
66
/usr/lib/python2.7/site-packages/sage/rings/rational.pyx in init sage.rings.rational (build/cythonized/sage/rings/rational.c:40976)()
94
95
---> 96 import sage.rings.real_mpfr
97 import sage.rings.real_double
98 from libc.stdint cimport uint64_t
/usr/lib/python2.7/site-packages/sage/rings/real_mpfr.pyx in init sage.rings.real_mpfr (build/cythonized/sage/rings/real_mpfr.c:44298)()
----> 1 r"""
2 Arbitrary Precision Real Numbers
3
4 AUTHORS:
5
/usr/lib/python2.7/site-packages/sage/rings/complex_number.pxd in init sage.libs.mpmath.utils (build/cythonized/sage/libs/mpmath/utils.c:8831)()
4 from .real_mpfr cimport RealNumber
5
----> 6 cdef class ComplexNumber(sage.structure.element.FieldElement):
7 cdef mpfr_t __re
8 cdef mpfr_t __im
/usr/lib/python2.7/site-packages/sage/rings/complex_double.pxd in init sage.rings.complex_number (build/cythonized/sage/rings/complex_number.c:24212)()
8
9
---> 10 cdef class ComplexDoubleField_class(sage.rings.ring.Field):
11 pass
12
/usr/lib/python2.7/site-packages/sage/rings/complex_double.pyx in init sage.rings.complex_double (build/cythonized/sage/rings/complex_double.c:24230)()
96 from cypari2.convert cimport new_gen_from_double, new_t_COMPLEX_from_double
97
---> 98 from . import complex_number
99
100 from .complex_field import ComplexField
ImportError: cannot import name complex_number
>>> from sage.rings.qqbar import QQx
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-4809457c5588> in <module>()
----> 1 from sage.rings.qqbar import QQx
/usr/lib/python2.7/site-packages/sage/rings/qqbar.py in <module>()
512 rich_to_bool, richcmp_not_equal,
513 op_EQ, op_NE)
--> 514 from sage.rings.real_mpfr import RR
515 from sage.rings.real_mpfi import RealIntervalField, RIF, is_RealIntervalFieldElement, RealIntervalField_class
516 from sage.rings.complex_field import ComplexField
/usr/lib/python2.7/site-packages/sage/rings/real_mpfr.pyx in init sage.rings.real_mpfr (build/cythonized/sage/rings/real_mpfr.c:44298)()
----> 1 r"""
2 Arbitrary Precision Real Numbers
3
4 AUTHORS:
5
/usr/lib/python2.7/site-packages/sage/rings/complex_number.pxd in init sage.libs.mpmath.utils (build/cythonized/sage/libs/mpmath/utils.c:8831)()
4 from .real_mpfr cimport RealNumber
5
----> 6 cdef class ComplexNumber(sage.structure.element.FieldElement):
7 cdef mpfr_t __re
8 cdef mpfr_t __im
/usr/lib/python2.7/site-packages/sage/rings/complex_double.pxd in init sage.rings.complex_number (build/cythonized/sage/rings/complex_number.c:24212)()
8
9
---> 10 cdef class ComplexDoubleField_class(sage.rings.ring.Field):
11 pass
12
/usr/lib/python2.7/site-packages/sage/rings/complex_double.pyx in init sage.rings.complex_double (build/cythonized/sage/rings/complex_double.c:24230)()
96 from cypari2.convert cimport new_gen_from_double, new_t_COMPLEX_from_double
97
---> 98 from . import complex_number
99
100 from .complex_field import ComplexField
ImportError: cannot import name complex_number
Thanks.
--
I wonder why for me the result is
>>> import_statements(QQ)
# ** Warning **: several names for that object: Q, QQ from sage.rings.rational_field import Q
And if anyone is dictating this, that one is surely not you, even if you're the OP. Shut up.
"shut up" is language issue? You dont know whats bad language.
And if anyone has language issue it surely is OP. hijack vs shut up, which one is more serious? Accusing someone hijacking something is a very serious accusation.
The Sage community is comprised of an international mixture of mathematicians, computer scientists, engineers, researchers, teachers, amateurs, and others with varied backgrounds. This diversity is one of our strengths, but it can also lead to communication problems and unhappiness. People who love working on Sage can more effectively collaborate with others if they follow this code.
1) Be friendly and patient.2) Be welcoming.
3) Be considerate.
4) Be respectful and polite.
How can I speed up this computation?H.<i,j,k> = QuaternionAlgebra(SR, -1, -1)def Q(a, b, c, d): return H(a + b*i + c*j + d*k)def P(n): return Q(x+1,1,1,1)*P(n-1) if n > 0 else Q(1,0,0,0)def p(n): return P(n)[0].list()for n in (0..20): print [n], p(n)[0] [1][1] [1, 1][2] [-2, 2, 1][3] [-8, -6, 3, 1][4] [-8, -32, -12, 4, 1][5] [16, -40, -80, -20, 5, 1][6] [64, 96, -120, -160, -30, 6, 1]...With Mathematica this takes 6 sec, with Sage it takeshours, (in fact I interrupted after n=15).Thanks, Peter