Message Boards Message Boards

0
|
2314 Views
|
10 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Format polynomials the way I want?

Posted 6 years ago

Hello! I would really appreciate any help with this as I have tried looking for an answer for more than 2 weeks. So this is my code

    K[Q_, n_Integer] := 
     Module[{z, x}, 
      SymmetricReduction[
        SeriesCoefficient[
         Product[ComposeSeries[Series[Q[z], {z, 0, n}], 
           Series[x[i] z, {z, 0, n}]], {i, 1, n}], n], 
        Table[x[i], {i, 1, n}], Table[Subscript[c, i], {i, 1, n}]][[1]]]

primeFactorForm[n_] := 
  If[Length@# == 1, First@#, CenterDot @@ #] &[
   Superscript @@@ FactorInteger[n]];

string = StringJoin[
  Riffle[Table[poly = K[Sqrt[#]/Tanh[Sqrt[#]] &, i] /. c -> p; 
    gcd = GCD @@ List @@ poly /. Rational[n_, d_]*c_ :> d;
    ToString[
     Inactive[Set][Subscript[L, i], 
      1/primeFactorForm[gcd]*Plus @@ List @@ Distribute[gcd*poly] /. 
       Times[Rational[n_, d_], e__] :> 
        primeFactorForm[n]/primeFactorForm[d]*e], TeXForm], {i, 0, 
     7}], "\\\\"]]
CopyToClipboard[string]

And this is the output (I have to do this many times with mad=ny different polynomials but the output is similar in all the cases):

$L_0=\frac{1}{1^1}\\L_1=\frac{\gcd \left(\frac{1}{3},p_1\right)+p_1+\frac{1}{3}}{\frac{1}{3}^{p_1}}\\L_2=\frac{7 p_2-p_1^2}{3^2\cdot 5^1}\\L_3=\frac{2 p_1^3-13 p_2 p_1+62 p_3}{3^3\cdot 5^1\cdot 7^1}\\L_4=\frac{\frac{p_2 p_1^2 2^1\cdot 11^1}{3^1}+\frac{p_3 p_1 -1^1\cdot 71^1}{3^1}+\frac{p_2^2 -1^1\cdot 19^1}{3^1}-p_1^4+127 p_4}{3^3\cdot 5^2\cdot 7^1}\\L_5=\frac{\frac{p_1^5 2^1}{3^1\cdot 7^1}+\frac{p_2 p_1^3 -1^1\cdot 83^1}{3^1\cdot 5^1\cdot 7^1}+\frac{p_3 p_1^2 79^1}{5^1\cdot 7^1}+\frac{p_4 p_1 -1^1\cdot 919^1}{3^1\cdot 5^1\cdot 7^1}+\frac{p_2^2 p_1 127^1}{3^1\cdot 5^1\cdot 7^1}+\frac{p_5 2^1\cdot 73^1}{3^1}+\frac{p_2 p_3 -1^1\cdot 2^4}{5^1}}{3^4\cdot 5^1\cdot 11^1}\\L_6=\frac{\frac{p_4 p_1^2 40841^1}{5^1}+\frac{p_1^6 -1^1\cdot 2^1\cdot 691^1}{3^1\cdot 5^1}+\frac{p_2 p_1^4 2^1\cdot 6421^1}{3^1\cdot 5^1}+\frac{p_3 p_1^3 -1^1\cdot 33863^1}{3^1\cdot 5^1}+\frac{p_2^2 p_1^2 -1^1\cdot 5527^1}{3^1}+\frac{p_2 p_3 p_1 83^1\cdot 349^1}{5^1}+\frac{p_5 p_1 -1^1\cdot 2^5\cdot 29^1\cdot 181^1}{5^1}+\frac{p_3^2 -1^1\cdot 167^1\cdot 241^1}{3^1\cdot 5^1}+\frac{p_2 p_4 -1^1\cdot 159287^1}{3^1\cdot 5^1}+\frac{p_6 2^1\cdot 23^1\cdot 89^1\cdot 691^1}{3^1\cdot 5^1}+\frac{p_2^3 2^1\cdot 1453^1}{5^1}}{3^5\cdot 5^2\cdot 7^2\cdot 11^1\cdot 13^1}\\L_7=\frac{\frac{p_1^7 2^2}{3^4\cdot 5^1\cdot 11^1}+\frac{p_2 p_1^5 -1^1\cdot 2^1\cdot 2161^1}{3^5\cdot 5^2\cdot 7^1\cdot 11^1}+\frac{p_3 p_1^4 2^2}{5^2\cdot 7^1}+\frac{p_4 p_1^3 -1^1\cdot 2^1\cdot 113^1}{3^4\cdot 5^1\cdot 7^1}+\frac{p_2^2 p_1^3 2^3}{3^2\cdot 5^1\cdot 7^1}+\frac{p_2 p_3 p_1^2 -1^1\cdot 39341^1}{3^5\cdot 5^2\cdot 7^1\cdot 11^1}+\frac{p_5 p_1^2 2^4\cdot 277^1}{3^4\cdot 5^2\cdot 7^1}+\frac{p_2^3 p_1 -1^1\cdot 2^1\cdot 3989^1}{3^5\cdot 5^2\cdot 7^1\cdot 11^1}+\frac{p_6 p_1 -1^1\cdot 2^1\cdot 305633^1}{3^5\cdot 5^2\cdot 7^1\cdot 11^1}+\frac{p_2 p_4 p_1 1399^1}{3^3\cdot 5^2\cdot 11^1}+\frac{p_3^2 p_1 22027^1}{3^5\cdot 5^2\cdot 7^1\cdot 11^1}+\frac{p_2^2 p_3 2^3\cdot 2087^1}{3^5\cdot 5^2\cdot 7^1\cdot 11^1}+\frac{p_3 p_4 -1^1\cdot 2^1\cdot 97^1\cdot 107^1}{3^4\cdot 5^2\cdot 7^1\cdot 11^1}+\frac{p_2 p_5 -1^1\cdot 2^1\cdot 23^2}{3^5\cdot 11^1}+\frac{p_7 2^2\cdot 8191^1}{3^4\cdot 5^1\cdot 11^1}}{3^2\cdot 5^1\cdot 7^1\cdot 13^1}$

Now here are the things I was struggling with. I need to use these results in a paper so I was requested to have a certain format. I need the variables out of the fraction and I need the common denominator to be in front of everything, instead of having a fraction over a fraction i.e. instead of let's say $\frac{\frac{7p_ 2}{2} -p_ 1^2}{3^2\cdot 5^1}$ I need $\frac{1}{3^2\cdot 5^1}(\frac{7}{2} p_ 2-p_ 1^2)$. Also, in the example I gave $L_1$ is weird and this happens sometimes in other polynomials. Why is this? (but this is less important). So, can anyone tell me how to fix this formating problem as I tried so so many functions and stuff, but nothing works, and doing it by hand in Latex, not only it is time consuming, but it is seriously prone to error... Thank you!

POSTED BY: Silviu Udrescu
10 Replies

When i=1 the poly in your code is a monomial, and the following code for gcd seems to be designed only for a poly that is a sum of monomials.

POSTED BY: Gianluca Gorni

This may be closer to what you want:

1/primeFactorForm[gcd]*Plus @@ List @@ Distribute[gcd*poly] /. 
  Times[Rational[n_, d_], e__] :> 
   RowBox[{primeFactorForm[n]/primeFactorForm[d], e}] /. 
 x_ :> TraditionalForm@
   DisplayForm@RowBox[{1/Denominator[x], "(", Numerator[x], ")"}]
POSTED BY: Gianluca Gorni
Posted 6 years ago

Great! Thank you so so much! Not as important, but do you have any idea why $L_1$ in the stuff I posted looks weird?

POSTED BY: Silviu Udrescu

I get correct LaTeX output this way:

ToString[1/primeFactorForm[gcd]*
    Plus @@ List @@ Distribute[gcd*poly] /. 
   Times[Rational[n_, d_], e__] :> 
    primeFactorForm[n]/primeFactorForm[d]*e /. 
  x_ :> TraditionalForm@
    DisplayForm@
     RowBox[{1/Denominator[x], "(", Numerator[x], ")"}], TeXForm]
POSTED BY: Gianluca Gorni
Posted 6 years ago

Could you please post the whole code that made it work and also the output? For some reason it still doesn't work for me... This is what I get for one of the outputs using that piece of code:

$\frac{1}{3^2\cdot 5^1\cdot 7^1\cdot 13^1}(\frac{2^2 p_1^7}{3^4\cdot 5^1\cdot 11^1}+\frac{-1^1\cdot 2^1\cdot 2161^1 p_2 p_1^5}{3^5\cdot 5^2\cdot 7^1\cdot 11^1}+\frac{p_3 2^2 p_1^4}{5^2\cdot 7^1}+\frac{-1^1\cdot 2^1\cdot 113^1 p_4 p_1^3}{3^4\cdot 5^1\cdot 7^1}+\frac{p_2^2 2^3 p_1^3}{3^2\cdot 5^1\cdot 7^1}+\frac{-1^1\cdot 39341^1 p_2 p_3 p_1^2}{3^5\cdot 5^2\cdot 7^1\cdot 11^1}+\frac{2^4\cdot 277^1 p_5 p_1^2}{3^4\cdot 5^2\cdot 7^1}+\frac{-1^1\cdot 2^1\cdot 3989^1 p_2^3 p_1}{3^5\cdot 5^2\cdot 7^1\cdot 11^1}+\frac{-1^1\cdot 2^1\cdot 305633^1 p_6 p_1}{3^5\cdot 5^2\cdot 7^1\cdot 11^1}+\frac{p_2 p_4 1399^1 p_1}{3^3\cdot 5^2\cdot 11^1}+\frac{p_3^2 22027^1 p_1}{3^5\cdot 5^2\cdot 7^1\cdot 11^1}+\frac{2^3\cdot 2087^1 p_2^2 p_3}{3^5\cdot 5^2\cdot 7^1\cdot 11^1}+\frac{-1^1\cdot 2^1\cdot 97^1\cdot 107^1 p_3 p_4}{3^4\cdot 5^2\cdot 7^1\cdot 11^1}+\frac{-1^1\cdot 2^1\cdot 23^2 p_2 p_5}{3^5\cdot 11^1}+\frac{2^2\cdot 8191^1 p_7}{3^4\cdot 5^1\cdot 11^1}$

The variables are still not out of the fraction, the way I need... (i.e. I need $\frac{2^2 }{3^4\cdot 5^1\cdot 11^1}p_1^7$ for the first term) Thank you!

POSTED BY: Silviu Udrescu

Thank you for you reply. Where exactly should I add that to my code?

POSTED BY: Gianluca Gorni

You may try a replacement rule:

(a/b + c/d)/e /. 
 x_ :> TraditionalForm@DisplayForm@RowBox[{1/Denominator[x], "(", Numerator[x], ")"}]
POSTED BY: Gianluca Gorni
Posted 6 years ago

Thank you for you reply. Where exactly should I add that to my code?

POSTED BY: Silviu Udrescu

I would try the replacement rule on the right-hand sides of your equations:

1/primeFactorForm[gcd]*Plus @@ List @@ Distribute[gcd*poly] /. 
  Times[Rational[n_, d_], e__] :> 
   primeFactorForm[n]/primeFactorForm[d]*e /. 
 x_ :> TraditionalForm@
   DisplayForm@RowBox[{1/Denominator[x], "(", Numerator[x], ")"}]
POSTED BY: Gianluca Gorni
Posted 6 years ago

It doesn't really work... this is the output:

1\left(L0=\frac{\gcd \left(0,\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&\right)+K\left[\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&,0\right]}{0^{\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&}}\right)\1\left(L1=\frac{\gcd \left(1,\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&\right)+K\left[\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&,1\right]}{1^{\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&}}\right)\1\left(L2=\frac{\gcd \left(2,\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&\right)+K\left[\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&,2\right]}{2^{\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&}}\right)\1\left(L3=\frac{\gcd \left(3,\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&\right)+K\left[\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&,3\right]}{3^{\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&}}\right)\1\left(L4=\frac{\gcd \left(4,\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&\right)+K\left[\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&,4\right]}{4^{\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&}}\right)\1\left(L5=\frac{\gcd \left(5,\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&\right)+K\left[\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&,5\right]}{5^{\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&}}\right)\1\left(L6=\frac{\gcd \left(6,\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&\right)+K\left[\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&,6\right]}{6^{\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&}}\right)\1\left(L7=\frac{\gcd \left(7,\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&\right)+K\left[\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&,7\right]}{7^{\frac{\sqrt{\text{ $\#$1}}}{\tanh \left(\sqrt{\text{ $\#$1}}\right)}\&}}\right)

POSTED BY: Silviu Udrescu
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract