Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is there a built in function or an easy rule to do the following transformation?

H[{A[0], A[1], ...},{B[0], B[1], ...},...]

into

{H[A[0], B[0]], H[A[1], B[1]], ...}
share|improve this question
3  
Thread might help you. –  Spawn1701D Apr 15 '13 at 12:44
    
and what happened to A[0] and B[0]? –  Pinguin Dirk Apr 15 '13 at 12:45
    
@Spawn1701D ok Thread does it, thanks. @PinguinDirk, corrected. –  Lorenzo Pistone Apr 15 '13 at 12:46
1  
Possible dupe of mathematica.stackexchange.com/q/13748/5 –  rm -rf Apr 15 '13 at 13:11
add comment

1 Answer 1

up vote 1 down vote accepted

Thread does this:

expr = h[{a[0], a[1], a[2]}, {b[0], b[1], b[2]}, {c[0], c[1], c[2]}];

Thread[expr]
{h[a[0], b[0], c[0]], h[a[1], b[1], c[1]], h[a[2], b[2], c[2]]}

If however your subexpressions are not the same length you will get an error:

expr2 = h[{a[0], a[1], a[2]}, {b[0], b[1]}, {c[0]}];

Thread[expr2]

Thread::tdlen: Objects of unequal length in h[{a[0],a[1],a[2]},{b[0],b[1]},{c[0]}] cannot be combined. >>

One could use Flatten to do something like this:

h @@@ Flatten[List @@ expr2, {2}]
{h[a[0], b[0], c[0]], h[a[1], b[1]], h[a[2]]}

To understand Flatten see: Flatten command: matrix as second argument.

There is a problem with the simple form above however. Note what happens if h is made to hold its arguments unevaluated, and a is given a definition which we do not wish to trigger:

Attributes[h] = {HoldAll};

a[_] = "Fail!";

Thread still works as desired:

Thread[expr]
{h[a[0], b[0], c[0]], h[a[1], b[1], c[1]], h[a[2], b[2], c[2]]}

The form with Flatten does not:

h @@@ Flatten[List @@ expr2, {2}]
{h["Fail!", b[0], c[0]], h["Fail!", b[1]], h["Fail!"]}

We can get around that with:

List @@ Flatten[h @@@ expr2, {2}, h]
{h[a[0], b[0], c[0]], h[a[1], b[1]], h[a[2]]}

Which could be generalized as:

raggedThread[x : h1_[h2_[___] ..]] := h2 @@ Flatten[h1 @@@ x, {2}, h1]

raggedThread[expr2]
{h[a[0], b[0], c[0]], h[a[1], b[1]], h[a[2]]}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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