Your task is to take two single-variable integer polynomial expressions and multiply them into their unsimplified first-term-major left-to-right expansion (A.K.A. FOIL in the case of binomials). Do not combine like terms or reorder the result. To be more explicit about the expansion, multiply the first term in the first expression by each term in the second, in order, and continue in the first expression until all terms have been multiplied by all other terms. Expressions will be given in a simplified LaTeX variant.
Each expression will be a sequence of terms separated by
+ (with exactly one space on each side) Each term will conform to the following regular expression: (PCRE notation)
In plain English, the term is an optional leading
- followed by one or more digits followed by
x and a nonnegative integer power (with
An example of a full expression:
6x^3 + 1337x^2 + -4x^1 + 2x^0
When plugged into LaTeX, you get \$6x^3 + 1337x^2 + -4x^1 + 2x^0\$
The output should also conform to this format.
Since brackets do not surround exponents in this format, LaTeX will actually render multi-digit exponents incorrectly. (e.g.
4x^3 + -2x^14 + 54x^28 + -4x^5 renders as \$4x^3 + -2x^14 + 54x^28 + -4x^5\$) You do not need to account for this and you should not include the brackets in your output.
Example Test Cases
5x^4 3x^23 15x^27
6x^2 + 7x^1 + -2x^0 1x^2 + -2x^3 6x^4 + -12x^5 + 7x^3 + -14x^4 + -2x^2 + 4x^3
3x^1 + 5x^2 + 2x^4 + 3x^0 3x^0 9x^1 + 15x^2 + 6x^4 + 9x^0
4x^3 + -2x^14 + 54x^28 + -4x^5 -0x^7 0x^10 + 0x^21 + 0x^35 + 0x^12
4x^3 + -2x^4 + 0x^255 + -4x^5 -3x^4 + 2x^2 -12x^7 + 8x^5 + 6x^8 + -4x^6 + 0x^259 + 0x^257 + 12x^9 + -8x^7
Rules and Assumptions
- You may assume that all inputs conform to this exact format. Behavior for any other format is undefined for the purposes of this challenge.
- It should be noted that any method of taking in the two polynomials is valid, provided that both are read in as strings conforming to the above format.
- The order of the polynomials matters due to the expected order of the product expansion.
- You must support input coefficients between \$-128\$ and \$127\$ and input exponents up to \$255\$.
- Output coefficents between \$-16,256\$ and \$16,384\$ and exponents up to \$510\$ must therefore be supported.
- You may assume each input polynomial contains no more than 16 terms
- Therefore you must (at minimum) support up to 256 terms in the output
- Terms with zero coefficients should be left as is, with exponents being properly combined
- Negative zero is allowed in the input, but is indistinguishable from positive zero semantically. Always output positive zero. Do not omit zero terms.
Happy Golfing! Good luck!