forked from gonum/gonum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permutation.go
49 lines (43 loc) · 1.28 KB
/
permutation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright ©2020 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package distmat
import (
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/mat"
)
// UniformPermutation is a uniform distribution over the n!
// permutation matrices of size n×n for a given n.
type UniformPermutation struct {
rnd *rand.Rand
indices []int
}
// NewUniformPermutation constructs a new permutation matrix
// generator using the given random source.
func NewUniformPermutation(src rand.Source) *UniformPermutation {
return &UniformPermutation{rnd: rand.New(src)}
}
// PermTo sets the given matrix to be a random permutation matrix.
// It does not zero the destination's elements, so it is the responsibility
// of the caller to ensure it is correctly conditioned prior to the call.
//
// PermTo panics if dst is not square.
func (p *UniformPermutation) PermTo(dst *mat.Dense) {
r, c := dst.Dims()
if r != c {
panic(mat.ErrShape)
}
if r == 0 {
return
}
if len(p.indices) != r {
p.indices = make([]int, r)
for k := range p.indices {
p.indices[k] = k
}
}
p.rnd.Shuffle(r, func(i, j int) { p.indices[i], p.indices[j] = p.indices[j], p.indices[i] })
for i, j := range p.indices {
dst.Set(i, j, 1)
}
}