# Transforming a mutiplication into an addition

How can I force Mathematica to transform

5a


into

a+a+a+a+a

-
Mathematica will automatically simplify a+a+a to 3 a. Unless one uses Hold or use String to write the result which makes it not useful. What is the point of doing this? What are you trying to do with the result you are after? – Nasser Apr 27 '14 at 10:41
Thank you. You assumed correctly that there is no real senseful use; this question just emerged as I was playing around a little bit with the commands of mathematica yesterday :) – Gab Apr 27 '14 at 10:49

## 3 Answers

If it is just for fun:

Thanks for the bug report by Kuba, made it support negative sums

expr = +5 a;
r = "";
expr /. Times[n_, m_] :>
If[Abs[m] == 1, expr, Last@Table[r = r <> If[m < 0, "-", {}] <> ToString@n
<> If[i < Abs@m, If[m > 0, "+", {}], {}], {i, Abs@m}]]

(*a+a+a+a+a*)

expr = -a;
(* -a *)

expr = -5 a;
(* -a-a-a-a-a *)


And I am sure there are many other ways to do this and in shorter way. But the result is a string.

-
thank you, I have to admit, this is a nice workaround :D – Gab Apr 27 '14 at 11:02
Shorter: 5 a /. n_ x_ :> x~ConstantArray~n~Row~"+" – Simon Woods Apr 27 '14 at 11:38
@SimonWoods I am an ex-Fortran programmer :) (nice solution btw!) – Nasser Apr 27 '14 at 11:42

I propose using HoldForm rather than strings etc.

{a, -a, 3 a, -3 a} /.
n_Integer s_ /; Abs[n] > 1 :> Plus @@@ HoldForm @@ {Sign[n] Table[s, {Abs @ n}]}


Output:

Not only does this format exactly like manual entry of this expression, you can release it:

% // ReleaseHold

{a, -a, 3 a, -3 a}


If you want it to be automatically released when you copy and paste or edit the output (converting it to input), use Defer instead of HoldForm.

-
My answer has uses HoldForm, too, and can be released. – m_goldberg Apr 27 '14 at 20:46
@m_goldberg Sorry, I lost that in all the Riffling with Strings. :-/ However, my method will work with other expressions, e.g. 3 a^2, because I am not converting back and forth between strings. Doesn't the string conversion somewhat miss the point of HoldForm? (I genuinely don't mean to be a jerk. It would never occur to me to write it the way you did, which is why I stopped reading when I saw the first Riffle line.) – Mr.Wizard Apr 27 '14 at 20:55
I was, as I said, trying to do it the way an ex-Fortran programmer would do it. And, of course, there is no doubt at all the your answer is much better. But not as funny. – m_goldberg Apr 27 '14 at 22:22
@m_goldberg Thanks for having a sense of humor. Also, this wouldn't be the first time if my code haiku is unintelligible to the OP and he prefers the procedural approach. :-) – Mr.Wizard Apr 27 '14 at 22:55

Here's another take on this question; perhaps even a better example of the ex-Fortran programmer style than Nasser's answer.

timesExpand[x_Symbol] := x
timesExpand[expr : -1 x_Symbol] := expr
timesExpand[k_Integer x_Symbol] :=
Module[{name, chrs},
name = SymbolName[x];
If[k > 0,
chrs = Riffle[ConstantArray[name, k], ConstantArray["+", k - 1]],
chrs = Riffle[ConstantArray["-", -k], ConstantArray[name, -k]]];
ToExpression[StringJoin[chrs], StandardForm, HoldForm]]

timesExpand[5 a]
timesExpand[-5 a]
timesExpand[a]
timesExpand[-a]


-
nice. But for -5 a case, you need an extra - for that first a – Nasser Apr 27 '14 at 13:42
You're right. I'll fix it. – m_goldberg Apr 27 '14 at 13:43
@Nasser. Fixed. – m_goldberg Apr 27 '14 at 13:55
For what's it's worth this can be shortened with: chrs = Riffle[ConstantArray[name, Abs@k], ##] & @@ If[k > 0, {"+"}, {"-", {1, -2, 2}}] – Mr.Wizard Apr 28 '14 at 3:45
@Mr.Wizard. It'a a wonderful example of your wizardry, but it would spoil my joke to edit my answer to make use of it. It's too much in the Mathematica-wizard style. All hints of the ex-Fortran programmer authorship would be gone. But many thanks for posting it, it increased my understanding of Riffle considerably. – m_goldberg Apr 28 '14 at 4:16