-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcauchy_matrix_demo.js
280 lines (252 loc) · 6.58 KB
/
cauchy_matrix_demo.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// @flow
/* eslint-env browser */
'use strict';
/* ::
import { type Field } from './field';
import { Field256Element } from './field_256';
import { Field257Element } from './field_257';
import { BigRational } from './rational';
import {
VChildError,
handleVChildError,
impossible,
spanNoWrap,
matrixStringLengthBound,
} from './demo_common';
import { Matrix, newCauchyMatrix } from './matrix';
import {
type FieldType,
fieldTypeChoice,
parseField256ElementListCapped,
parseField257ElementListCapped,
parseBigRationalListCapped,
listInput,
} from './matrix_demo_common';
import { inlineMath, displayMath } from './math';
*/
/*
global
preact,
VChildError,
handleVChildError,
impossible,
spanNoWrap,
newCauchyMatrix,
fieldTypeChoice,
parseField256ElementListCapped,
parseField257ElementListCapped,
parseBigRationalListCapped,
listInput,
matrixStringLengthBound,
inlineMath,
displayMath,
*/
const checkForDuplicates = /* :: <T: Field<*>> */ (
x /* : T[] */,
y /* : T[] */
) => {
const xy = x.concat(y);
const name = i => (i < x.length ? `x_{${i}}` : `y_{${i - x.length}}`);
const elems /* : { [string]: number } */ = {};
for (let i = 0; i < xy.length; i += 1) {
const n = xy[i].toString(10);
if (n in elems) {
throw new VChildError([
inlineMath(name(elems[n])),
' and ',
inlineMath(name(i)),
' must be distinct.',
]);
}
elems[n] = i;
}
};
const xyLengthBound = 50;
const parseCauchyMatrix = (
fieldType /* : FieldType */,
xStr /* : string */,
yStr /* : string */
) /* : Matrix<Field256Element> | Matrix<Field257Element> | Matrix<BigRational> */ => {
// Duplicate some code below to work around limitations of flow's
// type system.
switch (fieldType) {
case 'gf256': {
const x = parseField256ElementListCapped('x', xStr, xyLengthBound);
const y = parseField256ElementListCapped('y', yStr, xyLengthBound);
checkForDuplicates(x, y);
return newCauchyMatrix(x, y);
}
case 'gf257': {
const x = parseField257ElementListCapped('x', xStr, xyLengthBound);
const y = parseField257ElementListCapped('y', yStr, xyLengthBound);
checkForDuplicates(x, y);
return newCauchyMatrix(x, y);
}
case 'rational': {
const x = parseBigRationalListCapped('x', xStr, xyLengthBound);
const y = parseBigRationalListCapped('y', yStr, xyLengthBound);
checkForDuplicates(x, y);
return newCauchyMatrix(x, y);
}
default:
return impossible(fieldType);
}
};
const xyInput = (
fieldType /* : FieldType */,
xValue /* : string */,
yValue /* : string */,
onXChange /* : (string) => void */,
onYChange /* : (string) => void */,
inputClass /* : ?string */
) => {
const inputSize = 15;
return [
spanNoWrap(
inlineMath('x = ['),
' ',
listInput(
fieldType,
xValue,
s => onXChange(s),
xyLengthBound,
inputSize,
inputClass
),
' ',
inlineMath(']')
),
' and ',
spanNoWrap(
inlineMath('y = ['),
' ',
listInput(
fieldType,
yValue,
s => onYChange(s),
xyLengthBound,
inputSize,
inputClass
),
' ',
inlineMath(']\\text{.}')
),
];
};
/* ::
type CauchyMatrixDemoProps = {
name: string,
header?: HTMLElement,
containerClass?: string,
inputClass?: string,
allowFieldTypeChanges: boolean,
};
type CauchyMatrixDemoState = {
x: string,
y: string,
fieldType: FieldType,
};
*/
// eslint-disable-next-line no-unused-vars
class CauchyMatrixDemo extends preact.Component /* :: <CauchyMatrixDemoProps, CauchyMatrixDemoState> */ {
constructor(
// Should be { initialX: x, initialY: y, initialFieldType: fieldType, ...props },
// but that is unsupported by Safari.
props /* : CauchyMatrixDemoProps & {
initialX: string,
initialY: string,
initialFieldType: FieldType,
} */
) {
super(props);
this.state = {
x: props.initialX,
y: props.initialY,
fieldType: props.initialFieldType,
};
}
onXChange(x /* : string */) {
// Should be { ...state, x }, but that is unsupported by Safari.
this.setState(state => ({ x, y: state.y, fieldType: state.fieldType }));
}
onYChange(y /* : string */) {
// Should be { ...state, y }, but that is unsupported by Safari.
this.setState(state => ({ x: state.x, y, fieldType: state.fieldType }));
}
onFieldTypeChange(fieldType /* : FieldType */) {
// Should be { ...state, fieldType }, but that is unsupported by Safari.
this.setState(state => ({ x: state.x, y: state.y, fieldType }));
}
render(
props /* : CauchyMatrixDemoProps */,
state /* : CauchyMatrixDemoState */
) {
const children = handleVChildError(() => {
const m = parseCauchyMatrix(state.fieldType, state.x, state.y);
const mStr = m.toLaTeXString();
if (mStr.length > matrixStringLengthBound) {
throw new VChildError([
'The Cauchy matrix constructed from ',
inlineMath('x'),
' and ',
inlineMath('y'),
' is too big to display.',
]);
}
const t = [
'Then, the Cauchy matrix constructed from ',
inlineMath('x'),
' and ',
inlineMath('y'),
' is ',
];
if (m.rows() === m.columns()) {
const mInv /* : typeof m */ = m.inverse();
const mInvStr = mInv.toLaTeXString();
if (mInvStr.length > matrixStringLengthBound) {
throw new VChildError([
'The Cauchy matrix constructed from ',
inlineMath('x'),
' and ',
inlineMath('y'),
' has an inverse which is too big to display.',
]);
}
t.push(displayMath(`${mStr}\\text{,}`));
t.push(' which has inverse ');
t.push(displayMath(`${mInvStr}\\text{.}`));
} else {
t.push(displayMath(`${mStr}\\text{.}`));
}
return t;
});
const preInput = props.allowFieldTypeChanges
? [
'Working over the ',
fieldTypeChoice(
`${props.name}FieldTypeChoice`,
state.fieldType,
fieldType => this.onFieldTypeChange(fieldType),
','
),
' let ',
]
: ['Let '];
return preact.h(
'div',
{ class: props.containerClass },
props.header,
preInput,
xyInput(
state.fieldType,
state.x,
state.y,
s => this.onXChange(s),
s => this.onYChange(s),
props.inputClass
),
' ',
children
);
}
}