0
$\begingroup$

From a following program (part of a longer program) I will get a recurrence in an elegant format.

 sold =
 {624, -1490, 1244, -430, 52, 0, 0, 0, 0, 0, 0, -6648, 13539, -9945, 
 3117, -351, 0, 0, 0, 0, 0, 0, 12384, -22716, 15216, -4416, 468, 0, 
 0, 0, 0, 0, 0, 3456, -8672, 6752, -2032, 208, 0, 0, 0, 0, 0, 0, 
 53760, -86912, 47552, -10624, 832, 0, 0, 0, 0, 0, 0};
 maxdenom = 624; maxdeg = 10; j = 4;
 strecur = ""; ikoef = 0;
 Do[sign = 0; dejmin = 0; numpol = Factor[Sum[ikoef++; 
  If[sold[[ikoef]] != 0, sign = Sign[sold[[ikoef]]]];
  sold[[ikoef]]*n^i, {i, 0, maxdeg}]];
  If[ord == 0, signan = sign; 
  If[signan < 0, numpol = -numpol; sign = 1;]];
  If[ord > 0 && signan > 0, If[sign == 1, dejmin = 1, 
  If[sign != 0, numpol = Factor[-numpol]; sign = 1];];];
  If[ord > 0 && signan < 0, 
  If[sign == -1, dejmin = 1; numpol = Factor[-numpol]; sign = 1];];
  strpol = ToString[TraditionalForm[numpol*a[n - ord]]];
  If[sign == 1, If[ord < 2, znam = "", znam = "+"], 
  If[ord > 1, znam = "+", znam = ""]];
  If[dejmin == 1, znam = "-"];
  If[sign != 0 && strpol != "0", strecur = strecur <> znam <> strpol];
  If[ord == 0, strecur = strecur <> "="];, {ord, 0, j}];
 Print[strecur]

enter image description here

How to replace spaces with * in the TraditionalForm output ? I can export this (manually) as "plain text"

 2 (n-1) (2 n-3) (13 n^2-75 n+104) a(n)=3 (117 n^4-1039 n^3+3315 n^2-4513 n+2216) a(n-1)-12 (39 n^4-368 n^3+1268 n^2-1893 n+1032) a(n-2)-16 (n-4) (13 n^3-75 n^2+122 n-54) a(n-3)-64 (n-5) (n-4) (13 n^2-49 n+42) a(n-4)

and replace all spaces with * manually in an external editor. But I want to do this in Mathematica and print it (with *) inside the program. I need a string

 "2*(n-1)*(2*n-3)*(13*n^2-75*n+104)*a(n)=3*(117*n^4-1039*n^3+3315*n^2-4513*n+2216)*a(n-1)-12*(39*n^4-368*n^3+1268*n^2-1893*n+1032)*a(n-2)-16*(n-4)*(13*n^3-75*n^2+122*n-54)*a(n-3)-64*(n-5)*(n-4)*(13*n^2-49*n+42)*a(n-4)"

After

 ToString[strecur]

I get an absolutelly unusable output with exponents on separate lines.

enter image description here

So, my question is: How to convert it from a traditional format to a string (without changing the order of terms) ? Then it's easy to do StringReplace["string", " " -> "*"]

$\endgroup$

3 Answers 3

1
$\begingroup$

Change the line starting with strpol to

strpol = ToString[numpol*a[n - ord] /. Times -> Inactive[Times], TraditionalForm]

and you should get output like the following (click on the image to enlarge):

output

$\endgroup$
2
  • $\begingroup$ Thank you for a good idea! Unfortunately we have in the output unwanted +-368 and +-1039 $\endgroup$ Mar 12, 2018 at 13:39
  • $\begingroup$ "+-" is in the variable strpol immediatelly after this statement. Maybe is a solution to replace "+-" -> "-" in the final string ?! I have to analyze whether this will always be right. $\endgroup$ Mar 12, 2018 at 13:52
0
$\begingroup$

I find a clear solution of my problem. After the conversion with ToString, the following program merges two lines with the exponents back to one line. Then is the final expression in the required format and it's possible to replace spaces with *.

 line1 = Characters[StringSplit[ToString[strecur], "\n"][[1]]];
 line2 = Characters[StringSplit[ToString[strecur], "\n"][[2]]];
 formula = ""; Do[If[k <= Length[line1], 
 If[line1[[k]] != " ", formula = formula <> "^" <> line1[[k]], 
 formula = formula <> line2[[k]];], 
 formula = formula <> line2[[k]];], {k, 1, Length[line2]}];
 formula = StringReplace[formula, "+ " -> "+"];
 formula = StringReplace[formula, " +" -> "+"];
 formula = StringReplace[formula, "- " -> "-"];
 formula = StringReplace[formula, " -" -> "-"];
 formula = StringReplace[formula, " " -> "*"]

 (* "2*(n-1)*(2*n-3)*(13*n^2-75*n+104)*a(n)=3*(117*n^4-1039*n^3+3315*n^2-4513*n+2216)*a(n-1)-12*(39*n^4-368*n^3+1268*n^2-1893*n+1032)*a(n-2)-16*(n-4)*(13*n^3-75*n^2+122*n-54)*a(n-3)-64*(n-5)*(n-4)*(13*n^2-49*n+42)*a(n-4)" *)
$\endgroup$
-1
$\begingroup$

The best way would be to convert strecur into a Mathematica expression

exrecur=ToExpression[StringReplace[strecur, "=" -> "=="]

You need to replace "=" with "==" because you don't want to make an assignment. Next just convert it back to string using the InputForm

strecurInput = ToString[exrecur,InputForm]

Similarly as above you can replace back "==" with "=" if you like to.

$\endgroup$
2
  • $\begingroup$ Your output is useless to me. The purpose of this program is to convert recurrence into a publishable format, in traditional format, with round brackets and normal operand order. InputForm changes the order. The polynomials are in the reverse order. $\endgroup$ Mar 13, 2018 at 7:51
  • $\begingroup$ Sorry but that wasn't so clear from the question. Have you tried to use TeXForm on the Mathematica output (my exrecur to be clear). It should yield polynomials in the right order. Then all you have to do is copy it in LaTeX and compile it. $\endgroup$
    – MannyC
    Mar 14, 2018 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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