473,662 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Faster matrix permutation algorithm?

Hi!

I'm lookin for a faster permutation algorithm for matrices. I know that
it can be done with multiplying a matrix with a permutation matrix. It
just seems a waste to iterate through all those zeroes. Is there an
algorithm for matrixes that is optimized just for permutations? The
matrices that I use are fairly small (around 6*6) and I only use
positive integers as elements.

Thanks for help,

Jack Middleton
Nov 15 '05 #1
3 7128
Jack Middleton wrote:
I'm lookin for a faster permutation algorithm for matrices. I know that
it can be done with multiplying a matrix with a permutation matrix. It
just seems a waste to iterate through all those zeroes. Is there an
algorithm for matrixes that is optimized just for permutations? The
matrices that I use are fairly small (around 6*6) and I only use
positive integers as elements.


Note that this is off-topic in comp.lang.c from where I am posting.
-> f'up2c.p

Some thoughts on your question:
- Do you want to restrict yourself to certain classes of permutations
or do you just want the matrix elements reordered in a random way?
- In the latter case, if you are programming in a language where you
know the memory layout and where the memory layout of the matrix is
essentially an array, you can just use (perfect) shuffling -- or,
if you need all permutations, a loop through all array permutations.
- Otherwise, just perform the effective action of the matrix-matrix
multiplication: Interchange the rows/columns of the target matrix as
intended.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #2
[Followups set to comp.programmin g]

Jack Middleton wrote:
Hi!

I'm lookin for a faster permutation algorithm for matrices. I know that
it can be done with multiplying a matrix with a permutation matrix. It
just seems a waste to iterate through all those zeroes. Is there an
algorithm for matrixes that is optimized just for permutations? The
matrices that I use are fairly small (around 6*6) and I only use
positive integers as elements.


Consider a mapping between a matrix X * Y elements in size and a series of
numbers from 0 to X * Y - 1. Such a mapping is reversible. To map from the
matrix cell references to a single number, use:

m = x * Y + y;

To map from the single number to cell references, use:

x = (m - y) / Y;
y = m % Y;

Set up a list of single numbers, 0 to X * Y - 1, in an array, and permute
the array, using the normal recursive technique. T is some (sensible)
assignable type. Perm is a pointer to the first element in an array of n
elements of type T. The final parameter, 'unchanged', is set to 0 in the
initial call:

void Permute(T *Perm, size_t n, size_t unchanged)
{
size_t outer = 0;
size_t inner = 0;
T temp = 0;

if(unchanged < n)
{
for(outer = unchanged; outer < n; outer++)
{
temp = Perm[outer];
for(inner = outer; inner > unchanged; inner--)
{
Perm[inner] = Perm[inner - 1];
}
Perm[unchanged] = temp;
Permute(Perm,
n,
unchanged + 1);

for(inner = unchanged; inner < outer; inner++)
{
Perm[inner] = Perm[inner + 1];
}
Perm[outer] = temp;
}
}
else
{
/* Perm[0] through Perm[n - 1] now form a permutation.
* If T is char, Perm points to a modifiable string
* containing the three letters 'a', 'b', 'c', n is 3, and
* unchanged is initially 0, then this else-block will be
* visited six times, and a printf("%s\n", Perm); at this
* point will write each of the six possible permutations,
* one per visit.
*/
}
}

Call this once, and it will do all your permutations for you. By making T
int, you can get a simple correspondence between the array and your matrix.

If you're feeling really brave, you could modify Permute() to talk to your
matrix directly, using the mapping I suggested earlier.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
mail: rjh at above domain
Nov 15 '05 #3
In comp.lang.c Jack Middleton <bi*******@mira for.com> wrote:
Hi!
I'm lookin for a faster permutation algorithm for matrices. I know that
it can be done with multiplying a matrix with a permutation matrix. It
just seems a waste to iterate through all those zeroes. Is there an
algorithm for matrixes that is optimized just for permutations? The
matrices that I use are fairly small (around 6*6) and I only use
positive integers as elements.

[...]
If you want to permute rows/cols (or both) then you can simply iterate
through one or more maps/indexes. Manipulate the maps directly and
only use them as indexes at the last second.
==========
Before you ask a question you must know most of the answer.
-- anon

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
5619
by: Talin | last post by:
I'm sure I am not the first person to do this, but I wanted to share this: a generator which returns all permutations of a list: def permute( lst ): if len( lst ) == 1: yield lst else: head = lst for x in permute( lst ): yield head + x
6
1813
by: fool | last post by:
Dear group, Given a string I have to print the permutation, using some looping tricks. This is not a Home work problem. My best try as beginner is: #include<stdio.h> #include<stdlib.h> int main(void)
21
4637
by: =?UTF-8?B?TWFydGluIFDDtnBwaW5n?= | last post by:
Hello, I´m using a very large 2-dimensional double matrix (45.000x45.000) in my program. At the initialization of the matrix: double matrix = new double I am getting an out of memory error.
52
5103
by: Nomad.C | last post by:
Hi I've been thinking of learning Fortran as number crunching kinda language for my Physics degree......but then looking around the internet, people are saying that the libraries/ Algorithms once used for number crunching is now slowly converting into C, so do you think I should stick with C, since I know C already, or should I proceed learning fortran?? Any advice?? Thanks
2
5837
by: Zhengzheng Pan | last post by:
Hi all, I'm trying to check whether a (stochastic/transition) matrix converges, i.e. a function/method that will return True if the input matrix sequence shows convergence and False otherwise. The background is a Markov progress, so the special thing about the transition matrix is the values of elements in each row add up to be 1. Fail to find any relevant build-in methods in Python... Currently the standard theorem on convergence in...
4
8086
by: krishnai888 | last post by:
I had already asked this question long back but no one has replied to me..I hope someone replies to me because its very important for me as I am doing my internship. I am currently writing a code involving lot of matrices. At one point I need to calculate the square root of a matrix e.g. A which contains non-zero off-diagonal elements. I searched for a lot of info on net but no algorithm worked. My best bet for finding square root was to find...
3
4968
by: PeteJ | last post by:
Hello all.. I wrote code in C to invert n x n matrices, where 1<n<39 and the matrices are guaranteed to be invertible. Prior to this task, I haven't really seen linear algebra since my sophmore year of undergrad, but I believe my algorithm implements Gaussian elimination. I append an identity matrix to the right of the one I wish to invert, use row addition to first turn the lower matrix to zeroes and then turn the upper matrix to zeroes, and...
7
4082
by: xirowei | last post by:
Let's say i create a String array that store 4 Alphabets {"A","B","C","D"} How can i get the result if i need permutation of 4P3 and 4P2? I had refer to many examples from the internet, but those examples cannot compute n selection from m elements. They only able to computer permutation of m elements without selection e.g. 4P4. Hence i need guideline in how to compute this kind of permutation. If i use manual calculation of 4P3...
1
3105
by: almurph | last post by:
Hi everyone, Concerning the Needleman-Wunsch algorithm (cf. http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm) I have noticed a possible loop. Inside the algorithm there is an important decision making mechanism. Its a "if, else if, else if" structure like:
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8857
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8546
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5654
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.