Alternating Total

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite
1












Any cleaner solutions than what I came up with? (Maybe by multiplying every other entry by -1)



altTotal[x_] := Total@x[[;; ;; 2]] - Total@x[[2 ;; ;; 2]]






share|improve this question
























    up vote
    3
    down vote

    favorite
    1












    Any cleaner solutions than what I came up with? (Maybe by multiplying every other entry by -1)



    altTotal[x_] := Total@x[[;; ;; 2]] - Total@x[[2 ;; ;; 2]]






    share|improve this question






















      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      Any cleaner solutions than what I came up with? (Maybe by multiplying every other entry by -1)



      altTotal[x_] := Total@x[[;; ;; 2]] - Total@x[[2 ;; ;; 2]]






      share|improve this question












      Any cleaner solutions than what I came up with? (Maybe by multiplying every other entry by -1)



      altTotal[x_] := Total@x[[;; ;; 2]] - Total@x[[2 ;; ;; 2]]








      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 21 at 21:43









      qwr

      1878




      1878




















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          3
          down vote













          Maybe



          altTotal2[x_] := Subtract @@ Total[Partition[x, 2]]





          share|improve this answer



























            up vote
            3
            down vote













            altTotal3[x_] := Total@x - 2 Total@x[[2 ;; ;; 2]]


            is a bit faster.






            share|improve this answer




















            • That is a very nice example for floating point arithmetic being significantly faster than memory operations.
              – Henrik Schumacher
              Aug 22 at 9:47

















            up vote
            2
            down vote













            Here's a way to take + and - each successive member using Riffle:



            altTotal5[x_]:=Total[x Riffle[ConstantArray[1, Length[x]], -1][[1 ;; Length[x]]]]





            share|improve this answer



























              up vote
              2
              down vote













              altTotal4[x_] := Total@Developer`PartitionMap[Subtract @@ # &, x, 2]


              Edit: altTotal4 is quite surprisingly slow. The following Fold is much better but still not competititve.



              altTotal5[x_] := First@Fold[With[s = #1[[2]], s*#2 + #1[[1]], -s] &, 0, 1, -1, x]





              share|improve this answer






















              • That's slower by more than two orders of magnitude than altTotal3.
                – Henrik Schumacher
                Aug 22 at 9:44


















              up vote
              0
              down vote













              "Maybe" goes as below:



              altTotal4 = SparseArray[i_?EvenQ -> -1, Length[#], 1].# &


              Or



              altTotal5[x_] := Total@MapIndexed[(-1)^(1 + First[#2]) # &, x]





              share|improve this answer






















                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',
                convertImagesToLinks: false,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                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%2f180402%2falternating-total%23new-answer', 'question_page');

                );

                Post as a guest






























                5 Answers
                5






                active

                oldest

                votes








                5 Answers
                5






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                3
                down vote













                Maybe



                altTotal2[x_] := Subtract @@ Total[Partition[x, 2]]





                share|improve this answer
























                  up vote
                  3
                  down vote













                  Maybe



                  altTotal2[x_] := Subtract @@ Total[Partition[x, 2]]





                  share|improve this answer






















                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    Maybe



                    altTotal2[x_] := Subtract @@ Total[Partition[x, 2]]





                    share|improve this answer












                    Maybe



                    altTotal2[x_] := Subtract @@ Total[Partition[x, 2]]






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Aug 21 at 22:01









                    Henrik Schumacher

                    36.2k249102




                    36.2k249102




















                        up vote
                        3
                        down vote













                        altTotal3[x_] := Total@x - 2 Total@x[[2 ;; ;; 2]]


                        is a bit faster.






                        share|improve this answer




















                        • That is a very nice example for floating point arithmetic being significantly faster than memory operations.
                          – Henrik Schumacher
                          Aug 22 at 9:47














                        up vote
                        3
                        down vote













                        altTotal3[x_] := Total@x - 2 Total@x[[2 ;; ;; 2]]


                        is a bit faster.






                        share|improve this answer




















                        • That is a very nice example for floating point arithmetic being significantly faster than memory operations.
                          – Henrik Schumacher
                          Aug 22 at 9:47












                        up vote
                        3
                        down vote










                        up vote
                        3
                        down vote









                        altTotal3[x_] := Total@x - 2 Total@x[[2 ;; ;; 2]]


                        is a bit faster.






                        share|improve this answer












                        altTotal3[x_] := Total@x - 2 Total@x[[2 ;; ;; 2]]


                        is a bit faster.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Aug 21 at 22:10









                        JimB

                        15k12456




                        15k12456











                        • That is a very nice example for floating point arithmetic being significantly faster than memory operations.
                          – Henrik Schumacher
                          Aug 22 at 9:47
















                        • That is a very nice example for floating point arithmetic being significantly faster than memory operations.
                          – Henrik Schumacher
                          Aug 22 at 9:47















                        That is a very nice example for floating point arithmetic being significantly faster than memory operations.
                        – Henrik Schumacher
                        Aug 22 at 9:47




                        That is a very nice example for floating point arithmetic being significantly faster than memory operations.
                        – Henrik Schumacher
                        Aug 22 at 9:47










                        up vote
                        2
                        down vote













                        Here's a way to take + and - each successive member using Riffle:



                        altTotal5[x_]:=Total[x Riffle[ConstantArray[1, Length[x]], -1][[1 ;; Length[x]]]]





                        share|improve this answer
























                          up vote
                          2
                          down vote













                          Here's a way to take + and - each successive member using Riffle:



                          altTotal5[x_]:=Total[x Riffle[ConstantArray[1, Length[x]], -1][[1 ;; Length[x]]]]





                          share|improve this answer






















                            up vote
                            2
                            down vote










                            up vote
                            2
                            down vote









                            Here's a way to take + and - each successive member using Riffle:



                            altTotal5[x_]:=Total[x Riffle[ConstantArray[1, Length[x]], -1][[1 ;; Length[x]]]]





                            share|improve this answer












                            Here's a way to take + and - each successive member using Riffle:



                            altTotal5[x_]:=Total[x Riffle[ConstantArray[1, Length[x]], -1][[1 ;; Length[x]]]]






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Aug 22 at 3:48









                            bill s

                            50.6k373142




                            50.6k373142




















                                up vote
                                2
                                down vote













                                altTotal4[x_] := Total@Developer`PartitionMap[Subtract @@ # &, x, 2]


                                Edit: altTotal4 is quite surprisingly slow. The following Fold is much better but still not competititve.



                                altTotal5[x_] := First@Fold[With[s = #1[[2]], s*#2 + #1[[1]], -s] &, 0, 1, -1, x]





                                share|improve this answer






















                                • That's slower by more than two orders of magnitude than altTotal3.
                                  – Henrik Schumacher
                                  Aug 22 at 9:44















                                up vote
                                2
                                down vote













                                altTotal4[x_] := Total@Developer`PartitionMap[Subtract @@ # &, x, 2]


                                Edit: altTotal4 is quite surprisingly slow. The following Fold is much better but still not competititve.



                                altTotal5[x_] := First@Fold[With[s = #1[[2]], s*#2 + #1[[1]], -s] &, 0, 1, -1, x]





                                share|improve this answer






















                                • That's slower by more than two orders of magnitude than altTotal3.
                                  – Henrik Schumacher
                                  Aug 22 at 9:44













                                up vote
                                2
                                down vote










                                up vote
                                2
                                down vote









                                altTotal4[x_] := Total@Developer`PartitionMap[Subtract @@ # &, x, 2]


                                Edit: altTotal4 is quite surprisingly slow. The following Fold is much better but still not competititve.



                                altTotal5[x_] := First@Fold[With[s = #1[[2]], s*#2 + #1[[1]], -s] &, 0, 1, -1, x]





                                share|improve this answer














                                altTotal4[x_] := Total@Developer`PartitionMap[Subtract @@ # &, x, 2]


                                Edit: altTotal4 is quite surprisingly slow. The following Fold is much better but still not competititve.



                                altTotal5[x_] := First@Fold[With[s = #1[[2]], s*#2 + #1[[1]], -s] &, 0, 1, -1, x]






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Aug 22 at 12:38

























                                answered Aug 22 at 0:15









                                Alan

                                5,7621022




                                5,7621022











                                • That's slower by more than two orders of magnitude than altTotal3.
                                  – Henrik Schumacher
                                  Aug 22 at 9:44

















                                • That's slower by more than two orders of magnitude than altTotal3.
                                  – Henrik Schumacher
                                  Aug 22 at 9:44
















                                That's slower by more than two orders of magnitude than altTotal3.
                                – Henrik Schumacher
                                Aug 22 at 9:44





                                That's slower by more than two orders of magnitude than altTotal3.
                                – Henrik Schumacher
                                Aug 22 at 9:44











                                up vote
                                0
                                down vote













                                "Maybe" goes as below:



                                altTotal4 = SparseArray[i_?EvenQ -> -1, Length[#], 1].# &


                                Or



                                altTotal5[x_] := Total@MapIndexed[(-1)^(1 + First[#2]) # &, x]





                                share|improve this answer


























                                  up vote
                                  0
                                  down vote













                                  "Maybe" goes as below:



                                  altTotal4 = SparseArray[i_?EvenQ -> -1, Length[#], 1].# &


                                  Or



                                  altTotal5[x_] := Total@MapIndexed[(-1)^(1 + First[#2]) # &, x]





                                  share|improve this answer
























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    "Maybe" goes as below:



                                    altTotal4 = SparseArray[i_?EvenQ -> -1, Length[#], 1].# &


                                    Or



                                    altTotal5[x_] := Total@MapIndexed[(-1)^(1 + First[#2]) # &, x]





                                    share|improve this answer














                                    "Maybe" goes as below:



                                    altTotal4 = SparseArray[i_?EvenQ -> -1, Length[#], 1].# &


                                    Or



                                    altTotal5[x_] := Total@MapIndexed[(-1)^(1 + First[#2]) # &, x]






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Aug 22 at 3:27

























                                    answered Aug 22 at 3:21









                                    Αλέξανδρος Ζεγγ

                                    1,419720




                                    1,419720



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180402%2falternating-total%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Comments

                                        Popular posts from this blog

                                        What does second last employer means? [closed]

                                        List of Gilmore Girls characters

                                        Confectionery