By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

code for the trasnpose of a matrix

P: n/a
i would like to have a C++ code for the transpose of a matrix if
anyone could assist me with it i would be glad to have it
Jul 23 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
dorcas wrote:
i would like to have a C++ code for the transpose of a matrix if
anyone could assist me with it i would be glad to have it


If you're too lazy to do your own homework, don't ask us. Go to
any Web search engine and look for it. At least then nobody will
have assisted you in cheating.
Jul 23 '05 #2

P: n/a
Victor Bazarov wrote:
dorcas wrote:
i would like to have a C++ code for the transpose of a matrix if
anyone could assist me with it i would be glad to have it


If you're too lazy to do your own homework, don't ask us. Go to
any Web search engine and look for it. At least then nobody will
have assisted you in cheating.


I know this is not what the OP requested, but if the exercice is to
perform an "in situ" transpose - i.e. perform the transpose by swapping
matrix elements - it turns out to be surprisingly difficult. Try it if
you've nothing better to do.

--
Lionel B

Jul 23 '05 #3

P: n/a
Lionel B wrote:
Victor Bazarov wrote:
dorcas wrote:
i would like to have a C++ code for the transpose of a matrix if
anyone could assist me with it i would be glad to have it


If you're too lazy to do your own homework, don't ask us. Go to
any Web search engine and look for it. At least then nobody will
have assisted you in cheating.

I know this is not what the OP requested, but if the exercice is to
perform an "in situ" transpose - i.e. perform the transpose by swapping
matrix elements - it turns out to be surprisingly difficult. Try it if
you've nothing better to do.


I am not sure whom you're addressing in your last sentence.
Jul 23 '05 #4

P: n/a
Victor Bazarov wrote:
Lionel B wrote:
Victor Bazarov wrote:
dorcas wrote:

i would like to have a C++ code for the transpose of a matrix if
anyone could assist me with it i would be glad to have it

[...]


I know this is not what the OP requested, but if the exercice is to
perform an "in situ" transpose - i.e. perform the transpose by
swapping matrix elements - it turns out to be surprisingly
difficult. Try it if you've nothing better to do.


I am not sure whom you're addressing in your last sentence.


The World (sorry if that wasn't clear).

--
Lionel B

Jul 23 '05 #5

P: n/a
Lionel B wrote:
Victor Bazarov wrote:
Lionel B wrote:
Victor Bazarov wrote:
dorcas wrote:
>i would like to have a C++ code for the transpose of a matrix if
>anyone could assist me with it i would be glad to have it

[...]

I know this is not what the OP requested, but if the exercice is to
perform an "in situ" transpose - i.e. perform the transpose by
swapping matrix elements - it turns out to be surprisingly
difficult. Try it if you've nothing better to do.


I am not sure whom you're addressing in your last sentence.

The World (sorry if that wasn't clear).


No worries. I for some reason find it surprising that you find the
in-place transpose "surprisingly difficult". Of course, it very much
depends on the data structure for the matrix and the performance you
want to achieve, I guess, but for a straight-forward representation
that has nested operator[] defined (like for a two-dimensional array
or a vector of vectors), two nested loops of calls to 'std::swap' is
all you need. Is that what you call "difficult"? I can see how it
could be difficult to the OP, of course...

V
Jul 23 '05 #6

P: n/a
Lionel B wrote:
I know this is not what the OP requested, but if the exercice is to
perform an "in situ" transpose - i.e. perform the transpose by swapping
matrix elements - it turns out to be surprisingly difficult. Try it if
you've nothing better to do.


Why ?

Transposing the matrix in place can be done easily by looping trough all
elements (i,j) of the upper triangular part of the matrix, and swap each
element (i,j) with element (j,i).

--
Ares Lagae
Computer Graphics Research Group, Katholieke Universiteit Leuven
http://www.cs.kuleuven.ac.be/~ares/
Jul 23 '05 #7

P: n/a
Ares Lagae wrote:
Lionel B wrote:
I know this is not what the OP requested, but if the exercice is to
perform an "in situ" transpose - i.e. perform the transpose by
swapping matrix elements - it turns out to be surprisingly
difficult. Try it if you've nothing better to do.


Why ?

Transposing the matrix in place can be done easily by looping trough
all elements (i,j) of the upper triangular part of the matrix, and
swap each element (i,j) with element (j,i).


Try that for a non-square matrix ;-)

--
Lionel B

Jul 23 '05 #8

P: n/a
Lionel B wrote:
Ares Lagae wrote:
Lionel B wrote:

I know this is not what the OP requested, but if the exercice is to
perform an "in situ" transpose - i.e. perform the transpose by
swapping matrix elements - it turns out to be surprisingly
difficult. Try it if you've nothing better to do.


Why ?

Transposing the matrix in place can be done easily by looping trough
all elements (i,j) of the upper triangular part of the matrix, and
swap each element (i,j) with element (j,i).

Try that for a non-square matrix ;-)


With certain data structures it's by definition impossible.

V
Jul 23 '05 #9

P: n/a
Victor Bazarov wrote:
Lionel B wrote:
Ares Lagae wrote:
Lionel B wrote:

I know this is not what the OP requested, but if the exercice is
to perform an "in situ" transpose - i.e. perform the transpose by
swapping matrix elements - it turns out to be surprisingly
difficult. Try it if you've nothing better to do.

Why ?

Transposing the matrix in place can be done easily by looping
trough all elements (i,j) of the upper triangular part of the
matrix, and swap each element (i,j) with element (j,i).


Try that for a non-square matrix ;-)


With certain data structures it's by definition impossible.


Let's say sequential, row-major storage. As far as I know this problem
was first solved in the paper:

Windley, P. F. "Transposing Matrices in a Digital Computer." Computer
J. 2, 47-48, Apr. 1959.

available online at:

http://www3.oup.co.uk/computer_journ...7.sgm.abs.html

--
Lionel B

Jul 23 '05 #10

P: n/a
dorcas wrote:
I would like to have a C++ code for the transpose of a matrix if
anyone could assist me with it i would be glad to have it
Take a look at
The C++ Scalar, Vector, Matrix and Tensor class Library

http://www.netwood.net/~edwin/svmtl/
cat svmtl/src/vector/doubleVector/doubleVector.cc

Jul 23 '05 #11

P: n/a
E. Robert Tisdale wrote:
dorcas wrote:
I would like to have a C++ code for the transpose of a matrix if
anyone could assist me with it i would be glad to have it


Take a look at
The C++ Scalar, Vector, Matrix and Tensor class Library

http://www.netwood.net/~edwin/svmtl/

[...]


Interesting... is this your own algorithm? Having another look at the
paper:

Windley, P. F. "Transposing Matrices in a Digital Computer." Computer
J. 2, 47-48, Apr. 1959.

mentioned in a previous post of mine, I think your algorithm is the
same as one attributed there to one "J. C. Gower". The author's own
algorithm, he says, "... saves going around each cycle once, but ...
involves more reading and writing operations ...".

Knuth (natch) also gives several variations in:

Knuth, D. E. "Transposing a Rectangular Matrix." Ch. 1.3.3 Ex. 12. The
Art of Computer Programming, Vol. 1: Fundamental Algorithms, 3rd ed.

--
Lionel B

Jul 23 '05 #12

P: n/a
Lionel B wrote:
E. Robert Tisdale wrote:
dorcas wrote:
I would like to have a C++ code for the transpose of a matrix if
anyone could assist me with it i would be glad to have it
Take a look at
The C++ Scalar, Vector, Matrix and Tensor class Library

http://www.netwood.net/~edwin/svmtl/

[...]


Interesting... is this your own algorithm?


I find it very difficult to read and understand papers
like the ones that you cite without first implementing
my own algorithm.
Having another look at the paper:

Windley, P. F. "Transposing Matrices in a Digital Computer."
Computer J. 2, 47-48, Apr. 1959.
The on-line copy of this is a tiff file which my browser (Mozilla)
doesn't handle very well -- I couldn't print it.
mentioned in a previous post of mine,
I think your algorithm is the same as
one attributed there to one "J. C. Gower".
The author's own algorithm, he says,
"... saves going around each cycle once, but ...
involves more reading and writing operations ...". Knuth (natch) also gives several variations in:

Knuth, D. E. "Transposing a Rectangular Matrix." Ch. 1.3.3 Ex. 12. The
Art of Computer Programming, Vol. 1: Fundamental Algorithms, 3rd ed.


I appreciate the references.
I have since collected and read many papers on this topic
but they all seem to require a search for the cycles.
I would expect a performance implementation of the SVMTL
to implement an optimal algorithm for in-place transpose
but I thought that my implementation was more appropriate
for the reference library because it seemed to me to be
more transparent.
Jul 23 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.