Group Abstract Group Abstract

Message Boards Message Boards

2
|
9.3K Views
|
9 Replies
|
12 Total Likes
View groups...
Share
Share this post:

Transpose-like function to interchange Heads, Rows, Columns

Posted 11 years ago

Dear Wolframers, I am looking for some builtin transpose-like function such that :

TransposeLike[f[g[x11,x12,...x2n],g[x21,x22,...,x2n],...,g[xm1,xm2,...,xmn]]
= g[f[x11,x21,...xm1],f[x12,x22,...,xm2],...,f[x1n,x2n,...,xmn]]

ie not only do the lines and columns get exchanged, but the Heads of the underlying lists as well, so to speak.

Does anyone know about such a builtin ?

Thanks a lot for your help.

POSTED BY: Fabien Todescato
9 Replies
Posted 11 years ago

Syntactic fun, similar to Daniel's approach, less general but slightly faster:

 g @@ f @@@ Transpose @* List @@ List @@@ data
POSTED BY: Kuba Podkalicki
Posted 11 years ago

Hi Fabien,

here is another approach, possible for every dimension and general functions without known number of variables or functionnames

Transposelike[expr_] := 
  Block[ {numberm, numbern, numbermn}, 
   numberm = Length[Level[expr, {1}]];
   numbermn = Length[Level[expr, {2}]];
   numbern = numbermn/numberm;
   expr[[1]][[0]] @@ Head[expr] @@@ Transpose[Partition[ Level[expr, {2}] , numbern]];];

Here is a result

expr = f[g[a, b], g[c, d], g[e, h], g[i, j]];

gives

g[f[a, c, e, i], f[b, d, h, j]]

and

f[x_, y_, z_, r_] := x y z r; g[x_, y_] := x + y;
expr = f[g[a, b], g[c, d], g[e, h], g[i, j]];

leads to

a c e i + b d h j
POSTED BY: Till Luc Lange

Thanks for your elaborate answer :) I will have to study it a bit though, not having reached that level of sophistication and mastery of the language that seems to be yours.

A great opportunity for me to learn :)

Thanks.

POSTED BY: Fabien Todescato

Not built in but easy to code using the usual Transpose.

transposeLike[args : f_[g_[_ ..] ..]] := With[
  {listOfLists = args /. {f -> List, g -> List}},
  Apply[g, Apply[f, Transpose[listOfLists], {1}]]]

transposeLike[f[g[a11, a12, a13], g[a21, a22, a23]]]

(* Out[7]= g[f[a11, a21], f[a12, a22], f[a13, a23]] *)
POSTED BY: Daniel Lichtblau

Thanks Daniel :) It is always pleasing to learn by reading concise and aesthetically written code, and your solution has the conciseness and degree of generality I was looking for.

POSTED BY: Fabien Todescato

Thanks David, though I am interested in the general case for m and n, reading the code you offer definitely made me discover some new useful tricks.

Thanks.

POSTED BY: Fabien Todescato
POSTED BY: Marco Thiel

Thank you Marco for putting in the time and effort to try to come up with an interesting answer :) But my question was kind of addressing the the general case where f and g are a priori unknown, and the subscript notation was just indicative of the matrix-like layout of the variables.

POSTED BY: Fabien Todescato
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard