Test Driven Development in Linear Algebra










0














I want to learn TDD for working in Linear Algebra (or basically with matrices) in scientific programming(quantum chemistry). However since intermediates are not known and the only thing sometimes known are the end results I dont know how to do it.

Can you show me how you would do TDD in the above example?



for i,j = start1:end1, k,l = start2:end2
A[i,j] = B[i,k] * C[k,l] * D[l,j]
end


with A being the new Matrix calculated in the function.

with B C D being predefined Matrices.

with start1, start2, end1, end2 being predefined integers.



In a usual program I have around 20 of these blocks. So I think I dont know anything about the new A.

Most bugs I have are simple typing errors like doing



B[k,i]


instead of the above.

The only debugging scheme I am able to do if the end result is wrong is to write everything twice and then checking both A and hope I have not done the same typing error twice.



THX for your help










share|improve this question


























    0














    I want to learn TDD for working in Linear Algebra (or basically with matrices) in scientific programming(quantum chemistry). However since intermediates are not known and the only thing sometimes known are the end results I dont know how to do it.

    Can you show me how you would do TDD in the above example?



    for i,j = start1:end1, k,l = start2:end2
    A[i,j] = B[i,k] * C[k,l] * D[l,j]
    end


    with A being the new Matrix calculated in the function.

    with B C D being predefined Matrices.

    with start1, start2, end1, end2 being predefined integers.



    In a usual program I have around 20 of these blocks. So I think I dont know anything about the new A.

    Most bugs I have are simple typing errors like doing



    B[k,i]


    instead of the above.

    The only debugging scheme I am able to do if the end result is wrong is to write everything twice and then checking both A and hope I have not done the same typing error twice.



    THX for your help










    share|improve this question
























      0












      0








      0







      I want to learn TDD for working in Linear Algebra (or basically with matrices) in scientific programming(quantum chemistry). However since intermediates are not known and the only thing sometimes known are the end results I dont know how to do it.

      Can you show me how you would do TDD in the above example?



      for i,j = start1:end1, k,l = start2:end2
      A[i,j] = B[i,k] * C[k,l] * D[l,j]
      end


      with A being the new Matrix calculated in the function.

      with B C D being predefined Matrices.

      with start1, start2, end1, end2 being predefined integers.



      In a usual program I have around 20 of these blocks. So I think I dont know anything about the new A.

      Most bugs I have are simple typing errors like doing



      B[k,i]


      instead of the above.

      The only debugging scheme I am able to do if the end result is wrong is to write everything twice and then checking both A and hope I have not done the same typing error twice.



      THX for your help










      share|improve this question













      I want to learn TDD for working in Linear Algebra (or basically with matrices) in scientific programming(quantum chemistry). However since intermediates are not known and the only thing sometimes known are the end results I dont know how to do it.

      Can you show me how you would do TDD in the above example?



      for i,j = start1:end1, k,l = start2:end2
      A[i,j] = B[i,k] * C[k,l] * D[l,j]
      end


      with A being the new Matrix calculated in the function.

      with B C D being predefined Matrices.

      with start1, start2, end1, end2 being predefined integers.



      In a usual program I have around 20 of these blocks. So I think I dont know anything about the new A.

      Most bugs I have are simple typing errors like doing



      B[k,i]


      instead of the above.

      The only debugging scheme I am able to do if the end result is wrong is to write everything twice and then checking both A and hope I have not done the same typing error twice.



      THX for your help







      testing tdd linear-algebra linear scientific-computing






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Oct 31 '18 at 8:38









      Danny Potter

      63




      63






















          3 Answers
          3






          active

          oldest

          votes


















          0















          Can you show me how you would do TDD in the above example?




          The good news is that you are fundamentally dealing with a function.



          A = function(B,C,D,start1,end1,start2,end2)


          There are two general approaches that I see to introducing tests



          1) start with simple problems - one by one matrices, unit matrices; each time you work out what the answer should be, and check that the correct answer is produced by the function



          2) start with simple validations - you might start by ensuring that the answer has the right number of rows and columns, that doubling one of the inputs doubles the output, and so on. Nat Pryce's demonstration of the Diamond Kata should give you a sense for how this might work.






          share|improve this answer




























            0














            A technique I've used to test matrix multiplication routines is to use matrices with just one 1, all else being zero.
            Let E(i,j) be the matrix



            E(i,j)[k,l] = delta(i,k)*delta(j,l)


            (ie has a 1 at [i,j] and zeroes elsewhere)
            A bit of algebra shows that



            E(i,j)*E(i',j') = delta(j,i')*E(i,j')


            The test is to check that the multiplication routine gets the write answer for all pow(n,4) tuples i,j,i',j' with nxn matrices.






            share|improve this answer




















            • So basically you check what happens if you have a matrix representing a kronecker delta as input? I will try that one
              – Danny Potter
              Nov 12 '18 at 16:33










            • @DannyPotter Yes, except that you make all the matrices 'kronecker deltas' and check the results for all possible combinations. If you can convince yourself that your function is linear in each arguent (as yours clearly is) then since every matrix can be written as a linear combination of the E matrices above, you have good evidence that your function is correct
              – dmuir
              Nov 12 '18 at 17:11


















            0














            A Friend of mine showed me how to test these Functions.

            It is done by mocking the matrices.

            You can imagine



            B[i,k] 


            as



            function B(i,k): 
            return B[i,k]


            Now you can test these by creating a list which tracks the inputs.

            This gives the following function.



            function B(i,k):
            list.append(i,k)
            return B[i,k]


            This list can then be tested on which index is the faster one. This depends on column or row first and how you define your loop but you will either get something like:



            1 1
            1 2
            1 3
            2 1
            2 2
            2 3
            3 1
            3 2
            3 3


            or



            1 1
            2 1
            3 1
            1 2
            2 2
            3 2
            1 3
            2 3
            3 3


            Now B[i,k] and B[k,i] in the function of the question will produce different lists therefor you can detect the error and so you can test them.






            share|improve this answer




















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              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: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              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%2fstackoverflow.com%2fquestions%2f53079296%2ftest-driven-development-in-linear-algebra%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









              0















              Can you show me how you would do TDD in the above example?




              The good news is that you are fundamentally dealing with a function.



              A = function(B,C,D,start1,end1,start2,end2)


              There are two general approaches that I see to introducing tests



              1) start with simple problems - one by one matrices, unit matrices; each time you work out what the answer should be, and check that the correct answer is produced by the function



              2) start with simple validations - you might start by ensuring that the answer has the right number of rows and columns, that doubling one of the inputs doubles the output, and so on. Nat Pryce's demonstration of the Diamond Kata should give you a sense for how this might work.






              share|improve this answer

























                0















                Can you show me how you would do TDD in the above example?




                The good news is that you are fundamentally dealing with a function.



                A = function(B,C,D,start1,end1,start2,end2)


                There are two general approaches that I see to introducing tests



                1) start with simple problems - one by one matrices, unit matrices; each time you work out what the answer should be, and check that the correct answer is produced by the function



                2) start with simple validations - you might start by ensuring that the answer has the right number of rows and columns, that doubling one of the inputs doubles the output, and so on. Nat Pryce's demonstration of the Diamond Kata should give you a sense for how this might work.






                share|improve this answer























                  0












                  0








                  0







                  Can you show me how you would do TDD in the above example?




                  The good news is that you are fundamentally dealing with a function.



                  A = function(B,C,D,start1,end1,start2,end2)


                  There are two general approaches that I see to introducing tests



                  1) start with simple problems - one by one matrices, unit matrices; each time you work out what the answer should be, and check that the correct answer is produced by the function



                  2) start with simple validations - you might start by ensuring that the answer has the right number of rows and columns, that doubling one of the inputs doubles the output, and so on. Nat Pryce's demonstration of the Diamond Kata should give you a sense for how this might work.






                  share|improve this answer













                  Can you show me how you would do TDD in the above example?




                  The good news is that you are fundamentally dealing with a function.



                  A = function(B,C,D,start1,end1,start2,end2)


                  There are two general approaches that I see to introducing tests



                  1) start with simple problems - one by one matrices, unit matrices; each time you work out what the answer should be, and check that the correct answer is produced by the function



                  2) start with simple validations - you might start by ensuring that the answer has the right number of rows and columns, that doubling one of the inputs doubles the output, and so on. Nat Pryce's demonstration of the Diamond Kata should give you a sense for how this might work.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 31 '18 at 11:58









                  VoiceOfUnreason

                  19.6k21847




                  19.6k21847























                      0














                      A technique I've used to test matrix multiplication routines is to use matrices with just one 1, all else being zero.
                      Let E(i,j) be the matrix



                      E(i,j)[k,l] = delta(i,k)*delta(j,l)


                      (ie has a 1 at [i,j] and zeroes elsewhere)
                      A bit of algebra shows that



                      E(i,j)*E(i',j') = delta(j,i')*E(i,j')


                      The test is to check that the multiplication routine gets the write answer for all pow(n,4) tuples i,j,i',j' with nxn matrices.






                      share|improve this answer




















                      • So basically you check what happens if you have a matrix representing a kronecker delta as input? I will try that one
                        – Danny Potter
                        Nov 12 '18 at 16:33










                      • @DannyPotter Yes, except that you make all the matrices 'kronecker deltas' and check the results for all possible combinations. If you can convince yourself that your function is linear in each arguent (as yours clearly is) then since every matrix can be written as a linear combination of the E matrices above, you have good evidence that your function is correct
                        – dmuir
                        Nov 12 '18 at 17:11















                      0














                      A technique I've used to test matrix multiplication routines is to use matrices with just one 1, all else being zero.
                      Let E(i,j) be the matrix



                      E(i,j)[k,l] = delta(i,k)*delta(j,l)


                      (ie has a 1 at [i,j] and zeroes elsewhere)
                      A bit of algebra shows that



                      E(i,j)*E(i',j') = delta(j,i')*E(i,j')


                      The test is to check that the multiplication routine gets the write answer for all pow(n,4) tuples i,j,i',j' with nxn matrices.






                      share|improve this answer




















                      • So basically you check what happens if you have a matrix representing a kronecker delta as input? I will try that one
                        – Danny Potter
                        Nov 12 '18 at 16:33










                      • @DannyPotter Yes, except that you make all the matrices 'kronecker deltas' and check the results for all possible combinations. If you can convince yourself that your function is linear in each arguent (as yours clearly is) then since every matrix can be written as a linear combination of the E matrices above, you have good evidence that your function is correct
                        – dmuir
                        Nov 12 '18 at 17:11













                      0












                      0








                      0






                      A technique I've used to test matrix multiplication routines is to use matrices with just one 1, all else being zero.
                      Let E(i,j) be the matrix



                      E(i,j)[k,l] = delta(i,k)*delta(j,l)


                      (ie has a 1 at [i,j] and zeroes elsewhere)
                      A bit of algebra shows that



                      E(i,j)*E(i',j') = delta(j,i')*E(i,j')


                      The test is to check that the multiplication routine gets the write answer for all pow(n,4) tuples i,j,i',j' with nxn matrices.






                      share|improve this answer












                      A technique I've used to test matrix multiplication routines is to use matrices with just one 1, all else being zero.
                      Let E(i,j) be the matrix



                      E(i,j)[k,l] = delta(i,k)*delta(j,l)


                      (ie has a 1 at [i,j] and zeroes elsewhere)
                      A bit of algebra shows that



                      E(i,j)*E(i',j') = delta(j,i')*E(i,j')


                      The test is to check that the multiplication routine gets the write answer for all pow(n,4) tuples i,j,i',j' with nxn matrices.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 10 '18 at 14:00









                      dmuir

                      1,642186




                      1,642186











                      • So basically you check what happens if you have a matrix representing a kronecker delta as input? I will try that one
                        – Danny Potter
                        Nov 12 '18 at 16:33










                      • @DannyPotter Yes, except that you make all the matrices 'kronecker deltas' and check the results for all possible combinations. If you can convince yourself that your function is linear in each arguent (as yours clearly is) then since every matrix can be written as a linear combination of the E matrices above, you have good evidence that your function is correct
                        – dmuir
                        Nov 12 '18 at 17:11
















                      • So basically you check what happens if you have a matrix representing a kronecker delta as input? I will try that one
                        – Danny Potter
                        Nov 12 '18 at 16:33










                      • @DannyPotter Yes, except that you make all the matrices 'kronecker deltas' and check the results for all possible combinations. If you can convince yourself that your function is linear in each arguent (as yours clearly is) then since every matrix can be written as a linear combination of the E matrices above, you have good evidence that your function is correct
                        – dmuir
                        Nov 12 '18 at 17:11















                      So basically you check what happens if you have a matrix representing a kronecker delta as input? I will try that one
                      – Danny Potter
                      Nov 12 '18 at 16:33




                      So basically you check what happens if you have a matrix representing a kronecker delta as input? I will try that one
                      – Danny Potter
                      Nov 12 '18 at 16:33












                      @DannyPotter Yes, except that you make all the matrices 'kronecker deltas' and check the results for all possible combinations. If you can convince yourself that your function is linear in each arguent (as yours clearly is) then since every matrix can be written as a linear combination of the E matrices above, you have good evidence that your function is correct
                      – dmuir
                      Nov 12 '18 at 17:11




                      @DannyPotter Yes, except that you make all the matrices 'kronecker deltas' and check the results for all possible combinations. If you can convince yourself that your function is linear in each arguent (as yours clearly is) then since every matrix can be written as a linear combination of the E matrices above, you have good evidence that your function is correct
                      – dmuir
                      Nov 12 '18 at 17:11











                      0














                      A Friend of mine showed me how to test these Functions.

                      It is done by mocking the matrices.

                      You can imagine



                      B[i,k] 


                      as



                      function B(i,k): 
                      return B[i,k]


                      Now you can test these by creating a list which tracks the inputs.

                      This gives the following function.



                      function B(i,k):
                      list.append(i,k)
                      return B[i,k]


                      This list can then be tested on which index is the faster one. This depends on column or row first and how you define your loop but you will either get something like:



                      1 1
                      1 2
                      1 3
                      2 1
                      2 2
                      2 3
                      3 1
                      3 2
                      3 3


                      or



                      1 1
                      2 1
                      3 1
                      1 2
                      2 2
                      3 2
                      1 3
                      2 3
                      3 3


                      Now B[i,k] and B[k,i] in the function of the question will produce different lists therefor you can detect the error and so you can test them.






                      share|improve this answer

























                        0














                        A Friend of mine showed me how to test these Functions.

                        It is done by mocking the matrices.

                        You can imagine



                        B[i,k] 


                        as



                        function B(i,k): 
                        return B[i,k]


                        Now you can test these by creating a list which tracks the inputs.

                        This gives the following function.



                        function B(i,k):
                        list.append(i,k)
                        return B[i,k]


                        This list can then be tested on which index is the faster one. This depends on column or row first and how you define your loop but you will either get something like:



                        1 1
                        1 2
                        1 3
                        2 1
                        2 2
                        2 3
                        3 1
                        3 2
                        3 3


                        or



                        1 1
                        2 1
                        3 1
                        1 2
                        2 2
                        3 2
                        1 3
                        2 3
                        3 3


                        Now B[i,k] and B[k,i] in the function of the question will produce different lists therefor you can detect the error and so you can test them.






                        share|improve this answer























                          0












                          0








                          0






                          A Friend of mine showed me how to test these Functions.

                          It is done by mocking the matrices.

                          You can imagine



                          B[i,k] 


                          as



                          function B(i,k): 
                          return B[i,k]


                          Now you can test these by creating a list which tracks the inputs.

                          This gives the following function.



                          function B(i,k):
                          list.append(i,k)
                          return B[i,k]


                          This list can then be tested on which index is the faster one. This depends on column or row first and how you define your loop but you will either get something like:



                          1 1
                          1 2
                          1 3
                          2 1
                          2 2
                          2 3
                          3 1
                          3 2
                          3 3


                          or



                          1 1
                          2 1
                          3 1
                          1 2
                          2 2
                          3 2
                          1 3
                          2 3
                          3 3


                          Now B[i,k] and B[k,i] in the function of the question will produce different lists therefor you can detect the error and so you can test them.






                          share|improve this answer












                          A Friend of mine showed me how to test these Functions.

                          It is done by mocking the matrices.

                          You can imagine



                          B[i,k] 


                          as



                          function B(i,k): 
                          return B[i,k]


                          Now you can test these by creating a list which tracks the inputs.

                          This gives the following function.



                          function B(i,k):
                          list.append(i,k)
                          return B[i,k]


                          This list can then be tested on which index is the faster one. This depends on column or row first and how you define your loop but you will either get something like:



                          1 1
                          1 2
                          1 3
                          2 1
                          2 2
                          2 3
                          3 1
                          3 2
                          3 3


                          or



                          1 1
                          2 1
                          3 1
                          1 2
                          2 2
                          3 2
                          1 3
                          2 3
                          3 3


                          Now B[i,k] and B[k,i] in the function of the question will produce different lists therefor you can detect the error and so you can test them.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 26 '18 at 12:55









                          Danny Potter

                          63




                          63



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • 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.

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





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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.

                              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%2fstackoverflow.com%2fquestions%2f53079296%2ftest-driven-development-in-linear-algebra%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

                              𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

                              Crossroads (UK TV series)

                              ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế