-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_demo_common.js
206 lines (179 loc) · 4.4 KB
/
matrix_demo_common.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
// @flow
/* eslint-env browser */
'use strict';
/* ::
import {
VChildError,
impossible,
parseField256Element,
parseField257Element,
parseListCapped,
field256Pattern,
field257Pattern,
listPattern,
styleNoWrap,
textInput,
} from './demo_common';
import { Field256Element } from './field_256';
import { Field257Element } from './field_257';
import { BigRational } from './rational';
import { inlineMath } from './math';
*/
/*
global
preact,
BigInteger,
VChildError,
impossible,
parseField256Element,
parseField257Element,
parseListCapped,
field256Pattern,
field257Pattern,
listPattern,
styleNoWrap,
textInput,
BigRational,
inlineMath,
*/
/* ::
type FieldType = 'gf256' | 'gf257' | 'rational'
*/
// eslint-disable-next-line no-unused-vars
const fieldTypeChoice = (
name /* : string */,
chosenFieldType /* : FieldType */,
onFieldTypeChange /* : (FieldType) => void */,
trailer /* : string */
) => {
const { h } = preact;
const choiceRadio = (
fieldType /* : FieldType */,
description /* : string */
) =>
h(
'label',
styleNoWrap,
h('input', {
checked: chosenFieldType === fieldType,
name,
type: 'radio',
onChange: () => onFieldTypeChange(fieldType),
}),
description
);
return [
choiceRadio('gf256', ' field with 256 elements'),
' ',
choiceRadio('gf257', ' field with 257 elements'),
' ',
choiceRadio('rational', ` field of rational numbers${trailer}`),
];
};
// eslint-disable-next-line no-unused-vars
const parseField256ElementListCapped = (
name /* : string */,
s /* : string */,
lengthBound /* : number */
) /* : Field256Element[] */ => {
const strs = parseListCapped(name, s, lengthBound);
return strs.map((t, i) => parseField256Element(`{${name}}_{${i}}`, t));
};
// eslint-disable-next-line no-unused-vars
const parseField257ElementListCapped = (
name /* : string */,
s /* : string */,
lengthBound /* : number */
) /* : Field257Element[] */ => {
const strs = parseListCapped(name, s, lengthBound);
return strs.map((t, i) => parseField257Element(`{${name}}_{${i}}`, t));
};
const rationalDigitBound = 25;
const parseBigRational = (
name /* : string */,
s /* : string */
) /* : BigRational */ => {
const match = s.match(/^([+-]?[0-9]+)(?:\s*\/\s*([+-]?[0-9]+))?$/);
if (!match) {
throw new VChildError([
inlineMath(name),
' is not a valid rational number.',
]);
}
const n = new BigInteger(match[1], 10);
if (n.abs().toString(10).length > rationalDigitBound) {
throw new VChildError([
'The numerator of ',
inlineMath(name),
' has too big an absolute value.',
]);
}
let d;
if (match[2]) {
d = new BigInteger(match[2], 10);
if (d.signum() === 0) {
throw new VChildError([
inlineMath(name),
' cannot have a zero denominator.',
]);
}
if (d.abs().toString(10).length > rationalDigitBound) {
throw new VChildError([
'The denominator of ',
inlineMath(name),
' has too big an absolute value.',
]);
}
} else {
d = BigInteger.ONE;
}
return new BigRational(n, d);
};
// eslint-disable-next-line no-unused-vars
const parseBigRationalListCapped = (
name /* : string */,
s /* : string */,
lengthBound /* : number */
) /* : BigRational[] */ => {
const strs = parseListCapped(name, s, lengthBound);
return strs.map((t, i) => parseBigRational(`{${name}}_{${i}}`, t));
};
const intPattern = `[+-]?0*[0-9]{1,${rationalDigitBound}}`;
const intOrRatPattern = `${intPattern}(\\s*\\/\\s*${intPattern})?`;
// eslint-disable-next-line no-unused-vars
const listInput = (
fieldType /* : FieldType */,
value /* : string */,
onChange /* : (string) => void */,
lengthBound /* : number */,
size /* : number */,
inputClass /* : ?string */
) => {
let pattern;
switch (fieldType) {
case 'gf256':
pattern = field256Pattern;
break;
case 'gf257':
pattern = field257Pattern;
break;
case 'rational':
pattern = intOrRatPattern;
break;
default:
pattern = impossible(fieldType);
}
pattern = listPattern(pattern, lengthBound);
return textInput(value, onChange, size, pattern, inputClass);
};
/* ::
export {
fieldTypeChoice,
parseField256ElementListCapped,
parseField257ElementListCapped,
parseBigRational,
parseBigRationalListCapped,
listInput,
};
export type { FieldType };
*/