# pattern matching for third degree reciprocal equation

I am trying to create a pattern for third degree reciprocal equation.

I try this

a_ + b_ x_ + b_ x_^2 + a_ x^3


This matches 3 x^3 + 13 x^2 + 13 x + 3 == 0(first species) but it doesn't match 3 x^3 + 7 x^2 - 7 x - 3 (second species).

What pattern should I use for the second species?

Quick note: I was not familiar with the term "reciprocal equations", so I looked it up. These are polynomial equations $f(x)=0$ such that $f(x)=\pm \ x^n f(1/x)$, which can be true if and only if the coefficients satisfy $a_i=\pm \ a_{n-i}$. These are interesting because they can be reduced to equations of a lower degree by substituting $t = x+1/x$ in the original equation. Cool. Learn something every day.

As xslittlegrass suggested, your pattern is too restrictive:

a_ + b_ x_ + b_ x_^2 + a_ x^3


If we concentrate on the b coefficients of the 2nd and 1st degree terms, this pattern will only match if these coefficients are exactly equal down to their sign, not equal in absolute value (i.e. ignoring their sign, as you should given the definition above).

Your second example $3 x^3 + 7 x^2 - 7 x - 3$ won't be matched because e.g. a cannot be 3 and -3 at the same time, nor can b match 7 and -7.

Xslittlegrass has already provided a conditional pattern expression that would match only your second case. However, I understand your question to mean that you would like to match both at the same time.

As an example, this could be accomplished as follows:

Cases[
{3 x^3 + 13 x^2 + 13 x + 3, 3 x^3 + 7 x^2 - 7 x - 3},
a_ | (-a_) + b_ | (-b_) x_ + b_ x_^2 + a_ x_^3
]

(* Out: {3 + 13 x + 13 x^2 + 3 x^3, -3 - 7 x + 7 x^2 + 3 x^3} *)


As you can see, both expressions are matched.

The reason it doesn't work is because the pattern -b_ x_ doesn't match the expression - 7 x, since the latter one has a full form of Times[-7, x].

So you can do something like

Cases[3 x^3 + 7 x^2 - 7 x - 3 == 0, a_ x_^3 + b_ x_^2 + c_ x_ + d_ /; c == -b && d == -a]
(*{-3 - 7 x + 7 x^2 + 3 x^3}*)


The reason it doesn't work is because you are using the same multiplier for the x_ and x_^2 term (i.e.,b) and the same is true for constant and x_^3 term (i.e., a).

In the second expression

3 x^3 + 7 x^2 - 7 x - 3


7 doesn't match -7 and 3 fails to match -3.

If you only want the absolute value you could use

MatchQ[3 x^3 + 13 x^2 + 13 x + 3,
a_ x^3 + b_ x_^2 + c_ x_ + d_ /;
Abs[a] == Abs[d] && Abs[b] == Abs[c]]


True

MatchQ[3 x^3 + 7 x^2 - 7 x - 3,
a_ x^3 + b_ x_^2 + c_ x_ + d_ /;
Abs[a] == Abs[d] && Abs[b] == Abs[c]]


True