A polynomial over a variable x is a function of the form
p(x) = anxn + an-1xn-1 + ... + a1x + a0
where a0 ... an are the coefficients. In the simplest case, the coefficients are integers, e.g.
p1(x) = 3x2 + 4x + 1
By allowing the coefficients to be polynomials over another variable, we can define polynomials over multiple variables. For example, here is a polynomial over x whose coefficients are polynomials over y with integer coefficients.
p2(x,y) = (2y + 1)x2 + (3y2 - 5y + 1)x + (-4y + 7)
However, we often prefer to write these fully expanded as
p2(x,y) = 2x2y + x2 + 3xy2 - 5xy + x - 4y + 7
Your task is to write a program that will pretty-print a given polynomial in this format.
Input
Your program will receive a single line on standard input* describing a polynomial over multiple variables in the following format.
<name> <variables> <coefficients>
<name>is one or more arbitrary non-whitespace characters.<variables>is one or more characters betweenaandzinclusive, each representing a variable. There are no duplicates, but note that the order is significant, as it affects how the terms should be ordered in the output.<coefficients>is one or more coefficients, separated by spaces, in order of increasing degree.
Each coefficient is either an integer in the usual base 10 notation, or another list of coefficients between square brackets. The different variables correspond to the nesting level of the coefficients, with the "outermost" list corresponding to the first variable and so on. The number of nesting levels will never exceed the number of variables.
For example, the polynomials p1 and p2 above can be represented as follows.
p1 x 1 4 3
p2 xy [7 -4] [1 -5 3] [1 2]
Since constant polynomials can be interpreted as polynomials over any set of variables, some polynomials
may be written in multiple ways. For example, the coefficients of p3(x,y,z) = 4 can be written as 4, [4]
or [[4]].
Output
Your program should pretty-print the input polynomial on standard output* in the following format.
<name>(<variables>) = <terms>
<name>is the name of the polynomial as received.<variables>is the list of variables in the order received, interspersed with commas.<terms>are the terms of the fully expanded polynomial, formatted according to the rules below.Each term is an integer coefficient followed by powers of each variable in the order defined in the input, all separated by a single space. The general format is
<variable>^<exponent>.The terms are sorted by the exponents of the variables in decreasing lexicographical order. For example,
3 x^2 y^4 z^3should come before9 x^2 y z^13because2 4 3 > 2 1 13lexicographically.Omit terms where the integer coefficient is zero, unless the whole polynomial is zero.
Omit the integer coefficient if its absolute value is one, i.e. write
xinstead of1 x, unless this is a constant term.When the exponent of a variable in a term is zero, omit the variable from that term.
Similarly, when the exponent of a variable is one, omit the exponent.
The terms should be separated by
+or-signs, with a space on each side, according to the sign of the integer coefficient. Similarly, include the sign in front of the first term if it's negative.
Examples
Input Output
================================ =======================================================
p1 x 1 4 3 p1(x) = 3 x^2 + 4 x + 1
p2 xy [7 -4] [1 -5 3] [1 2] p2(x,y) = 2 x^2 y + x^2 + 3 x y^2 - 5 x y + x - 4 y + 7
p3 xyz [4] p3(x,y,z) = 4
foo bar [0 2 [0 1]] [[3] [5 -1]] foo(b,a,r) = - b a r + 5 b a + 3 b + a^2 r + 2 a
quux y 0 0 0 0 0 1337 quux(y) = 1337 y^5
cheese xyz [[0 1]] cheese(x,y,z) = z
zero x 0 zero(x) = 0
Winning criteria
This is code golf. The shortest program wins. Standard code golf rules apply.
Bonus challenge
Write another program which does the reverse operation, i.e. takes the pretty-printed output of your original program and gives you back one of the possible corresponding inputs. This is purely for bragging rights and has no effect on your score.
* If your chosen programming language doesn't support standard input/output, you may specify how I/O is handled.
p1andquuxexamples are inconsistent.p1doesn't have brackets andquuxdoes. Which is right? – Keith Randall May 2 '12 at 23:32