## Python 3
## Code to obtain symbolic E_{eta}[u](t,t0)
## u(t)=(u_0(t),\cdots, u_m(t))
## u_0(t)=1
## t0\in \mathbb{R}
## E_{x_{i_1}eta}[u](t,t0)=int^t_{t0} u_{i_1}(t)*E_{eta}[u](t,t0)dt

## Libraries
import math
import itertools
from sympy import *

t = Symbol('t')

# define input function
def u(t):
    return 1,sin(t),cos(t)

# define eeta function for one word 
def Eeta(eta,t0):
    Eetatemp=1
    for i in eta:
        Eetatemp=integrate(u(t)[i]*Eetatemp,(t,t0,t))
    return Eetatemp

# we can check with either a tuple or a list
# print(Eeta((1,),0))
# print(Eeta([1,1],0))

# define eeta function for a list of words
def Eetavec(etalist,t0):
    Eetavectemp=[]
    for i in etalist:
        Eetavectemp.append(Eeta(i,t0))
    return Eetavectemp

#Eetavec([[0,0],[1,0,0,1]],0)

# generate all possible words from the vocabulary generated by the m+1 letters, truncated to size J
def getalletas(m,J):
    alletas=[]
    vocabulary=list(range(m+1))
    for i in list(range(1,J+1)):
        alletas.append(itertools.product(vocabulary, repeat = i))
    return alletas


#for i in getalletas(2,3):
#    for j in i:
#        print(j)

# generate all eeta functions for all words previously generated
# it is a list of lists where each one contains Eetas for all words of a fixed size
# [[Eetas words of size one],[Eetas words of size two],[Eetas words of size three],...]
def Eetafliess(J,t0):
    m=len(u(t))-1
    Eetafliesstmp=[]
    for i in getalletas(m,J):
        Eetafliesstmp.append(Eetavec(i,t0))
    return Eetafliesstmp
   
# check    
# Eetafliess(2,0)

# writes Eetafliess as only one list: [Eetas words of size one,Eetas words of size two,...]
def Eetafliessv(J,t0):
    Eetafliessvtmp=[]
    for i in Eetafliess(J,t0):
        for j in i:   
            Eetafliessvtmp.append(j)
    return Eetafliessvtmp

# example
v=Eetafliessv(2,0)

# evaluates Eetafliessv at a final point tf
def Eetafeval(v,tf):
    Eetafevaltmp=[]
    for i in v:
        Eetafevaltmp.append(i.subs({t:tf}).evalf())
    return Eetafevaltmp
        
print(Eetafeval(v,1))
