Multipling and taking the inverse of Matrices with C++

Sep 22, 2011   //   by Admin   //   C++  //  No Comments

This program was written to show several different matrix math functions. Please note that the inverse function was written assuming that the matrix given is homogeneous.

// This is a sample C++ program which will ask the user for a 4X4 matrix,
// call a function to compute Matrix A*B, their it’s transpose,
// Matrix A * Point Puvw, the inverse of Matrix A, and output the result.
#include <iostream>     // Include the standard C++ IO library functions
#include <cstdlib>       // For exit() prototype
using namespace std;//================Matrix class=================================
class Point;

class Matrix44 {
private:
double element[4][4];
friend class Point;
public:
Matrix44(void);
Matrix44 transpose(void) const;
Matrix44 inverse(Matrix44 x) const;
friend istream& operator>>(istream& s, Matrix44& t);
friend ostream& operator<<(ostream& s, const Matrix44& t);
Matrix44 operator *(Matrix44 b);
Point operator*(const Point & P);
};

//Point class
class Point {
double element[4];
friend class Matrix44;
public:
friend istream& operator>>(istream& s, Point& p);
friend ostream& operator<<(ostream& s, const Point& p);
Point(void) {
element[0] = element[1] = element[2] = 0;
element[3] = 1;
}
Point(double x, double y, double z){
element [0]=x;
element [1]=y;
element [2]=z;
element [3]=1;
}

};

//====================Matrix44 Class functions==========================

Matrix44::Matrix44(void) {
for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
if (i==j) element[i][j] = 1.;
else      element[i][j] = 0.;
}
//User Input
istream& operator>> (istream& s, Matrix44& t) {
for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
s >> t.element[i][j];
if (!s) { cerr << “Error reading Matrix from stream”;  exit(0); }
return s;
}
//User Output
ostream& operator<< (ostream& s, const Matrix44& t) {
for (int i=0; i<4; i++) {
for (int j=0; j<4; j++)
s << t.element[i][j] << ”   “;
s << endl;
}
if (!s) { cerr << “Error writing Matrix to stream”;  exit(0); }
return s;
}

//Math Functions================================================================
//Multiply
Matrix44 Matrix44::operator*(Matrix44 b) {
Matrix44 result;
for (int i = 0; i < 4; i++) {
for (int k = 0; k < 4; k++) {
result.element[i][k] = 0;
for (int j = 0; j < 4; j++) {
result.element[i][k] = element[i][j] * b.element[j][k] + result.element[i][k];
}
}
}
return result;
}

//Transpose
Matrix44 Matrix44::transpose(void) const {
Matrix44 result;
for (int i=0; i<4; i++)
for (int j=0; j<4; j++)
result.element[i][j] = element[j][i];
return result;
}

//Point Class fucntions===================================
Point Matrix44::operator*(const Point& p) {
Point result;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
result.element[i] += element[i][j] * p.element[j];

}
}
return result;
}
//User Input
istream& operator>> (istream& s, Point& p) {
for (int i=0; i<4; i++)
s >> p.element[i];
if (!s) { cerr << “Error reading Matrix from stream”;  exit(0); }
return s;
}
//User Output
ostream& operator<< (ostream& s, const Point& p) {
for (int i=0; i<4; i++) {
s << p.element[i] << ”   “;
s << endl;
}
if (!s) { cerr << “Error writing Matrix to stream”;  exit(0); }
return s;
}

//Inverse Function ======================
Matrix44 Matrix44::inverse(Matrix44 x)const {
Matrix44 result;
Point t;
Point temp;

for (int i=0; i<3; i++){
temp.element[i] = -x.element[i][3];
}

result = x.transpose();
t = result* temp;
for (int i=0; i<4; i++){
if (i<=2){
result.element[3][i] = 0;
} else if (i=3){
result.element[3][i]= 1;
}
}
for (int i=0; i<3; i++){
result.element[i][3] = t.element[i];
}

return result;
}

//Main program
int main(int, char* [])
{
Matrix44 a;
Matrix44 b;
Point p; //point to multiply
Point pr; //point result
Matrix44 invermat;//holder for the inverse matrix

//input
cout << endl << “Please enter the 16 elements (by rows) of matrix A:” << endl;
cin >> a;
cout << endl << “Please enter the 16 elements (by rows) of matrix B:” << endl;
cin >> b;
cout << endl << “Please enter a 4×1 Puvw: “ << endl;
cin >> p;

//Output
cout << endl << \n********************OutPut***************************\n;
//tranposes output
cout << endl << “The transpose of matrix A:” << endl << a.transpose();
cout << endl << “The transpose of matrix B:” << endl << b.transpose();

//multiply
Matrix44 c;
c = a*b;
//multiply output
cout << endl << \n1. The matrix A * matrix B results in matrix C:” << endl << c;

//transform point

pr = a*p;
cout << endl << \n2. The matrix A * Point Puvw results :” << endl << pr;

//Inverse
cout<< endl<<\n3. The inverse of matrix A: “ << endl << invermat.inverse(a);

cout << \n\n===============================END===============================\n\n;

return 0;
}

The output should look like this:

Source: Matrices Souce.zip

Your Ad Here

Leave a comment

*

Tutorials Categories