8
\$\begingroup\$

A symmetric polynomial is a polynomial which is unchanged under permutation of its variables.

In other words, a polynomial f(x,y) is symmetric if and only if f(x,y) = f(y,x); a polynomial g(x,y,z) is symmetric iff g(x,y,z) = g(x,z,y) = g(y,x,z) = etc.

For example, x^2+2xy+y^2, xy and x^3+x^2y+xy^2+y^3 are symmetric polynomials, where 2x+y and x^2+y are not.


The challenge

You will be given a polynomial, and your program should output truthy/falsy values, depending on if the given polynomial is a symmetric polynomial.

The input format is allowed in two ways. A string, and an array, like ["x^2","2xy","y^2"], where the polynomial is the sum of each elements.


Example

x^2+2xy+y^2 => true
xy => true
xy+yz+xz-3xyz => true
(x+y)(x-y) => false
2x+y => false
x^2+y => false
x+2y+3 => false

Specs

The operation has orders, just like in normal math. the order is like this:

() => ^ => * => +-

rules apply.

All the characters in the alphabet (a~z) are accepted as variables, everything else are numbers.

The given polynomial will have 2 or more variables.

Multiplication does not require the * operator, You only need to detect juxtaposition. (detecting by juxtaposition is not neccessary, use the better option)

\$\endgroup\$

closed as unclear what you're asking by Peter Taylor, Laikoni, Blue, pajonk, fəˈnɛtɪk Feb 10 '17 at 13:32

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Is a function a valid input? \$\endgroup\$ – Pavel Feb 4 '17 at 5:36
  • 2
    \$\begingroup\$ Please specify the allowed inputs exactly and include test cases that cover the space well. \$\endgroup\$ – xnor Feb 4 '17 at 7:45
  • 2
    \$\begingroup\$ I don´t think its fair to ask someone to spend time writing an answer and then you decide if it´s valid. So I agree with @xnor. Also you indicated () => ^ => */ => +- but your examples do not show all these. I would have imagined we could expect - but not /. As you have mentioned () are we expected to handle in the format (-1+x)(-y-3)? \$\endgroup\$ – Level River St Feb 4 '17 at 10:43
  • 2
    \$\begingroup\$ This is still unclear. Can arbitrary degree polynomials be multiplied? Can operations be done to exponents? Where can minus signs appear? \$\endgroup\$ – xnor Feb 4 '17 at 18:35
  • 1
    \$\begingroup\$ "The input format is allowed in two ways" but they give two very different challenges, and a lot of the "spec" section is irrelevant if the second one if chosen. \$\endgroup\$ – Peter Taylor Feb 6 '17 at 17:49
5
\$\begingroup\$

Maxima, 40 bytes

f(p):=listp(tpartpol(p,showratvars(p)));

Try It Online!

A function that takes a polynomial as input and returns true if it is symmetric else returns false

\$\endgroup\$
8
\$\begingroup\$

Mathematica, 43 bytes

Last@SymmetricReduction[#,Variables@#]===0&

Unnamed function taking as input a polynomial in the given format (except that juxtaposed variables must be separated by a space) and returning True or False. Variables@# detects the variables appearing in the input (and thus the input can contain all kinds of weird variable names, not just single letters). SymmetricReduction returns an ordered pair of polynomials, where the first one is symmetric and the two sum to the original polynomial; therefore we can detect whether the input is symmetric by seeing whether the second polynomial is identically 0.

\$\endgroup\$
  • \$\begingroup\$ According to OP, you take input as a function, which could maybe let you golf that more. \$\endgroup\$ – Pavel Feb 4 '17 at 5:40
1
\$\begingroup\$

TI-Basic, 46 bytes

Basically, how this works is the polynomial is entered into a (two-byte) variable type that is dynamically evaluated. Then, we swap the X and Y values enough times to see if the function is symmetric.

Prompt u
For(I,0,8
rand->X
Ans->W
rand->Y
u->Z
Y->X
W->Y
If u=W
End
Ans=9
\$\endgroup\$
  • \$\begingroup\$ I know the spec is shaky, so: this program takes input as a string, works with either juxtaposition or the * operator, and outputs 1 for true and 0 for false. \$\endgroup\$ – Timtech Feb 10 '17 at 12:00
  • \$\begingroup\$ Sure, that should be enough to detect one. \$\endgroup\$ – lol Feb 11 '17 at 12:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.