code for the trasnpose of a matrix

D

dorcas

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
 
V

Victor Bazarov

dorcas said:
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.
 
L

Lionel B

Victor said:
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.
 
V

Victor Bazarov

Lionel said:
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.
 
L

Lionel B

Victor said:
Lionel said:
Victor said:
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).
 
V

Victor Bazarov

Lionel said:
Victor said:
Lionel said:
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
 
A

Ares Lagae

Lionel said:
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).
 
L

Lionel B

Ares said:
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 ;-)
 
L

Lionel B

E. Robert Tisdale said:
dorcas said:
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.
 
E

E. Robert Tisdale

Lionel said:
E. Robert Tisdale said:
dorcas said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top