1
$\begingroup$

I want to construct a function that plus equations. For example:

SomaEqns[eq1,eq2,...] == eq1 + eq2 + ...

It's possible do that with recursive way? Using less then possible the functions of Mathematica.

I know Mathematica already can do that. It's just for exercise.

$\endgroup$
  • $\begingroup$ perhaps you'll find Fold helpful for a functional approach that circumvents possible problems with recursion limits $\endgroup$ – user42582 Sep 18 '18 at 17:22
  • 1
    $\begingroup$ By "It's possible do that with recursive way? Using less then possible the functions of Mathematica.", I assume you are looking for a solution that uses recursion and does not use Thread, Fold, etc. One way to do this f[{eqn_}] := eqn f[eqn_List] := First[eqn] + f[Rest[eqn]] f[{eq1, eq2, eq3, eq4}] gives eq1 + eq2 + eq3 + eq4 $\endgroup$ – Rohit Namjoshi Sep 18 '18 at 17:42
  • $\begingroup$ SomaEqns = Plus? $\endgroup$ – AccidentalFourierTransform Sep 18 '18 at 17:45
  • $\begingroup$ Something like that @RohitNamjoshi. But in this case I don't want to write my equations as a List $\endgroup$ – Mateus Sep 18 '18 at 19:29
0
$\begingroup$

Here's a recursive way (like Rohit's comment but without the list):

f[first_, rest__] := first + f[rest]
f[last_] := last

f[1, x, x^2, x^3]

1 + x + x^2 + x^3

But note that Plus is exactly what you describe (as pointed out what AccidentalFourierTransform):

f[eqns__] := Plus[eqns]
f[1, x, x^2, x^3]

1 + x + x^2 + x^3

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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