5
$\begingroup$

A minimum example is this:

ls = {1 + I, 2 - I};
Max@Re[ls]
Min@Im[ls]
#[ls] & /@ {Max@Re, Min@Im} (*want {2, -1}*)

The result is:

{{1, 2}, {1, -1}}

I don't know why is this..

$\endgroup$
  • 2
    $\begingroup$ @ does not denote function composition. It denotes function applications. f@g[x] is not (f@g)[x] but f@(g[x]), merely a different notation for f[g[x]]. $\endgroup$ – Szabolcs Apr 7 at 9:41
  • $\begingroup$ You could just do {Max@Re[#], Min@Im@#} &@ls applying your function to ls instead of mapping it. $\endgroup$ – N.J.Evans Apr 7 at 14:59
  • $\begingroup$ @Szabolcs But Max[Re[ls]] gives just a number, why need composite? Also Max@Re[ls] gives a number $\endgroup$ – an offer can't refuse Apr 8 at 3:54
10
$\begingroup$

For a minimal change in your code, you can replace @ with Composition (@*)

#[ls] & /@ {Max@*Re, Min@*Im} 
 {2, -1}

Consider also Through:

Through @ {Max @* Re, Min @* Im} @ ls
{2, -1}

Aside: Why your code gives {{1, 2}, {1, -1}}:

Trace[#[ls] & /@ {Max@Re, Min@Im}] // Column

enter image description here

Notice that Max@Re is replaced with Re and Min@Im is replaced with Im in the very first step of evaluation.

| improve this answer | |
$\endgroup$
  • $\begingroup$ If you can further add the reason why we need composite functions, the answer would be better. For example, why Max@Re[ls] Min@Im[ls] work as expected but not work when using Apply? $\endgroup$ – an offer can't refuse Apr 8 at 3:56
  • $\begingroup$ @anoffercan'trefuse, added a note re why you get {{1, 2}, {1, -1}} if you use {Max@Re, Min@Im} instead of {Max@*Re, Min@*Im} or {Max@Re@#&, Min@Im@#&} $\endgroup$ – kglr Apr 8 at 15:46
2
$\begingroup$
Map[{Max[Re[#]], Min[Im[#]]} &, ls, {0}]

Gives

{2, -1}

From help on Map

Level 0 corresponds to the whole expression.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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