What’s the easiest way to remove overall factors from polynomials?

I have code that outputs polynomials (in #1) such as the ones below (note that the trailing Function character & is left off for later):

poly1 = 1/64 (3 c^2 e – f^2 – #1) (c^2 (4 e + b^2 g) – 4 (f^2 + #1))^2 (c^2 (4 e + (a^2 + b^2) g) – 4 (f^2 + #1));

poly2 = -(1/64) (b^2 c^2 – 4 #1)^2 (a^2 c^2 + b^2 c^2 – 4 #1) #1;

poly3 = #1^42 (-(1/2) c^2 d^2 + #1)^6;

I’d like to process these polynomials to

drop overall prefactors (like 1/64 from poly1 and poly2), and
and drop factors that are powers of #1 alone

so that results should look like:

(*1*) (3 c^2 e – f^2 – #1) (c^2 (4 e + b^2 g) – 4 (f^2 + #1))^2 (c^2 (4 e + (a^2 + b^2) g) – 4 (f^2 + #1))

(*2*) (b^2 c^2 – 4 #1)^2 (a^2 c^2 + b^2 c^2 – 4 #1)

(*3*) (-(1/2) c^2 d^2 + #1)^6

Is there a fail-proof way to get the job done?

=================

2

 

& isn’t a “trailing function character”. Don’t left it off “for later”
– Dr. belisarius
Feb 4 at 20:34

  

 

@Dr.belisarius I didn’t mean to use “trailing function character” as a technical term. I’m just saying that I’m not adding it for certain reasons.
– QuantumDot
Feb 4 at 21:42

=================

2 Answers
2

=================

It’s not entirely well defined due to handling of numbers, but the following should be at least close to what’s wanted.

poly1 = 1/64 (3 c^2 e – f^2 –
x) (c^2 (4 e + b^2 g) –
4 (f^2 + x))^2 (c^2 (4 e + (a^2 + b^2) g) – 4 (f^2 + x));
poly2 = -(1/64) (b^2 c^2 – 4 x)^2 (a^2 c^2 + b^2 c^2 – 4 x) x;
poly3 = x^42 (-(1/2) c^2 d^2 + x)^6;

removeFactorsIn[poly_, keep_] :=
With[{allbut2 = FactorTermsList[poly, keep][[3 ;; -1]]},
Times @@ Factor[allbut2]]

keepVars = {b, c, e, f, g};

In[52]:= removeFactorsIn[poly1, keepVars]

(* Out[52]= (4 c^2 e – 4 f^2 + b^2 c^2 g – 4 x)^2 (4 c^2 e – 4 f^2 +
a^2 c^2 g + b^2 c^2 g – 4 x) (3 c^2 e – f^2 – x) *)

In[54]:= removeFactorsIn[poly2, keepVars]

(* Out[54]= (b^2 c^2 – 4 x)^2 (a^2 c^2 + b^2 c^2 – 4 x) *)

In[53]:= removeFactorsIn[poly3, keepVars]

(* Out[53]= (c^2 d^2 – 2 x)^6 *)

  

 

What is keepVars supposed to be? Do I put x in there? Comment: the most essential thing for my application is only to drop powers of x. Dropping of constants is only for cosmetic reasons. Since you say the second part is not “well defined”, would it be easier to code it with this relaxed request?
– QuantumDot
Feb 4 at 21:46

  

 

@QuantumDot Sorry, I forgot to paste that in originally. It’s there now. Not x but rather all other variables go in there (which I confess I find confusing, but that was the original design well before I happend onto the function).
– Daniel Lichtblau
Feb 4 at 22:46

How about

Replace[poly, {_Rational -> 1, x^_. -> 1}, {1}]

This line assumes that the overall prefactors are rationals (but you could easily add _Complex if needed) and that the head of poly is indeed Times.