Test Driven Development in Linear Algebra
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
add a comment |
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
add a comment |
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
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
testing tdd linear-algebra linear scientific-computing
asked Oct 31 '18 at 8:38
Danny Potter
63
63
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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.
add a comment |
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.
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
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Oct 31 '18 at 11:58
VoiceOfUnreason
19.6k21847
19.6k21847
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 26 '18 at 12:55
Danny Potter
63
63
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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