kill(tc,onepower)$ tc[0](x):=1$ tc[1](x):=rat(x)$ tc[n](x):=2*x*tc[n-1](x)-tc[n-2](x)$ /*here's another way to generate cheby polys, if you are in a hurry to get a few large ones, and not all the intermediate ones */ tc2[0](x):=1$ tc2[1](x):=x$ tc2[n](x):= if evenp(n) then 2*tc2[n/2](x)^2-1 else 2*tc2[(n+1)/2](x)*tc2[(n-1)/2](x)-x$ ToChebyt(e,x,n):= /* convert analytic expression, depending on x, to Chebyshev series, via taylor series */ block([ tay: taylor(e,x,0,n),q,r,ans:[]], power2cheby(makelist(ratcoef(tay,x,i),i,0,n)))$ /* ToCheby2 is similar, but this one converts an analytic function lambda([x]...expression in x) to Chebyshev series by evaluation. The expression f may be a polynomial or function in other variables, too. */ ToCheby2(f,n):= /* this one takes a function, e,g, lambda([x],..) */ block([in:1.0d0/n,p:?pi/n], apply('chebseries,makelist(AJS1x(f,j,n,in,p,0),j,0,n-1)))$ AJS1(%%f,j,n,in,p,h):= /* compute a[j]s, by eval at gauss-lobatto points */ 2*in*(2-(if (j=n) then 0 else 1))* sum( (h:p*(k+.5d0), cos(h*j)*%%f(cos(h))),k,0,n-1)$ FromCheby(a,x):= /* If x is a symbol, this will convert Chebyshev series to an expression explicitly written out in the power basis, i.e. polynomial in x. If x is a number, this will evaluate the series at that point and provide a number. (assuming typical situation: a is just numbers). This algorithm is based on recurrence in Clenshaw alg. */ block([N:length(a)-1,keepfloat:true, bj2:0,bj1:0,bj0:0, z:reverse(a)], for j: 1 thru N do (bj0:2*x*bj1-bj2+first(z), bj2:bj1, bj1:bj0,z:rest(z)), x*bj1+first(z)/2 -bj2)$ /* We end up generating series that have very small terms in them, like 1e-16, which are painful to look at so we can remove them with this program: */ small_to_zero(expr,eps):= /* Replaces all numbers smaller in absolute value than EPS by zero . */ fullmap(lambda([x], if numberp(x) and abs(x)