#!/bin/python

#automatic generation of Taylor coefficients
#representation of derivations as sums
#use Runge 1924, p 287 algorithm
#g -> dg/dx + f dg/dy

#make this recursive
#we trust compiler to optimize this 
#alternative would be linear programming 

#a term is a list of factors

import string

class exp:
	def __init__(self,type,data):
		self.type = type
		self.data = data
	def unfold(self):
		if self.type == 'tderiv':
			self.data = [(self.data[0]-1),exp('sum', [exp('xderiv',[1,self.data[1]]), \
			exp('ffact',[1,[exp('yderiv',[1,self.data[1]])]])])]
	def __repr__(self):
		if self.type == 'xderiv':
			return "(" + "%s" % self.data[1] + "/dx^" + "%d" % self.data[0] + ")"
		if self.type == 'yderiv':
			return "(" + "%s" % self.data[1] + "/dy^" + "%d" % self.data[0] + ")" 
		if self.type == 'tderiv':
			return "(" + "%s" % self.data[1] + "/dt^" + "%d" % self.data[0] + ")"
		if self.type == 'ffact':
			return "(f^" + "%d" % self.data[0] + "*"+"%s" % self.data[1]+")"
		if self.type == 'base':
			return '@'
		if self.type == 'sum':
			s = '('
			for i in self.data:
				s = s + "%s+" % i 
			s = s + ')'
			return s

t = exp('tderiv',[2, exp('base','')])
print t
t.unfold()
print t
t.unfold()
print t

