34
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, ...
28
votes
Accepted
Faster derangements?
Chunks of derangements
Since I've already written library link code generating permutations, generating derangements requires just few tweaks:
...
26
votes
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]
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:
...
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 ...
18
votes
Accepted
StringContainsQ, but anywhere in order
StringMatchQ["aabbc", "*a*c*"]
True
StringMatchQ["aabbc", "*b*a*"]
False
You can also use ...
18
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}}
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 ...
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, ...
17
votes
Accepted
ls Ordering[Ordering[list]] optimal?
No, Ordering[Ordering[list]] not optimal. And yes, there is a faster method:
...
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}];
15
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 ...
14
votes
Permutations of lists of fixed even numbers
The permutation you described is called "derangement". There is a function Derangement in Combinatoricapackage.
...
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$. ...
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 ...
13
votes
Accepted
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....
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 ...
12
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. ...
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:
...
12
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 ...
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, ...
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:
...
11
votes
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 & ...
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,
...
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
...
10
votes
Faster derangements?
Recursive Derangements
Here is my approach at doing this recursively:
...
10
votes
Delete duplicates from list of lists as if on a necklace
This problem is simple enough, that halirutan's brute force method is still $O(n)$ complexity for comparing two necklaces of length $n$ (which is optimal). But I would like to offer an intelligent ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
permutation × 275list-manipulation × 89
combinatorics × 80
group-theory × 29
performance-tuning × 24
matrix × 21
summation × 13
graphs-and-networks × 9
function-construction × 8
pattern-matching × 8
string-manipulation × 7
filtering × 7
tensors × 7
sorting × 7
replacement × 6
algorithm × 6
partitions × 6
mathematical-optimization × 5
code-review × 5
functions × 4
memory × 4
subset × 4
tuples × 4
canonicalization × 4
probability-or-statistics × 3