Hi there. I am creating a mock up of a physics library currently, and I am currently trying to deal with 2d arrays. Right now I am having issues with transposing the array. here is the code with the messy stuff cut out

//TMatrix.h
#pragma once
#include "main.h"

class TMatrix
{
public:
	TMatrix(void);
	TMatrix(double mx00, double mx01, double mx02, double mx10, 
		double mx11, double mx12, double mx20, double mx21, double mx22);
	double _Mx [3][3];
	~TMatrix(void);

TMatrix trans(TMatrix m1);	//transpose	

	//other funtions
	void printm();
};
//TMatrix.cpp
#include "TMatrix.h"

TMatrix::TMatrix(void)
{
}

TMatrix::TMatrix(double mx00, double mx01, double mx02,
double mx10, double mx11, double mx12,
double mx20, double mx21, double mx22) {
_Mx[0][0]=mx00; _Mx[0][1]=mx01; _Mx[0][2]=mx02;
_Mx[1][0]=mx10; _Mx[1][1]=mx11; _Mx[1][2]=mx12;
_Mx[2][0]=mx20; _Mx[2][1]=mx21; _Mx[2][2]=mx22;
}

TMatrix::~TMatrix(void)
{
}

//transpose matrix
TMatrix TMatrix::trans(TMatrix m1)
{
	for (int i = 0; i <= 2; i++)
	{
		for (int j = 0; j <= 2; j++)
		{
			m1._Mx[i][j] = m1._Mx[j][i];
		}
	}
	return *this;
}

//print function
void TMatrix::printm()
{
	for (int i = 0; i <= 2; i++)
	{
		for (int j = 0; j <= 2; j++)
		{
			cout << "   " << _Mx[i][j] << "   ";
		}
		putchar('\n');
	}
	cout << "\n" << endl;
}

In main I am testing it with this:

//build matrices
	TMatrix m1, m2, m3;
	m1 = TMatrix(1, 0 ,0,
				 0, 1, 0,
				 0, 0, 1);
	
	m2 = TMatrix(1, 2 ,3,
				 4, 5, 6,
				 7, 8, 9);
//transpose matrix
	m2.printm();
	m2.trans(m2);
	m2.printm();

The current output is :
1 2 3
4 5 6
7 8 9.

Any idea what's the issue here?

This syntax seems awkward:

m2.trans(m2);

If you call the function trans() on an object, it shouldn't take any parameters and it should return void:

m2.trans();

Only if you called the function as a static function would take seem right:

m2 = trans(m1);

Also, if the goal of your project is not to write a matrix class, I strongly suggest you do not do so, but rather use an existing library. I use VNL (part of the VXL package), but there are many others (Eigen, etc).

David

Yeah, one of my goals is to create my own matrix class which rules out using other libraries sadly. Haven't had time to implement your suggestions. Will produce more feedback shortly. Thank you ever so much for your help.

trying the above method is am getting the following out put:

147
456
678

ie. only the first column is being transposed into a row.

yeah one second currently playing with it broke it beyond all belief.

You should look into using a revision control system (svn, or the latest and greatest, git). There is certainly a lot of work at the beginning figuring out how to use it, but once you get used to it it will allow you to "undo" any of your changes. E.g. in this case you could revert back to a working version :)

from TMatrix.h

void TMatrix::trans();	//transpose

from TMatrix.cpp

//transpose matrix
void TMatrix::trans()
{
	for (int r = 0; r <= 2; r++)
	{
		for (int c = 0; c <= 2; c++)
		{
			_Mx[r][c] = _Mx[c][r];
		}
	}
}

main.cpp

//transpose matrix
	m2.printm();
	m2.trans();
	m2.printm();

You need to make a temporary matrix. When you do this:

_Mx[0][1] = _Mx[1][0];

then later this:

_Mx[1][0] = _Mx[0][1];

you won't have changed anything.

Say you have the matrix:

1 2 3
4 5 6
7 8 9

When you do this:

_Mx[0][1] = _Mx[1][0];

you now have

1 4 3
4 5 6
7 8 9

You have lost the 2 forever!

I'm suggesting that the first thing you do is

matrix::transpose()
{
 matrix temp = *this;
 for (your loop...)
 {
   this[i][j] = temp[j][i];
 }
}

This way you won't lose any numbers :) This has nothing to do with why you aren't outputting more than 1 row, but it must be fixed as well.

David

I have tried using *this, but its being a struggle to get it to compile, often throwing up errors along the lines of the TMatrix object not allowing the use of [].

I have attempted the following code with a successful compile. But in essence it has achieved nothing as it simply returns a untransposed matrix.

void TMatrix::trans()
{
	for (int r = 0; r <= 2; r++)
	{
		for (int c = 0; c <= 2; c++)
		{
			temp = _Mx[r][c];
			_Mx[r][c] = _Mx[c][r];
			_Mx[c][r] = temp;
		}
	}
}

This does not compile correctly with VS2010:

void TMatrix::trans()
{
	TMatrix temp =*this;
	for (int r = 0; r <= 2; r++)
	{
		for (int c = 0; c <= 2; c++)
		{
			_Mx[r][c] = _Mx[c][r];
			this[r][c] = temp[c][r];
		}
	}
}

It throws the following error:

'TMatrix' does not define this operator or a conversion to a type acceptable to the predefined operator
'TMatrix' does not define this operator or a conversion to a type acceptable to the predefined operator

The restriction imposed on me to used 2d arrays is becoming rather troublesome. Back to researching this.

The first thing you posted there doesn't do anything better than what you were doing before. The second version is good, but you must dereference the 'this' pointer:

for (int c = 0; c <= 2; c++)
{
*(this)[r][c] = temp[c][r];
}

The first thing you posted there doesn't do anything better than what you were doing before. The second version is good, but you must dereference the 'this' pointer:

for (int c = 0; c <= 2; c++)
{
*(this)[r][c] = temp[c][r];
}

Sorry, my appologies I should have stated when dereferencing *this the complier threw this error:

TMatrix' does not define this operator or a conversion to a type acceptable to the predefined operator
TMatrix' does not define this operator or a conversion to a type acceptable to the predefined operator


In particular it highlighted the [c] associated with the *(this) and the use of the [] with the temp variable.

To clarify this is what I tried to impliment:

//transpose matrix
void TMatrix::trans()
{
	TMatrix temp = *this;
	for (int r = 0; r <= 2; r++)
	{
		for (int c = 0; c <= 2; c++)
		{
			_Mx[r][c] = _Mx[c][r];
			*(this)[r][c] = temp[c][r];
		}
	}
}

Edited 6 Years Ago by Tsunami49: n/a

My apologies I had the solution my method of prinitng was flawed and simply returning the original m2 matrix.

The code I used was the following:

TMatrix TMatrix::trans(TMatrix m1)
{
	TMatrix temp;
	for (int i = 0; i <= 2; i++)
        for (int j = 0; j <= 2; j++)
		     temp._Mx[j][i] =  m1._Mx[i][j];
	return temp;
}
This question has already been answered. Start a new discussion instead.