-
Notifications
You must be signed in to change notification settings - Fork 72
/
pivot.go
69 lines (61 loc) · 1.33 KB
/
pivot.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2009 The GoMatrix 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 matrix
/*
A space-optimized structure for pivot matrices, ie a matrix with
exactly one 1 in each row and each column.
*/
type PivotMatrix struct {
matrix
pivots []int
pivotSign float64
}
func (P *PivotMatrix) Get(i, j int) float64 {
i = i % P.rows
if i < 0 {
i = P.rows - i
}
j = j % P.cols
if j < 0 {
j = P.cols - j
}
if P.pivots[j] == i {
return 1
}
return 0
}
/*
Convert this PivotMatrix into a DenseMatrix.
*/
func (P *PivotMatrix) DenseMatrix() *DenseMatrix {
A := Zeros(P.rows, P.cols)
for j := 0; j < P.rows; j++ {
A.Set(P.pivots[j], j, 1)
}
return A
}
/*
Convert this PivotMatrix into a SparseMatrix.
*/
func (P *PivotMatrix) SparseMatrix() *SparseMatrix {
A := ZerosSparse(P.rows, P.cols)
for j := 0; j < P.rows; j++ {
A.Set(P.pivots[j], j, 1)
}
return A
}
/*
Make a copy of this PivotMatrix.
*/
func (P *PivotMatrix) Copy() *PivotMatrix { return MakePivotMatrix(P.pivots, P.pivotSign) }
func MakePivotMatrix(pivots []int, pivotSign float64) *PivotMatrix {
n := len(pivots)
P := new(PivotMatrix)
P.rows = n
P.cols = n
P.pivots = pivots
P.pivotSign = pivotSign
return P
}
func (A *PivotMatrix) String() string { return String(A) }