def phi(i, x):
    if i == 0:
            return 0.5 * x
    else:
            return phi(i-1, x*1.4)

def x_i(i, x): 
    if i == 0:
            return x
    else:
            return x_i(i-1, x * 0.8)

x0 = 6
max_j = max_i = 10
lookup = []

print r'\hline'
print ' &', ' & '.join(map(lambda x: "$x_%d$" % x, range(0, max_i))), r'\\'
print r'\hline'

for i in range(0,max_i):
    r = []
    
    for j in range(0,max_j):
        r.append("%.2f" % phi(i, x_i(j, x0)))

    print '$\phi_%d(x)$ & ' % i, ' & '.join(r), r'\\'
    print r'\hline'
    lookup.append(r)
    r = []

print " === calculations ==="

n = 10
x_step = float(x0) / n
s = []
left = float(x0)

for i in range(0, n):
    left -= x_step
    s.append(phi(i, x_i(i, x_step)))

print left, s, len(s), sum(s)

print "x_n = x0 / n = %.3f =>" % x_step, sum([phi(i, x_i(i, x_step)) for i in range(0, n)])

for f in range(2, 10, 1):
    x = float(x0)
    left = x

    for d in range(0, n):
        x /= f
    
    s = []

    for i in range(0, n-1):
        print x
        x *= f
        left -= x
        s.append(phi(i, x_i(i, x)))
    
    s.append(phi(n, x_i(n, left)))
    print left, s, len(s)

    print "x_n = x * %d =>" % f, sum(s)


print "all x in phi():", phi(n, x_i(n, float(x0)))
