#OK to post homework #Victoria Chayes, 4/14/19, Assignment 21 Help:=proc(): print(`EvalFCdiffk(L,x,x0,k), R0(A,B,x,y), EvalNR2(L,c1, c2, x0,y0)`): end: #1 Write a procedure EvalFCdiffk(L,x,x0,k): inputs a functional chain L, variable x, and number or symbol x0 and outputs the k-th derivative of EvalFC(L,x,y) w.r.t. y and y=x0. EvalFCdiffk:=proc(L,x,x0,k) local i,Ld,M,L0: L0:=EvalFC(L,x,x0): Ld:=[seq(diff(L[i],x$k),i=1..nops(L))]: M:=[subs(x=x0,Ld[1])]: for i from 2 to nops(L) do M:=[op(M), subs(x=L0[i-1] ,Ld[i]) ]: od: convert(M,`*`): end: #2 Find out whether the number of terms of diff(f(g(x)),x$i) and the sum of its coefficients are in the OEIS. #Number of terms: #seq(nops(diff(f(g(x)),x$i)),i=1..10); # 2, 2, 3, 5, 7, 11, 15, 22, 30, 42 # The sequence, if the first "2" is taken out, matches a(n) is the number of partitions of n (the partition numbers) up to at least 40 entries. There was a comment of "Numbers of terms to be added when expanding the n-th derivative of 1/F(x)" which seems to be the specific case of f(x)=1/x, g(x)=F(x). #Sum of coefficients: #seq(SumCoeff(diff(f(g(x)),x$i)),i=2..10); # 2, 5, 15, 52, 203, 877, 4140, 21147, 115975 # The sequence matches the Bell or exponential numbers: number of ways to partition a set of n labeled elements, up to at least 15 entries. #3 Find out whether the number of terms of diff(f(g(h(x))),x$i) and the sum of its coefficients are in the OEIS. #Number of terms: #seq(nops(diff(f(g(h(x))),x$i)),i=1..10); # 3, 3, 6, 13, 23, 44, 74, 129, 210, 345 #Listed as: Number of terms in n-th derivative of a function composed with itself 3 times. #Sum of coefficients: #seq(SumCoeff(diff(f(g(h(x))),x$i)),i=2..10); # 3, 12, 60, 358, 2471, 19302, 167894, 1606137, 16733779 #Listed as: E.g.f.: exp(exp(exp(x)-1)-1). Matches up to at least 20 terms. #4 [Optional Challenge] This question sets up a neural net with two inputs, n layers with two neurons in each layer, and one output. For a 2 by 2 matrix A (given as a list of lists) [[a11,a12],[a21,a22]], and a vector of length 2, B=[b1,b2], define R(A,B)(x,y):=[max(a11*x+a12*y+b1,0), max(a21*x+a22*y+b2,0)]. Write a procedure EvalNR2(L,c1, c2, x0,y0) that inputs a list L of pairs [A1,B1],[A2,B2], ..., [An,Bn] and ouputs the value in going from left to right and applying to the current vector the transformation [x,y] -> [max(a11*x+a12*y+b1,0), max(a21*x+a22*y+b2,0)] for the current A=[[a11,a12],[a21,a22]], and B=[b1,b2], max(c1*x+c2*y,0) to the final [x,y]. Use plots[plot3d] to plot a few such functions. R0:=proc(A,B,x,y): [max(A[1][1]*x+A[1][2]*y+B[1],0), max(A[2][1]*x+A[2][2]*y+B[2],0)]: end: EvalNR2:=proc(L,c1, c2, x0,y0) local X: n:=nops(L): X:=R0(L[1][1], L[1][2], x0, y0): if n=1 then return(max(c1*X[1]+c2*X[2],0): fi: for i from 2 to n do X:=R0(L[i][1], L[i][2], X[1], X[2]): od: max(c1*X[1]+c2*X[2],0): end: ########################################################## #C21.txt, April 11, 2019, Functional chains (and networks) Help21:=proc(): print(` EvalFC1(L,x,0), EvalFC(L,x,x0) , RP(d,K,x) , RC(d,K,n,x) `): print(`EvalFCdiff(L,x,x0), SumCoeff(A) `): end: #y=a*x+b #Data-Set [x1,y1], ..., [xn,yn] L(a,b):-=Sum(a*xi+b-yi)^2,i=1..n); #diff(L,a)=0, diff(L,b)=0 a*x_{n+1}+b # [x1,..., xm; y]: Loss(a1, ..., am,a0):=Sum((a1*x1+...+am*xm+a0-y)^2 , data points in the set) #[m1,m2,m3, ..., mk] m1xm2 +m2xm3+.... #CNN #functional chain x->f1(x)->f2(f1(x)) #Def: A functional chain is a list of expressions [f1, ..., fn] in x #EvalFC1(L,x,x0): inputs L=[f1,..., fn] expressions in x and a number (or symbol) x0 #outputs f1(f2(...fn(x0))))) EvalFC1:=proc(L,x,x0) local i,L1: if nops(L)=1 then RETURN(subs(x=x0,L[1])): fi: L1:=[op(1..nops(L)-1,L)]: expand(subs( x=EvalFC1(L1,x,x0),L[nops(L)])): end: #RP(d,K,x): a random polynomial of degree d in x with coeff. between -K and K RP:=proc(d,K,x) local ra,i: ra:=rand(-K..K): add(ra()*x^i,i=0..d): end: #RC(d,K,n,x): a random functional chain of length n RC:=proc(d,K,n,x) local i: [seq(RP(d,K,x),i=1..n)]: end: #f(g(x))'= f'(g(x))*g'(x) #f1(f2(f3(x))'=f1'(f2(f3(x))*f2'(f3(x))*f3'(x) #(fk(f(k-1)(f(k-2)))'= fk'(f(k-1)....) [(f(k-1)(f(k-2)....) f1'(x0)] #x0->f1(x0)->f2(f1(x0)->.... fn(f_{n-1}(x)) ... #EvalFC(L,x,x0): inputs L=[f1,..., fn] expressions in x and a number (or symbol) x0 #outputs [f1(x0),f2(f1(x0)), f3(f2(f1(x0)), ..., fn(....)]: EvalFC:=proc(L,x,x0) local i,L1: if nops(L)=1 then RETURN([subs(x=x0,L[1])]): fi: L1:=[op(1..nops(L)-1,L)]: L1:=EvalFC(L1,x,x0): [op(L1),subs(x=L1[nops(L1)] , L[nops(L)])]: end: #EvalFCdiff(L,x,x0): inputs a functional chain L, variable x, and number or symbol x0 #outputs the derivative of EvalFC(L,x,y) w.r.t y and y=x0 # EvalFCdiff:=proc(L,x,x0) local i,Ld,M,L0: L0:=EvalFC(L,x,x0): Ld:=[seq(diff(L[i],x),i=1..nops(L))]: M:=[subs(x=x0,Ld[1])]: for i from 2 to nops(L) do M:=[op(M), subs(x=L0[i-1] ,Ld[i]) ]: od: convert(M,`*`): end: #SumCoeff(A): the sum of the coefficient in a + expressin SumCoeff:=proc(A) local su,i, lauren: su:=0: if not type(A,`+`) then RETURN(FAIL): fi: for i from 1 to nops(A) do lauren:=op(1,op(i,A)): if type(lauren,integer) then su:=su+lauren: else su:=su+1: fi: od: su: end: