#C23.txt, April 18, 2019, Computing derivatives and gradients of complicated functions Help:=proc(): print(`Box(n), IsGG(v) , Fsss(n) , Fss(n) , Fs(n), F(n,K) , EvalC(L,x,x0)`): print(`RP(x,d,K), RC(x,d,K,n) , EvalCD(L,x,x0) `): end: #Box(n): the set of 0-1 vectors of lenght n Box:=proc(n) local S,s: if n=0 then RETURN({[]}): else S:=Box(n-1): RETURN({seq([op(s),0], s in S),seq([op(s),1], s in S)}): fi: end: #IsGG(v): Does v contain 11 IsGG:=proc(v) local i: for i from 1 to nops(v)-1 do if v[i]=1 and v[i+1]=1 then RETURN(false): fi: od: true: end: #Fsss(n):the n-th Fibonacci number using the extremely stupid way #One of the Combinatorial definition of fibonacci number, the number #of 0-1 vectors of length n avoiding 11 Fsss:=proc(n) local S,G,co,s: S:=Box(n): co:=0: for s in S do if IsGG(s) then co:=co+1: fi: od: co: end: #Fss(n): the n-th Fibonacci number using the very stupid way Fss:=proc(n): if n=0 or n=1 then n: else Fss(n-1)+Fss(n-2): fi: end: #Fs(n): the n-th Fibonacci number using the usual way mod K Fs:=proc(n,K) option remember: if n=0 or n=1 then n: else Fs(n-1,K)+Fs(n-2,K) mod K: fi: end: #F(n,K): the n-th Fibonacci number the clever way mod K F:=proc(n,K) local a,b,c,i: a:=0: b:=1: for i from 1 to n do c:=a+b mod K: a:=b: b:=c: od: a: end: #RP(x,d,K): a random poly in x of degree k with coefficients from 1 to K do RP:=proc(x,d,K) local ra,i: ra:=rand(1..K): add(ra()*x^i,i=0..d): end: #RC(x,d,K,n): a random chain of length n of poly in x of degree k with coefficients from 1 to K do RC:=proc(x,d,K,n) local i: [seq(RP(x,d,K),i=1..n)]: end: #[x0->x1->x2-> ... ->xn] #EvalC(L,x,x0): inputs a listL of expressions in the variable x and a number x0 #outputs L[n](L[n-1](.... L[1](x0))))))))) mod K: #x0->L[1](x0)->L[2](L[1](x0))-.... EvalC:=proc(L,x,x0) local a,i: a:=x0: for i from 1 to nops(L) do a:=subs(x=a,L[i]): od: a: end: #EvalCD(L,x,x0): inputs a listL of expressions in the variable x and a number x0 #outputs L[n](L[n-1](.... L[1](x0))))))))) followed by the derivative of the composition #at x0: #x0->L[1](x0)->L[2](L[1](x0))-.... #f(g(x))'= f'(g(x))*g'(x) . (F[i](Previous(x)))'(x0)= F[i]'(Previoius(x)|x=x0)* (Previous'(x)|x=x0) EvalCD:=proc(L,x,x0) local a,b,i: a:=x0: b:=1: for i from 1 to nops(L) do b:=b*subs(x=a,diff(L[i],x)) : a:=subs(x=a,L[i]): od: a,b: end: