473,322 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

code for the trasnpose of a matrix

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
12 3852
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: christopher diggins | last post by:
Here is some code I wrote for Matrix multiplication for arbitrary dimensionality known at compile-time. I am curious how practical it is. For instance, is it common to know the dimensionality of...
23
by: Babak | last post by:
Hi Everyone, I've written a standard C code for a simple finite element analysis in MSVC++ . When I save the file as a cpp file, it compiles and runs perfectly, but when I save it as a c file,...
2
by: DarrenWeber | last post by:
Below is a module (matrix.py) with a class to implement some basic matrix operations on a 2D list. Some things puzzle me about the best way to do this (please don't refer to scipy, numpy and...
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
8
by: mohammaditraders | last post by:
#include <iostream.h> #include <stdlib.h> #include <conio.h> #include <string.h> class Matrix { private : int numRows, numCols ; int elements ;
6
by: lovecreatesbea... | last post by:
Hello experts, I code an function to rotate a matrix by 90 degrees clockwise. The matrix can be in any size provided its length equals to width. The one minor limitation is that this requires an...
3
by: xmail123 | last post by:
Why does this code work? I am new to C# and have been studying this piece of code. It loops through an Adjacency Matrix table to populate a tree view. I have two questions about why this code...
12
by: MrTemplar | last post by:
Hello everyone, i am new to these forums and in fact new to C++. I am working on a project, and i would be most grateful if i could have my code checked by people who have a better understanding of...
10
by: Curious | last post by:
A business associate told me that I should be able to run C++ code in C#.NET environment because C++ and C# belong to the same family. He sent me the code below and asked me to run in C#.NET...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.