Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.9K Views
|
4 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Simplify the syntax of calling multiple functions at the same time

Posted 3 years ago

See my following code snippet:

In[870]:= cclPGAK227Left1
ls1 = {Length[#], Dimensions[First[#]], Tr[First[#]], 
     Det[First[#]]} & /@ cclPGAK227Left1 // Sort

Out[870]= {{{{-1, 0, 0}, {0, -1, 0}, {0, 0, -1}}}, {{{-1, 0, 
    0}, {0, -1, 0}, {0, 0, 1}}, {{-1, 0, 0}, {0, 1, 0}, {0, 
    0, -1}}, {{1, 0, 0}, {0, -1, 0}, {0, 0, -1}}}, {{{-1, 0, 0}, {0, 
    0, -1}, {0, -1, 0}}, {{-1, 0, 0}, {0, 0, 1}, {0, 1, 0}}, {{0, -1, 
    0}, {-1, 0, 0}, {0, 0, -1}}, {{0, 0, -1}, {0, -1, 0}, {-1, 0, 
    0}}, {{0, 0, 1}, {0, -1, 0}, {1, 0, 0}}, {{0, 1, 0}, {1, 0, 
    0}, {0, 0, -1}}}, {{{-1, 0, 0}, {0, 0, -1}, {0, 1, 0}}, {{-1, 0, 
    0}, {0, 0, 1}, {0, -1, 0}}, {{0, -1, 0}, {1, 0, 0}, {0, 
    0, -1}}, {{0, 0, -1}, {0, -1, 0}, {1, 0, 0}}, {{0, 0, 1}, {0, -1, 
    0}, {-1, 0, 0}}, {{0, 1, 0}, {-1, 0, 0}, {0, 0, -1}}}, {{{-1, 0, 
    0}, {0, 1, 0}, {0, 0, 1}}, {{1, 0, 0}, {0, -1, 0}, {0, 0, 
    1}}, {{1, 0, 0}, {0, 1, 0}, {0, 0, -1}}}, {{{0, -1, 0}, {-1, 0, 
    0}, {0, 0, 1}}, {{0, 0, -1}, {0, 1, 0}, {-1, 0, 0}}, {{0, 0, 
    1}, {0, 1, 0}, {1, 0, 0}}, {{0, 1, 0}, {1, 0, 0}, {0, 0, 1}}, {{1,
     0, 0}, {0, 0, -1}, {0, -1, 0}}, {{1, 0, 0}, {0, 0, 1}, {0, 1, 
    0}}}, {{{0, -1, 0}, {0, 0, -1}, {-1, 0, 0}}, {{0, -1, 0}, {0, 0, 
    1}, {1, 0, 0}}, {{0, 0, -1}, {-1, 0, 0}, {0, -1, 0}}, {{0, 
    0, -1}, {1, 0, 0}, {0, 1, 0}}, {{0, 0, 1}, {-1, 0, 0}, {0, 1, 
    0}}, {{0, 0, 1}, {1, 0, 0}, {0, -1, 0}}, {{0, 1, 0}, {0, 
    0, -1}, {1, 0, 0}}, {{0, 1, 0}, {0, 0, 1}, {-1, 0, 0}}}, {{{0, -1,
     0}, {0, 0, -1}, {1, 0, 0}}, {{0, -1, 0}, {0, 0, 1}, {-1, 0, 
    0}}, {{0, 0, -1}, {-1, 0, 0}, {0, 1, 0}}, {{0, 0, -1}, {1, 0, 
    0}, {0, -1, 0}}, {{0, 0, 1}, {-1, 0, 0}, {0, -1, 0}}, {{0, 0, 
    1}, {1, 0, 0}, {0, 1, 0}}, {{0, 1, 0}, {0, 0, -1}, {-1, 0, 
    0}}, {{0, 1, 0}, {0, 0, 1}, {1, 0, 0}}}, {{{0, -1, 0}, {1, 0, 
    0}, {0, 0, 1}}, {{0, 0, -1}, {0, 1, 0}, {1, 0, 0}}, {{0, 0, 
    1}, {0, 1, 0}, {-1, 0, 0}}, {{0, 1, 0}, {-1, 0, 0}, {0, 0, 
    1}}, {{1, 0, 0}, {0, 0, -1}, {0, 1, 0}}, {{1, 0, 0}, {0, 0, 
    1}, {0, -1, 0}}}, {{{1, 0, 0}, {0, 1, 0}, {0, 0, 1}}}}

Out[871]= {{1, {3, 3}, -3, -1}, {1, {3, 3}, 3, 1}, {3, {3, 3}, -1, 
  1}, {3, {3, 3}, 1, -1}, {6, {3, 3}, -1, -1}, {6, {3, 3}, -1, 
  1}, {6, {3, 3}, 1, -1}, {6, {3, 3}, 1, 1}, {8, {3, 3}, 
  0, -1}, {8, {3, 3}, 0, 1}}

My code looks bloated and clumsy. Are there some methods to simplify the syntax of calling multiple functions at the same time?

Regards, Zhao

POSTED BY: Hongyi Zhao
4 Replies
Posted 3 years ago

One can use Through to apply several functions to the same argument. But if that is simplier in your case is a matter of taste.

Through[{Length, Dimensions@*First, Tr@*First, Det@*First}[#]] & /@ cclPGAK227Left1 // Sort
POSTED BY: Hans Milton
Posted 3 years ago

Yes. It works. Thank you for your tips.

In[26]:= 
ls1 = {Length[#], Dimensions[First[#]], Tr[First[#]], 
      Det[First[#]]} & /@ cclPGAK227Left1 // Sort;
cclPGAK227Left1 // 
  Map[(Prepend[
      OperatorApplied[Composition][First] /@ {Dimensions, Tr, Det}, 
      Length])/*Through] // Sort
% == ls1


Out[27]= {{1, {3, 3}, -3, -1}, {1, {3, 3}, 3, 1}, {3, {3, 3}, -1, 
  1}, {3, {3, 3}, 1, -1}, {6, {3, 3}, -1, -1}, {6, {3, 3}, -1, 
  1}, {6, {3, 3}, 1, -1}, {6, {3, 3}, 1, 1}, {8, {3, 3}, 
  0, -1}, {8, {3, 3}, 0, 1}}

Out[28]= True
POSTED BY: Hongyi Zhao

I think this works:

cclPGAK227Left1 // 
 Map[(Prepend[
     OperatorApplied[Composition][First] /@ {Dimensions, Tr, Det}, 
     Length])/*Through]

but it is hard ot read

POSTED BY: Gustavo Delfino
Posted 3 years ago

As you can see, First is still a common factor in these commands. So, how to further simplify, in order to use this command only once?

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