Symbolic Differentiation 2: Back on the Chain Gang
Write a program that takes in a certain sort of function from stdin and differentiates it with respect to x, using the chain rule.* The input function will be a string of the following form:
"trig_1 trig_2 trig_3 ... x"
trig_n is a trigonometric function
cos, and there are at most four functions in the term. The example string
"cos sin sin x"
represents the function cos(sin(sin(x))).
Your output must take the following form:
k(trig_1 trig_2 x)(trig_a trig_b trig_c x)(trig_m trig_n x) et cetera
k is a constant and parentheses represent groups to be multiplied together.
*The chain rule states that
D[f(g(x))] = f'(g(x)) * g'(x)
"sin sin sin sin x" ==> 1(cos x)(cos sin x)(cos sin sin x)(cos sin sin sin x) "cos x" ==> -1(sin x) "cos sin x" ==> -1(cos x)(sin sin x)
Your score is the length of your program in bytes, multiplied by three if you use a built-in or a library that does symbolic algebra.