2
$\begingroup$

I have a more complicated case however a basic example is;

ci = {X1, X2, X3, X4};
g = ci[[1]] + ci[[2]] + ci[[3]] + ci[[4]];
f[t_] := g /. ci[[1]] -> ci[[1]][t] /. ci[[2]] -> ci[[2]][t] /. 
   ci[[3]] -> ci[[3]][t] /. ci[[4]] -> ci[[4]][t]

And I was wondering if there was a way to do this for the list without typing out each element. Everything that I have tried hasn't worked :(

Pls send help.

$\endgroup$
3
  • 2
    $\begingroup$ f[t_] := Tr@Through[ci[t]]?... $\endgroup$ – ciao Aug 12 '15 at 23:44
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Aug 13 '15 at 0:46
  • 1
    $\begingroup$ ...or f[t_] := Total@Through[ci[t]]. $\endgroup$ – David G. Stork Aug 13 '15 at 1:16
4
$\begingroup$

Here are a number of one-liners that will define f

ci = {X1, X2, X3, X4};

This first one is perhaps the easiest for_Mathematica_ newcomers to understand.

Clear[f, t];
f[t_] := Evaluate[Sum[ci[[i]][t], {i, Length[ci]}]];
Definition[f]

This one is pretty easy to understand too.

Clear[f, t];
f[t_] := Evaluate[Plus @@ Through[ci[t]]];
Definition[f]

Currently the conciseness champion.

Clear[f, t];
f[t_] := Evaluate[Tr[Through[ci[t]]]];
Definition[f]

Using Total may be overkill, but it certainly does the job.

Clear[f, t];
f[t_] := Evaluate[Total[Through[ci[t]]]];
Definition[f]

Using functional iteration may seem opaque to newcomers, but it is concise.

Clear[f, t];
f[t_] := Evaluate[Fold[#1 + #2[t] &, 0, ci]];
Definition[f]

All the above, of course, produce

f[t_] := X1[t] + X2[t] + X3[t] + X4[t]
$\endgroup$
4
  • $\begingroup$ What if the function isn't the summation? Like for example; (X1*X2+X3)^X4, like a more generic form? $\endgroup$ – TRL Aug 13 '15 at 3:23
  • $\begingroup$ "Using Total may be overkill" - tho Tr[] is certainly more compact, I still use Total[] for readability, reserving Tr[] for actually taking (generalized) matrix traces. $\endgroup$ – J. M.'s ennui Aug 13 '15 at 8:54
  • 1
    $\begingroup$ @TRL: define your n-argument function f and Apply it to your length-n vector ci: f @@ ci $\endgroup$ – J. M.'s ennui Aug 13 '15 at 8:56
  • $\begingroup$ @Guess Tr used to be an order of magnitude faster than Total on packed arrays; that was a better reason that brevity for its use in cases like this. $\endgroup$ – Mr.Wizard Aug 13 '15 at 10:37
1
$\begingroup$

In answer to the comment about more general combining functions...

vars = {X1, X2, X3, X4};
g[x1_, x2_, x3_, x4_] := (x1*x2 + x3)^x4;
f[t] := Evaluate[g @@ (#[t] & /@ vars)];

Where g is specific form of the general 'combining function'.

This could all be done in place without defining vars and g...

f[t] := Evaluate[((#1*#2 + #3)^#4) & @@ (#[t] & /@ {X1, X2, X3, X4})];
$\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.