Created
August 28, 2017 18:17
-
-
Save jferard/864f4be6e4b63979da176bff380e6c62 to your computer and use it in GitHub Desktop.
Carry computation with sympy (https://codegolf.stackexchange.com/a/140655/72753)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sympy import * | |
from sympy.abc import i | |
zs = [0]*16 | |
aux = [0]*16 | |
bux = [0]*16 | |
cux = [0]*16 | |
dux = [0]*16 | |
# step 1 : z0, aux, bux | |
for i in range(16): | |
aux[i] = Indexed('a',i) + Indexed('b',i) | |
bux[i] = Indexed('a',i) * Indexed('b',i) | |
zs[0] = bux[0] | |
# step 2: z1, cux, dux (cux can be computed at previous step) | |
for i in range(1,16): | |
cux[i] = aux[i]*aux[i-1] | |
dux[i] = aux[i]*bux[i-1] | |
zs[1] = aux[1]*zs[0] + bux[1] | |
# next steps | |
for i in range(2,15,2): | |
zs[i] = aux[i]*zs[i-1] + bux[i] | |
zs[i+1] = cux[i+1]*zs[i-1] + dux[i+1] + bux[i+1] | |
# check values | |
cs = [0]*16 | |
cs[0] = Indexed('a',0)*Indexed('b',0) | |
for i in range(1,16): | |
cs[i] = Indexed('a',i)*Indexed('b',i)+Indexed('a',i)*cs[i-1]+Indexed('b',i)*cs[i-1] | |
for i, (c,z) in enumerate(zip(cs, zs)): | |
print ("check"+str(i)) | |
assert expand(c-z) == 0, "{0} => {1}".format(i, expand(c-z)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment