I use mathematica compute functions I need for some external coding. However I can't use terms like x^2+y^3 since my programming language needs this term written as x*x+y*y*y. I would love to copy and paste the results from mathematica so how do I convert these terms?
Asked
Active
Viewed 1,127 times
4
2 Answers
8
If you want it in string form you can do it like this
x^3 /. Power -> (StringRiffle[#, "*"] &@ConstantArray[#1, #2] &)
"x * x * x"
This makes use of the fact that x^3 is internally written as Power[x,3].
AccidentalTaylorExpansion
- 1,267
- 6
- 10
-
1
-
8
x^3 /. Power[a_, n_] :> Inactive[Times] @@ ConstantArray[a, n]is another way to do it. This makes it easy to convert back as well withActivate. – Sjoerd Smit Jan 14 '21 at 14:41 -
2
StringReplace["x^2+y^3", a : LetterCharacter ~~ "^" ~~ n : DigitCharacter :> StringRiffle[ConstantArray[a, FromDigits[n]], "*"]]? – J. M.'s missing motivation Jan 14 '21 at 11:28ToString@InputForm[expr]to get a string with powers written in^notation – Hausdorff Jan 14 '21 at 11:40