Mapping a function f[xi_,xj_] over a list {x1, …, xn} with the i < j restriction





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}
.everyonelovesstackoverflow{position:absolute;height:1px;width:1px;opacity:0;top:0;left:0;pointer-events:none;}








5














$begingroup$


I am often facing the situation where I have a list ${...x_k...}$ and where I want to compute:



$$
{...f[x_i,x_j]...}text{ with } i<j
$$



Unfortunately, I have not been able to find an elegant solution for this problem. What I'm doing right now seems horrible to me (indirections etc...):



n = 4;
x = Table[RandomReal, n]



{0.547984, 0.601967, 0.000917483, 0.738287}




Map[f[Apply[Sequence, x[[#]]]] &, 
Flatten[Table[{i, j}, {i, 1, n}, {j, i + 1, n}], 1]]



{f[0.547984, 0.601967], f[0.547984, 0.000917483], f[0.547984,
0.738287], f[0.601967, 0.000917483], f[0.601967, 0.738287], f[0.000917483, 0.738287]}




(in real situation f is defined f[xi_,xj_]:= xi-xj for instance)



Question: what is a better (builtin?) solution?










share|improve this question












$endgroup$






















    5














    $begingroup$


    I am often facing the situation where I have a list ${...x_k...}$ and where I want to compute:



    $$
    {...f[x_i,x_j]...}text{ with } i<j
    $$



    Unfortunately, I have not been able to find an elegant solution for this problem. What I'm doing right now seems horrible to me (indirections etc...):



    n = 4;
    x = Table[RandomReal, n]



    {0.547984, 0.601967, 0.000917483, 0.738287}




    Map[f[Apply[Sequence, x[[#]]]] &, 
    Flatten[Table[{i, j}, {i, 1, n}, {j, i + 1, n}], 1]]



    {f[0.547984, 0.601967], f[0.547984, 0.000917483], f[0.547984,
    0.738287], f[0.601967, 0.000917483], f[0.601967, 0.738287], f[0.000917483, 0.738287]}




    (in real situation f is defined f[xi_,xj_]:= xi-xj for instance)



    Question: what is a better (builtin?) solution?










    share|improve this question












    $endgroup$


















      5












      5








      5


      1



      $begingroup$


      I am often facing the situation where I have a list ${...x_k...}$ and where I want to compute:



      $$
      {...f[x_i,x_j]...}text{ with } i<j
      $$



      Unfortunately, I have not been able to find an elegant solution for this problem. What I'm doing right now seems horrible to me (indirections etc...):



      n = 4;
      x = Table[RandomReal, n]



      {0.547984, 0.601967, 0.000917483, 0.738287}




      Map[f[Apply[Sequence, x[[#]]]] &, 
      Flatten[Table[{i, j}, {i, 1, n}, {j, i + 1, n}], 1]]



      {f[0.547984, 0.601967], f[0.547984, 0.000917483], f[0.547984,
      0.738287], f[0.601967, 0.000917483], f[0.601967, 0.738287], f[0.000917483, 0.738287]}




      (in real situation f is defined f[xi_,xj_]:= xi-xj for instance)



      Question: what is a better (builtin?) solution?










      share|improve this question












      $endgroup$




      I am often facing the situation where I have a list ${...x_k...}$ and where I want to compute:



      $$
      {...f[x_i,x_j]...}text{ with } i<j
      $$



      Unfortunately, I have not been able to find an elegant solution for this problem. What I'm doing right now seems horrible to me (indirections etc...):



      n = 4;
      x = Table[RandomReal, n]



      {0.547984, 0.601967, 0.000917483, 0.738287}




      Map[f[Apply[Sequence, x[[#]]]] &, 
      Flatten[Table[{i, j}, {i, 1, n}, {j, i + 1, n}], 1]]



      {f[0.547984, 0.601967], f[0.547984, 0.000917483], f[0.547984,
      0.738287], f[0.601967, 0.000917483], f[0.601967, 0.738287], f[0.000917483, 0.738287]}




      (in real situation f is defined f[xi_,xj_]:= xi-xj for instance)



      Question: what is a better (builtin?) solution?







      list-manipulation programming






      share|improve this question
















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 28 at 11:40







      Picaud Vincent

















      asked May 28 at 11:14









      Picaud VincentPicaud Vincent

      1,5827 silver badges18 bronze badges




      1,5827 silver badges18 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          7
















          $begingroup$

          Subsets[f @@ x, {2}]



          {f[0.817389, 0.11142], f[0.817389, 0.789526], f[0.817389, 0.187803],
          f[0.11142, 0.789526], f[0.11142, 0.187803], f[0.789526, 0.187803]}




          You can also use



          f @@@ Subsets[x, {2}]
          Join @@ Table[f @@ x[[{i, j}]], {i, 1, n}, {j, i + 1, n}]





          share|improve this answer












          $endgroup$

















            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "387"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });















            draft saved

            draft discarded
















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f199243%2fmapping-a-function-fxi-xj-over-a-list-x1-xn-with-the-i-j-restrict%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown


























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            7
















            $begingroup$

            Subsets[f @@ x, {2}]



            {f[0.817389, 0.11142], f[0.817389, 0.789526], f[0.817389, 0.187803],
            f[0.11142, 0.789526], f[0.11142, 0.187803], f[0.789526, 0.187803]}




            You can also use



            f @@@ Subsets[x, {2}]
            Join @@ Table[f @@ x[[{i, j}]], {i, 1, n}, {j, i + 1, n}]





            share|improve this answer












            $endgroup$




















              7
















              $begingroup$

              Subsets[f @@ x, {2}]



              {f[0.817389, 0.11142], f[0.817389, 0.789526], f[0.817389, 0.187803],
              f[0.11142, 0.789526], f[0.11142, 0.187803], f[0.789526, 0.187803]}




              You can also use



              f @@@ Subsets[x, {2}]
              Join @@ Table[f @@ x[[{i, j}]], {i, 1, n}, {j, i + 1, n}]





              share|improve this answer












              $endgroup$


















                7














                7










                7







                $begingroup$

                Subsets[f @@ x, {2}]



                {f[0.817389, 0.11142], f[0.817389, 0.789526], f[0.817389, 0.187803],
                f[0.11142, 0.789526], f[0.11142, 0.187803], f[0.789526, 0.187803]}




                You can also use



                f @@@ Subsets[x, {2}]
                Join @@ Table[f @@ x[[{i, j}]], {i, 1, n}, {j, i + 1, n}]





                share|improve this answer












                $endgroup$



                Subsets[f @@ x, {2}]



                {f[0.817389, 0.11142], f[0.817389, 0.789526], f[0.817389, 0.187803],
                f[0.11142, 0.789526], f[0.11142, 0.187803], f[0.789526, 0.187803]}




                You can also use



                f @@@ Subsets[x, {2}]
                Join @@ Table[f @@ x[[{i, j}]], {i, 1, n}, {j, i + 1, n}]






                share|improve this answer















                share|improve this answer




                share|improve this answer








                edited May 28 at 11:37

























                answered May 28 at 11:32









                kglrkglr

                221k10 gold badges251 silver badges508 bronze badges




                221k10 gold badges251 silver badges508 bronze badges


































                    draft saved

                    draft discarded



















































                    Thanks for contributing an answer to Mathematica Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    Use MathJax to format equations. MathJax reference.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f199243%2fmapping-a-function-fxi-xj-over-a-list-x1-xn-with-the-i-j-restrict%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown









                    Popular posts from this blog

                    He _____ here since 1970 . Answer needed [closed]What does “since he was so high” mean?Meaning of “catch birds for”?How do I ensure “since” takes the meaning I want?“Who cares here” meaningWhat does “right round toward” mean?the time tense (had now been detected)What does the phrase “ring around the roses” mean here?Correct usage of “visited upon”Meaning of “foiled rail sabotage bid”It was the third time I had gone to Rome or It is the third time I had been to Rome

                    Bunad

                    Færeyskur hestur Heimild | Tengill | Tilvísanir | LeiðsagnarvalRossið - síða um færeyska hrossið á færeyskuGott ár hjá færeyska hestinum