Solved

multiplication and division for a 2x2 matrix

Posted on 2004-05-01
3
1,662 Views
Last Modified: 2008-02-01
#include <iostream>

using namespace std;

class Matrix {
     
public:
     Matrix(int val = 0) : nValue(val) { Initialise(); }
     ~Matrix() {};
     Matrix add(const Matrix& rhs);
     Matrix sub(const Matrix& rhs);
     Matrix mul(const Matrix& rhs);
     Matrix div(const Matrix& rhs);
     void print();
     void Initialise();
     void Clear();                        
private:
     int M[2][2];
     int nCols;
     int nRows;
     int size;
     int nValue;
};


void Matrix::Initialise()
{
     for (int r = 0; r < 2; r++) {
          for (int c = 0; c < 2; c++) {
               M[r][c] = nValue;
          }
     }
}

void Matrix::Clear()
{
     for (int r = 0; r < 2; r++) {
          for (int c = 0; c < 2; c++) {
               M[r][c] = 0;
          }
     }
}

Matrix Matrix::add(const Matrix& rhs)
{
     Matrix temp;

     for (int r = 0; r < 2; r++) {
          for (int c = 0; c < 2; c++) {
               temp.M[r][c] = rhs.M[r][c] + M[r][c];
          }
     }
     return temp;
}

Matrix Matrix::sub(const Matrix& rhs)
{
     Matrix temp;

     for (int r = 0; r < 2; r++) {
          for (int c = 0; c < 2; c++) {
               temp.M[r][c] = rhs.M[r][c] - M[r][c];
          }
     }
     return temp;
}

Matrix Matrix::mul(const Matrix& rhs)
{
     Matrix temp;

     for (int r = 0; r < 2; r++) {
          for (int c = 0; c < 2; c++) {
               temp.M[r][c] = rhs.M[r][c] * M[r][c];
          }
     }
     return temp;
}

Matrix Matrix::div(const Matrix& rhs)
{
     Matrix temp;

     for (int r = 0; r < 2; r++) {
          for (int c = 0; c < 2; c++) {
               temp.M[r][c] = rhs.M[r][c] / M[r][c];
          }
     }
     return temp;
}

void Matrix::print()
{
     for (int r = 0; r < 2; r++) {
          for (int c = 0; c < 2; c++) {
               cout << M[r][c] << "\t";
          }
          cout << endl;
     }
}


int main()
{
     Matrix a(2);
     Matrix b(3);
     Matrix c = a.add(b);
     c.print();

     Matrix d(2);
     Matrix e(3);
     Matrix f = a.sub(b);
     f.print();

     Matrix g(2);
     Matrix h(3);
     Matrix i = a.mul(b);
     i.print();

     Matrix j(2);
     Matrix k(3);
     Matrix l = a.div(b);
     l.print();
     
     return 0;

}

output:

5       5
5       5
1       1
1       1
6       6
6       6

How would you write the implementation for multiplication and division?  I have looked at some examples but cannot grasp the concept.  Please advise.
0
Comment
Question by:edelossantos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 
LVL 4

Assisted Solution

by:PerryDK
PerryDK earned 500 total points
ID: 10969766
Have a look at http://www.maths-help.co.uk/Knowldge/Alg/Matrices/Question.htm

Secondly I'm not real clear on your Martrix design.  Is this intended to be a class that can only handle 2x2 matricies?  Or is your Matrix class supposed to handle any r by c matrix?
0
 
LVL 4

Accepted Solution

by:
PerryDK earned 500 total points
ID: 10969777
Here is matrix multiplication for 2x2 matrix given the definition at http://www.maths-help.co.uk/Knowldge/Alg/Matrices/Question.htm

Matrix Matrix::mul(const Matrix& rhs)
{
  Matrix temp;

  for (int r = 0; r < 2; r++)
  {
    for (int c = 0; c < 2; c++)
    {
      temp.M[r][c] = 0;

      for(int x = 0; x < 2; x++
      {
        temp.M[r][c] += rhs.M[c][x] * M[x][c];
      }
    }
  }
  return temp;
}

For you Matrix division I'm not exactly sure what matrix division is unless you mean multiplying by the inverse of a matrix?
0
 
LVL 4

Assisted Solution

by:PerryDK
PerryDK earned 500 total points
ID: 10969781
temp.M[r][c] += rhs.M[c][x] * M[x][c];

Change this to

temp.M[r][c] += rhs.M[x][c] * M[c][x];
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ get user from AD  (VS6) 7 111
C++ standard library based binary archive format 6 112
Which Linux flavors will this run on? 6 116
what type of pointer is this pointer? 3 31
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question