Is this the correct way to implement matrix?











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?



Thanks.



Code:



#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()
{

}









share|improve this question


























    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?



    Thanks.



    Code:



    #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()
    {

    }









    share|improve this question
























      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?



      Thanks.



      Code:



      #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()
      {

      }









      share|improve this question













      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?



      Thanks.



      Code:



      #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 machine-learning neural-network






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 13 mins ago









      kimchiboy03

      475




      475



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

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

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: 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/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%2fcodereview.stackexchange.com%2fquestions%2f208428%2fis-this-the-correct-way-to-implement-matrix%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208428%2fis-this-the-correct-way-to-implement-matrix%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python