1
$\begingroup$

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

  1. drop overall prefactors (like 1/64 from poly1 and poly2), and
  2. 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?

$\endgroup$
  • 2
    $\begingroup$ & isn't a "trailing function character". Don't left it off "for later" $\endgroup$ – Dr. belisarius Feb 4 '16 at 20:34
  • $\begingroup$ @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. $\endgroup$ – QuantumDot Feb 4 '16 at 21:42
5
$\begingroup$

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 *)
$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – QuantumDot Feb 4 '16 at 21:46
  • $\begingroup$ @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). $\endgroup$ – Daniel Lichtblau Feb 4 '16 at 22:46
0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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