Ticket #13609: trac_13609-rebase.patch

File trac_13609-rebase.patch, 3.0 KB (added by kcrisman, 8 years ago)

Apply only this

  • sage/symbolic/expression.pyx

    # HG changeset patch
    # User Burcin Erocal <burcin@erocal.org>
    # Date 1353512376 -3600
    # Node ID bbd130ddf35b00d8b3f021441c1a875ff13f641d
    # Parent  9361d5d719e9fdeaa4bdd5b82380d01ba32f9a74
    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  
    26302630            in I with defining polynomial x^2 + 1' and 'Finite Field in b of
    26312631            size 3^2'
    26322632
     2633        Check if multiplication works when content is in `F_{2^k}`,
     2634        examples from :trac:`13107`::
     2635
     2636            sage: var('c1,c2,c3,r1,r2')
     2637            (c1, c2, c3, r1, r2)
     2638            sage: ff.<z> = GF(2**8, 'z')
     2639            sage: ex = -(c1 + r2 - c2*r1)/c3
     2640            sage: ex.substitute(r1=z, r2=z)
     2641            -(c1 + z*c2 + z)/c3
     2642
     2643        Note the content and the leading coefficient of the numerator after
     2644        the substitution::
     2645
     2646            sage: num = ex.op[0].subs({r1: z, r2: z}); num
     2647            -c1 + z*c2 + z
     2648            sage: num.leading_coefficient(c1)
     2649            -1
     2650            sage: num.content(c1)
     2651            1
     2652            sage: num.content(c1).pyobject().parent()
     2653            Finite Field in z of size 2^8
     2654
     2655        The leading coefficient is a negative number. The normalization process
     2656        tries to convert it to a positive number and extract the content, by
     2657        multiplying by ``-1/c``.  However, the content is in a field of
     2658        characteristic 2, so negating it does not change the leading
     2659        coefficient.
     2660
     2661        Another example where there is no problem::
     2662
     2663            sage: ex = -(r2 - c2*r1)/c3
     2664            sage: num = ex.op[0].subs({r1: z, r2: z}); num
     2665            z*c2 + z
     2666            sage: num.leading_coefficient(c2)
     2667            z
     2668            sage: num.content(c2)
     2669            1
     2670            sage: num.content(c2).pyobject().parent()
     2671            Finite Field in z of size 2^8
     2672
     2673        Since the leading coefficient is not negative, no normalization is
     2674        performed.
     2675
     2676        The leading coefficient of the expression depends on the order of the
     2677        variables, which is chosen to be lexicographic in Sage. Hence, using
     2678        different variable names may change behavior and hide the bug. To cover
     2679        these cases we test with different variable names::
     2680
     2681            sage: var('a,b')
     2682            (a, b)
     2683            sage: ex = -(c1 + b - c2*a)/c3
     2684            sage: ex.substitute(a=z, b=z)
     2685            -(c1 + z*c2 + z)/c3
     2686            sage: var('x1,x2,x3')
     2687            (x1, x2, x3)
     2688            sage: ex = -(x1 + r2 - x2*r1)/x3
     2689            sage: ex.substitute(a=z, b=z)
     2690            (r1*x2 - r2 - x1)/x3
    26332691        """
    26342692        cdef GEx x
    26352693        cdef Expression _right = <Expression>right