Search code examples
pythonsymbolic-math

Convert a polynomial w(z) to w((1-z)/(1+z))


I am writing a code which takes a "code" (Coding Theory) as an input and I have calculated the weight enumerator of it. I want to find the weight enumerator of dual code using MacWilliams Identity.

I have W(z), the weight enumerator of the code, and I want to convert it to W((1-z)/(1+z)) using python.


Solution

  • If you are going to be working with polynomials in an extended fashion, you should be working with a proper computer algebra system (CAS). sympy is great for manipulations like these. For example:

    from sympy import poly
    from sympy.abc import x,z
    
    W1 = poly(x**2 + 3*x)
    
    # Evaluate the polynomial at x=(1-z)/(1+z)
    W2 = W1((1-z)/(1+z))
    
    print W1
    print W2
    
    >>> Poly(x**2 + 3*x, x, domain='ZZ')
    >>> (-2*z**2 - 2*z + 4)/(z**2 + 2*z + 1)
    

    You can simplify any answer with sympy.simplify and .expand for example:

    import sympy
    
    expr = (4*x**2 - 4*x + 4)*(x**3 + 3*x**2 + 3*x + 1)/(4*(x**2 + 2*x + 1))
    print sympy.simplify(sympy.expand(expr))
    >>> x**3 + 1