We already have a challenge about multiplying multiply single-variable polynomials. This challenge is about multiply two polynomials with multiple variables
Your task is given two multi-variable polynomials (for instance given as nested lists) to return their product.
Examples
(x²+x+1)*(x²+x+1)=x⁴+2x³+3x²+2x+1
(x+y)*(x+y)=x²+2xy+y²
(x+y+z)*(x-y+z) = x²-y²+2xz +z²
a*a=a²
when representing polynomials as nested lists1:
[1,1,1], [1,1,1] -> [1, 2, 3, 2, 1]
[[0,1],[1]], [[0,1],[1]] -> [[0, 0, 1], [0, 2], [1]]
[[[0,1],[1]],[[1]]], [[[0,1],[-1]],[[1]]] -> [[[0, 0, 1], [0], [-1]], [[0, 2]], [[1]]]
[[],[[[[[[[[[1]]]]]]]]]], [[],[[[[[[[[[1]]]]]]]]]] -> [[], [], [[[[[[[[[1]]]]]]]]]]
(non-golfed) solution in Python
Rules
- You can Input/Output the polynomials in any convenient format (as long as each output can only represent one polynomial)
- Input and Output have to use the same format for polynomials
- You can assume that all coefficients are integers
- If you take input as lists you can assume that all numbers in both inputs appear in the nesting depth
- You are allowed to omit zero coefficients in the output (as long as the output value are unambiguous)
- You can use the empty list to represent zero/the zero polynomial
- If your language has a built-in for multi-variable polynomial multiplication consider adding a non-built-in solution as well
- This is code-golf the shortest solution wins
1short explanation of the nested list format used in the examples (you are free to use a different IO format):
Each list represents a single variable polynomial with its coefficients begin either integers or other lists representing polynomials in one less variable. The coefficients are multiplied with the polynomial variable to the power of their index:
[[[0,1],[1]],[[1]]] -> "[[0,1],[1]]"*1+"[[1]]"*z -> ("[0,1]"*1+"[1]"*y)*1+"[1]"*1*z -> (x*1+1*y)*1+1*1*z -> x+y+z
The variables are assigned by nesting layer. I use x
for the innermost variable y
for the next level, then z
.
[[0, 1], [0, 1]], [[0, 1], [0, 1]]
? \$\endgroup\$×
function (multidimensional convolution). Unfortunately it doesn't work on the version on ATO, so I won't post an answer for it. \$\endgroup\$[[0, 1], [0, 1]]
is(0*1+1*y)*1+(0*1+1*y)*x=x+x*y
. \$\endgroup\$