.CPP AND MAIN.CPP FILES

.CPP AND MAIN.CPP FILES

// transform11.h file
#ifndef _TRANSFORM11_H
#define _TRANSFORM11_H
class transform11
{
private:
int c;
int x;
int f;
int t;
int r;

public:
transform11();
int setc(int C[4][4]);
int setx(int X[4][4]);
int setf(int M[4][4]);
int gett(int T[4][4]);
int getr(int R[4][4]);
};
#endif

How can write the .cpp and main.cpp files from the above .h file.

the idea is if i set 'c','x' and 'f' to a given 4x4 matrix, then i should get 't' which is transpose of 'c', 'r' which is product of 'x' and 'c'. the programe should be in c++

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

As posted, that code wont work for a matrix class. I noticed in the other thread, a very good suggestion that you use one of the popular matrix classes already available was posted. There are a number of really good ones. Off the top of my head, I would suggest Intel's MKL library, the Eigen library, or the Boost libraries.

One of the major benefits of programming in C or C++ is the ability to reuse code. IE - to not reinvent the wheel. At one point in time, I could see rewritting a class like this for performace, but with highly optimized libraries like Intel's MKL available I would really recommend you use an existing library.

That being said, to answer your question you first need to fix your class definition. Your matrix elements in the private section must either be pointers or arrays or arrays. Either works, you just need to choose. It is probably much easier to do something like this though:


class basicMatrix_i4x4
{
private:
	int valArray[4][4];

public:
	void fillMatrix(int vals[4][4]);

	void setMember(int i, int j, int val);
	int getMember(int i, int j) {return valArray[i][j];}

	static basicMatrix_i4x4 matrixMultiply(basicMatrix_i4x4 a, basicMatrix_i4x4 b);
	basicMatrix_i4x4 matrixTranspose(void);

	void outputMatrix();

	basicMatrix_i4x4();
	~basicMatrix_i4x4();
};

class transform11
{
private:
	basicMatrix_i4x4 c;
	basicMatrix_i4x4 x;
	basicMatrix_i4x4 r;

public:

	void setc(int vals[4][4]) {c.fillMatrix(vals);};
	void setx(int vals[4][4]) {x.fillMatrix(vals);};
	void setr(int vals[4][4]) {r.fillMatrix(vals);};

	basicMatrix_i4x4 getCX(void) {return basicMatrix_i4x4::matrixMultiply(c,x);};
	basicMatrix_i4x4 getTranspose(void) {return c.matrixTranspose();};

	transform11() {};
	~transform11() {};
};
  

The benefits of this over the way you are trying to do it is you create the functions needed to multiply and do the transpose only once. Once again, far far better than actually writing the basicMatrix_i4x4 class would be to just use one of the fine linear algebra libraries already written. Now to actually write the transform11.cpp file you would do something like this:

#include "transform11.h"

basicMatrix_i4x4::basicMatrix_i4x4()
{
	for(int i=0; i<4; i++)
		for(int j=0; j<4; j++)
			valArray[i][j]=0;
}


basicMatrix_i4x4::~basicMatrix_i4x4()
{

}


void basicMatrix_i4x4::fillMatrix( int vals[4][4] )
{
	for(int i=0; i<4; i++)
		for(int j=0; j<4; j++)
			valArray[i][j]=vals[i][j];
}


void basicMatrix_i4x4::setMember( int i, int j, int val )
{
  valArray[i][j]=val;
}

basicMatrix_i4x4 basicMatrix_i4x4::matrixMultiply( basicMatrix_i4x4 a, basicMatrix_i4x4 b )
{
 basicMatrix_i4x4 returnMe;
 for(int i=0; i<4; i++)
	 for(int j=0; j<4; j++)
		 returnMe.setMember(i,j, a.getMember(i,0)*b.getMember(0,j)+a.getMember(i,1)*b.getMember(1,j)+
						         a.getMember(i,2)*b.getMember(2,j)+a.getMember(i,3)*b.getMember(3,j));

 return returnMe;
}


basicMatrix_i4x4 basicMatrix_i4x4::matrixTranspose( void )
{
	basicMatrix_i4x4 returnMe;
	for(int i=0; i<4; i++)
		for(int j=0; j<4; j++)
			returnMe.setMember(i,j,valArray[j][i]);

	return returnMe;
}


void basicMatrix_i4x4::outputMatrix()
{
	for(int i=0; i<4; i++){
		for(int j=0; j<4; j++)
			std::cout << valArray[i][j] << " ";
		std::cout << std::endl;
	}

}

That will be able to get you the transpose and any combination of matrix multiplications you want. Once again let me go back to what I said at the beginning and others have recommended for you. This is an extremely common task that has been solved hundreds of times. While this code works and might give you a starting point, you should use one of the publically available libraries. They are optimized to do this work far faster and with a great deal more functionality.

I also want to note that part of the process of learning how to program is working through simple examples like this yourself (I am taking it on faith that this was not a homework problem btw). A really really great way to do that is to go to one of the libraries someone else has written - like boost whose source code is readily available. Look through what they have done and try to understand it. That way you'll have a good idea if you can use their library or how to write your own if you cannot.

thank you a million times. i noticed you have combined two classes: basicMatrix_i4x4 and transform11. was the first intended to show the principle? the .cpp file does not carry the transform11 class, principle too? confirm

Coding style is very muchlinked to personal preference. In this case, the class basicMatrix_i4x4 was designed for sole use in the transform11 class, so I included it as a "sub class" in the same file. In other words, I wouldn't want someone using that class for their own code. I could have included it inside the transform11 class to make that link explicit, but I dislike nested classes. It probably would have been the proper thing to do however. More on why I separated it out in a second.

The transform11 class is actually entirely defined as inlinefunctions, so has no functions defined in the .cpp file. Put another way, if you copy those two files into transform11.h and transform11.cpp, then include transform11.h in your program it will compile and function.

For example, the statement:

basicMatrix_i4x4 getCX(void) {return basicMatrix_i4x4::matrixMultiply(c,x);};

is actually the same thing as having this in your transform.h file:

basicMatrix_i4x4 getCX(void); // in your transform.h file

and this in your transform.cpp file:

basicMatrix_i4x4 transform11::getCX(void) // in transform.cpp 
{
     return basic_Matrix_i4x4::matrixMultiply(c,x);
}

Why do it one way as opposed to the other? Just convenience. Quickly I did want to go back to why I separated it into two classes. The reason is you can use the transform11 class as I have written it with any matrix library. For example, if you wanted to use the boost libraries, you could rewrite that class as this (after installing the Boost libraries of course!):

#include  
#include  
#include  
using namespace boost::numeric::ublas; 
class transform11 {
private: 
	matrix c(4,4); 
	matrix x(4,4); 
	matrix r(4,4); 
public: 
	void fillMatrix(int vals[4][4], matrix &tofill(4,4); 
	void setc(int vals[4][4]) {fillMatrix(vals,c);}; 
	void setx(int vals[4][4]) {fillMatrix(vals,x);}; 
	void setr(int vals[4][4]) {fillMatrix(vals,r);}; 

	basicMatrix_i4x4 getCX(void) {return prod(c,x);}; 
	basicMatrix_i4x4 getTranspose(void) {return trans(c);}; 

	transform11() {}; 
	~transform11() {}; 
}; 

Then all you would need to do was define the fillMatrix function (which should be pretty straightforward). This is actually a good thing. It means you can change from a very basic inefficient matrix algorithm like I provided to a very thorough and efficient algorithm like those Boost provides. I would like to take a moment again to recommend that you find an appropriatelinear algebra library for your code. It will make your programming task much easier.

Leave a Comment

Please sign in to add a comment. Not a member? Join today