Message Boards Message Boards

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

Transpose-like function to interchange Heads, Rows, Columns

Posted 8 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

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

 g @@ f @@@ Transpose @* List @@ List @@@ data
POSTED BY: Kuba Podkalicki
Posted 8 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

If you are satisfied with 2 x 2 matrix-like:

TransposeExpression::usage = 
  "TransposeExpression[expression,{f,g}] will transpose and \
interchange f and g for an expression that is a matrix when f and g \
are replaced by Lists.";
TransposeExpression::notmatrixlike = 
  "The expression was not converted to a matrix.";
TransposeExpression[expression_, {f_, g_}] :=
 Module[{work = expression},
  work = work /. f | g -> List;
  If[! MatrixQ[work], Message[TransposeExpression::notmatrixlike]; 
   Abort[]];
  g @@ f @@@ Transpose[work]
  ]

expression = f @@ g @@@ Array[x[#1, #2] &, {3, 3}]

Then

TransposeExpression[expression, {f, g}]  

Gives

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

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

Hi Fabien,

this is not built-in, but might do something similar to what you require.

f[g[Subscript[x, 1, 1], Subscript[x, 1, 2], Subscript[x, 1, 3]], g[Subscript[x, 2, 1], Subscript[x, 2, 2], Subscript[x, 2, 3]], g[Subscript[x, 3, 1], Subscript[x, 3, 2], Subscript[x, 3, 3]]] /. {g -> f, f -> g, Subscript[c_, a_, b_] -> Subscript[c, b, a]}

enter image description here

It can be done if the indices are part of the variable names like in your case, but it is less elegant. By the way, I usually refrain from using subscripts.

Best wishes,

Marco

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

Group Abstract Group Abstract