Manipulating a general length function












5












$begingroup$


As a minimal example, define a function like s[1,2,3]; this is a function of three arguments in this case, but I'd like to apply this general idea to functions like s[3,4,5,6,7] which don't necessarily start at 1.



From an identity from theory, I need to re-express this as as sum of 2-argument functions that cycle over all of the possible combinations of the original function, but don't duplicate. Explicitly, this would be the same as writing



s[1,2,3]=f[1,2]+f[1,3]+f[2,3],



or in the longer more general case (and to illustrate my point)



s[3,4,5,6,7] = f[3,4]+f[3,5]+f[3,6]+f[3,7]+f[4,5]+f[4,6]+f[4,7]+f[5,6]+f[5,7]+f[6,7]


I was previously using the definition with simple replacement rules for each of the these chains, with general cases of s[i_,j_,k_] and such similar rules. However, this isn't optimal in larger problems, where I have many thousands of these results, and leads to remarkably long evaluation times.



To see my functional but slow solution, I have attached a partial screenshot here, but naturally, I think there must be a better method. I was considering using Partitions, as in this SE post or BlankSpaces, but I struggle to implement more complex uses of these. Any guidance or other ideas would be gratefully appreciated. Also, a title suggestion would be great as well!










share|improve this question









$endgroup$












  • $begingroup$
    Look into Subsets[listOfArguments, {2}]
    $endgroup$
    – MarcoB
    7 hours ago
















5












$begingroup$


As a minimal example, define a function like s[1,2,3]; this is a function of three arguments in this case, but I'd like to apply this general idea to functions like s[3,4,5,6,7] which don't necessarily start at 1.



From an identity from theory, I need to re-express this as as sum of 2-argument functions that cycle over all of the possible combinations of the original function, but don't duplicate. Explicitly, this would be the same as writing



s[1,2,3]=f[1,2]+f[1,3]+f[2,3],



or in the longer more general case (and to illustrate my point)



s[3,4,5,6,7] = f[3,4]+f[3,5]+f[3,6]+f[3,7]+f[4,5]+f[4,6]+f[4,7]+f[5,6]+f[5,7]+f[6,7]


I was previously using the definition with simple replacement rules for each of the these chains, with general cases of s[i_,j_,k_] and such similar rules. However, this isn't optimal in larger problems, where I have many thousands of these results, and leads to remarkably long evaluation times.



To see my functional but slow solution, I have attached a partial screenshot here, but naturally, I think there must be a better method. I was considering using Partitions, as in this SE post or BlankSpaces, but I struggle to implement more complex uses of these. Any guidance or other ideas would be gratefully appreciated. Also, a title suggestion would be great as well!










share|improve this question









$endgroup$












  • $begingroup$
    Look into Subsets[listOfArguments, {2}]
    $endgroup$
    – MarcoB
    7 hours ago














5












5








5





$begingroup$


As a minimal example, define a function like s[1,2,3]; this is a function of three arguments in this case, but I'd like to apply this general idea to functions like s[3,4,5,6,7] which don't necessarily start at 1.



From an identity from theory, I need to re-express this as as sum of 2-argument functions that cycle over all of the possible combinations of the original function, but don't duplicate. Explicitly, this would be the same as writing



s[1,2,3]=f[1,2]+f[1,3]+f[2,3],



or in the longer more general case (and to illustrate my point)



s[3,4,5,6,7] = f[3,4]+f[3,5]+f[3,6]+f[3,7]+f[4,5]+f[4,6]+f[4,7]+f[5,6]+f[5,7]+f[6,7]


I was previously using the definition with simple replacement rules for each of the these chains, with general cases of s[i_,j_,k_] and such similar rules. However, this isn't optimal in larger problems, where I have many thousands of these results, and leads to remarkably long evaluation times.



To see my functional but slow solution, I have attached a partial screenshot here, but naturally, I think there must be a better method. I was considering using Partitions, as in this SE post or BlankSpaces, but I struggle to implement more complex uses of these. Any guidance or other ideas would be gratefully appreciated. Also, a title suggestion would be great as well!










share|improve this question









$endgroup$




As a minimal example, define a function like s[1,2,3]; this is a function of three arguments in this case, but I'd like to apply this general idea to functions like s[3,4,5,6,7] which don't necessarily start at 1.



From an identity from theory, I need to re-express this as as sum of 2-argument functions that cycle over all of the possible combinations of the original function, but don't duplicate. Explicitly, this would be the same as writing



s[1,2,3]=f[1,2]+f[1,3]+f[2,3],



or in the longer more general case (and to illustrate my point)



s[3,4,5,6,7] = f[3,4]+f[3,5]+f[3,6]+f[3,7]+f[4,5]+f[4,6]+f[4,7]+f[5,6]+f[5,7]+f[6,7]


I was previously using the definition with simple replacement rules for each of the these chains, with general cases of s[i_,j_,k_] and such similar rules. However, this isn't optimal in larger problems, where I have many thousands of these results, and leads to remarkably long evaluation times.



To see my functional but slow solution, I have attached a partial screenshot here, but naturally, I think there must be a better method. I was considering using Partitions, as in this SE post or BlankSpaces, but I struggle to implement more complex uses of these. Any guidance or other ideas would be gratefully appreciated. Also, a title suggestion would be great as well!







list-manipulation performance-tuning pattern-matching






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 8 hours ago









BradBrad

768




768












  • $begingroup$
    Look into Subsets[listOfArguments, {2}]
    $endgroup$
    – MarcoB
    7 hours ago


















  • $begingroup$
    Look into Subsets[listOfArguments, {2}]
    $endgroup$
    – MarcoB
    7 hours ago
















$begingroup$
Look into Subsets[listOfArguments, {2}]
$endgroup$
– MarcoB
7 hours ago




$begingroup$
Look into Subsets[listOfArguments, {2}]
$endgroup$
– MarcoB
7 hours ago










2 Answers
2






active

oldest

votes


















7












$begingroup$

Something like the following?



ClearAll[s]
s[seq__] := Total[f@@@Subsets[List[seq], {2}]]





share|improve this answer









$endgroup$













  • $begingroup$
    Hi @MarcoB - Subsets was indeed the function I was looking for. I was thinking perhaps of more of a general replacement rule at the end, where in my final stage I remove all of the (previously defined) functions s, in exchange for these functions p. I will try and play around and see if I can figure out how to create a replacement rule based on this method. Thank you for your help:
    $endgroup$
    – Brad
    7 hours ago












  • $begingroup$
    After changing my definition of recursion to work with a symbolic sX, which actually makes evaluations much much faster overall, I can utilise this to get exactly what I need. Thank you all for your help, this is the fastest solution I found.
    $endgroup$
    – Brad
    6 hours ago



















7












$begingroup$

ClearAll[s]
s[f_] := Total @ Subsets[f @ ##, {2}] &;

s[f][a, b, c]



f[a, b] + f[a, c] + f[b, c]




s[f][3, 4, 5, 6, 7]



f[3, 4] + f[3, 5] + f[3, 6] + f[3, 7] + f[4, 5] + f[4, 6] + f[4, 7] +
f[5, 6] + f[5, 7] + f[6, 7]







share|improve this answer











$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    });
    });
    }, "mathjax-editing");

    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/3.0/"u003ecc by-sa 3.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%2f192481%2fmanipulating-a-general-length-function%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    7












    $begingroup$

    Something like the following?



    ClearAll[s]
    s[seq__] := Total[f@@@Subsets[List[seq], {2}]]





    share|improve this answer









    $endgroup$













    • $begingroup$
      Hi @MarcoB - Subsets was indeed the function I was looking for. I was thinking perhaps of more of a general replacement rule at the end, where in my final stage I remove all of the (previously defined) functions s, in exchange for these functions p. I will try and play around and see if I can figure out how to create a replacement rule based on this method. Thank you for your help:
      $endgroup$
      – Brad
      7 hours ago












    • $begingroup$
      After changing my definition of recursion to work with a symbolic sX, which actually makes evaluations much much faster overall, I can utilise this to get exactly what I need. Thank you all for your help, this is the fastest solution I found.
      $endgroup$
      – Brad
      6 hours ago
















    7












    $begingroup$

    Something like the following?



    ClearAll[s]
    s[seq__] := Total[f@@@Subsets[List[seq], {2}]]





    share|improve this answer









    $endgroup$













    • $begingroup$
      Hi @MarcoB - Subsets was indeed the function I was looking for. I was thinking perhaps of more of a general replacement rule at the end, where in my final stage I remove all of the (previously defined) functions s, in exchange for these functions p. I will try and play around and see if I can figure out how to create a replacement rule based on this method. Thank you for your help:
      $endgroup$
      – Brad
      7 hours ago












    • $begingroup$
      After changing my definition of recursion to work with a symbolic sX, which actually makes evaluations much much faster overall, I can utilise this to get exactly what I need. Thank you all for your help, this is the fastest solution I found.
      $endgroup$
      – Brad
      6 hours ago














    7












    7








    7





    $begingroup$

    Something like the following?



    ClearAll[s]
    s[seq__] := Total[f@@@Subsets[List[seq], {2}]]





    share|improve this answer









    $endgroup$



    Something like the following?



    ClearAll[s]
    s[seq__] := Total[f@@@Subsets[List[seq], {2}]]






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 7 hours ago









    MarcoBMarcoB

    36.5k556112




    36.5k556112












    • $begingroup$
      Hi @MarcoB - Subsets was indeed the function I was looking for. I was thinking perhaps of more of a general replacement rule at the end, where in my final stage I remove all of the (previously defined) functions s, in exchange for these functions p. I will try and play around and see if I can figure out how to create a replacement rule based on this method. Thank you for your help:
      $endgroup$
      – Brad
      7 hours ago












    • $begingroup$
      After changing my definition of recursion to work with a symbolic sX, which actually makes evaluations much much faster overall, I can utilise this to get exactly what I need. Thank you all for your help, this is the fastest solution I found.
      $endgroup$
      – Brad
      6 hours ago


















    • $begingroup$
      Hi @MarcoB - Subsets was indeed the function I was looking for. I was thinking perhaps of more of a general replacement rule at the end, where in my final stage I remove all of the (previously defined) functions s, in exchange for these functions p. I will try and play around and see if I can figure out how to create a replacement rule based on this method. Thank you for your help:
      $endgroup$
      – Brad
      7 hours ago












    • $begingroup$
      After changing my definition of recursion to work with a symbolic sX, which actually makes evaluations much much faster overall, I can utilise this to get exactly what I need. Thank you all for your help, this is the fastest solution I found.
      $endgroup$
      – Brad
      6 hours ago
















    $begingroup$
    Hi @MarcoB - Subsets was indeed the function I was looking for. I was thinking perhaps of more of a general replacement rule at the end, where in my final stage I remove all of the (previously defined) functions s, in exchange for these functions p. I will try and play around and see if I can figure out how to create a replacement rule based on this method. Thank you for your help:
    $endgroup$
    – Brad
    7 hours ago






    $begingroup$
    Hi @MarcoB - Subsets was indeed the function I was looking for. I was thinking perhaps of more of a general replacement rule at the end, where in my final stage I remove all of the (previously defined) functions s, in exchange for these functions p. I will try and play around and see if I can figure out how to create a replacement rule based on this method. Thank you for your help:
    $endgroup$
    – Brad
    7 hours ago














    $begingroup$
    After changing my definition of recursion to work with a symbolic sX, which actually makes evaluations much much faster overall, I can utilise this to get exactly what I need. Thank you all for your help, this is the fastest solution I found.
    $endgroup$
    – Brad
    6 hours ago




    $begingroup$
    After changing my definition of recursion to work with a symbolic sX, which actually makes evaluations much much faster overall, I can utilise this to get exactly what I need. Thank you all for your help, this is the fastest solution I found.
    $endgroup$
    – Brad
    6 hours ago











    7












    $begingroup$

    ClearAll[s]
    s[f_] := Total @ Subsets[f @ ##, {2}] &;

    s[f][a, b, c]



    f[a, b] + f[a, c] + f[b, c]




    s[f][3, 4, 5, 6, 7]



    f[3, 4] + f[3, 5] + f[3, 6] + f[3, 7] + f[4, 5] + f[4, 6] + f[4, 7] +
    f[5, 6] + f[5, 7] + f[6, 7]







    share|improve this answer











    $endgroup$


















      7












      $begingroup$

      ClearAll[s]
      s[f_] := Total @ Subsets[f @ ##, {2}] &;

      s[f][a, b, c]



      f[a, b] + f[a, c] + f[b, c]




      s[f][3, 4, 5, 6, 7]



      f[3, 4] + f[3, 5] + f[3, 6] + f[3, 7] + f[4, 5] + f[4, 6] + f[4, 7] +
      f[5, 6] + f[5, 7] + f[6, 7]







      share|improve this answer











      $endgroup$
















        7












        7








        7





        $begingroup$

        ClearAll[s]
        s[f_] := Total @ Subsets[f @ ##, {2}] &;

        s[f][a, b, c]



        f[a, b] + f[a, c] + f[b, c]




        s[f][3, 4, 5, 6, 7]



        f[3, 4] + f[3, 5] + f[3, 6] + f[3, 7] + f[4, 5] + f[4, 6] + f[4, 7] +
        f[5, 6] + f[5, 7] + f[6, 7]







        share|improve this answer











        $endgroup$



        ClearAll[s]
        s[f_] := Total @ Subsets[f @ ##, {2}] &;

        s[f][a, b, c]



        f[a, b] + f[a, c] + f[b, c]




        s[f][3, 4, 5, 6, 7]



        f[3, 4] + f[3, 5] + f[3, 6] + f[3, 7] + f[4, 5] + f[4, 6] + f[4, 7] +
        f[5, 6] + f[5, 7] + f[6, 7]








        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 6 hours ago

























        answered 7 hours ago









        kglrkglr

        186k10203422




        186k10203422






























            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%2f192481%2fmanipulating-a-general-length-function%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

            GameSpot

            日野市

            Tu-95轟炸機