Matrix arithmetic operations
up vote
0
down vote
favorite
I am currently attempting to implement Matrix Math for another project I am working on.
However, I am not sure whether this implementation will work. Can someone please tell me if there are any errors with my implementation?
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
typedef vector<vector<double> > Matrix;
Matrix add(Matrix a, Matrix b)
{
assert(a.size() == b.size() && a[0].size() == b[0].size());
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[i][j] + b[i][j];
}
}
return output;
}
Matrix subtract(Matrix a, Matrix b)
{
assert(a.size() == b.size() && a[0].size() == b[0].size());
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[i][j] - b[i][j];
}
}
return output;
}
Matrix multiply(Matrix a, double b)
{
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[i][j] * b;
}
}
return output;
}
Matrix multiply(Matrix a, Matrix b)
{
assert(a.size() == b.size() && a[0].size() == b[0].size());
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[i][j] * b[i][j];
}
}
return output;
}
Matrix dotProduct(Matrix a, Matrix b)
{
assert(a[0].size() == b.size());
int numRow = a.size(), numCol = b[0].size();
Matrix output(numRow, vector<double>(numCol, 0));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
for(unsigned int k = 0; k < a[0].size(); k++)
{
output[i][j] += a[i][k] * b[k][j];
}
}
}
return output;
}
Matrix transpose(Matrix a)
{
int numRow = a[0].size(), numCol = a.size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[j][i];
}
}
return output;
}
Matrix applyFunc(Matrix a, double (*f)(double))
{
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = (*f)(a[i][j]);
}
}
return output;
}
int main()
{
}
c++ c++11 matrix
add a comment |
up vote
0
down vote
favorite
I am currently attempting to implement Matrix Math for another project I am working on.
However, I am not sure whether this implementation will work. Can someone please tell me if there are any errors with my implementation?
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
typedef vector<vector<double> > Matrix;
Matrix add(Matrix a, Matrix b)
{
assert(a.size() == b.size() && a[0].size() == b[0].size());
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[i][j] + b[i][j];
}
}
return output;
}
Matrix subtract(Matrix a, Matrix b)
{
assert(a.size() == b.size() && a[0].size() == b[0].size());
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[i][j] - b[i][j];
}
}
return output;
}
Matrix multiply(Matrix a, double b)
{
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[i][j] * b;
}
}
return output;
}
Matrix multiply(Matrix a, Matrix b)
{
assert(a.size() == b.size() && a[0].size() == b[0].size());
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[i][j] * b[i][j];
}
}
return output;
}
Matrix dotProduct(Matrix a, Matrix b)
{
assert(a[0].size() == b.size());
int numRow = a.size(), numCol = b[0].size();
Matrix output(numRow, vector<double>(numCol, 0));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
for(unsigned int k = 0; k < a[0].size(); k++)
{
output[i][j] += a[i][k] * b[k][j];
}
}
}
return output;
}
Matrix transpose(Matrix a)
{
int numRow = a[0].size(), numCol = a.size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[j][i];
}
}
return output;
}
Matrix applyFunc(Matrix a, double (*f)(double))
{
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = (*f)(a[i][j]);
}
}
return output;
}
int main()
{
}
c++ c++11 matrix
1
I have rolled back your last edit. Please don't change or add to the code in your question after you have received answers. See What should I do when someone answers my question? Thank you.
– Martin R
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am currently attempting to implement Matrix Math for another project I am working on.
However, I am not sure whether this implementation will work. Can someone please tell me if there are any errors with my implementation?
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
typedef vector<vector<double> > Matrix;
Matrix add(Matrix a, Matrix b)
{
assert(a.size() == b.size() && a[0].size() == b[0].size());
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[i][j] + b[i][j];
}
}
return output;
}
Matrix subtract(Matrix a, Matrix b)
{
assert(a.size() == b.size() && a[0].size() == b[0].size());
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[i][j] - b[i][j];
}
}
return output;
}
Matrix multiply(Matrix a, double b)
{
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[i][j] * b;
}
}
return output;
}
Matrix multiply(Matrix a, Matrix b)
{
assert(a.size() == b.size() && a[0].size() == b[0].size());
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[i][j] * b[i][j];
}
}
return output;
}
Matrix dotProduct(Matrix a, Matrix b)
{
assert(a[0].size() == b.size());
int numRow = a.size(), numCol = b[0].size();
Matrix output(numRow, vector<double>(numCol, 0));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
for(unsigned int k = 0; k < a[0].size(); k++)
{
output[i][j] += a[i][k] * b[k][j];
}
}
}
return output;
}
Matrix transpose(Matrix a)
{
int numRow = a[0].size(), numCol = a.size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[j][i];
}
}
return output;
}
Matrix applyFunc(Matrix a, double (*f)(double))
{
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = (*f)(a[i][j]);
}
}
return output;
}
int main()
{
}
c++ c++11 matrix
I am currently attempting to implement Matrix Math for another project I am working on.
However, I am not sure whether this implementation will work. Can someone please tell me if there are any errors with my implementation?
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
typedef vector<vector<double> > Matrix;
Matrix add(Matrix a, Matrix b)
{
assert(a.size() == b.size() && a[0].size() == b[0].size());
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[i][j] + b[i][j];
}
}
return output;
}
Matrix subtract(Matrix a, Matrix b)
{
assert(a.size() == b.size() && a[0].size() == b[0].size());
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[i][j] - b[i][j];
}
}
return output;
}
Matrix multiply(Matrix a, double b)
{
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[i][j] * b;
}
}
return output;
}
Matrix multiply(Matrix a, Matrix b)
{
assert(a.size() == b.size() && a[0].size() == b[0].size());
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[i][j] * b[i][j];
}
}
return output;
}
Matrix dotProduct(Matrix a, Matrix b)
{
assert(a[0].size() == b.size());
int numRow = a.size(), numCol = b[0].size();
Matrix output(numRow, vector<double>(numCol, 0));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
for(unsigned int k = 0; k < a[0].size(); k++)
{
output[i][j] += a[i][k] * b[k][j];
}
}
}
return output;
}
Matrix transpose(Matrix a)
{
int numRow = a[0].size(), numCol = a.size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = a[j][i];
}
}
return output;
}
Matrix applyFunc(Matrix a, double (*f)(double))
{
int numRow = a.size(), numCol = a[0].size();
Matrix output(numRow, vector<double>(numCol));
for(int i = 0; i < numRow; i++)
{
for(int j = 0; j < numCol; j++)
{
output[i][j] = (*f)(a[i][j]);
}
}
return output;
}
int main()
{
}
c++ c++11 matrix
c++ c++11 matrix
edited yesterday
Martin R
15.4k12264
15.4k12264
asked 2 days ago
kimchiboy03
495
495
1
I have rolled back your last edit. Please don't change or add to the code in your question after you have received answers. See What should I do when someone answers my question? Thank you.
– Martin R
yesterday
add a comment |
1
I have rolled back your last edit. Please don't change or add to the code in your question after you have received answers. See What should I do when someone answers my question? Thank you.
– Martin R
yesterday
1
1
I have rolled back your last edit. Please don't change or add to the code in your question after you have received answers. See What should I do when someone answers my question? Thank you.
– Martin R
yesterday
I have rolled back your last edit. Please don't change or add to the code in your question after you have received answers. See What should I do when someone answers my question? Thank you.
– Martin R
yesterday
add a comment |
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
Overview
Sure this is one way to represent a Matrix
.
typedef vector<vector<double> > Matrix;
The problem here is that there is no enforcement that these are rectangular. Your code makes the assumption they are rectangles and things will go very wrong if the assumption is wrong.
You don't use encapsulation.
Matrix add(Matrix a, Matrix b)
Matrix subtract(Matrix a, Matrix b)
Matrix multiply(Matrix a, double b)
Matrix multiply(Matrix a, Matrix b)
Matrix dotProduct(Matrix a, Matrix b)
All these are standalone methods. Not an absolute no-no but using classes correctly you can enforce the rectangular size requirements (preferably at compile time) but you could do it at runtime. If you use these methods then these would normally be member functions.
Also these functions are just wrong:
Matrix multiply(Matrix a, Matrix b)
Matrix dotProduct(Matrix a, Matrix b)
Neither of these functions do what they advertise. You should check out wikipedia for the definitions.
Am I wrong by saying multiply() is multiplying element to element and dotProduct() is multiplying row by column?
– kimchiboy03
2 days ago
Instead should I change my code so that multiplyElements() is multiplying element to element and multiply() is multiplying row by column?
– kimchiboy03
2 days ago
@kimchiboy03: Yes. Matrix multiplication is more complicated and involves multiplication and addition of elements. This will change the dimensions of the Matrix. A dot product generates a scalar value.
– Martin York
2 days ago
2
en.wikipedia.org/wiki/Matrix_multiplication_algorithm
– Martin York
2 days ago
2
mathinsight.org/dot_product_matrix_notation
– Martin York
2 days ago
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Overview
Sure this is one way to represent a Matrix
.
typedef vector<vector<double> > Matrix;
The problem here is that there is no enforcement that these are rectangular. Your code makes the assumption they are rectangles and things will go very wrong if the assumption is wrong.
You don't use encapsulation.
Matrix add(Matrix a, Matrix b)
Matrix subtract(Matrix a, Matrix b)
Matrix multiply(Matrix a, double b)
Matrix multiply(Matrix a, Matrix b)
Matrix dotProduct(Matrix a, Matrix b)
All these are standalone methods. Not an absolute no-no but using classes correctly you can enforce the rectangular size requirements (preferably at compile time) but you could do it at runtime. If you use these methods then these would normally be member functions.
Also these functions are just wrong:
Matrix multiply(Matrix a, Matrix b)
Matrix dotProduct(Matrix a, Matrix b)
Neither of these functions do what they advertise. You should check out wikipedia for the definitions.
Am I wrong by saying multiply() is multiplying element to element and dotProduct() is multiplying row by column?
– kimchiboy03
2 days ago
Instead should I change my code so that multiplyElements() is multiplying element to element and multiply() is multiplying row by column?
– kimchiboy03
2 days ago
@kimchiboy03: Yes. Matrix multiplication is more complicated and involves multiplication and addition of elements. This will change the dimensions of the Matrix. A dot product generates a scalar value.
– Martin York
2 days ago
2
en.wikipedia.org/wiki/Matrix_multiplication_algorithm
– Martin York
2 days ago
2
mathinsight.org/dot_product_matrix_notation
– Martin York
2 days ago
|
show 1 more comment
up vote
4
down vote
accepted
Overview
Sure this is one way to represent a Matrix
.
typedef vector<vector<double> > Matrix;
The problem here is that there is no enforcement that these are rectangular. Your code makes the assumption they are rectangles and things will go very wrong if the assumption is wrong.
You don't use encapsulation.
Matrix add(Matrix a, Matrix b)
Matrix subtract(Matrix a, Matrix b)
Matrix multiply(Matrix a, double b)
Matrix multiply(Matrix a, Matrix b)
Matrix dotProduct(Matrix a, Matrix b)
All these are standalone methods. Not an absolute no-no but using classes correctly you can enforce the rectangular size requirements (preferably at compile time) but you could do it at runtime. If you use these methods then these would normally be member functions.
Also these functions are just wrong:
Matrix multiply(Matrix a, Matrix b)
Matrix dotProduct(Matrix a, Matrix b)
Neither of these functions do what they advertise. You should check out wikipedia for the definitions.
Am I wrong by saying multiply() is multiplying element to element and dotProduct() is multiplying row by column?
– kimchiboy03
2 days ago
Instead should I change my code so that multiplyElements() is multiplying element to element and multiply() is multiplying row by column?
– kimchiboy03
2 days ago
@kimchiboy03: Yes. Matrix multiplication is more complicated and involves multiplication and addition of elements. This will change the dimensions of the Matrix. A dot product generates a scalar value.
– Martin York
2 days ago
2
en.wikipedia.org/wiki/Matrix_multiplication_algorithm
– Martin York
2 days ago
2
mathinsight.org/dot_product_matrix_notation
– Martin York
2 days ago
|
show 1 more comment
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Overview
Sure this is one way to represent a Matrix
.
typedef vector<vector<double> > Matrix;
The problem here is that there is no enforcement that these are rectangular. Your code makes the assumption they are rectangles and things will go very wrong if the assumption is wrong.
You don't use encapsulation.
Matrix add(Matrix a, Matrix b)
Matrix subtract(Matrix a, Matrix b)
Matrix multiply(Matrix a, double b)
Matrix multiply(Matrix a, Matrix b)
Matrix dotProduct(Matrix a, Matrix b)
All these are standalone methods. Not an absolute no-no but using classes correctly you can enforce the rectangular size requirements (preferably at compile time) but you could do it at runtime. If you use these methods then these would normally be member functions.
Also these functions are just wrong:
Matrix multiply(Matrix a, Matrix b)
Matrix dotProduct(Matrix a, Matrix b)
Neither of these functions do what they advertise. You should check out wikipedia for the definitions.
Overview
Sure this is one way to represent a Matrix
.
typedef vector<vector<double> > Matrix;
The problem here is that there is no enforcement that these are rectangular. Your code makes the assumption they are rectangles and things will go very wrong if the assumption is wrong.
You don't use encapsulation.
Matrix add(Matrix a, Matrix b)
Matrix subtract(Matrix a, Matrix b)
Matrix multiply(Matrix a, double b)
Matrix multiply(Matrix a, Matrix b)
Matrix dotProduct(Matrix a, Matrix b)
All these are standalone methods. Not an absolute no-no but using classes correctly you can enforce the rectangular size requirements (preferably at compile time) but you could do it at runtime. If you use these methods then these would normally be member functions.
Also these functions are just wrong:
Matrix multiply(Matrix a, Matrix b)
Matrix dotProduct(Matrix a, Matrix b)
Neither of these functions do what they advertise. You should check out wikipedia for the definitions.
answered 2 days ago
Martin York
72k481254
72k481254
Am I wrong by saying multiply() is multiplying element to element and dotProduct() is multiplying row by column?
– kimchiboy03
2 days ago
Instead should I change my code so that multiplyElements() is multiplying element to element and multiply() is multiplying row by column?
– kimchiboy03
2 days ago
@kimchiboy03: Yes. Matrix multiplication is more complicated and involves multiplication and addition of elements. This will change the dimensions of the Matrix. A dot product generates a scalar value.
– Martin York
2 days ago
2
en.wikipedia.org/wiki/Matrix_multiplication_algorithm
– Martin York
2 days ago
2
mathinsight.org/dot_product_matrix_notation
– Martin York
2 days ago
|
show 1 more comment
Am I wrong by saying multiply() is multiplying element to element and dotProduct() is multiplying row by column?
– kimchiboy03
2 days ago
Instead should I change my code so that multiplyElements() is multiplying element to element and multiply() is multiplying row by column?
– kimchiboy03
2 days ago
@kimchiboy03: Yes. Matrix multiplication is more complicated and involves multiplication and addition of elements. This will change the dimensions of the Matrix. A dot product generates a scalar value.
– Martin York
2 days ago
2
en.wikipedia.org/wiki/Matrix_multiplication_algorithm
– Martin York
2 days ago
2
mathinsight.org/dot_product_matrix_notation
– Martin York
2 days ago
Am I wrong by saying multiply() is multiplying element to element and dotProduct() is multiplying row by column?
– kimchiboy03
2 days ago
Am I wrong by saying multiply() is multiplying element to element and dotProduct() is multiplying row by column?
– kimchiboy03
2 days ago
Instead should I change my code so that multiplyElements() is multiplying element to element and multiply() is multiplying row by column?
– kimchiboy03
2 days ago
Instead should I change my code so that multiplyElements() is multiplying element to element and multiply() is multiplying row by column?
– kimchiboy03
2 days ago
@kimchiboy03: Yes. Matrix multiplication is more complicated and involves multiplication and addition of elements. This will change the dimensions of the Matrix. A dot product generates a scalar value.
– Martin York
2 days ago
@kimchiboy03: Yes. Matrix multiplication is more complicated and involves multiplication and addition of elements. This will change the dimensions of the Matrix. A dot product generates a scalar value.
– Martin York
2 days ago
2
2
en.wikipedia.org/wiki/Matrix_multiplication_algorithm
– Martin York
2 days ago
en.wikipedia.org/wiki/Matrix_multiplication_algorithm
– Martin York
2 days ago
2
2
mathinsight.org/dot_product_matrix_notation
– Martin York
2 days ago
mathinsight.org/dot_product_matrix_notation
– Martin York
2 days ago
|
show 1 more comment
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%2fcodereview.stackexchange.com%2fquestions%2f208428%2fmatrix-arithmetic-operations%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
1
I have rolled back your last edit. Please don't change or add to the code in your question after you have received answers. See What should I do when someone answers my question? Thank you.
– Martin R
yesterday