-
Notifications
You must be signed in to change notification settings - Fork 72
/
pivot_arithmetic.go
132 lines (118 loc) · 2.98 KB
/
pivot_arithmetic.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// 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
func (P *PivotMatrix) Minus(A MatrixRO) (Matrix, error) {
if P.rows != A.Rows() || P.cols != A.Cols() {
return nil, ErrorDimensionMismatch
}
B := P.DenseMatrix()
B.Subtract(A)
return B, nil
}
func (P *PivotMatrix) Plus(A MatrixRO) (Matrix, error) {
if P.rows != A.Rows() || P.cols != A.Cols() {
return nil, ErrorDimensionMismatch
}
B := P.DenseMatrix()
B.Add(A)
return B, nil
}
/*
Multiply this pivot matrix by another.
*/
func (P *PivotMatrix) Times(A MatrixRO) (Matrix, error) {
if P.Cols() != A.Rows() {
return nil, ErrorDimensionMismatch
}
B := Zeros(P.rows, A.Cols())
for i := 0; i < P.rows; i++ {
k := 0
for ; i != P.pivots[k]; k++ {
}
for j := 0; j < A.Cols(); j++ {
B.Set(i, j, A.Get(k, j))
}
}
return B, nil
}
/*
Multiplication optimized for when two pivots are the operands.
*/
func (P *PivotMatrix) TimesPivot(A *PivotMatrix) (*PivotMatrix, error) {
if P.rows != A.rows {
return nil, ErrorDimensionMismatch
}
newPivots := make([]int, P.rows)
newSign := P.pivotSign * A.pivotSign
for i := 0; i < A.rows; i++ {
newPivots[i] = P.pivots[A.pivots[i]]
}
return MakePivotMatrix(newPivots, newSign), nil
}
/*
Equivalent to PxA, but streamlined to take advantage of the datastructures.
*/
func (P *PivotMatrix) RowPivotDense(A *DenseMatrix) (*DenseMatrix, error) {
if P.rows != A.rows {
return nil, ErrorDimensionMismatch
}
B := Zeros(A.rows, A.cols)
for si := 0; si < A.rows; si++ {
di := P.pivots[si]
Astart := si * A.step
Bstart := di * B.step
for j := 0; j < A.cols; j++ {
B.elements[Bstart+j] = A.elements[Astart+j]
}
}
return B, nil
}
/*
Equivalent to AxP, but streamlined to take advantage of the datastructures.
*/
func (P *PivotMatrix) ColPivotDense(A *DenseMatrix) (*DenseMatrix, error) {
if P.rows != A.cols {
return nil, ErrorDimensionMismatch
}
B := Zeros(A.rows, A.cols)
for i := 0; i < B.rows; i++ {
Astart := i * A.step
Bstart := i * B.step
for sj := 0; sj < B.cols; sj++ {
dj := P.pivots[sj]
B.elements[Bstart+dj] = A.elements[Astart+sj]
}
}
return B, nil
}
/*
Equivalent to PxA, but streamlined to take advantage of the datastructures.
*/
func (P *PivotMatrix) RowPivotSparse(A *SparseMatrix) (*SparseMatrix, error) {
if P.rows != A.rows {
return nil, ErrorDimensionMismatch
}
B := ZerosSparse(A.rows, A.cols)
for index, value := range A.elements {
si, j := A.GetRowColIndex(index)
di := P.pivots[si]
B.Set(di, j, value)
}
return B, nil
}
/*
Equivalent to AxP, but streamlined to take advantage of the datastructures.
*/
func (P *PivotMatrix) ColPivotSparse(A *SparseMatrix) (*SparseMatrix, error) {
if P.rows != A.cols {
return nil, ErrorDimensionMismatch
}
B := ZerosSparse(A.rows, A.cols)
for index, value := range A.elements {
i, sj := A.GetRowColIndex(index)
dj := P.pivots[sj]
B.Set(i, dj, value)
}
return B, nil
}