Solved

2x2 matrix math class

Posted on 2004-04-05
4
743 Views
Last Modified: 2008-02-01
#ifndef MAT_H
#define MAT_H

// 2x2 Matrix Math

class Matrix {

public:

      Matrix(int=1, int=0, int=0, int=1);
      Matrix(const Matrix &m);
      Matrix &operator = (const Matrix &m);
      Matrix matriAdd(const Matrix &m) const;
      Matrix matriSub(const Matrix &m) const;
      Matrix matriMul(const Matrix &m) const;
                Matrix matriDiv(const Matrix &m) const;
      int getElement(void);
      void setElement(void);
      double &operator () int i, int j);
      void matriPrint() const;
        void matriClear() const;

private:

      int width;
      int height;
      
      double *data;

};

#endif#include <iostream>

using namespace std;

#include "mat.h"

Matrix::Matrix() {width=0; height=0; data=0;}

Matrix::Matrix(intw, int h) {

      width = w;
      height = h;

      date = new double[width*height];

}

Matrix::Matrix(const Matrix &m) {

      width = m.width;
      height = m.height;

      date = new double[width*height];

      for(int k=0; k < width*height; k++)
         data[k] = m.data[k];

}

Matrix::~Matrix() {

      delete[] data;

}

bool Matrix::operator == (const Matrix &m) const {

      if(width != m.width || height != m.height)

      return false;

      for(int k=0; k < width*height; k++)
         if(data[k] != m.data[k])
            return false;

      return true;

}

Matrix &Matrix::operator = (const Matrix &m) {

      if(&m != this) {
        delete[] data;

        width = m.width; height = m.height;
        data = new double[width*height];

        for(int k=0; k < width*height; k++)
           data[k] = m.data[k];

      }

      return *this;

}

Matrix Matrix::operator+(const Matrix &m) const {

      if(width != m.width; || height != m.height) {
        cerr << "Size error!\n";
      
      abort();

}

Matrix result(width, height);

      for(int k=0; k < width*height; k++)
         result.data[k] = data[k] + m.data[k];

      return result;

}

Matrix Matrix::operator*(const Matrix &m) const {

      if(width != m.height) {
        cerr << "Size error!\n";

      abort();

      }

      Matrix result(m.width, height);

            for(int i=0; i < m.width; i++0
               for(int j=0; j < height; j++) {
                  double s = 0;

                  for(int k=0; k < width; k++)
                        s += data[k+j*width]*m.data
                        [i + m.width*k];
                        result.data[i + m.width*j] = s;

               }

      return result;

}

double &Matrix::operator() (int i, int j) {

      if(i < 0 || i >= width || j < 0 || j >= height) {
        cerr << "Out of bounds!\n";

      abort();

      }

      return data[i + width*j];

}

void Matrix::print() const {

      for(int j=0; j < height; j++) {
         for(int i=0; i < width; i++)
            cout << " " << data[i + width * j];

      cout << "\n";

      }

}
               




#include <iostream>

using namespace std;

#include "mat.h"

int main(int argc, char **argv) {

      Matrix m(3,2), n(5,3);

      m(0,0) = 2.0; m(1,0) = 0.0; m(2,0) = 0.0;
      m(0,1) = 2.0; m(1,1) = 0.0; m(2,1) = 0.0;

      n(0,0) = 1.0; n(1,0) = 2.0; n(2,0) = 3.0; n(3,0)
      = 4.0; n(4,0) = 5.0;

      n(0,1) = 2.0; n(1,1) = 3.0; n(2,1) = 4.0; n(3,1)
      = 5.0; n(4,1) = 6.0;

      n(0,2) = 3.0; n(1,2) = 4.0; n(2,2) = 5.0; n(3,2)
      = 6.0; n(4,2) = 7.0;

      Matrix x = m*n;

      x.print();

}

I need to write a program that consists of a 2x2 array that uses member functions add, sub, mul, and div.  Need help, need program to follow the functions in the header.  As you can see, I am having trouble putting 2 and 2 together.  Please advise.  Thank you.  Del      
      



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
4 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 10762738
Is this homework?

If this is not homework, why re-invent the wheel.

Check out the following links:
Matrix class implemented using valarrays:
http://www.hep.ucl.ac.uk/atlas/atlfast/Doxygen/5.1.0/AtlfastUtils-00-00-10/Matrix_8h-source.html

blitz++:
http://www.oonumerics.org/blitz/ 

newmat:
http://www.robertnz.net/nm_intro.htm

Matrix Template Library (MTL):
http://www.osl.iu.edu/research/mtl/

boost libray multi-array class:
http://www.boost.org/libs/multi_array/doc/user.html
0
 
LVL 30

Expert Comment

by:Axter
ID: 10762744
Also

("C++ library for "tiny" vectors and matrices with expression templates")
http://tvmet.sourceforge.net/

("Numerical Matrix/Vector Classes in C++ ")
http://math.nist.gov/mv++

0
 
LVL 30

Accepted Solution

by:
Axter earned 500 total points
ID: 10762753
With all the availability of existing classes that have the functionality you're looking for, there's no real practical reason to start from scratch.

I recommend you use one of the above sources instead.
0
 

Author Comment

by:edelossantos
ID: 10821450
Yes, this is homework and this is what I have put together.  Need help in fine tunning the program so it will run.  Any ideas, please advise.  Del
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

739 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