3
$\begingroup$

I found a problem that I cannot solve. When I copied a recursion created in MAPLE into a Mathematica Notebook, I found the form of the function in MAPLE is different than Mathematica.

The difference is just the brackets. In MAPLE, the function brackets are "()", while in Mathematica, they are "[]". Now, in my recursion formula, there are more than 300 functions with "()", such as $f(n)$, $f(n+1)$, $f(n+2)$, etc. I want to replace them to $f[n]$, $f[n+1]$, $f[n+2]$. Now the only way I know is manually replacing them one by one, which bothers me very much. How can I deal with this efficiently?

For example,

(n^3 - 10*n^2 + 24*n)*u (n) + (3*n^3 - 17*n^2 + 15*n)*
  u (n + 1) + (-14*n^3 - 2*n^2 + n + 64)*
  u (n + 2) + (-15*n^3 - 43*n^2 - 54*n - 56)*
  u (n + 3) + (-15*n^3 - 184*n^2 - 539*n - 216)*
  u (n + 4) + (-36*n^3 - 432*n^2 - 1558*n - 1624)*
  u (n + 5) + (15*n^3 + 176*n^2 + 475*n)*
  u (n + 6) + (-15*n^3 - 317*n^2 - 2246*n - 5304)*
  u (n + 7) + (14*n^3 + 334*n^2 + 2655*n + 7096)*
  u (n + 8) + (3*n^3 + 89*n^2 + 863*n + 2744)*
  u (n + 9) + (-n^3 - 34*n^2 - 376*n - 1344)*u (n + 10),

replacing $u(n+k)$ with $u[n+k]$.

$\endgroup$
2
  • $\begingroup$ Yes, notebook.@Nasser $\endgroup$
    – zyynankai
    Oct 13, 2023 at 1:54
  • $\begingroup$ Isn't there some import or export functionality in either program? $\endgroup$ Oct 14, 2023 at 15:19

4 Answers 4

13
$\begingroup$

But one way to do this (for a self-contained Maple expression) is to use LaTeX.

This will work for any Maple expression. Well, almost any. It depends on the LaTeX source generated by Maple of course, but that has gotten much better in Maple 2023.

Here is using your example you just posted:

expr:=(n^3 - 10*n^2 + 24*n)*u (n) + (3*n^3 - 17*n^2 + 15*n)*
  u (n + 1) + (-14*n^3 - 2*n^2 + n + 64)*
  u (n + 2) + (-15*n^3 - 43*n^2 - 54*n - 56)*
  u (n + 3) + (-15*n^3 - 184*n^2 - 539*n - 216)*
  u (n + 4) + (-36*n^3 - 432*n^2 - 1558*n - 1624)*
  u (n + 5) + (15*n^3 + 176*n^2 + 475*n)*
  u (n + 6) + (-15*n^3 - 317*n^2 - 2246*n - 5304)*
  u (n + 7) + (14*n^3 + 334*n^2 + 2655*n + 7096)*
  u (n + 8) + (3*n^3 + 89*n^2 + 863*n + 2744)*
  u (n + 9) + (-n^3 - 34*n^2 - 376*n - 1344)*u (n + 10);

 lprint(latex(expr,output=string));

Gives

"\\left(n^{3}-10 n^{2}+24 n \\right) u \\! \\left(n \\right)+\\left(3 n^{3}-17 \
n^{2}+15 n \\right) u \\! \\left(n +1\\right)+\\left(-14 n^{3}-2 n^{2}+n +64\\r\
ight) u \\! \\left(n +2\\right)+\\left(-15 n^{3}-43 n^{2}-54 n -56\\right) u \\\
! \\left(n +3\\right)+\\left(-15 n^{3}-184 n^{2}-539 n -216\\right) u \\! \\lef\
t(n +4\\right)+\\left(-36 n^{3}-432 n^{2}-1558 n -1624\\right) u \\! \\left(n +\
5\\right)+\\left(15 n^{3}+176 n^{2}+475 n \\right) u \\! \\left(n +6\\right)+\\\
left(-15 n^{3}-317 n^{2}-2246 n -5304\\right) u \\! \\left(n +7\\right)+\\left(\
14 n^{3}+334 n^{2}+2655 n +7096\\right) u \\! \\left(n +8\\right)+\\left(3 n^{3\
}+89 n^{2}+863 n +2744\\right) u \\! \\left(n +9\\right)+\\left(-n^{3}-34 n^{2}\
-376 n -1344\\right) u \\! \\left(n +10\\right)"

Copy the output to Mathematica and do

 out = (*the above string pasted here *)
 ToExpression[out, TeXForm]

gives

Mathematica graphics

15 u[1]+u[2]+49 u[3]-168 u[4]-954 u[5]-3650 u[6]+666 u[7]-7882 u[8]+10099 u[9]+3699 u[10]-1755 u[11]

Notice that Mathematica simplified the expression.

If you do not want that, then do

  ToExpression[out, TeXForm, HoldForm]

Mathematica graphics

(n^3-10 n^2+24 n) u[n]+(3 n^3-17 n^2+15 n) u[n+1]+(-14 n^3-2 n^2+n+64) u[n+2]+(-15 n^3-43 n^2-54 n-56) u[n+3]+(-15 n^3-184 n^2-539 n-216) u[n+4]+(-36 n^3-432 n^2-1558 n-1624) u[n+5]+(15 n^3+176 n^2+475 n) u[n+6]+(-15 n^3-317 n^2-2246 n-5304) u[n+7]+(14 n^3+334 n^2+2655 n+7096) u[n+8]+(3 n^3+89 n^2+863 n+2744) u[n+9]+(-n^3-34 n^2-376 n-1344) u[n+10]
$\endgroup$
2
  • $\begingroup$ It WORKS!!! Thanks very much!! $\endgroup$
    – zyynankai
    Oct 13, 2023 at 2:45
  • $\begingroup$ This is probably best. An alternative is to paste into a string and then use string replacements followed by ToExpression. But this can be tricky if there are also parens you want to keep. $\endgroup$ Oct 13, 2023 at 22:28
2
$\begingroup$

Stealing from https://stackoverflow.com/questions/12752225/how-do-i-find-the-position-of-matching-parentheses-or-braces-in-a-given-piece-of the following assumes that if a left parenthesis is preceded by something other than a +, -, *, /, ^, (, ), [, or ] implies that it and the associated right parenthesis should be changed to square brackets (after removing spaces and other annoying characters).

mstyle[f_] := Module[{pLeft, pRight, c, pos, prevCharacter, s, s1},
  
(* Remove spaces, newlines, and semicolons *)
s = StringReplace[f, " " -> ""];
s = StringReplace[s, "\n" -> ""];
s = StringReplace[s, ";" -> ""];
  
  (* Find positions of each "(" *)
  pLeft = #[[1]] & /@ StringPosition[s, "("];
  
  (* Find corresponding ")" *)
  pRight = ConstantArray[0, Length[pLeft]];
  Do[c = 1;
   pos = pLeft[[i]];
   While[c > 0,
    pos = pos + 1;
    character = StringTake[s, {pos}];
    If[character == "(", c = c + 1,
     If[character == ")", c = c - 1]]
    ];
   pRight[[i]] = pos,
   {i, Length[pLeft]}];
  
  (* Convert () pairs to [] when the character immediately preceeding "(" is not 
     +, -, *, /, ^, ), (, [, or ] *)
  Do[If[pLeft[[i]] > 1,
    prevCharacter = StringTake[s, {pLeft[[i]] - 1}];
    s1 =.;
    If[! MemberQ[{"+", "-", "*", "/", "^", ")", "(", "[", "]"}, prevCharacter],
     s1 = StringReplacePart[s, "[", {pLeft[[i]], pLeft[[i]]}];
     s =.;
     s = StringReplacePart[s1, "]", {pRight[[i]], pRight[[i]]}]];
    ], {i, Length[pLeft]}];
  ToExpression[s]
  ]

For example:

f = "(n^3 - 10*n^2 + 24*n)*u (n) + (3*n^3 - 17*n^2 + 15*n)*
  u (n + 1) + (-14*n^3 - 2*n^2 + n + 64)*
  u (n + 2) + (-15*n^3 - 43*n^2 - 54*n - 56)*
  u (n + 3) + (-15*n^3 - 184*n^2 - 539*n - 216)*
  u (n + 4) + (-36*n^3 - 432*n^2 - 1558*n - 1624)*
  u (n + 5) + (15*n^3 + 176*n^2 + 475*n)*
  u (n + 6) + (-15*n^3 - 317*n^2 - 2246*n - 5304)*
  u (n + 7) + (14*n^3 + 334*n^2 + 2655*n + 7096)*
  u (n + 8) + (3*n^3 + 89*n^2 + 863*n + 2744)*
  u (n + 9) + (-n^3 - 34*n^2 - 376*n - 1344)*u (n + 10);";

mstyle[f]
(* (24 n - 10 n^2 + n^3) u[n] + (15 n - 17 n^2 + 3 n^3) u[1 + n] +
   (64 + n - 2 n^2 - 14 n^3) u[2 + n] + (-56 - 54 n - 43 n^2 - 15 n^3) u[3 + n] + 
   (-216 - 539 n - 184 n^2 - 15 n^3) u[4 + n] + (-1624 - 1558 n - 432 n^2 - 36 n^3) u[5 + n] + 
   (475 n + 176 n^2 + 15 n^3) u[6 + n] + (-5304 - 2246 n - 317 n^2 - 15 n^3) u[7 + n] + 
   (7096 + 2655 n + 334 n^2 + 14 n^3) u[8 + n] + (2744 + 863 n + 89 n^2 + 3 n^3) u[9 + n] +
   (-1344 - 376 n - 34 n^2 - n^3) u[10 + n] *)

What about more nested parentheses?

mstyle["a+b(x+((2+z)^2+7))"]
(* a + b[7 + x + (2 + z)^2] *)
$\endgroup$
1
$\begingroup$

The fastest is to replace sin( with Sin@( or whatever functions you might have with Replace All:

find, replace 1

If you then evaluate that the output will be with Sin[...] instead of sin(...)

find, replace 2

$\endgroup$
2
  • $\begingroup$ That presumes it is consistently formatted, always exactly one space between the function name and the opening parenthesis. $\endgroup$ Oct 14, 2023 at 15:17
  • $\begingroup$ @Peter Mortensen - if it's not consistently formatted you have to replace space+( with only ( first, and if you have more than one space in front of some parenthesis run the replace twice or thrice, Mathematica doesn't need the space in front of parenthesis multiplications anyway. $\endgroup$
    – Yukterez
    Oct 14, 2023 at 18:34
-2
$\begingroup$

If it's not too much trouble, take all the code, paste it into Microsoft Word and tell it to change round parentheses into squares, and paste it back into Mathematica.

$\endgroup$
3
  • 1
    $\begingroup$ that only works if all the parentheses are for functions, but you could also have Sin[(a+b)/c] or something like that, then your indiscriminate replacement would ruin the brackets around a+b. You also don't need an extra Word for that since Mathematica has a Find and Replace window as well. $\endgroup$
    – Yukterez
    Oct 14, 2023 at 3:01
  • $\begingroup$ Re "tell it to change": That ought to be more operational. Exactly how? $\endgroup$ Oct 14, 2023 at 15:07
  • $\begingroup$ @Peter Mortensen - since he uses Microsoft Word for that this would be offtopic anyway, but in my answer on which you already commented I can show you how to do it in Mathematica. $\endgroup$
    – Yukterez
    Oct 14, 2023 at 18:39

Your Answer

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

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