Skip to main content
33 votes
Accepted

Optimization of function taking a permutation

How about a Monte-Carlo-Metropolis search? I'll implement a simplistic version here. See complete universal code further down. Update: Cleaned-up code now available in the Wolfram Function Repository, ...
Roman's user avatar
  • 47.9k
28 votes
Accepted

Faster derangements?

Chunks of derangements Since I've already written library link code generating permutations, generating derangements requires just few tweaks: ...
jkuczm's user avatar
  • 15.1k
26 votes

Faster derangements?

This is the fastest method I have come up with: ...
Mr.Wizard's user avatar
  • 272k
22 votes

Fastest way to generate {{a,a},{a,b},{a,c},{b,b},{b,c},{c,c}} from {a,b,c}

Select[Tuples[{a,b,c},2],OrderedQ]
lericr's user avatar
  • 28.9k
19 votes

Faster derangements?

Here is one way to generate them directly: it is based on a way to generate all permutations but discards invalid ones early: ...
Martin Ender's user avatar
  • 8,774
19 votes
Accepted

Delete duplicates from list of lists as if on a necklace

A high-performance solution Since you are planning to work with thousands of necklaces, it may be much faster to introduce a canonical form. The main point is to write a necklace canonization ...
Ray Shadow's user avatar
  • 7,836
17 votes

Permutations of nested parentheses (Dyck words)

StringReplaceList I just realized that there is a comparatively clean though not highly efficient way to write this using ...
Mr.Wizard's user avatar
  • 272k
17 votes

Terse Method to Swap Lowest for Highest?

How about: Module[{tmp = test}, With[{ord=Ordering[tmp]}, tmp[[ord]] = Reverse @ tmp[[ord]]]; tmp ] {56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, ...
Carl Woll's user avatar
  • 131k
17 votes
Accepted

StringContainsQ, but anywhere in order

StringMatchQ["aabbc", "*a*c*"] True StringMatchQ["aabbc", "*b*a*"] False You can also use ...
kglr's user avatar
  • 396k
17 votes
Accepted

Fastest way to generate {{a,a},{a,b},{a,c},{b,b},{b,c},{c,c}} from {a,b,c}

GroupTheory`Tools`Multisets[{a, b, c}, 2] {{a,a},{a,b},{a,c},{b,b},{b,c},{c,c}}
yode's user avatar
  • 26.8k
16 votes
Accepted

ls Ordering[Ordering[list]] optimal?

No, Ordering[Ordering[list]] not optimal. And yes, there is a faster method: ...
Henrik Schumacher's user avatar
15 votes
Accepted

List all possible license plate numbers

I would use a = Alphabet[]; (* letter *) d = Range[0, 9]; (* digit *) result = Tuples[{a, a, d, d, d, d}];
Szabolcs's user avatar
  • 235k
14 votes

Permutations of lists of fixed even numbers

The permutation you described is called "derangement". There is a function Derangement in Combinatoricapackage. ...
vapor's user avatar
  • 7,921
14 votes
Accepted

Permutations of lists of fixed even numbers

Permutations where no element remains in its original place are called derangements. Counting them is easy enough: the number of derangements of a set of size $n$ is $!n$, or the subfactorial of $n$. ...
Martin Ender's user avatar
  • 8,774
14 votes

StringContainsQ, but anywhere in order

Is this what you need? StringContainsQ["aabbc","a" ~~ ___ ~~ "c"] True The following documentation pages should help you get going with string patterns in ...
Sjoerd Smit's user avatar
  • 23.6k
13 votes
Accepted

Number by permutation

Just a note - any method generating the permutations and the searching will get very slow very quickly, and blow RAM soon after. Something like this s/b much more efficient (e.g., on my goof-top, for ...
ciao's user avatar
  • 25.8k
13 votes
Accepted

Cyclic and Non-cyclic Permutations

Per the request, I post my comment as an answer: First question cy := Permute[#, CyclicGroup[Length@#]] & cy[Range@5] {{1, 2, 3, 4, 5}, {2, 3, 4, 5, 1}, {3, 4,...
yode's user avatar
  • 26.8k
13 votes

Delete duplicates from list of lists as if on a necklace

Preface If one could create a function f that calculates a canonical form of a necklace that turns all equivalent necklaces ...
halirutan's user avatar
  • 113k
13 votes
Accepted

Generating a list of integers that sums to zero

...
Roman's user avatar
  • 47.9k
13 votes
Accepted

Evaluating Pfaffian

In the answer to: "https://mathematica.stackexchange.com/questions/125794/compute-numeric-pfaffians-of-matrices-efficiently" there is a pointer to the code below. I hope this will be helpful....
Daniel Huber's user avatar
  • 52.6k
12 votes
Accepted

Connectivity in a molecule and permutations

Getting the graph from the XYZ file You have only a set of coordinates and atom types in an XYZ file. When you import it in Mathematica you can import 3 elements: the 3D plot, the coordinates, and ...
Jason B.'s user avatar
  • 68.7k
12 votes

Faster derangements?

Here is a straightforward compiled implementation of Knuth's "Algorithm X" for lexicographically generating restricted permutations, specialized to the derangement case: ...
J. M.'s missing motivation's user avatar
12 votes
Accepted

Can we turn this for loop into a more elegant Mathematica code?

Apparently, you try to apply a permutation given by list indirection to a vector duplicate. Here are several ways to do it, ...
Henrik Schumacher's user avatar
12 votes

Fastest way to generate {{a,a},{a,b},{a,c},{b,b},{b,c},{c,c}} from {a,b,c}

Another possibility is to use Pick: ...
Carl Woll's user avatar
  • 131k
11 votes

Permutations[Range[12]] produces an error instead of a list

Chunks of permutations Here is a LibraryFunction implementation of "CoolMulti" algorithm generating permutations of multisets. The algorithm is described in: A. ...
jkuczm's user avatar
  • 15.1k
11 votes

Delete duplicates from list of lists as if on a necklace

...
kglr's user avatar
  • 396k
11 votes
Accepted

How to generate all involutive permutations?

I wondered how well Involutions has been implemented, so I tried to reimplement it myself. The following implementation can be up to 15 times faster than ...
Henrik Schumacher's user avatar
11 votes
Accepted

How to represent a product of cycles in matrix form?

mat = {Sort @ #, #} & @ PermutationList[a]; MatrixForm @ mat // TeXForm $ \left( \begin{array}{ccccccccccc} 1 & 2 & 3 & 4 & 5 & 6 & ...
kglr's user avatar
  • 396k
11 votes
Accepted

How do I get a list of all possible sums in a list nested list?

Total[Tuples@rn, {2}] should do. For cases where a very large number of tuples would be generated, ...
ciao's user avatar
  • 25.8k
11 votes

Shuffling two lists into each other

s = {2, 3, 6}; a = {a1, a2, a3}; b = {b1, b2, b3, b4}; n = Length[a] + Length[b]; list = Range@n; list[[s]] = a; list[[Complement[Range[n], s]]] = b; list ...
cvgmt's user avatar
  • 74.8k

Only top scored, non community-wiki answers of a minimum length are eligible