Ticket #13609: trac_13609-gf2_content.take2.patch

File trac_13609-gf2_content.take2.patch, 3.0 KB (added by burcin, 9 years ago)

apply only this patch

  • sage/symbolic/expression.pyx

    # HG changeset patch
    # User Burcin Erocal <burcin@erocal.org>
    # Date 1353512376 -3600
    # Node ID 8b4e4271807f9ee147ff76a5a40e4ab23b626aa5
    # Parent  d012102e0e41f5f8cfd5b4a96bf4ff74d0ff80b9
    Trac 13609: fix symbolic expression auto evaluation when content is in GF(2^k)
    
    This fixes Pynac issue #12 (http://hg.pynac.org/pynac/issue/12).
    
    If the content of an add expression is in GF(2^k) the normalization code that
    extracts the content led to an infinite loop. This patch adds doctests for the
    fix in Pynac.
    
    diff --git a/sage/symbolic/expression.pyx b/sage/symbolic/expression.pyx
    a b  
    25532553            sage: SR(unsigned_infinity)*SR(oo)
    25542554            Infinity
    25552555
     2556        Check if multiplication works when content is in `F_{2^k}`,
     2557        examples from :trac:`13107`::
     2558
     2559            sage: var('c1,c2,c3,r1,r2')
     2560            (c1, c2, c3, r1, r2)
     2561            sage: ff.<z> = GF(2**8, 'z')
     2562            sage: ex = -(c1 + r2 - c2*r1)/c3
     2563            sage: ex.substitute(r1=z, r2=z)
     2564            -(c1 + z*c2 + z)/c3
     2565
     2566        Note the content and the leading coefficient of the numerator after
     2567        the substitution::
     2568
     2569            sage: num = ex.op[0].subs({r1: z, r2: z}); num
     2570            -c1 + z*c2 + z
     2571            sage: num.leading_coefficient(c1)
     2572            -1
     2573            sage: num.content(c1)
     2574            1
     2575            sage: num.content(c1).pyobject().parent()
     2576            Finite Field in z of size 2^8
     2577
     2578        The leading coefficient is a negative number. The normalization process
     2579        tries to convert it to a positive number and extract the content, by
     2580        multiplying by ``-1/c``.  However, the content is in a field of
     2581        characteristic 2, so negating it does not change the leading
     2582        coefficient.
     2583
     2584        Another example where there is no problem::
     2585
     2586            sage: ex = -(r2 - c2*r1)/c3
     2587            sage: num = ex.op[0].subs({r1: z, r2: z}); num
     2588            z*c2 + z
     2589            sage: num.leading_coefficient(c2)
     2590            z
     2591            sage: num.content(c2)
     2592            1
     2593            sage: num.content(c2).pyobject().parent()
     2594            Finite Field in z of size 2^8
     2595
     2596        Since the leading coefficient is not negative, no normalization is
     2597        performed.
     2598
     2599        The leading coefficient of the expression depends on the order of the
     2600        variables, which is chosen to be lexicographic in Sage. Hence, using
     2601        different variable names may change behavior and hide the bug. To cover
     2602        these cases we test with different variable names::
     2603
     2604            sage: var('a,b')
     2605            (a, b)
     2606            sage: ex = -(c1 + b - c2*a)/c3
     2607            sage: ex.substitute(a=z, b=z)
     2608            -(c1 + z*c2 + z)/c3
     2609            sage: var('x1,x2,x3')
     2610            (x1, x2, x3)
     2611            sage: ex = -(x1 + r2 - x2*r1)/x3
     2612            sage: ex.substitute(a=z, b=z)
     2613            (r1*x2 - r2 - x1)/x3
    25562614        """
    25572615        cdef GEx x
    25582616        cdef Expression _right = <Expression>right