7
$\begingroup$

How can I force Mathematica to transform

5a

into

a+a+a+a+a
$\endgroup$
2
  • 1
    $\begingroup$ 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? $\endgroup$
    – Nasser
    Apr 27, 2014 at 10:41
  • $\begingroup$ 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 :) $\endgroup$
    – Gab
    Apr 27, 2014 at 10:49

3 Answers 3

4
$\begingroup$

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.

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

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: enter image description here

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.

$\endgroup$
4
  • 1
    $\begingroup$ My answer has uses HoldForm, too, and can be released. $\endgroup$
    – m_goldberg
    Apr 27, 2014 at 20:46
  • $\begingroup$ @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.) $\endgroup$
    – Mr.Wizard
    Apr 27, 2014 at 20:55
  • $\begingroup$ 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. $\endgroup$
    – m_goldberg
    Apr 27, 2014 at 22:22
  • $\begingroup$ @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. :-) $\endgroup$
    – Mr.Wizard
    Apr 27, 2014 at 22:55
4
$\begingroup$

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]

output

$\endgroup$
5
  • $\begingroup$ nice. But for -5 a case, you need an extra - for that first a $\endgroup$
    – Nasser
    Apr 27, 2014 at 13:42
  • $\begingroup$ You're right. I'll fix it. $\endgroup$
    – m_goldberg
    Apr 27, 2014 at 13:43
  • 1
    $\begingroup$ @Nasser. Fixed. $\endgroup$
    – m_goldberg
    Apr 27, 2014 at 13:55
  • $\begingroup$ For what's it's worth this can be shortened with: chrs = Riffle[ConstantArray[name, Abs@k], ##] & @@ If[k > 0, {"+"}, {"-", {1, -2, 2}}] $\endgroup$
    – Mr.Wizard
    Apr 28, 2014 at 3:45
  • $\begingroup$ @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. $\endgroup$
    – m_goldberg
    Apr 28, 2014 at 4:16

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.