Numerical second order differentiationMerging different size, different order tableHow can I make threading more flexible?Issue with very large lists in MathematicaHow can I run a summation numerous times?Delete duplicates in list of interpolating functionsHow to efficiently manage lists of arbitrary length?Need to add a different random number to each part of the tableHow to generate all involutive permutations?Efficiency in array element replacementSpeed up Flatten[] of a large nested list

How can I use Charisma instead of Strength or Dexterity for weapon attacks without being a Hexblade warlock?

How to follow symbolic links on disk?

Why did quill pens persist until the early 19th century despite the capacity to make metal dip pens?

How can I tell if I have simplified my talk too much?

Does the size of chilli pieces effect the hotness

Power supply - purpose of the capacitor on the side of the transformer before full bridge rectifier

Got an email saying my password is weak, reason for concern?

Will the price change on Airbnb if I provide my ID later?

Black hole as a storage device?

Can the target of Feign Death speak, move, and dodge a fireball

Why is Ian Blackford allowed to speak in the Commons while having crossed the line?

Is there an algorithm for determining whether an expression involving nested radicals is rational?

Kitchen rewire gone wrong

Simple n-body class in C++

Help resolve territory acquisition design difference of opinion in MMO RTS

How can you coordinate an attack against a monster that cannot be gazed upon?

Calculate the internal angles of a regular polygon with N sides

What is the name for a placename that contains what the thing is in a different language?

Function defined everywhere but continuous nowhere

How Would Civilisations on Different Planes of a Cube Planet Interact?

Is 2FA via mobile phone still a good idea when phones are the most exposed device?

Apollo image lighting

How do we distinguish old craters from new ones on the Moon?

Is there more concise way to say "tomorrow morning"?



Numerical second order differentiation


Merging different size, different order tableHow can I make threading more flexible?Issue with very large lists in MathematicaHow can I run a summation numerous times?Delete duplicates in list of interpolating functionsHow to efficiently manage lists of arbitrary length?Need to add a different random number to each part of the tableHow to generate all involutive permutations?Efficiency in array element replacementSpeed up Flatten[] of a large nested list






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;

.everyonelovesstackoverflowposition:absolute;height:1px;width:1px;opacity:0;top:0;left:0;pointer-events:none;








4














$begingroup$


I have written a code for the second differentiation of numerical list



diff2list = ; diff2 = ConstantArray[0, Length@firstlist, 2];
Do[

diff2[[i]] = (firstlist[[i - 1, 2]] - 2 firstlist[[i, 2]] + firstlist[[i + 1, 2]])/(2*0.4);

AppendTo[diff2list, 0.4*i, diff2[[i]]]

, i, 2, Length@firstlist - 1]


How can I change my code to be more efficient for larger lists and small intervals. The firstlist can be generated as below:



firstlist = Table[0.4 i, N@Cos[[Pi]/7 i], i, 1, 30];


However, my list is different from this one.










share|improve this question












$endgroup$





















    4














    $begingroup$


    I have written a code for the second differentiation of numerical list



    diff2list = ; diff2 = ConstantArray[0, Length@firstlist, 2];
    Do[

    diff2[[i]] = (firstlist[[i - 1, 2]] - 2 firstlist[[i, 2]] + firstlist[[i + 1, 2]])/(2*0.4);

    AppendTo[diff2list, 0.4*i, diff2[[i]]]

    , i, 2, Length@firstlist - 1]


    How can I change my code to be more efficient for larger lists and small intervals. The firstlist can be generated as below:



    firstlist = Table[0.4 i, N@Cos[[Pi]/7 i], i, 1, 30];


    However, my list is different from this one.










    share|improve this question












    $endgroup$

















      4












      4








      4


      2



      $begingroup$


      I have written a code for the second differentiation of numerical list



      diff2list = ; diff2 = ConstantArray[0, Length@firstlist, 2];
      Do[

      diff2[[i]] = (firstlist[[i - 1, 2]] - 2 firstlist[[i, 2]] + firstlist[[i + 1, 2]])/(2*0.4);

      AppendTo[diff2list, 0.4*i, diff2[[i]]]

      , i, 2, Length@firstlist - 1]


      How can I change my code to be more efficient for larger lists and small intervals. The firstlist can be generated as below:



      firstlist = Table[0.4 i, N@Cos[[Pi]/7 i], i, 1, 30];


      However, my list is different from this one.










      share|improve this question












      $endgroup$




      I have written a code for the second differentiation of numerical list



      diff2list = ; diff2 = ConstantArray[0, Length@firstlist, 2];
      Do[

      diff2[[i]] = (firstlist[[i - 1, 2]] - 2 firstlist[[i, 2]] + firstlist[[i + 1, 2]])/(2*0.4);

      AppendTo[diff2list, 0.4*i, diff2[[i]]]

      , i, 2, Length@firstlist - 1]


      How can I change my code to be more efficient for larger lists and small intervals. The firstlist can be generated as below:



      firstlist = Table[0.4 i, N@Cos[[Pi]/7 i], i, 1, 30];


      However, my list is different from this one.







      list-manipulation differential-equations






      share|improve this question
















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 15 at 13:43







      Inzo Babaria

















      asked Jun 14 at 6:44









      Inzo BabariaInzo Babaria

      5422 silver badges9 bronze badges




      5422 silver badges9 bronze badges























          3 Answers
          3






          active

          oldest

          votes


















          6
















          $begingroup$

          You can use the two argument form of Differences (Differences[list, n]) to get nth differences:



          Transpose[firstlist[[2 ;; -2, 1]], 
          Differences[firstlist[[All, 2]], 2] / 2 / -1, 1.firstlist[[1, 2, 1]]]

          % == diff2list



          True




          Or make it a function:



          ClearAll[diF2]
          diF2 = Transpose[Most@Rest@#, Differences[#2, 2]/2/(#[[2]] - #[[1]]) & @@ Transpose@#] &;

          diF2 @ firstlist == diff2list



          True




          You can also use the following alternatives to Differences[lst, 2]:



          ListConvolve[1., -2., 1., lst]
          ListCorrelate[1., -2., 1., lst]
          1, -2, 1.lst[[1 ;; -3]], lst[[2 ;; -2]], lst[[3 ;; -1]]





          share|improve this answer












          $endgroup$






















            5
















            $begingroup$

            I'm pretty sure you've been trained in a language other than Mathematica because your style and syntax take advantage of none of Mathematica's unique power:



            myList = RandomInteger[0, 5, 10];

            diff2 = Differences[myList];

            diff3 = Differences[diff2]





            share|improve this answer












            $endgroup$














            • $begingroup$
              You are completely right
              $endgroup$
              – Inzo Babaria
              Jun 14 at 7:21










            • $begingroup$
              Or Nest[Differences, myList, 2] if you want to do it all at once.
              $endgroup$
              – Michael Seifert
              Jun 14 at 18:54


















            5
















            $begingroup$

            If you have to take the differences often, it may pay off to assemble a SparseArray that does it for you:



            n = 1000000;
            A = Plus[
            DiagonalMatrix[SparseArray@ConstantArray[-2., n]],
            DiagonalMatrix[SparseArray@ConstantArray[1., n - 1], 1],
            DiagonalMatrix[SparseArray@ConstantArray[1., n - 1], -1]
            ][[2 ;; -2]];

            myList = RandomReal[-1, 1, n];
            a = Differences[myList, 2]; // RepeatedTiming // First
            b = Subtract[myList[[1 ;; -3]] + myList[[3 ;; -1]], 2. myList[[2 ;; -2]]]; // RepeatedTiming // First
            c = A.myList; // RepeatedTiming // First
            a == b == c



            0.0268



            0.0064



            0.0038



            True




            I also suggest to keep the list of x- and y-values apart as two vectors of length $n$ (rather than muddling them to gether to a $n times 2$ matrix); that will quicken the read operations that you will most likely perform on the data.






            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%2f200337%2fnumerical-second-order-differentiation%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown


























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              6
















              $begingroup$

              You can use the two argument form of Differences (Differences[list, n]) to get nth differences:



              Transpose[firstlist[[2 ;; -2, 1]], 
              Differences[firstlist[[All, 2]], 2] / 2 / -1, 1.firstlist[[1, 2, 1]]]

              % == diff2list



              True




              Or make it a function:



              ClearAll[diF2]
              diF2 = Transpose[Most@Rest@#, Differences[#2, 2]/2/(#[[2]] - #[[1]]) & @@ Transpose@#] &;

              diF2 @ firstlist == diff2list



              True




              You can also use the following alternatives to Differences[lst, 2]:



              ListConvolve[1., -2., 1., lst]
              ListCorrelate[1., -2., 1., lst]
              1, -2, 1.lst[[1 ;; -3]], lst[[2 ;; -2]], lst[[3 ;; -1]]





              share|improve this answer












              $endgroup$



















                6
















                $begingroup$

                You can use the two argument form of Differences (Differences[list, n]) to get nth differences:



                Transpose[firstlist[[2 ;; -2, 1]], 
                Differences[firstlist[[All, 2]], 2] / 2 / -1, 1.firstlist[[1, 2, 1]]]

                % == diff2list



                True




                Or make it a function:



                ClearAll[diF2]
                diF2 = Transpose[Most@Rest@#, Differences[#2, 2]/2/(#[[2]] - #[[1]]) & @@ Transpose@#] &;

                diF2 @ firstlist == diff2list



                True




                You can also use the following alternatives to Differences[lst, 2]:



                ListConvolve[1., -2., 1., lst]
                ListCorrelate[1., -2., 1., lst]
                1, -2, 1.lst[[1 ;; -3]], lst[[2 ;; -2]], lst[[3 ;; -1]]





                share|improve this answer












                $endgroup$

















                  6














                  6










                  6







                  $begingroup$

                  You can use the two argument form of Differences (Differences[list, n]) to get nth differences:



                  Transpose[firstlist[[2 ;; -2, 1]], 
                  Differences[firstlist[[All, 2]], 2] / 2 / -1, 1.firstlist[[1, 2, 1]]]

                  % == diff2list



                  True




                  Or make it a function:



                  ClearAll[diF2]
                  diF2 = Transpose[Most@Rest@#, Differences[#2, 2]/2/(#[[2]] - #[[1]]) & @@ Transpose@#] &;

                  diF2 @ firstlist == diff2list



                  True




                  You can also use the following alternatives to Differences[lst, 2]:



                  ListConvolve[1., -2., 1., lst]
                  ListCorrelate[1., -2., 1., lst]
                  1, -2, 1.lst[[1 ;; -3]], lst[[2 ;; -2]], lst[[3 ;; -1]]





                  share|improve this answer












                  $endgroup$



                  You can use the two argument form of Differences (Differences[list, n]) to get nth differences:



                  Transpose[firstlist[[2 ;; -2, 1]], 
                  Differences[firstlist[[All, 2]], 2] / 2 / -1, 1.firstlist[[1, 2, 1]]]

                  % == diff2list



                  True




                  Or make it a function:



                  ClearAll[diF2]
                  diF2 = Transpose[Most@Rest@#, Differences[#2, 2]/2/(#[[2]] - #[[1]]) & @@ Transpose@#] &;

                  diF2 @ firstlist == diff2list



                  True




                  You can also use the following alternatives to Differences[lst, 2]:



                  ListConvolve[1., -2., 1., lst]
                  ListCorrelate[1., -2., 1., lst]
                  1, -2, 1.lst[[1 ;; -3]], lst[[2 ;; -2]], lst[[3 ;; -1]]






                  share|improve this answer















                  share|improve this answer




                  share|improve this answer








                  edited Jun 14 at 8:06

























                  answered Jun 14 at 6:59









                  kglrkglr

                  223k10 gold badges253 silver badges511 bronze badges




                  223k10 gold badges253 silver badges511 bronze badges


























                      5
















                      $begingroup$

                      I'm pretty sure you've been trained in a language other than Mathematica because your style and syntax take advantage of none of Mathematica's unique power:



                      myList = RandomInteger[0, 5, 10];

                      diff2 = Differences[myList];

                      diff3 = Differences[diff2]





                      share|improve this answer












                      $endgroup$














                      • $begingroup$
                        You are completely right
                        $endgroup$
                        – Inzo Babaria
                        Jun 14 at 7:21










                      • $begingroup$
                        Or Nest[Differences, myList, 2] if you want to do it all at once.
                        $endgroup$
                        – Michael Seifert
                        Jun 14 at 18:54















                      5
















                      $begingroup$

                      I'm pretty sure you've been trained in a language other than Mathematica because your style and syntax take advantage of none of Mathematica's unique power:



                      myList = RandomInteger[0, 5, 10];

                      diff2 = Differences[myList];

                      diff3 = Differences[diff2]





                      share|improve this answer












                      $endgroup$














                      • $begingroup$
                        You are completely right
                        $endgroup$
                        – Inzo Babaria
                        Jun 14 at 7:21










                      • $begingroup$
                        Or Nest[Differences, myList, 2] if you want to do it all at once.
                        $endgroup$
                        – Michael Seifert
                        Jun 14 at 18:54













                      5














                      5










                      5







                      $begingroup$

                      I'm pretty sure you've been trained in a language other than Mathematica because your style and syntax take advantage of none of Mathematica's unique power:



                      myList = RandomInteger[0, 5, 10];

                      diff2 = Differences[myList];

                      diff3 = Differences[diff2]





                      share|improve this answer












                      $endgroup$



                      I'm pretty sure you've been trained in a language other than Mathematica because your style and syntax take advantage of none of Mathematica's unique power:



                      myList = RandomInteger[0, 5, 10];

                      diff2 = Differences[myList];

                      diff3 = Differences[diff2]






                      share|improve this answer















                      share|improve this answer




                      share|improve this answer








                      edited Jun 14 at 7:01

























                      answered Jun 14 at 6:52









                      David G. StorkDavid G. Stork

                      26.4k2 gold badges24 silver badges59 bronze badges




                      26.4k2 gold badges24 silver badges59 bronze badges














                      • $begingroup$
                        You are completely right
                        $endgroup$
                        – Inzo Babaria
                        Jun 14 at 7:21










                      • $begingroup$
                        Or Nest[Differences, myList, 2] if you want to do it all at once.
                        $endgroup$
                        – Michael Seifert
                        Jun 14 at 18:54
















                      • $begingroup$
                        You are completely right
                        $endgroup$
                        – Inzo Babaria
                        Jun 14 at 7:21










                      • $begingroup$
                        Or Nest[Differences, myList, 2] if you want to do it all at once.
                        $endgroup$
                        – Michael Seifert
                        Jun 14 at 18:54















                      $begingroup$
                      You are completely right
                      $endgroup$
                      – Inzo Babaria
                      Jun 14 at 7:21




                      $begingroup$
                      You are completely right
                      $endgroup$
                      – Inzo Babaria
                      Jun 14 at 7:21












                      $begingroup$
                      Or Nest[Differences, myList, 2] if you want to do it all at once.
                      $endgroup$
                      – Michael Seifert
                      Jun 14 at 18:54




                      $begingroup$
                      Or Nest[Differences, myList, 2] if you want to do it all at once.
                      $endgroup$
                      – Michael Seifert
                      Jun 14 at 18:54











                      5
















                      $begingroup$

                      If you have to take the differences often, it may pay off to assemble a SparseArray that does it for you:



                      n = 1000000;
                      A = Plus[
                      DiagonalMatrix[SparseArray@ConstantArray[-2., n]],
                      DiagonalMatrix[SparseArray@ConstantArray[1., n - 1], 1],
                      DiagonalMatrix[SparseArray@ConstantArray[1., n - 1], -1]
                      ][[2 ;; -2]];

                      myList = RandomReal[-1, 1, n];
                      a = Differences[myList, 2]; // RepeatedTiming // First
                      b = Subtract[myList[[1 ;; -3]] + myList[[3 ;; -1]], 2. myList[[2 ;; -2]]]; // RepeatedTiming // First
                      c = A.myList; // RepeatedTiming // First
                      a == b == c



                      0.0268



                      0.0064



                      0.0038



                      True




                      I also suggest to keep the list of x- and y-values apart as two vectors of length $n$ (rather than muddling them to gether to a $n times 2$ matrix); that will quicken the read operations that you will most likely perform on the data.






                      share|improve this answer












                      $endgroup$



















                        5
















                        $begingroup$

                        If you have to take the differences often, it may pay off to assemble a SparseArray that does it for you:



                        n = 1000000;
                        A = Plus[
                        DiagonalMatrix[SparseArray@ConstantArray[-2., n]],
                        DiagonalMatrix[SparseArray@ConstantArray[1., n - 1], 1],
                        DiagonalMatrix[SparseArray@ConstantArray[1., n - 1], -1]
                        ][[2 ;; -2]];

                        myList = RandomReal[-1, 1, n];
                        a = Differences[myList, 2]; // RepeatedTiming // First
                        b = Subtract[myList[[1 ;; -3]] + myList[[3 ;; -1]], 2. myList[[2 ;; -2]]]; // RepeatedTiming // First
                        c = A.myList; // RepeatedTiming // First
                        a == b == c



                        0.0268



                        0.0064



                        0.0038



                        True




                        I also suggest to keep the list of x- and y-values apart as two vectors of length $n$ (rather than muddling them to gether to a $n times 2$ matrix); that will quicken the read operations that you will most likely perform on the data.






                        share|improve this answer












                        $endgroup$

















                          5














                          5










                          5







                          $begingroup$

                          If you have to take the differences often, it may pay off to assemble a SparseArray that does it for you:



                          n = 1000000;
                          A = Plus[
                          DiagonalMatrix[SparseArray@ConstantArray[-2., n]],
                          DiagonalMatrix[SparseArray@ConstantArray[1., n - 1], 1],
                          DiagonalMatrix[SparseArray@ConstantArray[1., n - 1], -1]
                          ][[2 ;; -2]];

                          myList = RandomReal[-1, 1, n];
                          a = Differences[myList, 2]; // RepeatedTiming // First
                          b = Subtract[myList[[1 ;; -3]] + myList[[3 ;; -1]], 2. myList[[2 ;; -2]]]; // RepeatedTiming // First
                          c = A.myList; // RepeatedTiming // First
                          a == b == c



                          0.0268



                          0.0064



                          0.0038



                          True




                          I also suggest to keep the list of x- and y-values apart as two vectors of length $n$ (rather than muddling them to gether to a $n times 2$ matrix); that will quicken the read operations that you will most likely perform on the data.






                          share|improve this answer












                          $endgroup$



                          If you have to take the differences often, it may pay off to assemble a SparseArray that does it for you:



                          n = 1000000;
                          A = Plus[
                          DiagonalMatrix[SparseArray@ConstantArray[-2., n]],
                          DiagonalMatrix[SparseArray@ConstantArray[1., n - 1], 1],
                          DiagonalMatrix[SparseArray@ConstantArray[1., n - 1], -1]
                          ][[2 ;; -2]];

                          myList = RandomReal[-1, 1, n];
                          a = Differences[myList, 2]; // RepeatedTiming // First
                          b = Subtract[myList[[1 ;; -3]] + myList[[3 ;; -1]], 2. myList[[2 ;; -2]]]; // RepeatedTiming // First
                          c = A.myList; // RepeatedTiming // First
                          a == b == c



                          0.0268



                          0.0064



                          0.0038



                          True




                          I also suggest to keep the list of x- and y-values apart as two vectors of length $n$ (rather than muddling them to gether to a $n times 2$ matrix); that will quicken the read operations that you will most likely perform on the data.







                          share|improve this answer















                          share|improve this answer




                          share|improve this answer








                          edited Jun 14 at 9:24

























                          answered Jun 14 at 7:24









                          Henrik SchumacherHenrik Schumacher

                          69.6k6 gold badges104 silver badges194 bronze badges




                          69.6k6 gold badges104 silver badges194 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%2f200337%2fnumerical-second-order-differentiation%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

                              Tamil (spriik) Luke uk diar | Nawigatjuun

                              Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

                              Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?