Example #1
0
def simple_compare(expr1, expr2, vars=[]):
    """ Performs a simple symbolic comparison between two expressions.
        This method will perform a very simple comparision between two
        algebraic expressions. No expansion or factorization is performed
        hence expressions like "(x+1)^2" and "(1+x)^2" will evaluate as
        the same but expressions like "(x+1)^2" and "x^2+2*x+1" will not.
        @type expr1: string
        @param expr1: the first expression to compare
        @type expr2: string
        @param expr2: the second expression to compare
        @type vars: array
        @param vars: an array of variable names used in the expressions
        @rtype: bool
        @return: true if the expressions are equal, false otherwise
    """
    web.debug("Simple compare of '%s' to '%s' with variables %s" %
              (expr1, expr2, vars))
    expr1 = replace_variables(expr1, vars)
    expr2 = replace_variables(expr2, vars)
    expr1 = algebra.convert_latex(expr1)
    expr2 = algebra.convert_latex(expr2)

    sage.misc.preparser.implicit_multiplication(10)
    web.debug("Expr1: " + expr1)
    web.debug("Expr2: " + expr2)
    web.debug("Level: " + str(sage.misc.preparser.implicit_multiplication()))
    expr1 = preparse(expr1).replace('Integer', '').replace('RealNumber', '')
    expr2 = preparse(expr2).replace('Integer', '').replace('RealNumber', '')
    web.debug("Expr1: " + expr1)
    web.debug("Expr2: " + expr2)
    f = symbolic_expression_from_string("(%s)-(%s)" % (expr1, expr2))
    # Simply check to see if the representation of the expression is
    # a string containing the single character '0'.
    result = {'expr1': expr1, 'expr2': expr2, 'result': (f.__repr__() == '0')}
    return result
Example #2
0
def full_compare(answer, response, vars=[], exclude=[]):
    """ Performs a full symbolic comparison between two expressions.
        This method will perform a full comparison between the two
        expressions passed to it. This will involve fully simplifying each
        and thencomparing to see if the result is the same. A list of the
        variables used must also be passed.

        Optionally a list of excluded expressions can also be passed. If
        the response matches one of the excluded expressions, using the
        simple compare method, then the comparison will return false. This
        is to allow questions such as "expand (x+1)^2" to stop the
        student just entering "(x+1)^2" and have SAGE accept it as a
        match to the correct answer.
        @type answer: string
        @param answer: the answer expression to compare against
        @type response: string
        @param response: the response expression entered by the student
        @type vars: array
        @param vars: an array of variable names used in the expressions
        @type exclude: array
        @param exclude: an array of expressions to exclude as invalid
        @rtype: bool
        @return: true if the expressions are equal, false otherwise
    """
    web.debug(
        "Full compare of '%s' to '%s' with variables '%s' excluding '%s'" %
        (answer, response, ','.join(vars), ';'.join(exclude)))
    try:
        answer = replace_variables(answer, vars)
        response = replace_variables(response, vars)
        answer = algebra.convert_latex(answer)
        response = algebra.convert_latex(response)
        sage.misc.preparser.implicit_multiplication(10)
        web.debug("Answer: " + answer)
        web.debug("Response: " + response)
        web.debug("Level: " +
                  str(sage.misc.preparser.implicit_multiplication()))
        answer = preparse(answer).replace('Integer',
                                          '').replace('RealNumber', '')
        response = preparse(response).replace('Integer',
                                              '').replace('RealNumber', '')
        web.debug("Answer: " + answer)
        web.debug("Response: " + response)
        answer_expr = symbolic_expression_from_string(answer)
        response_expr = symbolic_expression_from_string(response)
    except SyntaxError, e:
        web.debug("Error parsing answer and response expressions: %s" % e)
        f = "Error parsing answer and response expressions: %s" % e
        return f
Example #3
0
def SagePreparseTransformer(line):
    r"""
    EXAMPLES::

        sage: from sage.repl.interpreter import SagePreparseTransformer
        sage: spt = SagePreparseTransformer()
        sage: spt.push('1+1r+2.3^2.3r')
        "Integer(1)+1+RealNumber('2.3')**2.3"
        sage: preparser(False)
        sage: spt.push('2.3^2')
        '2.3^2'

    TESTS:

    Check that syntax errors in the preparser do not crash IPython,
    see :trac:`14961`. ::

        sage: preparser(True)
        sage: bad_syntax = "R.<t> = QQ{]"
        sage: preparse(bad_syntax)
        Traceback (most recent call last):
        ...
        SyntaxError: Mismatched ']'
        sage: from sage.repl.interpreter import get_test_shell
        sage: shell = get_test_shell()
        sage: shell.run_cell(bad_syntax)
          File "<string>", line unknown
        SyntaxError: Mismatched ']'
        <BLANKLINE>
    """
    if _do_preparse and not line.startswith('%'):
        return preparse(line)
    else:
        return line
    def taylor_series(self):
        """ Produces the taylor series expansion of the query with respect to
        the first free symbol it find (i.e. arbitrarily)

            >>> expression = Expression("((x^k)/k!) exp(-x)")
            >>> expression.taylor_series()
            'exp(-x) + k*(exp(-x)*log(x) + 0.577215664901533*exp(-x)) + k**2*(0.5*exp(-x)*log(x)**2 + 0.577215664901533*exp(-x)*log(x) - 0.655878071520254*exp(-x)) + O(k**3)'

        """
        expression = sympy_parser.parse_expr(preparser.preparse(self.__query))
        symbols = list(expression.free_symbols)
        if len(symbols) == 0:
            # no variables, so it's just a constant
            return sympy.N(expression)
        else:
            # arbitrarily pick the first variable to vary against

            # TODO at some point we are going to want the taylor series
            # expansion against each variable

            # TODO order is also aribtrary right now, later we're going to want
            # to vary it based upon required accuracy
            symbol = symbols[0]
            order = 5
            series_expansion = expression.series(symbol, 0, order)
            return str(sympy.N(series_expansion))
Example #5
0
def completions(s, globs, format=False, width=90, system="None"):
    """
    Return a list of completions in the given context.

    INPUT:

    - ``globs`` - a string:object dictionary; context in which to
      search for completions, e.g., :func:`globals()`

    - ``format`` - a bool (default: False); whether to tabulate the
      list
    
    - ``width`` - an int; character width of the table
    
    - ``system`` - a string (default: 'None'); system prefix for the
      completions

    OUTPUT:

    - a list of strings, if ``format`` is False, or a string
    """
    if system not in ['sage', 'python']:
        prepend = system + '.'
        s = prepend + s
    else:
        prepend = ''
    n = len(s)
    if n == 0:
        return '(empty string)'
    try:
        if not '.' in s and not '(' in s:
            v = [x for x in globs.keys() if x[:n] == s] + \
                [x for x in __builtins__.keys() if x[:n] == s] 
        else:
            if not ')' in s:
                i = s.rfind('.')
                method = s[i+1:]
                obj = s[:i]
                n = len(method)
            else:
                obj = preparse(s)
                method = ''
            try:
                O = eval(obj, globs)
                D = dir(O)
                try:
                    D += O.trait_names()
                except (AttributeError, TypeError):
                    pass
                if method == '':
                    v = [obj + '.'+x for x in D if x and x[0] != '_']
                else:
                    v = [obj + '.'+x for x in D if x[:n] == method]
            except Exception, msg:
                v = []
        v = list(set(v))   # make unique
        v.sort()
Example #6
0
def completions(s, globs, format=False, width=90, system="None"):
    """
    Return a list of completions in the given context.

    INPUT:

    - ``globs`` - a string:object dictionary; context in which to
      search for completions, e.g., :func:`globals()`

    - ``format`` - a bool (default: False); whether to tabulate the
      list
    
    - ``width`` - an int; character width of the table
    
    - ``system`` - a string (default: 'None'); system prefix for the
      completions

    OUTPUT:

    - a list of strings, if ``format`` is False, or a string
    """
    if system not in ['sage', 'python']:
        prepend = system + '.'
        s = prepend + s
    else:
        prepend = ''
    n = len(s)
    if n == 0:
        return '(empty string)'
    try:
        if not '.' in s and not '(' in s:
            v = [x for x in globs.keys() if x[:n] == s] + \
                [x for x in __builtins__.keys() if x[:n] == s]
        else:
            if not ')' in s:
                i = s.rfind('.')
                method = s[i + 1:]
                obj = s[:i]
                n = len(method)
            else:
                obj = preparse(s)
                method = ''
            try:
                O = eval(obj, globs)
                D = dir(O)
                try:
                    D += O.trait_names()
                except (AttributeError, TypeError):
                    pass
                if method == '':
                    v = [obj + '.' + x for x in D if x and x[0] != '_']
                else:
                    v = [obj + '.' + x for x in D if x[:n] == method]
            except Exception, msg:
                v = []
        v = list(set(v))  # make unique
        v.sort()
Example #7
0
def runsnake(command):
    """
    Graphical profiling with ``runsnake``

    INPUT:

    - ``command`` -- the command to be run as a string.

    EXAMPLES::

        sage: runsnake("list(SymmetricGroup(3))")        # optional - runsnake

    ``command`` is first preparsed (see :func:`preparse`)::

        sage: runsnake('for x in range(1,4): print x^2') # optional - runsnake
        1
        4
        9

    :func:`runsnake` requires the program ``runsnake``. Due to non
    trivial dependencies (python-wxgtk, ...), installing it within the
    Sage distribution is unpractical. Hence, we recommend installing
    it with the system wide Python. On Ubuntu 10.10, this can be done
    with::

        > sudo apt-get install python-profiler python-wxgtk2.8 python-setuptools
        > sudo easy_install RunSnakeRun

    See the ``runsnake`` website for instructions for other platforms.

    :func:`runsnake` further assumes that the system wide Python is
    installed in ``/usr/bin/python``.

    .. seealso::

        - `The runsnake website <http://www.vrplumber.com/programming/runsnakerun/>`_
        - ``%prun``
        - :class:`Profiler`

    """
    import cProfile, os
    from sage.misc.misc import tmp_filename, get_main_globals
    from sage.misc.preparser import preparse
    tmpfile = tmp_filename()
    cProfile.runctx(preparse(command.lstrip().rstrip()),
                    get_main_globals(),
                    locals(),
                    filename=tmpfile)
    os.system("/usr/bin/python -E `which runsnake` %s &" % tmpfile)
Example #8
0
def runsnake(command):
    """
    Graphical profiling with ``runsnake``

    INPUT:

    - ``command`` -- the command to be run as a string.

    EXAMPLES::

        sage: runsnake("list(SymmetricGroup(3))")        # optional - runsnake

    ``command`` is first preparsed (see :func:`preparse`)::

        sage: runsnake('for x in range(1,4): print x^2') # optional - runsnake
        1
        4
        9

    :func:`runsnake` requires the program ``runsnake``. Due to non
    trivial dependencies (python-wxgtk, ...), installing it within the
    Sage distribution is unpractical. Hence, we recommend installing
    it with the system wide Python. On Ubuntu 10.10, this can be done
    with::

        > sudo apt-get install python-profiler python-wxgtk2.8 python-setuptools
        > sudo easy_install RunSnakeRun

    See the ``runsnake`` website for instructions for other platforms.

    :func:`runsnake` further assumes that the system wide Python is
    installed in ``/usr/bin/python``.

    .. seealso::

        - `The runsnake website <http://www.vrplumber.com/programming/runsnakerun/>`_
        - ``%prun``
        - :class:`Profiler`

    """
    import cProfile, os
    from sage.misc.misc import tmp_filename, get_main_globals
    from sage.misc.preparser import preparse
    tmpfile = tmp_filename()
    cProfile.runctx(preparse(command.lstrip().rstrip()), get_main_globals(), locals(), filename=tmpfile)
    os.system("/usr/bin/python -E `which runsnake` %s &"%tmpfile)
Example #9
0
 def readfunc(self, prompt=''):
     line = raw_input(prompt)
     if prompt == getattr(sys, 'ps1', '>>> '):
         tokens = line.split()
         if line == '' and self.enter_advances:
             tokens = ('next',)
         if tokens and tokens[0] in self.expose:
             fn = getattr(self, tokens[0])
             if len(tokens) != len(inspect.getargspec(fn)[0]):
                 print "usage: %s %s" % (
                     tokens[0], ' '.join(inspect.getargspec(fn)[0][1:]))
             else:
                 self._add_history(line)
                 fn(*tokens[1:])
             return ''
         else:
             # if it not a special, we need to run it through Sage
             line = preparse(line)
     return line
Example #10
0
def completions(s, globs, format=False, width=90, system="None"):
    """
    Return a list of completions in the context of globs.
    """
    if system not in ['sage', 'python']:
        prepend = system + '.'
        s = prepend + s
    else:
        prepend = ''
    n = len(s)
    if n == 0:
        return '(empty string)'
    try:
        if not '.' in s and not '(' in s:
            v = [x for x in globs.keys() if x[:n] == s] + \
                [x for x in __builtins__.keys() if x[:n] == s] 
        else:
            if not ')' in s:
                i = s.rfind('.')
                method = s[i+1:]
                obj = s[:i]
                n = len(method)
            else:
                obj = preparse(s)
                method = ''
            try:
                O = eval(obj, globs)
                D = dir(O)
                try:
                    D += O.trait_names()
                except (AttributeError, TypeError):
                    pass
                if method == '':
                    v = [obj + '.'+x for x in D if x and x[0] != '_']
                else:
                    v = [obj + '.'+x for x in D if x[:n] == method]
            except Exception, msg:
                v = []
        v = list(set(v))   # make uniq
        v.sort()
Example #11
0
    def preparse_imports_from_sage(self, line):
        """
        Finds occurrences of strings such as ``sage(object)`` in
        *line*, converts ``object`` to :attr:`shell.interface`,
        and replaces those strings with their identifier in the new
        system.  This also works with strings such as
        ``maxima(object)`` if :attr:`shell.interface` is
        ``maxima``.

        :param line: the line to transform
        :type line: string

        .. warning::

            This does not parse nested parentheses correctly.  Thus,
            lines like ``sage(a.foo())`` will not work correctly.
            This can't be done in generality with regular expressions.

        EXAMPLES::

            sage: from sage.repl.interpreter import interface_shell_embed, InterfaceShellTransformer
            sage: shell = interface_shell_embed(maxima)
            sage: ift = InterfaceShellTransformer(shell=shell, config=shell.config, prefilter_manager=shell.prefilter_manager)
            sage: ift.shell.ex('a = 3')
            sage: ift.preparse_imports_from_sage('2 + sage(a)')
            '2 + sage0 '
            sage: maxima.eval('sage0')
            '3'
            sage: ift.preparse_imports_from_sage('2 + maxima(a)')
            '2 +  sage1 '
            sage: ift.preparse_imports_from_sage('2 + gap(a)')
            '2 + gap(a)'
        """
        for sage_code in self._sage_import_re.findall(line):
            expr = preparse(sage_code)
            result = self.shell.interface(eval(expr, self.shell.user_ns))
            self.temporary_objects.add(result)
            line = self._sage_import_re.sub(' ' + result.name() + ' ', line, 1)
        return line
Example #12
0
def crun(s, evaluator):
    """
    Profile single statement.

    - ``s`` -- string. Sage code to profile.

    - ``evaluator`` -- callable to evaluate.

    EXAMPLES::

        sage: import sage.misc.gperftools as gperf
        sage: ev = lambda ex:eval(ex, globals(), locals())
        sage: gperf.crun('gperf.run_100ms()', evaluator=ev)   # optional - gperftools
        PROFILE: interrupts/evictions/bytes = ...
        Using local file ...
        Using local file ...
    """
    prof = Profiler()
    from sage.misc.preparser import preparse
    py_s = preparse(s)
    prof.start()
    evaluator(py_s)
    prof.stop()
    prof.top()
Example #13
0
    def parse(self, string, *args):
        r"""
        A Sage specialization of :class:`doctest.DocTestParser`.

        INPUTS:

        - ``string`` -- the string to parse.
        - ``name`` -- optional string giving the name indentifying string,
          to be used in error messages.

        OUTPUTS:

        - A list consisting of strings and :class:`doctest.Example`
          instances.  There will be at least one string between
          successive examples (exactly one unless or long or optional
          tests are removed), and it will begin and end with a string.

        EXAMPLES::

            sage: from sage.doctest.parsing import SageDocTestParser
            sage: DTP = SageDocTestParser(True, ('sage','magma','guava'))
            sage: example = 'Explanatory text::\n\n    sage: E = magma("EllipticCurve([1, 1, 1, -10, -10])") # optional: magma\n\nLater text'
            sage: parsed = DTP.parse(example)
            sage: parsed[0]
            'Explanatory text::\n\n'
            sage: parsed[1].sage_source
            'E = magma("EllipticCurve([1, 1, 1, -10, -10])") # optional: magma\n'
            sage: parsed[2]
            '\nLater text'

        If the doctest parser is not created to accept a given
        optional argument, the corresponding examples will just be
        removed::

            sage: DTP2 = SageDocTestParser(True, ('sage',))
            sage: parsed2 = DTP2.parse(example)
            sage: parsed2
            ['Explanatory text::\n\n', '\nLater text']

        You can mark doctests as having a particular tolerance::

            sage: example2 = 'sage: gamma(1.6) # tol 2.0e-11\n0.893515349287690'
            sage: ex = DTP.parse(example2)[1]
            sage: ex.sage_source
            'gamma(1.6) # tol 2.0e-11\n'
            sage: ex.want
            '0.893515349287690\n'
            sage: type(ex.want)
            <class 'sage.doctest.parsing.MarkedOutput'>
            sage: ex.want.tol
            2e-11

        You can use continuation lines:

            sage: s = "sage: for i in range(4):\n....:     print i\n....:\n"
            sage: ex = DTP2.parse(s)[1]
            sage: ex.source
            'for i in range(Integer(4)):\n    print i\n'

        Sage currently accepts backslashes as indicating that the end
        of the current line should be joined to the next line.  This
        feature allows for breaking large integers over multiple lines
        but is not standard for Python doctesting.  It's not
        guaranteed to persist, but works in Sage 5.5::

            sage: n = 1234\
            ....:     5678
            sage: print n
            12345678
            sage: type(n)
            <type 'sage.rings.integer.Integer'>

        It also works without the line continuation::

            sage: m = 8765\
            4321
            sage: print m
            87654321
        """
        # Hack for non-standard backslash line escapes accepted by the current
        # doctest system.
        m = backslash_replacer.search(string)
        while m is not None:
            next_prompt = find_sage_prompt.search(string,m.end())
            g = m.groups()
            if next_prompt:
                future = string[m.end():next_prompt.start()] + '\n' + string[next_prompt.start():]
            else:
                future = string[m.end():]
            string = string[:m.start()] + g[0] + "sage:" + g[1] + future
            m = backslash_replacer.search(string,m.start())

        string = find_sage_prompt.sub(r"\1>>> sage: ", string)
        string = find_sage_continuation.sub(r"\1...", string)
        res = doctest.DocTestParser.parse(self, string, *args)
        filtered = []
        for item in res:
            if isinstance(item, doctest.Example):
                optional_tags = parse_optional_tags(item.source)
                if optional_tags:
                    for tag in optional_tags:
                        self.optionals[tag] += 1
                    if ('not implemented' in optional_tags) or ('not tested' in optional_tags):
                        continue
                    if 'long time' in optional_tags:
                        if self.long:
                            optional_tags.remove('long time')
                        else:
                            continue
                    if not (self.optional_tags is True or optional_tags.issubset(self.optional_tags)):
                        continue
                elif self.optional_only:
                    self.optionals['sage'] += 1
                    continue
                item.want = parse_tolerance(item.source, item.want)
                if item.source.startswith("sage: "):
                    item.sage_source = item.source[6:]
                    if item.sage_source.lstrip().startswith('#'):
                        continue
                    item.source = preparse(item.sage_source)
            filtered.append(item)
        return filtered
Example #14
0
def recompute_AL():
    field_label = None
    S = hmf_forms.find({})
    S = S.sort("label")

    while True:
        v = S.next()
        NN_label = v["level_label"]
        v_label = v["label"]

        try:
            if v["AL_eigenvalues_fixed"] == 'done' or v[
                    "AL_eigenvalues_fixed"] == 'working':
                continue
        except KeyError:
            print(v_label)
            print("...new, computing!")
            v["AL_eigenvalues_fixed"] = 'working'
            hmf_forms.save(v)

        if field_label is None or not field_label == v["field_label"]:
            field_label = v["field_label"]
            print("...new field " + field_label)

            F = fields.find_one({"label": field_label})
            F_hmf = hmf_fields.find_one({"label": field_label})

            magma.eval('P<x> := PolynomialRing(Rationals());')
            magma.eval('F<w> := NumberField(Polynomial(' +
                       str(F["coefficients"]) + '));')
            magma.eval('ZF := Integers(F);')
            magma.eval('ideals_str := [' +
                       ','.join([st for st in F_hmf["ideals"]]) + '];')
            magma.eval(
                'ideals := [ideal<ZF | {F!x : x in I}> : I in ideals_str];')

            magma.eval('primes_str := [' +
                       ','.join([st for st in F_hmf["primes"]]) + '];')
            magma.eval(
                'primes := [ideal<ZF | {F!x : x in I}> : I in primes_str];')

        NN_index = NN_label[NN_label.index('.') + 1:]
        magma.eval('NN := [I : I in ideals | Norm(I) eq ' +
                   str(v["level_norm"]) + '][' + str(NN_index) + '];')
        magma.eval('M := HilbertCuspForms(F, NN);')

        if v["hecke_polynomial"] != 'x':
            magma.eval('K<e> := NumberField(' + v["hecke_polynomial"] + ');')
        else:
            magma.eval('K := Rationals(); e := 1;')

        magma.eval('hecke_eigenvalues := [' +
                   ','.join([st for st in v["hecke_eigenvalues"]]) + '];')

        print("...Hecke eigenvalues loaded...")

        magma.eval(
            's := 0; KT := []; '
            'while KT cmpeq [] or Dimension(KT) gt 1 do '
            '  s +:= 1; '
            '  pp := primes[s]; '
            '  if Valuation(NN, pp) eq 0 then '
            '    T_pp := HeckeOperator(M, pp); '
            '    a_pp := hecke_eigenvalues[s]; '
            '    if KT cmpeq [] then '
            '      KT := Kernel(ChangeRing(Matrix(T_pp),K)-a_pp); '
            '    else '
            '      KT := KT meet Kernel(ChangeRing(Matrix(T_pp),K)-a_pp); '
            '    end if; '
            '  end if; '
            'end while;')
        magma.eval('assert Dimension(KT) eq 1;')

        print("...dimension 1 subspace found...")

        magma.eval('NNfact := Factorization(NN);')
        magma.eval('f := Vector(Basis(KT)[1]); '
                   'AL_eigenvalues := []; '
                   'for pp in NNfact do '
                   '  U_ppf := f*ChangeRing(AtkinLehnerOperator(M, pp[1]),K); '
                   '  assert U_ppf eq f or U_ppf eq -f; '
                   '  if U_ppf eq f then '
                   '    Append(~AL_eigenvalues, 1); '
                   '  else '
                   '    Append(~AL_eigenvalues, -1); '
                   '  end if; '
                   'end for;')

        #                   '  T_ppf := f*ChangeRing(HeckeOperator(M, pp[1]),K); '\
        #                   '  if pp[2] ge 2 then assert T_ppf eq 0*f; else assert T_ppf eq -U_ppf; end if; '\

        print("...AL eigenvalues computed!")

        AL_ind = eval(
            preparse(magma.eval('[Index(primes,pp[1])-1 : pp in NNfact]')))
        AL_eigenvalues_jv = eval(preparse(magma.eval('AL_eigenvalues')))
        AL_eigenvalues = [[F_hmf["primes"][AL_ind[i]], AL_eigenvalues_jv[i]]
                          for i in range(len(AL_ind))]
        pps_exps = eval(preparse(magma.eval('[pp[2] : pp in NNfact]')))

        hecke_eigenvalues = v["hecke_eigenvalues"]
        for j in range(len(AL_ind)):
            s = AL_ind[j]
            if pps_exps[j] >= 2:
                hecke_eigenvalues[s] = '0'
            else:
                hecke_eigenvalues[s] = str(-AL_eigenvalues[j][1])
        AL_eigenvalues = [[a[0], str(a[1])] for a in AL_eigenvalues]

        v["hecke_eigenvalues"] = hecke_eigenvalues
        v["AL_eigenvalues"] = AL_eigenvalues
        v["AL_eigenvalues_fixed"] = 'done'
        hmf_forms.save(v)
Example #15
0
import os
if 'SAGE_CLEAN' not in os.environ:
    import sage.misc.misc
    from sage.misc.interpreter import preparser, _ip
    preparser(True)

    import sage.all_cmdline
    sage.all_cmdline._init_cmdline(globals())

    _ip.ex('from sage.all import Integer, RealNumber')
    os.chdir(os.environ["CUR"])
    import sage.misc.interpreter

    from sage.misc.interpreter import attached_files

    if not os.environ.has_key(
            'SAGE_IMPORTALL') or os.environ['SAGE_IMPORTALL'] != "no":
        _ip.ex('from sage.all_cmdline import *')

    startup_file = os.environ.get('SAGE_STARTUP_FILE', '')
    if os.path.exists(startup_file):
        _ip.options.autoexec.append('load %s' % startup_file)

    from sage.misc.sage_timeit import sage_timeit
    _ip.expose_magic('timeit', lambda self, s: sage_timeit(s, _ip.user_ns))

    from sage.misc.preparser import preparse
    old_prun = _ip.IP.magic_prun
    _ip.expose_magic('prun', lambda self, s: old_prun(preparse(s)))
Example #16
0
def qexpansion(field_label=None):
    if field_label is None:
        query = {}
    else:
        query = {"field_label": field_label}
    S = db.hmf_forms.search(query, sort=["label"])

    field_label = None

    v = S.next()
    while True:
        # NN_label = v["level_label"] # never used
        v_label = v["label"]

        print v_label

        if v.get('q_expansion') is not None:
            v = S.next()
            continue

        if field_label is None or not field_label != v["field_label"]:
            field_label = v["field_label"]
            print "...new field " + field_label

            coeffs = db.nf_fields.lookup(field_label,
                                         projection="coefficients")
            F_hmf = db.hmf_fields.lookup(
                field_label,
                projection=["ideals", "primes", "narrow_class_number"])

            magma.eval('P<x> := PolynomialRing(Rationals());')
            magma.eval('F<w> := NumberField(Polynomial(' + str(coeffs) + '));')
            magma.eval('ZF := Integers(F);')
            magma.eval('ideals_str := [' +
                       ','.join([st for st in F_hmf["ideals"]]) + '];')
            magma.eval(
                'ideals := [ideal<ZF | {F!x : x in I}> : I in ideals_str];')

            magma.eval('primes_str := [' +
                       ','.join([st for st in F_hmf["primes"]]) + '];')
            magma.eval(
                'primes := [ideal<ZF | {F!x : x in I}> : I in primes_str];')

            if F_hmf.get("narrow_class_number") is None:
                F_hmf['narrow_class_number'] = eval(
                    preparse(magma.eval('NarrowClassNumber(F);')))

        if v["hecke_polynomial"] != 'x':
            magma.eval('fpol := ' + v["hecke_polynomial"] + ';')
            magma.eval('K<e> := NumberField(fpol);')
        else:
            magma.eval('fpol := x;')
            magma.eval('K := Rationals(); e := 1;')

        magma.eval('hecke_eigenvalues := [' +
                   ','.join([st for st in v["hecke_eigenvalues"]]) + '];')

        magma.eval(
            'mCl := ClassGroupPrimeRepresentatives(ZF, 1*ZF, RealPlaces(F)); '
            'Cl := [mCl(x) : x in Domain(mCl)]; '
            'dd := Different(ZF); '
            'ddinv := dd^(-1); '
            'Nd := Norm(dd); '
            'q_expansions := [* *]; '
            'for pp in Cl do '
            '  L0 := MinkowskiLattice(ZF!!(Nd*pp*ddinv)); '
            '  L := LatticeWithGram( GramMatrix(L0)/Nd^2 ); '
            '  ppdd_basis := Basis(pp*ddinv); '
            ' '
            '  det := Norm(pp)/Nd; '
            '  s := 1; '
            '  V := []; '
            '  while #V lt 10 do '
            '    S := ShortVectors(L, s*det); '
            '    S := [&+[Round(Eltseq(v[1])[i])*ppdd_basis[i] : i in [1..#ppdd_basis]] : v in S]; '
            '    S := [x : x in S | IsTotallyPositive(x)]; '
            '    V := S; '
            '    s +:= 1; '
            '  end while; '
            ' '
            '  Append(~q_expansions, [* [F!x : x in V[1..10]], [Index(ideals, x*dd) : x in V] *]); '
            'end for;')
        q_expansions_str = magma.eval(
            'hecke_eigenvalues_forideals := [1]; '
            'm := Max(&cat[t[2] : t in q_expansions]); '
            'for i := 2 to m do '
            '  nn := ideals[i]; '
            '  nnfact := Factorization(nn); '
            '  if #nnfact eq 1 then '
            '    pp := nnfact[1][1]; '
            '    e := nnfact[1][2]; '
            '    if e eq 1 then '
            '      ann := hecke_eigenvalues[Index(primes,pp)]; '
            '    else '
            '      ann := hecke_eigenvalues_forideals[Index(ideals,pp)]* '
            '             hecke_eigenvalues_forideals[Index(ideals,pp^(e-1))] - '
            '               Norm(pp)*hecke_eigenvalues_forideals[Index(ideals,pp^(e-2))]; '
            '    end if; '
            '  else '
            '    ann := &*[ hecke_eigenvalues_forideals[Index(ideals,qq[1]^qq[2])] : qq in nnfact ]; '
            '  end if; '
            '  Append(~hecke_eigenvalues_forideals, ann); '
            'end for; '
            ' '
            'print [* [* [* q[1][i], hecke_eigenvalues_forideals[q[2][i]] *] : i in [1..#q[1]] *] : q in q_expansions *];'
        )

        q_expansions = eval(
            preparse(q_expansions_str.replace('[*', '[').replace('*]', ']')))
        q_expansions = [[[str(c) for c in q[0]], [str(c) for c in q[1]]]
                        for q in q_expansions]

        v["q_expansions"] = q_expansions

        # UPDATES DON'T WORK
        #db.hmf_forms.save(v)

        v = S.next()
Example #17
0
def qexpansion(field_label=None):
    if field_label is None:
        S = hmf_forms.find({})
    else:
        S = hmf_forms.find({"field_label": field_label})
    S = S.sort("label")

    field_label = None

    v = S.next()
    while True:
        # NN_label = v["level_label"] # never used
        v_label = v["label"]

        print v_label

        if v.has_key('q_expansion'):
            v = S.next()
            continue

        if field_label is None or not field_label == v["field_label"]:
            field_label = v["field_label"]
            print "...new field " + field_label

            F = fields.find_one({"label": field_label})
            F_hmf = hmf_fields.find_one({"label": field_label})

            magma.eval('P<x> := PolynomialRing(Rationals());')
            magma.eval('F<w> := NumberField(Polynomial(' + str(F["coefficients"]) + '));')
            magma.eval('ZF := Integers(F);')
            magma.eval('ideals_str := [' + ','.join([st for st in F_hmf["ideals"]]) + '];')
            magma.eval('ideals := [ideal<ZF | {F!x : x in I}> : I in ideals_str];')

            magma.eval('primes_str := [' + ','.join([st for st in F_hmf["primes"]]) + '];')
            magma.eval('primes := [ideal<ZF | {F!x : x in I}> : I in primes_str];')

            if not F_hmf.has_key('narrow_class_number'):
                F_hmf['narrow_class_number'] = eval(preparse(magma.eval('NarrowClassNumber(F);')))

        if v["hecke_polynomial"] != 'x':
            magma.eval('fpol := ' + v["hecke_polynomial"] + ';')
            magma.eval('K<e> := NumberField(fpol);')
        else:
            magma.eval('fpol := x;')
            magma.eval('K := Rationals(); e := 1;')

        magma.eval('hecke_eigenvalues := [' + ','.join([st for st in v["hecke_eigenvalues"]]) + '];')

        magma.eval('mCl := ClassGroupPrimeRepresentatives(ZF, 1*ZF, RealPlaces(F)); '
                   'Cl := [mCl(x) : x in Domain(mCl)]; '
                   'dd := Different(ZF); '
                   'ddinv := dd^(-1); '
                   'Nd := Norm(dd); '
                   'q_expansions := [* *]; '
                   'for pp in Cl do '
                   '  L0 := MinkowskiLattice(ZF!!(Nd*pp*ddinv)); '
                   '  L := LatticeWithGram( GramMatrix(L0)/Nd^2 ); '
                   '  ppdd_basis := Basis(pp*ddinv); '
                   ' '
                   '  det := Norm(pp)/Nd; '
                   '  s := 1; '
                   '  V := []; '
                   '  while #V lt 10 do '
                   '    S := ShortVectors(L, s*det); '
                   '    S := [&+[Round(Eltseq(v[1])[i])*ppdd_basis[i] : i in [1..#ppdd_basis]] : v in S]; '
                   '    S := [x : x in S | IsTotallyPositive(x)]; '
                   '    V := S; '
                   '    s +:= 1; '
                   '  end while; '
                   ' '
                   '  Append(~q_expansions, [* [F!x : x in V[1..10]], [Index(ideals, x*dd) : x in V] *]); '
                   'end for;')
        q_expansions_str = magma.eval('hecke_eigenvalues_forideals := [1]; '
                                      'm := Max(&cat[t[2] : t in q_expansions]); '
                                      'for i := 2 to m do '
                                      '  nn := ideals[i]; '
                                      '  nnfact := Factorization(nn); '
                                      '  if #nnfact eq 1 then '
                                      '    pp := nnfact[1][1]; '
                                      '    e := nnfact[1][2]; '
                                      '    if e eq 1 then '
                                      '      ann := hecke_eigenvalues[Index(primes,pp)]; '
                                      '    else '
                                      '      ann := hecke_eigenvalues_forideals[Index(ideals,pp)]* '
                                      '             hecke_eigenvalues_forideals[Index(ideals,pp^(e-1))] - '
                                      '               Norm(pp)*hecke_eigenvalues_forideals[Index(ideals,pp^(e-2))]; '
                                      '    end if; '
                                      '  else '
                                      '    ann := &*[ hecke_eigenvalues_forideals[Index(ideals,qq[1]^qq[2])] : qq in nnfact ]; '
                                      '  end if; '
                                      '  Append(~hecke_eigenvalues_forideals, ann); '
                                      'end for; '
                                      ' '
                                      'print [* [* [* q[1][i], hecke_eigenvalues_forideals[q[2][i]] *] : i in [1..#q[1]] *] : q in q_expansions *];')

        q_expansions = eval(preparse(q_expansions_str.replace('[*', '[').replace('*]', ']')))
        q_expansions = [[[str(c) for c in q[0]], [str(c) for c in q[1]]] for q in q_expansions]

        v["q_expansions"] = q_expansions
        hmf_forms.save(v)

        v = S.next()
def import_data(hmf_filename, fileprefix=None, ferrors=None, test=True):
    if fileprefix is None:
        fileprefix = "."
    hmff = open(os.path.join(fileprefix, hmf_filename))

    if ferrors is None:
        ferrors = open('/home/jvoight/lmfdb/backups/import_data.err', 'a')

    # Parse field data
    v = hmff.readline()
    assert v[:9] == 'COEFFS :='
    coeffs = eval(v[10:][:-2])
    v = hmff.readline()
    assert v[:4] == 'n :='
    n = int(v[4:][:-2])
    v = hmff.readline()
    assert v[:4] == 'd :='
    d = int(v[4:][:-2])

    magma.eval('F<w> := NumberField(Polynomial(' + str(coeffs) + '));')
    magma.eval('ZF := Integers(F);')

    # Find the corresponding field in the database of number fields
    dkey = make_disc_key(ZZ(d))[1]
    sig = "%s,%s" % (n, 0)
    print("Finding field with signature %s and disc_key %s ..." % (sig, dkey))
    fields_matching = fields.find({"disc_abs_key": dkey, "signature": sig})
    cnt = fields_matching.count()
    print("Found %s fields" % cnt)
    assert cnt >= 1
    field_label = None
    co = str(coeffs)[1:-1].replace(" ", "")
    for i in range(cnt):
        nf = next(fields_matching)
        print("Comparing coeffs %s with %s" % (nf['coeffs'], co))
        if nf['coeffs'] == co:
            field_label = nf['label']
    assert field_label is not None
    print("...found!")

    # Find the field in the HMF database
    print("Finding field %s in HMF..." % field_label)
    F_hmf = hmf_fields.find_one({"label": field_label})
    if F_hmf is None:
        # Create list of ideals
        print("...adding!")
        ideals = eval(preparse(magma.eval('nice_idealstr(F);')))
        ideals_str = [str(c) for c in ideals]
        if test:
            print("Would now insert data for %s into hmf_fields" % field_label)
        else:
            hmf_fields.insert({
                "label": field_label,
                "degree": n,
                "discriminant": d,
                "ideals": ideals_str
            })
        F_hmf = hmf_fields.find_one({"label": field_label})
    else:
        print("...found!")

    print("Computing ideals...")
    ideals_str = F_hmf['ideals']
    ideals = [eval(preparse(c)) for c in ideals_str]
    ideals_norms = [c[0] for c in ideals]
    magma.eval('ideals_str := [' +
               ''.join(F_hmf['ideals']).replace('][', '], [') + ']')
    magma.eval('ideals := [ideal<ZF | {F!x : x in I}> : I in ideals_str];')

    # Add the list of primes
    print("Computing primes...")
    v = hmff.readline()  # Skip line
    v = hmff.readline()
    assert v[:9] == 'PRIMES :='
    primes_str = v[10:][:-2]
    primes_array = [str(t) for t in eval(preparse(primes_str))]
    magma.eval('primes_array := ' + primes_str)
    magma.eval('primes := [ideal<ZF | {F!x : x in I}> : I in primes_array];')
    magma.eval('primes_indices := [Index(ideals, pp) : pp in primes];')
    try:
        assert magma(
            '&and[primes_indices[j] gt primes_indices[j-1] : j in [2..#primes_indices]]'
        )
        resort = False
    except AssertionError:
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Primes reordered!")
        resort = True
        magma.eval('_, sigma := Sort(primes_indices, func<x,y | x-y>);')
        magma.eval(
            'perm := [[xx : xx in x] : x in CycleDecomposition(sigma) | #x gt 1]'
        )
        # Check at least they have the same norm
        magma.eval(
            'for tau in perm do assert #{Norm(ideals[primes_indices[t]]) : t in tau} eq 1; end for;'
        )
        primes_resort = eval(magma.eval('Eltseq(sigma)'))
        primes_resort = [c - 1 for c in primes_resort]

    primes_indices = eval(magma.eval('primes_indices'))
    primes_str = [ideals_str[j - 1] for j in primes_indices]
    assert len(primes_array) == len(primes_str)
    print("...comparing...")
    for i in range(len(primes_array)):
        assert magma('ideal<ZF | {F!x : x in ' + primes_array[i] + '}> eq ' +
                     'ideal<ZF | {F!x : x in ' + primes_str[i] + '}>;')
    if resort:
        # Important also to resort the list of primes themselves!
        # not just the a_pp's
        primes_str = [primes_str[i] for i in primes_resort]

    if 'primes' in F_hmf:  # Nothing smart: make sure it is the same
        assert F_hmf['primes'] == primes_str
    else:
        F_hmf['primes'] = primes_str
        if test:
            print("Would now save primes string %s... into hmf_fields" %
                  primes_str[:100])
        else:
            hmf_fields.save(F_hmf)
    print("...saved!")

    # Collect levels
    v = hmff.readline()
    if v[:9] == 'LEVELS :=':
        # Skip this line since we do not use the list of levels
        v = hmff.readline()
    for i in range(3):
        if v[:11] != 'NEWFORMS :=':
            v = hmff.readline()
        else:
            break

    # Finally, newforms!
    print("Starting newforms!")
    while v != '':
        v = hmff.readline()[1:-3]
        if len(v) == 0:
            break
        data = eval(preparse(v))
        level_ideal = data[0]
        level_norm = data[0][0]
        label_suffix = data[1]
        weight = [2 for i in range(n)]

        level_ind = int(
            magma('Index(ideals, ideal<ZF | {F!x : x in ' + str(level_ideal) +
                  '}>)')) - 1  # Magma counts starting at 1
        level_ideal = ideals_str[level_ind]
        assert magma('ideal<ZF | {F!x : x in ' + str(level_ideal) + '}> eq ' +
                     'ideal<ZF | {F!x : x in ' + str(data[0]) + '}>;')
        level_dotlabel = level_ind - ideals_norms.index(
            level_norm) + 1  # Start counting at 1
        assert level_dotlabel > 0
        level_label = str(level_norm) + '.' + str(level_dotlabel)

        label = construct_full_label(field_label, weight, level_label,
                                     label_suffix)
        short_label = level_label + '-' + label_suffix

        if len(data) == 3:
            hecke_polynomial = x
            hecke_eigenvalues = data[2]
        else:
            hecke_polynomial = data[2]
            hecke_eigenvalues = data[3]

        if resort:
            hecke_eigenvalues = [hecke_eigenvalues[i] for i in primes_resort]

        # Sort out Atkin-Lehner involutions
        magma.eval('NN := ideal<ZF | {F!x : x in ' + str(level_ideal) + '}>;')
        magma.eval('NNfact := Factorization(NN);')
        ALind = eval(
            preparse(
                magma.eval(
                    '[Index(primes, pp[1])-1 : pp in NNfact | Index(primes,pp[1]) gt 0];'
                )))
        AL_eigsin = [hecke_eigenvalues[c] for c in ALind]
        try:
            assert all([abs(int(c)) <= 1 for c in AL_eigsin])
        except Exception:
            ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')

        AL_eigenvalues = []
        ees = eval(preparse(magma.eval('[pp[2] : pp in NNfact]')))
        for i in range(len(AL_eigsin)):
            if ees[i] >= 2:
                if hecke_eigenvalues[ALind[i]] != 0:
                    ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')
            else:
                try:
                    if abs(int(AL_eigsin[i])) != 1:
                        ferrors.write(
                            str(n) + '/' + str(d) + ' ' + label + '\n')
                    else:
                        AL_eigenvalues.append(
                            [primes_str[ALind[i]], -AL_eigsin[i]])
                except TypeError:
                    ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')

        assert magma(
            '[Valuation(NN, ideal<ZF | {F!x : x in a}>) gt 0 : a in [' +
            str(''.join([a[0]
                         for a in AL_eigenvalues])).replace('][', '], [') +
            ']];')

        # Constrain eigenvalues to size 2MB
        hecke_eigenvalues = [str(c) for c in hecke_eigenvalues]
        leftout = 0
        while sum([len(s) for s in hecke_eigenvalues]) > 2000000:
            leftout += 1
            hecke_eigenvalues = hecke_eigenvalues[:-1]
        if leftout > 0:
            print("Left out", leftout)

        info = {
            "label": label,
            "short_label": short_label,
            "field_label": field_label,
            "level_norm": int(level_norm),
            "level_ideal": str(level_ideal),
            "level_label": level_label,
            "weight": str(weight),
            "label_suffix": label_suffix,
            "dimension": hecke_polynomial.degree(),
            "hecke_polynomial": str(hecke_polynomial),
            "hecke_eigenvalues": hecke_eigenvalues,
            "AL_eigenvalues": [[str(a[0]), str(a[1])] for a in AL_eigenvalues]
        }
        print(info['label'])

        existing_forms = hmf_forms.find({"label": label})
        assert existing_forms.count() <= 1
        if existing_forms.count() == 0:
            if test:
                print("Would now insert form data %s into hmf_forms" % info)
            else:
                hmf_forms.insert(info)
        else:
            existing_form = next(existing_forms)
            assert info['hecke_polynomial'] == existing_form[
                'hecke_polynomial']
            try:
                assert info['hecke_eigenvalues'] == existing_form[
                    'hecke_eigenvalues']
                print("...duplicate")
            except AssertionError:
                print(
                    "...Hecke eigenvalues do not match!  Checking for permutation"
                )
                assert set(info['hecke_eigenvalues'] +
                           ['0', '1', '-1']) == set(
                               existing_form['hecke_eigenvalues'] +
                               [u'0', u'1', u'-1'])
                # Add 0,1,-1 to allow for Atkin-Lehner eigenvalues, if not computed
                print("As sets, ignoring 0,1,-1, the eigenvalues match!")
                if test:
                    print("Would now replace permuted form data %s with %s" %
                          (existing_form, info))
                else:
                    existing_form['hecke_eigenvalues'] = info[
                        'hecke_eigenvalues']
                    hmf_forms.save(existing_form)
Example #19
0
     answer = preparse(answer).replace('Integer',
                                       '').replace('RealNumber', '')
     response = preparse(response).replace('Integer',
                                           '').replace('RealNumber', '')
     web.debug("Answer: " + answer)
     web.debug("Response: " + response)
     answer_expr = symbolic_expression_from_string(answer)
     response_expr = symbolic_expression_from_string(response)
 except SyntaxError, e:
     web.debug("Error parsing answer and response expressions: %s" % e)
     f = "Error parsing answer and response expressions: %s" % e
     return f
 # First check for exlcuded responses
 for exc in exclude:
     exc = algebra.convert_latex(exc)
     exc = preparse(exc)
     # Create and expression from the excluded string
     expr = symbolic_expression_from_string(exc)
     # Take a difference between the excluded expression and the
     # response provided by the student
     diff = response_expr - expr
     # See if the difference has a representation of zero. If so it
     # matches with a simple comparison and so should be exlcuded.
     web.debug(diff)
     if diff.__repr__() == '0':
         # Response is excluded so immediately return false
         return {'expr1': answer, 'expr2': response, 'result': False}
 # Create an expression that is the difference of the answer and response
 web.debug('FINAL %s - %s' % (answer_expr, response_expr))
 f = (answer_expr) - (response_expr)
 web.debug('RESULT %s' % f)
Example #20
0
def import_data(hmf_filename):
    hmff = file(hmf_filename)

    ferrors = file('/home/jvoight/lmfdb/backups/import_data.err', 'a')

    # Parse field data
    v = hmff.readline()
    assert v[:9] == 'COEFFS :='
    coeffs = eval(v[10:][:-2])
    v = hmff.readline()
    assert v[:4] == 'n :='
    n = int(v[4:][:-2])
    v = hmff.readline()
    assert v[:4] == 'd :='
    d = int(v[4:][:-2])

    magma.eval('F<w> := NumberField(Polynomial(' + str(coeffs) + '));')
    magma.eval('ZF := Integers(F);')

    # Find the corresponding field in the database of number fields
    print "Finding field..."
    fields_matching = fields.find({"signature": [n, int(0)],
                                   "discriminant": d})
    cnt = fields_matching.count()
    assert cnt >= 1
    field_label = None
    for i in range(cnt):
        nf = fields_matching.next()
        if nf['coefficients'] == coeffs:
            field_label = nf['label']
    assert field_label is not None
    print "...found!"

    # Find the field in the HMF database
    print "Finding field in HMF..."
    F_hmf = hmf_fields.find_one({"label": field_label})
    if F_hmf is None:
        # Create list of ideals
        print "...adding!"
        ideals = eval(preparse(magma.eval('nice_idealstr(F);')))
        ideals_str = [str(c) for c in ideals]
        hmf_fields.insert({"label": field_label,
                           "degree": n,
                           "discriminant": d,
                           "ideals": ideals_str})
        F_hmf = hmf_fields.find_one({"label": field_label})
    else:
        print "...found!"

    print "Computing ideals..."
    ideals_str = F_hmf['ideals']
    ideals = [eval(preparse(c)) for c in ideals_str]
    ideals_norms = [c[0] for c in ideals]
    magma.eval('ideals_str := [' + ''.join(F_hmf['ideals']).replace('][', '], [') + ']')
    magma.eval('ideals := [ideal<ZF | {F!x : x in I}> : I in ideals_str];')

    # Add the list of primes
    print "Computing primes..."
    v = hmff.readline()  # Skip line
    v = hmff.readline()
    assert v[:9] == 'PRIMES :='
    primes_str = v[10:][:-2]
    primes_array = [str(t) for t in eval(preparse(primes_str))]
    magma.eval('primes_array := ' + primes_str)
    magma.eval('primes := [ideal<ZF | {F!x : x in I}> : I in primes_array];')
    magma.eval('primes_indices := [Index(ideals, pp) : pp in primes];')
    try:
        assert magma('&and[primes_indices[j] gt primes_indices[j-1] : j in [2..#primes_indices]]')
        resort = False
    except AssertionError:
        print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Primes reordered!"
        resort = True
        magma.eval('_, sigma := Sort(primes_indices, func<x,y | x-y>);')
        magma.eval('perm := [[xx : xx in x] : x in CycleDecomposition(sigma) | #x gt 1]')
        # Check at least they have the same norm
        magma.eval('for tau in perm do assert #{Norm(ideals[primes_indices[t]]) : t in tau} eq 1; end for;')
        primes_resort = eval(magma.eval('Eltseq(sigma)'))
        primes_resort = [c - 1 for c in primes_resort]

    primes_indices = eval(magma.eval('primes_indices'))
    primes_str = [ideals_str[j - 1] for j in primes_indices]
    assert len(primes_array) == len(primes_str)
    print "...comparing..."
    for i in range(len(primes_array)):
        assert magma('ideal<ZF | {F!x : x in ' + primes_array[i] + '}> eq '
                     + 'ideal<ZF | {F!x : x in ' + primes_str[i] + '}>;')

    if 'primes' in F_hmf:  # Nothing smart: make sure it is the same
        assert F_hmf['primes'] == primes_str
    else:
        F_hmf['primes'] = primes_str
        hmf_fields.save(F_hmf)
    print "...saved!"

    # Collect levels
    v = hmff.readline()
    if v[:9] == 'LEVELS :=':
        # skip this line, we do not use the list of levels
        v = hmff.readline()
    for i in range(3):
        if v[:11] != 'NEWFORMS :=':
            v = hmff.readline()
        else:
            break

    # Finally, newforms!
    print "Starting newforms!"
    while v != '':
        v = hmff.readline()[1:-3]
        if len(v) == 0:
            break
        data = eval(preparse(v))
        level_ideal = data[0]
        level_norm = data[0][0]
        label_suffix = data[1]
        weight = [2 for i in range(n)]

        level_ind = int(magma('Index(ideals, ideal<ZF | {F!x : x in ' + str(level_ideal) + '}>)')
                        ) - 1  # Magma counts starting at 1
        level_ideal = ideals_str[level_ind]
        assert magma('ideal<ZF | {F!x : x in ' + str(level_ideal) + '}> eq '
                     + 'ideal<ZF | {F!x : x in ' + str(data[0]) + '}>;')
        level_dotlabel = level_ind - ideals_norms.index(level_norm) + 1   # Start counting at 1
        assert level_dotlabel > 0
        level_label = str(level_norm) + '.' + str(level_dotlabel)

        label = parse_label(field_label, weight, level_label, label_suffix)
        short_label = level_label + '-' + label_suffix

        if len(data) == 3:
            hecke_polynomial = x
            hecke_eigenvalues = data[2]
        else:
            hecke_polynomial = data[2]
            hecke_eigenvalues = data[3]

        if resort:
            hecke_eigenvalues = [hecke_eigenvalues[i] for i in primes_resort]

        # Sort out Atkin-Lehner involutions
        magma.eval('NN := ideal<ZF | {F!x : x in ' + str(level_ideal) + '}>;')
        magma.eval('NNfact := Factorization(NN);')
        ALind = eval(
            preparse(magma.eval('[Index(primes, pp[1])-1 : pp in NNfact | Index(primes,pp[1]) gt 0];')))
        AL_eigsin = [hecke_eigenvalues[c] for c in ALind]
        try:
            assert all([abs(int(c)) <= 1 for c in AL_eigsin])
        except:
            ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')

        AL_eigenvalues = []
        ees = eval(preparse(magma.eval('[pp[2] : pp in NNfact]')))
        for i in range(len(AL_eigsin)):
            if ees[i] >= 2:
                if hecke_eigenvalues[ALind[i]] != 0:
                    ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')
            else:
                try:
                    if abs(int(AL_eigsin[i])) != 1:
                        ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')
                    else:
                        AL_eigenvalues.append([primes_str[ALind[i]], -AL_eigsin[i]])
                except TypeError:
                    ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')

        assert magma('[Valuation(NN, ideal<ZF | {F!x : x in a}>) gt 0 : a in [' +
                     str(''.join([a[0] for a in AL_eigenvalues])).replace('][', '], [') + ']];')

        # Constrain eigenvalues to size 2MB
        hecke_eigenvalues = [str(c) for c in hecke_eigenvalues]
        leftout = 0
        while sum([len(s) for s in hecke_eigenvalues]) > 2000000:
            leftout += 1
            hecke_eigenvalues = hecke_eigenvalues[:-1]
        if leftout > 0:
            print "Left out", leftout

        info = {"label": label,
                "short_label": short_label,
                "field_label": field_label,
                "level_norm": int(level_norm),
                "level_ideal": str(level_ideal),
                "level_label": level_label,
                "weight": str(weight),
                "label_suffix": label_suffix,
                "dimension": hecke_polynomial.degree(),
                "hecke_polynomial": str(hecke_polynomial),
                "hecke_eigenvalues": hecke_eigenvalues,
                "AL_eigenvalues": [[str(a[0]), str(a[1])] for a in AL_eigenvalues]}
        print info['label']

        existing_forms = hmf_forms.find({"label": label})
        assert existing_forms.count() <= 1
        if existing_forms.count() == 0:
            hmf_forms.insert(info)
        else:
            existing_form = existing_forms.next()
            assert info['hecke_eigenvalues'] == existing_form['hecke_eigenvalues']
            print "...duplicate"
Example #21
0
import os
if 'SAGE_CLEAN' not in os.environ:
    import sage.misc.misc
    from sage.misc.interpreter import preparser, _ip
    preparser(True)

    import sage.all_cmdline
    sage.all_cmdline._init_cmdline(globals())

    _ip.ex('from sage.all import Integer, RealNumber')
    os.chdir(os.environ["CUR"])
    import sage.misc.interpreter

    from sage.misc.interpreter import attached_files

    if not os.environ.has_key('SAGE_IMPORTALL') or os.environ['SAGE_IMPORTALL'] != "no":
        _ip.ex('from sage.all_cmdline import *')


    startup_file = os.environ.get('SAGE_STARTUP_FILE', '')
    if os.path.exists(startup_file):
        _ip.options.autoexec.append('load %s'%startup_file)

    from sage.misc.sage_timeit import sage_timeit
    _ip.expose_magic('timeit', lambda self, s: sage_timeit(s, _ip.user_ns))

    from sage.misc.preparser import preparse
    old_prun = _ip.IP.magic_prun
    _ip.expose_magic('prun', lambda self, s: old_prun(preparse(s)))
Example #22
0
 def _compile(self):
     style = getattr(self, 'no_return', False) and 'exec' or 'single'
     # preparse here, so that it pretty prints above, but executes in Sage correctly
     return code.compile_command(preparse(''.join(self._stack)), '<input>', style)
Example #23
0
def recompute_AL(field_label=None, skip_odd=False):
    if field_label is None:
        S = hmf_forms.find({"AL_eigenvalues_fixed": None})
    else:
        S = hmf_forms.find({"field_label": field_label, "AL_eigenvalues_fixed": None})
    S = S.sort("label")

    field_label = None

    magma.eval('SetVerbose("ModFrmHil", 1);')

    v = S.next()
    while True:
        NN_label = v["level_label"]
        v_label = v["label"]

        print v_label

        if field_label is None or not field_label == v["field_label"]:
            field_label = v["field_label"]
            print "...new field " + field_label

            F = fields.find_one({"label": field_label})
            F_hmf = hmf_fields.find_one({"label": field_label})

            magma.eval('P<x> := PolynomialRing(Rationals());')
            magma.eval('F<w> := NumberField(Polynomial(' + str(F["coefficients"]) + '));')
            magma.eval('ZF := Integers(F);')
            magma.eval('ideals_str := [' + ','.join([st for st in F_hmf["ideals"]]) + '];')
            magma.eval('ideals := [ideal<ZF | {F!x : x in I}> : I in ideals_str];')

            magma.eval('primes_str := [' + ','.join([st for st in F_hmf["primes"]]) + '];')
            magma.eval('primes := [ideal<ZF | {F!x : x in I}> : I in primes_str];')

            magma.eval('classno := NarrowClassNumber(F);')

        if skip_odd and F["degree"] % 2 == 1 and v["level_norm"] > 300:
            print "...level norm > 300, skipping!"
            try:
                v = S.next()
                continue
            except StopIteration:
                break

        NN_index = NN_label[NN_label.index('.') + 1:]
        magma.eval(
            'NN := [I : I in ideals | Norm(I) eq ' + str(v["level_norm"]) + '][' + str(NN_index) + '];')
        magma.eval('Mfull := HilbertCuspForms(F, NN);')
        magma.eval('M := NewSubspace(Mfull);')
        magma.eval('O := QuaternionOrder(M); B := Algebra(O); DD := Discriminant(B);')

        if v["hecke_polynomial"] != 'x':
            magma.eval('fpol := ' + v["hecke_polynomial"] + ';')
            magma.eval('K<e> := NumberField(fpol);')
        else:
            magma.eval('fpol := x;')
            magma.eval('K := Rationals(); e := 1;')

        magma.eval('hecke_eigenvalues := [' + ','.join([st for st in v["hecke_eigenvalues"]]) + '];')

        print "...Hecke eigenvalues loaded..."

        magma.eval('denom := Lcm([Denominator(a) : a in hecke_eigenvalues]); q := NextPrime(200);')
        magma.eval(
            'while #Roots(fpol, GF(q)) eq 0 or Valuation(denom,q) gt 0 do q := NextPrime(q); end while;')
        magma.eval('if K cmpeq Rationals() then mk := hom<K -> GF(q) | >; else mk := hom<K -> GF(q) | Roots(fpol,GF(q))[1][1]>; end if;')

        magma.eval(
            '_<xQ> := PolynomialRing(Rationals()); rootsofunity := [r[1] : r in Roots(xQ^(2*classno)-1,K)];')

        magma.eval('s := 0; KT := []; '
                   'while KT cmpeq [] or Dimension(KT) gt 1 do '
                   '  s +:= 1; '
                   '  if s gt Min(50,#hecke_eigenvalues) then '
                   '    q := NextPrime(q); while #Roots(fpol, GF(q)) eq 0 or Valuation(denom,q) gt 0 do q := NextPrime(q); end while; '
                   '    if K cmpeq Rationals() then mk := hom<K -> GF(q) | >; else mk := hom<K -> GF(q) | Roots(fpol,GF(q))[1][1]>; end if; '
                   '    s := 1; '
                   '    KT := []; '
                   '  end if; '
                   '  pp := primes[s]; '
                   '  if Valuation(NN, pp) eq 0 then '
                   '    T_pp := HeckeOperator(M, pp); '
                   '    T_pp := Matrix(Nrows(T_pp),Ncols(T_pp),[mk(c) : c in Eltseq(T_pp)]); '
                   '    a_pp := hecke_eigenvalues[s]; '
                   '    if KT cmpeq [] then '
                   '      KT := Kernel(T_pp-mk(a_pp)); '
                   '    else '
                   '      KT := KT meet Kernel(T_pp-mk(a_pp)); '
                   '    end if; '
                   '  end if; '
                   'end while;')
        magma.eval('assert Dimension(KT) eq 1;')

        print "...dimension 1 subspace found..."

        magma.eval('NNfact := [pp : pp in Factorization(NN) | pp[1] in primes];')
        magma.eval('f := Vector(Basis(KT)[1]); '
                   'AL_eigenvalues := []; '
                   'for pp in NNfact do '
                   '  if Valuation(DD,pp[1]) gt 0 then U_pp := -HeckeOperator(M, pp[1]); '
                   '  else U_pp := AtkinLehnerOperator(M, pp[1]); end if; '
                   '  U_pp := Matrix(Nrows(U_pp),Ncols(U_pp),[mk(c) : c in Eltseq(U_pp)]); '
                   '  U_ppf := f*U_pp; found := false; '
                   '  for mu in rootsofunity do '
                   '    if U_ppf eq mk(mu)*f then Append(~AL_eigenvalues, mu); found := true; end if; '
                   '  end for; '
                   '  assert found; '
                   'end for;')

        print "...AL eigenvalues computed!"

        AL_ind = eval(preparse(magma.eval('[Index(primes,pp[1])-1 : pp in NNfact]')))
        AL_eigenvalues_jv = eval(preparse(magma.eval('AL_eigenvalues')))
        AL_eigenvalues = [[F_hmf["primes"][AL_ind[i]], AL_eigenvalues_jv[i]] for i in range(len(AL_ind))]
        pps_exps = eval(preparse(magma.eval('[pp[2] : pp in NNfact]')))

        hecke_eigenvalues = v["hecke_eigenvalues"]
        for j in range(len(AL_ind)):
            s = AL_ind[j]
            try:
                if pps_exps[j] >= 2:
                    hecke_eigenvalues[s] = '0'
                else:
                    hecke_eigenvalues[s] = str(-AL_eigenvalues[j][1])
            except IndexError:
                pass

        AL_eigenvalues = [[a[0], str(a[1])] for a in AL_eigenvalues]

        v["hecke_eigenvalues"] = hecke_eigenvalues
        v["AL_eigenvalues"] = AL_eigenvalues
        v["AL_eigenvalues_fixed"] = 'done'
        hmf_forms.save(v)

        v = S.next()
Example #24
0
def recompute_AL(field_label=None, skip_odd=False):
    if field_label is None:
        S = hmf_forms.find({"AL_eigenvalues_fixed": None})
    else:
        S = hmf_forms.find({"field_label": field_label, "AL_eigenvalues_fixed": None})
    S = S.sort("label")

    field_label = None

    magma.eval('SetVerbose("ModFrmHil", 1);')

    v = S.next()
    while True:
        NN_label = v["level_label"]
        v_label = v["label"]

        print(v_label)

        if field_label is None or not field_label == v["field_label"]:
            field_label = v["field_label"]
            print("...new field " + field_label)

            F = fields.find_one({"label": field_label})
            F_hmf = hmf_fields.find_one({"label": field_label})

            magma.eval('P<x> := PolynomialRing(Rationals());')
            magma.eval('F<w> := NumberField(Polynomial(' + str(F["coefficients"]) + '));')
            magma.eval('ZF := Integers(F);')
            magma.eval('ideals_str := [' + ','.join([st for st in F_hmf["ideals"]]) + '];')
            magma.eval('ideals := [ideal<ZF | {F!x : x in I}> : I in ideals_str];')

            magma.eval('primes_str := [' + ','.join([st for st in F_hmf["primes"]]) + '];')
            magma.eval('primes := [ideal<ZF | {F!x : x in I}> : I in primes_str];')

            magma.eval('classno := NarrowClassNumber(F);')

        if skip_odd and F["degree"] % 2 == 1 and v["level_norm"] > 300:
            print("...level norm > 300, skipping!")
            try:
                v = S.next()
                continue
            except StopIteration:
                break

        NN_index = NN_label[NN_label.index('.') + 1:]
        magma.eval(
            'NN := [I : I in ideals | Norm(I) eq ' + str(v["level_norm"]) + '][' + str(NN_index) + '];')
        magma.eval('Mfull := HilbertCuspForms(F, NN);')
        magma.eval('M := NewSubspace(Mfull);')
        magma.eval('O := QuaternionOrder(M); B := Algebra(O); DD := Discriminant(B);')

        if v["hecke_polynomial"] != 'x':
            magma.eval('fpol := ' + v["hecke_polynomial"] + ';')
            magma.eval('K<e> := NumberField(fpol);')
        else:
            magma.eval('fpol := x;')
            magma.eval('K := Rationals(); e := 1;')

        magma.eval('hecke_eigenvalues := [' + ','.join([st for st in v["hecke_eigenvalues"]]) + '];')

        print("...Hecke eigenvalues loaded...")

        magma.eval('denom := Lcm([Denominator(a) : a in hecke_eigenvalues]); q := NextPrime(200);')
        magma.eval(
            'while #Roots(fpol, GF(q)) eq 0 or Valuation(denom,q) gt 0 do q := NextPrime(q); end while;')
        magma.eval('if K cmpeq Rationals() then mk := hom<K -> GF(q) | >; else mk := hom<K -> GF(q) | Roots(fpol,GF(q))[1][1]>; end if;')

        magma.eval(
            '_<xQ> := PolynomialRing(Rationals()); rootsofunity := [r[1] : r in Roots(xQ^(2*classno)-1,K)];')

        magma.eval('s := 0; KT := []; '
                   'while KT cmpeq [] or Dimension(KT) gt 1 do '
                   '  s +:= 1; '
                   '  if s gt Min(50,#hecke_eigenvalues) then '
                   '    q := NextPrime(q); while #Roots(fpol, GF(q)) eq 0 or Valuation(denom,q) gt 0 do q := NextPrime(q); end while; '
                   '    if K cmpeq Rationals() then mk := hom<K -> GF(q) | >; else mk := hom<K -> GF(q) | Roots(fpol,GF(q))[1][1]>; end if; '
                   '    s := 1; '
                   '    KT := []; '
                   '  end if; '
                   '  pp := primes[s]; '
                   '  if Valuation(NN, pp) eq 0 then '
                   '    T_pp := HeckeOperator(M, pp); '
                   '    T_pp := Matrix(Nrows(T_pp),Ncols(T_pp),[mk(c) : c in Eltseq(T_pp)]); '
                   '    a_pp := hecke_eigenvalues[s]; '
                   '    if KT cmpeq [] then '
                   '      KT := Kernel(T_pp-mk(a_pp)); '
                   '    else '
                   '      KT := KT meet Kernel(T_pp-mk(a_pp)); '
                   '    end if; '
                   '  end if; '
                   'end while;')
        magma.eval('assert Dimension(KT) eq 1;')

        print("...dimension 1 subspace found...")

        magma.eval('NNfact := [pp : pp in Factorization(NN) | pp[1] in primes];')
        magma.eval('f := Vector(Basis(KT)[1]); '
                   'AL_eigenvalues := []; '
                   'for pp in NNfact do '
                   '  if Valuation(DD,pp[1]) gt 0 then U_pp := -HeckeOperator(M, pp[1]); '
                   '  else U_pp := AtkinLehnerOperator(M, pp[1]); end if; '
                   '  U_pp := Matrix(Nrows(U_pp),Ncols(U_pp),[mk(c) : c in Eltseq(U_pp)]); '
                   '  U_ppf := f*U_pp; found := false; '
                   '  for mu in rootsofunity do '
                   '    if U_ppf eq mk(mu)*f then Append(~AL_eigenvalues, mu); found := true; end if; '
                   '  end for; '
                   '  assert found; '
                   'end for;')

        print("...AL eigenvalues computed!")

        AL_ind = eval(preparse(magma.eval('[Index(primes,pp[1])-1 : pp in NNfact]')))
        AL_eigenvalues_jv = eval(preparse(magma.eval('AL_eigenvalues')))
        AL_eigenvalues = [[F_hmf["primes"][AL_ind[i]], AL_eigenvalues_jv[i]] for i in range(len(AL_ind))]
        pps_exps = eval(preparse(magma.eval('[pp[2] : pp in NNfact]')))

        hecke_eigenvalues = v["hecke_eigenvalues"]
        for j in range(len(AL_ind)):
            s = AL_ind[j]
            try:
                if pps_exps[j] >= 2:
                    hecke_eigenvalues[s] = '0'
                else:
                    hecke_eigenvalues[s] = str(-AL_eigenvalues[j][1])
            except IndexError:
                pass

        AL_eigenvalues = [[a[0], str(a[1])] for a in AL_eigenvalues]

        v["hecke_eigenvalues"] = hecke_eigenvalues
        v["AL_eigenvalues"] = AL_eigenvalues
        v["AL_eigenvalues_fixed"] = 'done'
        hmf_forms.save(v)

        v = S.next()
Example #25
0
 def _pre_execute_filter(self, line):
     line = preparse(line)
     if line[-1] == '?':
         return 'introspect(%s, format="print")'%line[:-1]
     else:
         return line
Example #26
0
def recompute_AL():
    field_label = None
    S = hmf_forms.find({})
    S = S.sort("label")

    while True:
        v = S.next()
        NN_label = v["level_label"]
        v_label = v["label"]

        try:
            if v["AL_eigenvalues_fixed"] == 'done' or v["AL_eigenvalues_fixed"] == 'working':
                continue
        except KeyError:
            print v_label
            print "...new, computing!"
            v["AL_eigenvalues_fixed"] = 'working'
            hmf_forms.save(v)

        if field_label is None or not field_label == v["field_label"]:
            field_label = v["field_label"]
            print "...new field " + field_label

            F = fields.find_one({"label": field_label})
            F_hmf = hmf_fields.find_one({"label": field_label})

            magma.eval('P<x> := PolynomialRing(Rationals());')
            magma.eval('F<w> := NumberField(Polynomial(' + str(F["coefficients"]) + '));')
            magma.eval('ZF := Integers(F);')
            magma.eval('ideals_str := [' + ','.join([st for st in F_hmf["ideals"]]) + '];')
            magma.eval('ideals := [ideal<ZF | {F!x : x in I}> : I in ideals_str];')

            magma.eval('primes_str := [' + ','.join([st for st in F_hmf["primes"]]) + '];')
            magma.eval('primes := [ideal<ZF | {F!x : x in I}> : I in primes_str];')

        NN_index = NN_label[NN_label.index('.') + 1:]
        magma.eval(
            'NN := [I : I in ideals | Norm(I) eq ' + str(v["level_norm"]) + '][' + str(NN_index) + '];')
        magma.eval('M := HilbertCuspForms(F, NN);')

        if v["hecke_polynomial"] != 'x':
            magma.eval('K<e> := NumberField(' + v["hecke_polynomial"] + ');')
        else:
            magma.eval('K := Rationals(); e := 1;')

        magma.eval('hecke_eigenvalues := [' + ','.join([st for st in v["hecke_eigenvalues"]]) + '];')

        print "...Hecke eigenvalues loaded..."

        magma.eval('s := 0; KT := []; '
                   'while KT cmpeq [] or Dimension(KT) gt 1 do '
                   '  s +:= 1; '
                   '  pp := primes[s]; '
                   '  if Valuation(NN, pp) eq 0 then '
                   '    T_pp := HeckeOperator(M, pp); '
                   '    a_pp := hecke_eigenvalues[s]; '
                   '    if KT cmpeq [] then '
                   '      KT := Kernel(ChangeRing(Matrix(T_pp),K)-a_pp); '
                   '    else '
                   '      KT := KT meet Kernel(ChangeRing(Matrix(T_pp),K)-a_pp); '
                   '    end if; '
                   '  end if; '
                   'end while;')
        magma.eval('assert Dimension(KT) eq 1;')

        print "...dimension 1 subspace found..."

        magma.eval('NNfact := Factorization(NN);')
        magma.eval('f := Vector(Basis(KT)[1]); '
                   'AL_eigenvalues := []; '
                   'for pp in NNfact do '
                   '  U_ppf := f*ChangeRing(AtkinLehnerOperator(M, pp[1]),K); '
                   '  assert U_ppf eq f or U_ppf eq -f; '
                   '  if U_ppf eq f then '
                   '    Append(~AL_eigenvalues, 1); '
                   '  else '
                   '    Append(~AL_eigenvalues, -1); '
                   '  end if; '
                   'end for;')

#                   '  T_ppf := f*ChangeRing(HeckeOperator(M, pp[1]),K); '\
#                   '  if pp[2] ge 2 then assert T_ppf eq 0*f; else assert T_ppf eq -U_ppf; end if; '\

        print "...AL eigenvalues computed!"

        AL_ind = eval(preparse(magma.eval('[Index(primes,pp[1])-1 : pp in NNfact]')))
        AL_eigenvalues_jv = eval(preparse(magma.eval('AL_eigenvalues')))
        AL_eigenvalues = [[F_hmf["primes"][AL_ind[i]], AL_eigenvalues_jv[i]] for i in range(len(AL_ind))]
        pps_exps = eval(preparse(magma.eval('[pp[2] : pp in NNfact]')))

        hecke_eigenvalues = v["hecke_eigenvalues"]
        for j in range(len(AL_ind)):
            s = AL_ind[j]
            if pps_exps[j] >= 2:
                hecke_eigenvalues[s] = '0'
            else:
                hecke_eigenvalues[s] = str(-AL_eigenvalues[j][1])
        AL_eigenvalues = [[a[0], str(a[1])] for a in AL_eigenvalues]

        v["hecke_eigenvalues"] = hecke_eigenvalues
        v["AL_eigenvalues"] = AL_eigenvalues
        v["AL_eigenvalues_fixed"] = 'done'
        hmf_forms.save(v)
def import_data(hmf_filename, fileprefix=None, ferrors=None, test=True):
    if fileprefix==None:
        fileprefix="."
    hmff = file(os.path.join(fileprefix,hmf_filename))

    if ferrors==None:
        ferrors = file('/home/jvoight/lmfdb/backups/import_data.err', 'a')

    # Parse field data
    v = hmff.readline()
    assert v[:9] == 'COEFFS :='
    coeffs = eval(v[10:][:-2])
    v = hmff.readline()
    assert v[:4] == 'n :='
    n = int(v[4:][:-2])
    v = hmff.readline()
    assert v[:4] == 'd :='
    d = int(v[4:][:-2])

    magma.eval('F<w> := NumberField(Polynomial(' + str(coeffs) + '));')
    magma.eval('ZF := Integers(F);')

    # Find the corresponding field in the database of number fields
    dkey = make_disc_key(ZZ(d))[1]
    sig = "%s,%s" % (n,0)
    print("Finding field with signature %s and disc_key %s ..." % (sig,dkey))
    fields_matching = fields.find({"disc_abs_key": dkey, "signature": sig})
    cnt = fields_matching.count()
    print("Found %s fields" % cnt)
    assert cnt >= 1
    field_label = None
    co = str(coeffs)[1:-1].replace(" ","")
    for i in range(cnt):
        nf = fields_matching.next()
        print("Comparing coeffs %s with %s" % (nf['coeffs'], co))
        if nf['coeffs'] == co:
            field_label = nf['label']
    assert field_label is not None
    print "...found!"

    # Find the field in the HMF database
    print("Finding field %s in HMF..." % field_label)
    F_hmf = hmf_fields.find_one({"label": field_label})
    if F_hmf is None:
        # Create list of ideals
        print "...adding!"
        ideals = eval(preparse(magma.eval('nice_idealstr(F);')))
        ideals_str = [str(c) for c in ideals]
        if test:
            print("Would now insert data for %s into hmf_fields" % field_label)
        else:
            hmf_fields.insert({"label": field_label,
                               "degree": n,
                               "discriminant": d,
                               "ideals": ideals_str})
        F_hmf = hmf_fields.find_one({"label": field_label})
    else:
        print "...found!"

    print "Computing ideals..."
    ideals_str = F_hmf['ideals']
    ideals = [eval(preparse(c)) for c in ideals_str]
    ideals_norms = [c[0] for c in ideals]
    magma.eval('ideals_str := [' + ''.join(F_hmf['ideals']).replace('][', '], [') + ']')
    magma.eval('ideals := [ideal<ZF | {F!x : x in I}> : I in ideals_str];')

    # Add the list of primes
    print "Computing primes..."
    v = hmff.readline()  # Skip line
    v = hmff.readline()
    assert v[:9] == 'PRIMES :='
    primes_str = v[10:][:-2]
    primes_array = [str(t) for t in eval(preparse(primes_str))]
    magma.eval('primes_array := ' + primes_str)
    magma.eval('primes := [ideal<ZF | {F!x : x in I}> : I in primes_array];')
    magma.eval('primes_indices := [Index(ideals, pp) : pp in primes];')
    try:
        assert magma('&and[primes_indices[j] gt primes_indices[j-1] : j in [2..#primes_indices]]')
        resort = False
    except AssertionError:
        print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Primes reordered!"
        resort = True
        magma.eval('_, sigma := Sort(primes_indices, func<x,y | x-y>);')
        magma.eval('perm := [[xx : xx in x] : x in CycleDecomposition(sigma) | #x gt 1]')
        # Check at least they have the same norm
        magma.eval('for tau in perm do assert #{Norm(ideals[primes_indices[t]]) : t in tau} eq 1; end for;')
        primes_resort = eval(magma.eval('Eltseq(sigma)'))
        primes_resort = [c - 1 for c in primes_resort]

    primes_indices = eval(magma.eval('primes_indices'))
    primes_str = [ideals_str[j - 1] for j in primes_indices]
    assert len(primes_array) == len(primes_str)
    print "...comparing..."
    for i in range(len(primes_array)):
        assert magma('ideal<ZF | {F!x : x in ' + primes_array[i] + '}> eq '
                     + 'ideal<ZF | {F!x : x in ' + primes_str[i] + '}>;')
    if resort:
        # Important also to resort the list of primes themselves!
        # not just the a_pp's
        primes_str = [primes_str[i] for i in primes_resort]

    if 'primes' in F_hmf:  # Nothing smart: make sure it is the same
        assert F_hmf['primes'] == primes_str
    else:
        F_hmf['primes'] = primes_str
        if test:
            print("Would now save primes string %s... into hmf_fields" % primes_str[:100])
        else:
            hmf_fields.save(F_hmf)
    print "...saved!"

    # Collect levels
    v = hmff.readline()
    if v[:9] == 'LEVELS :=':
        # Skip this line since we do not use the list of levels
        v = hmff.readline()
    for i in range(3):
        if v[:11] != 'NEWFORMS :=':
            v = hmff.readline()
        else:
            break

    # Finally, newforms!
    print "Starting newforms!"
    while v != '':
        v = hmff.readline()[1:-3]
        if len(v) == 0:
            break
        data = eval(preparse(v))
        level_ideal = data[0]
        level_norm = data[0][0]
        label_suffix = data[1]
        weight = [2 for i in range(n)]

        level_ind = int(magma('Index(ideals, ideal<ZF | {F!x : x in ' + str(level_ideal) + '}>)')
                        ) - 1  # Magma counts starting at 1
        level_ideal = ideals_str[level_ind]
        assert magma('ideal<ZF | {F!x : x in ' + str(level_ideal) + '}> eq '
                     + 'ideal<ZF | {F!x : x in ' + str(data[0]) + '}>;')
        level_dotlabel = level_ind - ideals_norms.index(level_norm) + 1   # Start counting at 1
        assert level_dotlabel > 0
        level_label = str(level_norm) + '.' + str(level_dotlabel)

        label = construct_full_label(field_label, weight, level_label, label_suffix)
        short_label = level_label + '-' + label_suffix

        if len(data) == 3:
            hecke_polynomial = x
            hecke_eigenvalues = data[2]
        else:
            hecke_polynomial = data[2]
            hecke_eigenvalues = data[3]

        if resort:
            hecke_eigenvalues = [hecke_eigenvalues[i] for i in primes_resort]

        # Sort out Atkin-Lehner involutions
        magma.eval('NN := ideal<ZF | {F!x : x in ' + str(level_ideal) + '}>;')
        magma.eval('NNfact := Factorization(NN);')
        ALind = eval(
            preparse(magma.eval('[Index(primes, pp[1])-1 : pp in NNfact | Index(primes,pp[1]) gt 0];')))
        AL_eigsin = [hecke_eigenvalues[c] for c in ALind]
        try:
            assert all([abs(int(c)) <= 1 for c in AL_eigsin])
        except:
            ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')

        AL_eigenvalues = []
        ees = eval(preparse(magma.eval('[pp[2] : pp in NNfact]')))
        for i in range(len(AL_eigsin)):
            if ees[i] >= 2:
                if hecke_eigenvalues[ALind[i]] != 0:
                    ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')
            else:
                try:
                    if abs(int(AL_eigsin[i])) != 1:
                        ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')
                    else:
                        AL_eigenvalues.append([primes_str[ALind[i]], -AL_eigsin[i]])
                except TypeError:
                    ferrors.write(str(n) + '/' + str(d) + ' ' + label + '\n')

        assert magma('[Valuation(NN, ideal<ZF | {F!x : x in a}>) gt 0 : a in [' +
                     str(''.join([a[0] for a in AL_eigenvalues])).replace('][', '], [') + ']];')

        # Constrain eigenvalues to size 2MB
        hecke_eigenvalues = [str(c) for c in hecke_eigenvalues]
        leftout = 0
        while sum([len(s) for s in hecke_eigenvalues]) > 2000000:
            leftout += 1
            hecke_eigenvalues = hecke_eigenvalues[:-1]
        if leftout > 0:
            print "Left out", leftout

        info = {"label": label,
                "short_label": short_label,
                "field_label": field_label,
                "level_norm": int(level_norm),
                "level_ideal": str(level_ideal),
                "level_label": level_label,
                "weight": str(weight),
                "label_suffix": label_suffix,
                "dimension": hecke_polynomial.degree(),
                "hecke_polynomial": str(hecke_polynomial),
                "hecke_eigenvalues": hecke_eigenvalues,
                "AL_eigenvalues": [[str(a[0]), str(a[1])] for a in AL_eigenvalues]}
        print info['label']

        existing_forms = hmf_forms.find({"label": label})
        assert existing_forms.count() <= 1
        if existing_forms.count() == 0:
            if test:
                print("Would now insert form data %s into hmf_forms" % info)
            else:
                hmf_forms.insert(info)
        else:
            existing_form = existing_forms.next()
            assert info['hecke_polynomial'] == existing_form['hecke_polynomial']
            try:
                assert info['hecke_eigenvalues'] == existing_form['hecke_eigenvalues']
                print "...duplicate"
            except AssertionError:
                print "...Hecke eigenvalues do not match!  Checking for permutation"
                assert set(info['hecke_eigenvalues'] + ['0','1','-1']) == set(existing_form['hecke_eigenvalues'] + [u'0',u'1',u'-1'])
                # Add 0,1,-1 to allow for Atkin-Lehner eigenvalues, if not computed
                print "As sets, ignoring 0,1,-1, the eigenvalues match!"
                if test:
                    print("Would now replace permuted form data %s with %s" % (existing_form, info))
                else:
                    existing_form['hecke_eigenvalues'] = info['hecke_eigenvalues']
                    hmf_forms.save(existing_form)
Example #28
0
 def _pre_execute_filter(self, line):
     line = preparse(line)
     if line[-1] == '?':
         return 'introspect(%s, format="print")' % line[:-1]
     else:
         return line
Example #29
0
    def parse(self, string, *args):
        r"""
        A Sage specialization of :class:`doctest.DocTestParser`.

        INPUTS:

        - ``string`` -- the string to parse.
        - ``name`` -- optional string giving the name indentifying string,
          to be used in error messages.

        OUTPUTS:

        - A list consisting of strings and :class:`doctest.Example`
          instances.  There will be at least one string between
          successive examples (exactly one unless or long or optional
          tests are removed), and it will begin and end with a string.

        EXAMPLES::

            sage: from sage.doctest.parsing import SageDocTestParser
            sage: DTP = SageDocTestParser(True, ('sage','magma','guava'))
            sage: example = 'Explanatory text::\n\n    sage: E = magma("EllipticCurve([1, 1, 1, -10, -10])") # optional: magma\n\nLater text'
            sage: parsed = DTP.parse(example)
            sage: parsed[0]
            'Explanatory text::\n\n'
            sage: parsed[1].sage_source
            'E = magma("EllipticCurve([1, 1, 1, -10, -10])") # optional: magma\n'
            sage: parsed[2]
            '\nLater text'

        If the doctest parser is not created to accept a given
        optional argument, the corresponding examples will just be
        removed::

            sage: DTP2 = SageDocTestParser(True, ('sage',))
            sage: parsed2 = DTP2.parse(example)
            sage: parsed2
            ['Explanatory text::\n\n', '\nLater text']

        You can mark doctests as having a particular tolerance::

            sage: example2 = 'sage: gamma(1.6) # tol 2.0e-11\n0.893515349287690'
            sage: ex = DTP.parse(example2)[1]
            sage: ex.sage_source
            'gamma(1.6) # tol 2.0e-11\n'
            sage: ex.want
            '0.893515349287690\n'
            sage: type(ex.want)
            <class 'sage.doctest.parsing.MarkedOutput'>
            sage: ex.want.tol
            2.000000000000000000?e-11

        You can use continuation lines::

            sage: s = "sage: for i in range(4):\n....:     print i\n....:\n"
            sage: ex = DTP2.parse(s)[1]
            sage: ex.source
            'for i in range(Integer(4)):\n    print i\n'

        Sage currently accepts backslashes as indicating that the end
        of the current line should be joined to the next line.  This
        feature allows for breaking large integers over multiple lines
        but is not standard for Python doctesting.  It's not
        guaranteed to persist, but works in Sage 5.5::

            sage: n = 1234\
            ....:     5678
            sage: print n
            12345678
            sage: type(n)
            <type 'sage.rings.integer.Integer'>

        It also works without the line continuation::

            sage: m = 8765\
            4321
            sage: print m
            87654321
        """
        # Hack for non-standard backslash line escapes accepted by the current
        # doctest system.
        m = backslash_replacer.search(string)
        while m is not None:
            next_prompt = find_sage_prompt.search(string, m.end())
            g = m.groups()
            if next_prompt:
                future = string[m.end():next_prompt.start(
                )] + '\n' + string[next_prompt.start():]
            else:
                future = string[m.end():]
            string = string[:m.start()] + g[0] + "sage:" + g[1] + future
            m = backslash_replacer.search(string, m.start())

        string = find_sage_prompt.sub(r"\1>>> sage: ", string)
        string = find_sage_continuation.sub(r"\1...", string)
        res = doctest.DocTestParser.parse(self, string, *args)
        filtered = []
        for item in res:
            if isinstance(item, doctest.Example):
                optional_tags = parse_optional_tags(item.source)
                if optional_tags:
                    for tag in optional_tags:
                        self.optionals[tag] += 1
                    if ('not implemented'
                            in optional_tags) or ('not tested'
                                                  in optional_tags):
                        continue
                    if 'long time' in optional_tags:
                        if self.long:
                            optional_tags.remove('long time')
                        else:
                            continue
                    if not (self.optional_tags is True
                            or optional_tags.issubset(self.optional_tags)):
                        continue
                elif self.optional_only:
                    self.optionals['sage'] += 1
                    continue
                item.want = parse_tolerance(item.source, item.want)
                if item.source.startswith("sage: "):
                    item.sage_source = item.source[6:]
                    if item.sage_source.lstrip().startswith('#'):
                        continue
                    item.source = preparse(item.sage_source)
            filtered.append(item)
        return filtered
Example #30
0
 def preparsed(self):
     if '_preparsed' not in self.__dict__:
         from sage.misc.preparser import preparse
         self._preparsed = preparse(self._input, reset=True)
     return self._preparsed