Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. matrix multiplication

matrix multiplication

Scheduled Pinned Locked Moved C / C++ / MFC
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    VeganFanatic
    wrote on last edited by
    #1

    This does not work as expected, for matrix * matrix matrix operator*(const matrix that) { // matrix product AB != BA matrix ret; ret.resize(this->data.size(), that.data.size()); for (int i = 0; i < this->data.size(); i++) { for (int j = 0; j < this.data[i].size(); j++) { for (int k = 0; k < this->data.size(); k++) { #pragma omp parallel for for (int l = 0; l < this.data[i].size(); l++) { ret[i][k] += this->data[i][k] * that.data[j][k]; } } } } return ret;

    http://www.contract-developer.tk

    D CPalliniC 2 Replies Last reply
    0
    • V VeganFanatic

      This does not work as expected, for matrix * matrix matrix operator*(const matrix that) { // matrix product AB != BA matrix ret; ret.resize(this->data.size(), that.data.size()); for (int i = 0; i < this->data.size(); i++) { for (int j = 0; j < this.data[i].size(); j++) { for (int k = 0; k < this->data.size(); k++) { #pragma omp parallel for for (int l = 0; l < this.data[i].size(); l++) { ret[i][k] += this->data[i][k] * that.data[j][k]; } } } } return ret;

      http://www.contract-developer.tk

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      So what exactly is the question? Have you used the debugger to step through the code? Have you removed the #pragma directive? Have you worked out on paper how to do the multiplication?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "Man who follows car will be exhausted." - Confucius

      V 1 Reply Last reply
      0
      • D David Crow

        So what exactly is the question? Have you used the debugger to step through the code? Have you removed the #pragma directive? Have you worked out on paper how to do the multiplication?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "Man who follows car will be exhausted." - Confucius

        V Offline
        V Offline
        VeganFanatic
        wrote on last edited by
        #3

        The result from a test != book

        http://www.contract-developer.tk

        1 Reply Last reply
        0
        • V VeganFanatic

          This does not work as expected, for matrix * matrix matrix operator*(const matrix that) { // matrix product AB != BA matrix ret; ret.resize(this->data.size(), that.data.size()); for (int i = 0; i < this->data.size(); i++) { for (int j = 0; j < this.data[i].size(); j++) { for (int k = 0; k < this->data.size(); k++) { #pragma omp parallel for for (int l = 0; l < this.data[i].size(); l++) { ret[i][k] += this->data[i][k] * that.data[j][k]; } } } } return ret;

          http://www.contract-developer.tk

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          VeganFanatic wrote:

          for (int i = 0; i < this->data.size(); i++) { for (int j = 0; j < this.data[i].size(); j++) { for (int k = 0; k < this->data.size(); k++) { #pragma omp parallel for for (int l = 0; l < this.data[i].size(); l++) { ret[i][k] += this->data[i][k] * that.data[j][k]; } } } }

          AFAIK it should be

          for (int i = 0; i < this->rows; i++)
          for (int j = 0; j < that->cols; j++)
          {
          ret[i][j] = .0; // assuming your elements are doubles...
          for (int k = 0; k < this->cols; k++) //this->cols must be equal to that->rows
          ret[i][j] += this->data[i][k] * that.data[k][j];
          }

          Why do you need the further loop? :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups