# Simplify with Integer Factorization

I have some outputs of Solve applied to algebraic equations with combinatorial coefficients (i.e. integers), and I'd like to see the results from Simplify or FullSimplify to show for example $2^9$ instead of 512 and $2^{20}$ instead of 10484576.

How do I automate this for all integer coefficients instead of manually applying FactorInteger term by term afterwards?

In the example below there are other integers like $1715 = 5 \cdot 7^3$ that I'd also like to be factorized.

So far I haven't been able to find how to do this, and clearly TransformationFucntions isn't meant to be used like this:

ClearAll[A, r];
Simplify[(1715 \[Pi] (-5 + 3 r) (1575 \[Pi] + 512 A (-9 + 5 r)))/(
1048576 (-9 + 5 r)),
TransformationFunctions -> {Automatic, FactorInteger}]


Here I have hard-coded the output Rule from Solve since I guess the actual equations to be solved are not relevant here. However, I'm open to reformulating what to feed into Solve (to have a different output format) if that's the proper approach.

Thank you.

P.S.

The small example above might not be entirely motivating to ask for factorization. Below is a more typical set of output of Solve that I'm trying to handle:

{Subscript[b, 2] -> (21 \[Pi] (15 \[Pi] +
8 A (-5 + r)) (410233359375 \[Pi]^4 (-3 + r) (-2 + r) (-5 +
3 r) + 2147483648 (-7 + 3 r) (-9 + 5 r) (-3377 + 1619 r) -
96768000 \[Pi]^2 (-1678430 +
r (2422271 +
r (-1143022 + 175845 r)))))/(35184372088832 (-5 +
r) (-7 + 3 r) (-9 + 5 r)),
Subscript[b, 3] -> -((7 (32 +
3 A \[Pi] (-3 + r)) (136744453125 \[Pi]^4 (-2 + r) (-5 +
3 r) + 274877906944 (-7 + 3 r) (-9 + 5 r) -
516096000 \[Pi]^2 (26035 +
r (-28132 + 7545 r))))/(2199023255552 (-7 + 3 r) (-9 +
5 r))),
Subscript[b, 4] -> (175 \[Pi] (315 \[Pi] +
128 A (-7 + 3 r)) (17364375 \[Pi]^2 (-2 + r) (-5 + 3 r) -
262144 (-9 + 5 r) (-485 + 267 r)))/(137438953472 (-7 +
3 r) (-9 + 5 r)),
Subscript[b, 5] -> -((7 (512 +
75 A \[Pi] (-2 + r)) (1157625 \[Pi]^2 (-5 + 3 r) -
2097152 (-9 + 5 r)))/(536870912 (-9 + 5 r)))}


I indeed have the analytic functional form of the set of equations that lead to these solutions, but I don't know how to solve them myself algebraically (symbolically) so I employ Solve. These solutions will be used for further combinations and plotting etc. Therefore practically it would be better if I know what these integers are made of, and theoretically it is necessary if I wish to do some analysis.

Mathematica is an "infinite evaluation system", which means that it desperately wants to multiply out any products or powers that it can and almost anything you do to show a factorization will immediately be multiplied back together. Thus we have to find a way to guard your factored forms.

Strictly for display purposes, perhaps something like this will do what you are hoping for:

superfactorf[u_]:=Times@@(FactorInteger[u]/.{
{v_Integer,1}:>v,{v_Integer,w_Integer}:>Superscript[v,w]});
superfactor[expr_]:= expr/.{x_Integer:>superfactorf[x],
x_Rational:>superfactorf[Numerator[x]]/superfactorf[Denominator[x]]};


Then

superfactor[(1715 \[Pi](-5+3 r)(1575 \[Pi]+512 A(-9+5 r)))/(1048576 (-9+5 r))]


will be transformed and displayed as

$$(5 \pi (-5 + 3 r) (A 2^9 (5 r - 3^2) + 7 \pi 3^2 5^2) 7^3)/(2^{20} (5 r - 3^2))$$

If this doesn't do exactly what you are looking for then perhaps you can study this code and see if you can modify it to more closely suit your desires.

Use this with great caution. Test it and check the results each time until you discover any errors that might be lurking in this.

You should realize that once you have turned something into a superscript it then cannot be easily used in any further calculations.

Edit: I discovered my first flaw. The example you showed didn't happen to include a number which exposed this and I didn't do sufficient testing to see this before I posted.

Consider superfactor[60]. I assumed you didn't want to see that as$$2^2 3^1 5^1$$ Thus I eliminated exponents of 1. I assumed you didn't want to see that as$$\{2^2,3,5\}$$ so I multiplied the items in the list and instantly the infinite evaluation saw two integers and multiplied them giving $$15*2^2$$This could be corrected by not removing exponents of 1 or by changing {v_Integer,1}:>v to {v_Integer,1}:>Superscript[v," "], but that causes problems with negative constants, or by doing something other than simple multiplication on the list of factors.

It seems that any time you try to fight some deeply held principle within the design of Mathematica you then discover challenges that you have to try to overcome.

• Thank you very much for the careful considerations! Yeah there are some technicalities about dealing with exponents of 1, which I didn't even start to consider. I need some time to understand your approach to see how it works. As your reminder stated, I actually indeed will use the factored-simplified results for more calculations. – Lee David Chung Lin Mar 21 '17 at 23:36
• @LeeDavidChungLin If you need to use a result for further calculations then you will probably find it easier to calculate a result, use that to create a display you prefer, then use the original unmodified result to calculate your next result, etc. Many new users assume Mathematica can use display formatted results for further calculations and that is usually much more difficult or nearly impossible to do. – Bill Mar 22 '17 at 4:55

You could create a *Form wrapper that prints integers in factored form. Here is an implementation:

factoredForm /: MakeBoxes[factoredForm[expr_], fmt_] :=
InternalInheritedBlock[{MakeBoxes},
MakeBoxes[a_Integer, fmt] := With[
{factorForm = factoredInteger[factors @@@ FactorInteger[a]]},
With[{boxes = MakeBoxes[factorForm, fmt]},
InterpretationBox[boxes, a, SyntaxForm->"^"]
]
];
MakeBoxes[expr, fmt]
]
factoredInteger[{factors[-1,1],factors[a_,b_],c___}]:=factoredInteger[{factors[-a,b],c}]
factoredInteger[{f_factors}]:=f

factoredInteger /: MakeBoxes[factoredInteger[a_], fmt_] := RowBox @ BoxFormMakeInfixForm[a, "\[CenterDot]", fmt]

factors /: MakeBoxes[factors[a_, 1], fmt_:StandardForm] := ToString[a]
factors /: MakeBoxes[factors[a_, n_], fmt_:StandardForm] := SuperscriptBox[ToString[a], ToString[n]]


For the OP's first example:

factoredForm[
(1715 \[Pi](-5+3 r)(1575 \[Pi]-12 A(-9+5 r)))/(1048576 (-9+5 r))
] //HoldForm//TeXForm


$\frac{5\cdot 7^3 \pi (-5+3 r) \left(3^2\cdot 5^2\cdot 7 \pi -2^2\cdot 3 A \left(-3^2+5 r\right)\right)}{2^{20} \left(-3^2+5 r\right)}$

And the OP's second example:

factoredForm[
{Subscript[b, 2] -> (21 \[Pi] (15 \[Pi] +
8 A (-5 + r)) (410233359375 \[Pi]^4 (-3 + r) (-2 + r) (-5 +
3 r) + 2147483648 (-7 + 3 r) (-9 + 5 r) (-3377 + 1619 r) -
96768000 \[Pi]^2 (-1678430 +
r (2422271 +
r (-1143022 + 175845 r)))))/(35184372088832 (-5 +
r) (-7 + 3 r) (-9 + 5 r)),
Subscript[b, 3] -> -((7 (32 +
3 A \[Pi] (-3 + r)) (136744453125 \[Pi]^4 (-2 + r) (-5 +
3 r) + 274877906944 (-7 + 3 r) (-9 + 5 r) -
516096000 \[Pi]^2 (26035 +
r (-28132 + 7545 r))))/(2199023255552 (-7 + 3 r) (-9 +
5 r))),
Subscript[b, 4] -> (175 \[Pi] (315 \[Pi] +
128 A (-7 + 3 r)) (17364375 \[Pi]^2 (-2 + r) (-5 + 3 r) -
262144 (-9 + 5 r) (-485 + 267 r)))/(137438953472 (-7 +
3 r) (-9 + 5 r)),
Subscript[b, 5] -> -((7 (512 +
75 A \[Pi] (-2 + r)) (1157625 \[Pi]^2 (-5 + 3 r) -
2097152 (-9 + 5 r)))/(536870912 (-9 + 5 r)))}
] //HoldForm //TeXForm


$\left\{b_2\to \frac{3\cdot 7 \pi \left(3\cdot 5 \pi +2^3 A (-5+r)\right) \left(3^7\cdot 5^7\cdot 7^4 \pi ^{2^2} (-3+r) (-2+r) (-5+3 r)+2^{31} (-7+3 r) \left(-3^2+5 r\right) (-11\cdot 307+1619 r)-2^{12}\cdot 3^3\cdot 5^3\cdot 7 \pi ^2 (-2\cdot 5\cdot 13\cdot 12911+r (127\cdot 19073+r (-2\cdot 751\cdot 761+3\cdot 5\cdot 19\cdot 617 r)))\right)}{2^{45} (-5+r) (-7+3 r) \left(-3^2+5 r\right)},b_3\to -\frac{7 \left(2^5+3 A \pi (-3+r)\right) \left(3^6\cdot 5^7\cdot 7^4 \pi ^{2^2} (-2+r) (-5+3 r)+2^{38} (-7+3 r) \left(-3^2+5 r\right)-2^{16}\cdot 3^2\cdot 5^3\cdot 7 \pi ^2 \left(5\cdot 41\cdot 127+r \left(-2^2\cdot 13\cdot 541+3\cdot 5\cdot 503 r\right)\right)\right)}{2^{41} (-7+3 r) \left(-3^2+5 r\right)},b_{2^2}\to \frac{5^2\cdot 7 \pi \left(3^2\cdot 5\cdot 7 \pi +2^7 A (-7+3 r)\right) \left(3^4\cdot 5^4\cdot 7^3 \pi ^2 (-2+r) (-5+3 r)-2^{18} \left(-3^2+5 r\right) (-5\cdot 97+3\cdot 89 r)\right)}{2^{37} (-7+3 r) \left(-3^2+5 r\right)},b_5\to -\frac{7 \left(2^9+3\cdot 5^2 A \pi (-2+r)\right) \left(3^3\cdot 5^3\cdot 7^3 \pi ^2 (-5+3 r)-2^{21} \left(-3^2+5 r\right)\right)}{2^{29} \left(-3^2+5 r\right)}\right\}$