Symbolic Differentiation of Polynomials Symbolic Differentiation 1: Gone Coefishin'

Task

Write a program that takes in a polynomial in x from stdin (1 &lt; deg(p) &lt; 128) and differentiates it. The input polynomial will be a string of the following form:

"a + bx + cx^2 + dx^3 +" ...


where the coefficient of each term is an integer (-128 &lt; a &lt; 128). Each term is separated by one space, a +, and another space; linear and constant terms appear as above (i.e., no x^0 or x^1). Terms will appear in order of increasing degree, and those powers with zero coefficient are omitted. All terms with coefficient 1 or -1 display that coefficient explicitly.

Your output must have precisely the same form. Note that coefficients in the output might be as big as 127*127 == 16129.

Examples

"3 + 1x + 2x^2" ==&gt; "1 + 4x"
"1 + 2x + -3x^2 + 17x^17 + -1x^107" ==&gt; "2 + -6x + 289x^16 + -107x^106"
"17x + 1x^2" ==&gt; "17 + 2x"


Scoring

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.
Retina, 53 43 42 41 40 35 bytes

^[^x]+ |(\^1)?\w(?=1*x.(1+)| |$)
$2


For counting purposes each line goes in a separate file, but you can run the above as a single file by invoking Retina with the -s flag.

This expects the numbers in the input string to be given in unary and will yield output in the same format. E.g.

1 + 11x + -111x^11 + 11x^111 + -1x^11111
--&gt;
11 + -111111x + 111111x^11 + -11111x^1111


instead of

1 + 2x + -3x^2 + 2x^3 + -1x^5
--&gt;
2 + -6x + 6x^2 + -5x^4


Explanation

The code describes a single regex substitution, which is basically 4 substitutions compressed into one. Note that only one of the branches will fill group $2 so if any of the other three match, the match will simply be deleted from the string. So we can look at the four different cases separately:

^[^x]+&lt;space&gt;
&lt;empty&gt;


If it's possible to reach a space from the beginning of the string without encountering an x that means the first term is the constant term and we delete it. Due to the greediness of +, this will also match the plus and the second space after the constant term. If there is no constant term, this part will simply never match.

x(?= )
&lt;empty&gt;


This matches an x which is followed by a space, i.e. the x of the linear term (if it exists), and removes it. We can be sure that there's a space after it, because the degree of the polynomial is always at least 2.

1(?=1*x.(1+))
$1


This performs the multiplication of the coefficient by the exponent. This matches a single 1 in the coefficient and replaces it by the entire corresponding exponent via the lookahead.

(\^1)?1(?= |$)
&lt;empty&gt;


This reduces all remaining exponents by matching the trailing 1 (ensured by the lookahead). If it's possible to match ^11 (and a word boundary) we remove that instead, which takes care of displaying the linear term correctly.

For the compression, we notice that most of the conditions don't affect each other. (\^1)? won't match if the lookahead in the third case is true, so we can put those two together as

(\^1)?1(?=1*x.(1+)| |$)
$2


Now we already have the lookahead needed for the second case and the others can never be true when matching x, so we can simply generalise the 1 to a \w:

(\^1)?\w(?=1*x.(1+)| |$)
$2


The first case doesn't really have anything in common with the others, so we keep it separate.