10
$\begingroup$

I need to use:

Composition[G][x]

in the elements of the following list:

j = {{a, b, c}, {c, b, a}};

That is, I could do:

Table[Composition[j[[n]]][x], {n, 1, 2}]

which produces:

{{a, b, c}[x], {c, b, a}[x]}

but Composition doesn't seem to work with lists. I can't evaluate Composition[{a,b,c}][x]; it accepts only Composition[a,b,c][x].

Is there a simple way to convert Composition[{a,b,c}] to Composition[a,b,c]?

$\endgroup$
0

4 Answers 4

13
$\begingroup$
lis = {a, b, c}
Composition[Sequence @@ lis][x]

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ Doesn't just Composition @@ lis work just fine? $\endgroup$
    – LSpice
    Nov 22, 2021 at 2:12
  • 4
    $\begingroup$ @LSpice yes, and it needs to be enclosed by parentheses before applying to an argument, e.g. (Composition @@ lis)[x] $\endgroup$
    – polfosol
    Nov 22, 2021 at 15:27
  • $\begingroup$ @polfosol or just x // Composition @@ lis $\endgroup$ Nov 29, 2021 at 18:49
17
$\begingroup$

Apply is designed for this purpose:

Apply[Composition][{a, b, c}][x]
(*  a[b[c[x]]]  *)

Or

Apply[Composition, {a, b, c}][x]
$\endgroup$
2
  • 2
    $\begingroup$ Shorthand for Apply is @@. E.g. (Composition @@ {a,b,c})[x] or x // Composition @@ {a,b,c}. $\endgroup$
    – Džuris
    Nov 20, 2021 at 11:52
  • 1
    $\begingroup$ @Džuris Yep, I usually use @@. Except, a personal preference, I dislike parentheses, which led me to avoid @@ in these situations. With command completion, the length of typing is about the same, and no parentheses feels easier to type & read for me. $\endgroup$
    – Michael E2
    Nov 20, 2021 at 21:24
13
$\begingroup$

With:

alist = {a, b, c}

A variant using Fold could be:

Fold[Composition, alist][x]

a[b[c[x]]]

Another variant using ComposeList could be:

ComposeList[Reverse@alist, x]

{x, c[x], b[c[x]], a[b[c[x]]]}

from which the last item can be extracted.

$\endgroup$
2
  • 4
    $\begingroup$ x // Fold[Composition] /@ {{a, b, c}, {c, b, a}} // Through shows how naturally Fold and Composition can express the solution to OP's problem. Or myFuncList = Fold[Composition] /@ {{a, b, c}, {c, b, a}}; which can be used later, either as individual functions or in Through[myFuncList[x]] $\endgroup$
    – Michael E2
    Nov 20, 2021 at 15:52
  • 1
    $\begingroup$ A variation of this solution is Fold[ReverseApplied[Construct], x, Reverse@{a, b, c}]. $\endgroup$ Dec 9, 2021 at 20:36
5
$\begingroup$

Since no one seems to have answered how to Apply Composition to a List like j given by OP, here it is:

Composition[##][x]&@@#&/@j

{a[b[c[x]]],c[b[a[x]]]}

$\endgroup$
2
  • 5
    $\begingroup$ Or Composition[##][x]&@@@j if always a list of lists of functions. :) $\endgroup$
    – Michael E2
    Nov 21, 2021 at 14:09
  • 1
    $\begingroup$ A simpler version is: #[x]& /@ Composition@@@j $\endgroup$
    – polfosol
    Nov 22, 2021 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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