1 | initial version |
I have found a way to do it and I'll share the code here in case someone else wants to do it.
Hope it helps someone else.
def TaylorOrdered(vi,n):
# the added 1 solves the problem of single terms with iterator (numerator and denominator instead of monomials)
DLtmp = 1+(vi.taylor(${LIST OF VARIABLES AND VALUES TO BE REPLACED},n+1).expand())
if DLtmp.is_symbol() or DLtmp.is_constant() or DLtmp.is_numeric():
vo = DLtmp;
else:
for t in DLtmp.iterator():
deg_t = 0
for v in vi.variables():
deg_t = deg_t + powrel[v]*t.degree(v)
if deg_t <= n :
vo = vo + t;
vo = vo.expand()-1;
return vo
2 | No.2 Revision |
I have found a way to do it and I'll share the code here in case someone else wants to do it.
Hope it helps someone else.
def TaylorOrdered(vi,n):
TaylorOrdered(func, vartup, order):
if (not isinstance(vartup,tuple)) or \
((not isinstance(vartup[0],tuple)) and (len(vartup)!=3)) or \
((isinstance(vartup[0],tuple)) and (len(vartup[0])!=3)):
show(vartup[0])
print( (not isinstance(vartup,tuple), not isinstance(vartup[0],tuple), len(vartup[0])!=3) )
raise NotImplementedError, "The second argument should be a tuple of 3-items tuples"
powrel = {}
taylist = []
if not isinstance(vartup[0],tuple):
powrel[vartup[0]] = Integer(vartup[2])
taylist.append( (vartup[0], vartup[1]) )
else:
for i in range(len(vartup)):
powrel[vartup[i][0]] = Integer(vartup[i][2])
taylist.append( (vartup[i][0], vartup[i][1]) )
taylist.append(order+1)
taytup = tuple(taylist)
# the added 1 solves the problem of single terms with iterator (numerator and denominator instead of monomials)
DLtmp = 1+(vi.taylor(${LIST OF VARIABLES AND VALUES TO BE REPLACED},n+1).expand())
1+(func.taylor(*taytup).expand())
if DLtmp.is_symbol() or DLtmp.is_constant() or DLtmp.is_numeric():
vo = DLtmp;
else:
vo = func*0
for t in DLtmp.iterator():
deg_t = 0
for v in vi.variables():
func.variables():
if v in powrel.keys():
deg_t = deg_t + += powrel[v]*t.degree(v)
if deg_t <= n order :
vo = += t;
vo + t;
vo = vo.expand()-1;
return vo
This is the description of the input:
TaylorOrdered(Func, (Var, Point, Rel), Order) # for monovariables
TaylorOrdered(Func, ( (Var1, Point1, Rel1), (Var2, Point2, Rel2), ...), Order) # for multivariables
Here are some examples:
show(TaylorOrdered(sqrt(1+X)+sqrt(1+Y)/h, (X,0,1), 2))
show(TaylorOrdered(sqrt(1+X)+sqrt(1+Y)/h, ((X,0,1),(h,Infinity,-1)), 2))