1
$\begingroup$

The following calculation is very fast.

k = 3 5 7 11 13 17; Short[Factor[x^k - 1], 5] // AbsoluteTiming

enter image description here

However, if the specific coefficients are expressed in red as follows, the calculation speed becomes too slow.

k = 3 5 7 11 13; Factor[x^k - 1] /. Times[x_Integer, y_] :> Times[If[x > 1 || x < -1, Style[x, Red, Bold], x], y] // AbsoluteTiming

This is much slower when I try to create a CDF with Manipulate.

Manipulate[ Short[Factor[x^n - 1], 2] /. Times[x_Integer, y_] :> 
Times[If[x > 1 || x < -1, Style[x, Red, Bold], x], y] // AbsoluteTiming, {n, 3 5 7 11 13 }, AppearanceElements -> None, ContinuousAction -> None ]

Q1. I want to find a way to speed up the calculation.

Q2. I have the following results expressed in Manipulate, I would like to display the screen to control the set size limit as shown above.

enter image description here

| improve this question | | | | |
$\endgroup$
2
$\begingroup$

This is the most fast version I found.

The trick is, to treat the result of "Factor" as a list.

k = 3 5 7 11*13  ; (fa13 = Factor[x^k - 1]);

(# /. (b_Integer /; b != 0 && b != 1 && b != -1)*x^a_ -> 
  Style[b, Red, Bold]*x^a) & /@ fa13 // AbsoluteTiming

enter image description here

Even the conversion of factor up to 17 takes less than one second

k = 3 5 7 11*13*17  ; (fa17 = Factor[x^k - 1]);

(# /. (b_Integer /; b != 0 && b != 1 && b != -1)*x^a_ -> 
  Style[b, Red, Bold]*x^a) & /@ fa17 // AbsoluteTiming

enter image description here

| improve this answer | | | | |
$\endgroup$
  • $\begingroup$ Thank you for your kind reply. $\endgroup$ – user21427 Jul 3 '17 at 12:11
1
$\begingroup$

As we can see from the above output, coefficients and variables are reversed. Moreover, the red values are moved to the very end.

To preserve the original order:

form[m : Times[a_Integer, b_]] /; a > 1 :=
 Row[{Style[a, Red], Spacer@3, b}]

form[a_] := a

fac = Factor[x^165 - 1];

Unprotect[Plus];
ClearAttributes[Plus, Orderless];

res = Map[form, fac, {2}];

An image of the last factor:

enter image description here

Don't forget to reverse the changes to Plus:

SetAttributes[Plus, Orderless];
Protect[Plus];
| improve this answer | | | | |
$\endgroup$
  • $\begingroup$ Thank you for your kind reply. $\endgroup$ – user21427 Jul 3 '17 at 12:11

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.