On Mon, Nov 19, 2018 at 11:58 AM John Cremona <john.crem...@gmail.com> wrote: > > I recommend importing anything you need from sage.all since the details of > where everything is might change in time. This works perfectly well: > > $ sage -python # so we use Sage's python not my system-wide one > Python 2.7.15 (default, Nov 2 2018, 14:32:42) > [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> from sage.all import PolynomialRing, QQ > >>> R = PolynomialRing(QQ,'x') > >>> x = R.gen() > >>> f = x^3+1 # no good as no preparser > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File "sage/structure/element.pyx", line 944, in > sage.structure.element.Element.__xor__ > (build/cythonized/sage/structure/element.c:9006) > RuntimeError: Use ** for exponentiation, not '^', which means xor > in Python, and has the wrong precedence. > > # Now we can work with this: > > >>> f = x**3+1 # that's better > >>> f.factor() > (x + 1) * (x^2 - x + 1) > > Another thing I do is to put my commands into a .py file and run pyflakes on > it, as that reveals what things need to be imported. Then import them all > from sage.all.
you can also find out exactly what you need to import: sage: import_statements("QQ") from sage.rings.rational_field import QQ > > John Cremona > > On Mon, 19 Nov 2018 at 11:48, Kolen Cheung <christian.ko...@gmail.com> wrote: >> >> 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. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "sage-support" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to sage-support+unsubscr...@googlegroups.com. >> To post to this group, send email to sage-support@googlegroups.com. >> Visit this group at https://groups.google.com/group/sage-support. >> For more options, visit https://groups.google.com/d/optout. > > -- > You received this message because you are subscribed to the Google Groups > "sage-support" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sage-support+unsubscr...@googlegroups.com. > To post to this group, send email to sage-support@googlegroups.com. > Visit this group at https://groups.google.com/group/sage-support. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "sage-support" group. To unsubscribe from this group and stop receiving emails from it, send an email to sage-support+unsubscr...@googlegroups.com. To post to this group, send email to sage-support@googlegroups.com. Visit this group at https://groups.google.com/group/sage-support. For more options, visit https://groups.google.com/d/optout.