-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrow_reduce.js
199 lines (175 loc) · 4.3 KB
/
row_reduce.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// @flow
/* eslint-env browser */
'use strict';
/* ::
import { type Field } from './field';
import { Matrix } from './matrix';
*/
/* global Matrix */
/* ::
type RowReduceInitialState<T: Field<*>> = {
type: 'initial',
aLeft: Matrix<T>,
aRight: Matrix<T>,
};
type RowReduceSwapState<T: Field<*>> = {
type: 'swap',
i: number,
aLeftPrev: Matrix<T>,
aRightPrev: Matrix<T>,
aLeft: Matrix<T>,
aRight: Matrix<T>,
rowA: number,
rowB: number,
};
type RowReduceSingularState<T: Field<*>> = {
type: 'singular',
aLeft: Matrix<T>,
aRight: Matrix<T>,
};
type RowReduceDivideState<T: Field<*>> = {
type: 'divide',
i: number,
aLeftPrev: Matrix<T>,
aRightPrev: Matrix<T>,
aLeft: Matrix<T>,
aRight: Matrix<T>,
row: number,
divisor: Field<T>,
};
type RowReduceSubtractScaledState<T: Field<*>> = {
type: 'subtractScaled',
i: number,
j: number,
aLeftPrev: Matrix<T>,
aRightPrev: Matrix<T>,
aLeft: Matrix<T>,
aRight: Matrix<T>,
rowDest: number,
rowSrc: number,
scale: ?Field<T>,
};
type RowReduceInverseFoundState<T: Field<*>> = {
type: 'inverseFound',
aLeft: Matrix<T>,
aRight: Matrix<T>,
};
type RowReduceState<T: Field<*>> =
| RowReduceInitialState<T>
| RowReduceSwapState<T>
| RowReduceSingularState<T>
| RowReduceDivideState<T>
| RowReduceSubtractScaledState<T>
| RowReduceInverseFoundState<T>;
*/
// eslint-disable-next-line no-unused-vars
const rowReduceInitialState = /* :: <T: Field<*>> */ (
m /* : Matrix<T> */
) /* : RowReduceInitialState<T> */ => {
const rows = m.rows();
const columns = m.columns();
if (rows !== columns) {
throw new Error('Non-square matrix');
}
const aRight = new Matrix(
rows,
rows,
(i, j) => (i === j ? m.oneElement() : m.zeroElement())
);
return { type: 'initial', aLeft: m, aRight };
};
// eslint-disable-next-line no-unused-vars
const rowReduceNextState = /* :: <T: Field<*>> */ (
state /* : RowReduceState<T> */
) /* : RowReduceState<T> */ => {
if (state.type === 'singular' || state.type === 'inverseFound') {
throw new Error('Unexpected terminal state');
}
// We could save some work by storing the current index, but that
// makes things more complicated for no real gain, since we'll be
// working with small matrices anyway.
const { aLeft, aRight } = state;
const zero = aLeft.zeroElement();
const one = aLeft.oneElement();
for (let i = 0; i < aLeft.rows(); i += 1) {
const pivot = aLeft.at(i, i);
if (pivot.equals(zero)) {
for (let j = i + 1; j < aLeft.rows(); j += 1) {
if (!aLeft.at(j, i).equals(zero)) {
return {
type: 'swap',
i,
aLeftPrev: aLeft,
aRightPrev: aRight,
aLeft: aLeft.swapRows(i, j),
aRight: aRight.swapRows(i, j),
rowA: i,
rowB: j,
};
}
}
return {
type: 'singular',
aLeft,
aRight,
};
}
if (!pivot.equals(one)) {
return {
type: 'divide',
i,
aLeftPrev: aLeft,
aRightPrev: aRight,
aLeft: aLeft.divideRow(i, pivot),
aRight: aRight.divideRow(i, pivot),
row: i,
divisor: pivot,
};
}
for (let j = i + 1; j < aLeft.rows(); j += 1) {
const t = aLeft.at(j, i);
if (!t.equals(zero)) {
return {
type: 'subtractScaled',
i: j,
j: i,
aLeftPrev: aLeft,
aRightPrev: aRight,
aLeft: aLeft.subtractScaledRow(j, i, t),
aRight: aRight.subtractScaledRow(j, i, t),
rowDest: j,
rowSrc: i,
scale: t.equals(one) ? null : t,
};
}
}
}
for (let i = aLeft.rows() - 1; i >= 0; i -= 1) {
for (let j = i - 1; j >= 0; j -= 1) {
const t = aLeft.at(j, i);
if (!t.equals(zero)) {
return {
type: 'subtractScaled',
i: j,
j: i,
aLeftPrev: aLeft,
aRightPrev: aRight,
aLeft: aLeft.subtractScaledRow(j, i, t),
aRight: aRight.subtractScaledRow(j, i, t),
rowDest: j,
rowSrc: i,
scale: t.equals(one) ? null : t,
};
}
}
}
return {
type: 'inverseFound',
aLeft,
aRight,
};
};
/* ::
export type { RowReduceState };
export { rowReduceInitialState, rowReduceNextState };
*/