22
2
Given two polynomials f,g
of arbitrary degree over the integers, your program/function should evaluate the first polynomial in the second polynomial. f(g(x))
(a.k.a. the composition (fog)(x)
of the two polynomials)
Details
Builtins are allowed. You can assume any reasonable formatting as input/output, but the input and output format should match. E.g. formatting as a string
x^2+3x+5
or as as list of coefficients:
[1,3,5] or alternatively [5,3,1]
Furthermore the input polynomials can be assumed to be fully expanded, and the outputs are also expected to be fully expanded.
Examples
A(x) = x^2 + 3x + 5, B(y) = y+1
A(B(y)) = (y+1)^2 + 3(y+1) + 5 = y^2 + 5y + 9
A(x) = x^6 + x^2 + 1, B(y) = y^2 - y
A(B(y))= y^12 - 6y^11 + 15y^10 - 20y^9 + 15y^8 - 6y^7 + y^6 + y^4 - 2 y^3 + y^2 + 1
A(x) = 24x^3 - 144x^2 + 288x - 192, B(y) = y + 2
A(B(y)) = 24y^3
A(x) = 3x^4 - 36x^3 + 138x^2 - 180x + 27, B(y) = 2y + 3
A(B(y)) = 48y^4 - 96y^2
what about builtins? – Maltysen – 2016-04-23T22:15:17.583
1@Maltysen "Details: Builtins are allowed.(...)" :D – flawr – 2016-04-23T22:17:50.423
2I think "any reasonable format" might be a bit stretchable. If a function that evaluates the polynomial is allowed, then the composition function
(.)
is an answer in Haskell. You probably mean some representation of the list of coefficients. – xnor – 2016-04-24T00:03:31.377@xnor I don't think you could describe such a function as fully expanded. – feersum – 2016-04-24T01:29:28.090
Must we avoid needless zero coefficients? – xnor – 2016-04-24T05:33:27.973
@xnor If the Haskell function composition operatur would again fully expand the other functions that would be ok, but I doubt it does that. But thanks for mentioning the (leading) zero coefficients. I think these should be avoided. Or can you give me an example where these make sense? – flawr – 2016-04-24T09:51:16.227
1The title! I just got it :-D – Luis Mendo – 2016-04-24T11:46:27.927
2@LuisMendo Quick thinker =P – flawr – 2016-04-24T11:53:56.337