-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield_256_demo.js
179 lines (154 loc) · 4.37 KB
/
field_256_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
// @flow
/* eslint-env browser */
'use strict';
/* ::
import { Field256Element } from './field_256';
import {
handleVChildError,
parseField256Element,
field256Pattern,
binaryOpInput,
} from './demo_common';
import { inlineMath } from './math';
*/
/*
global
preact,
Field256Element,
handleVChildError,
parseField256Element,
field256Pattern,
binaryOpInput,
inlineMath,
*/
/* ::
type Field256DemoProps = {
header?: HTMLElement,
containerClass?: string,
inputClass?: string,
resultColor: string,
};
type Field256DemoState = {
a: string,
b: string,
};
*/
// eslint-disable-next-line no-unused-vars
class Field256Demo extends preact.Component /* :: <Field256DemoProps, Field256DemoState> */ {
constructor(
// Should be { initialA: a, initialB: b, ...props }, but that is
// unsupported by Safari.
props /* : Field256DemoProps & { initialA: string, initialB: string } */
) {
super(props);
this.state = {
a: props.initialA,
b: props.initialB,
};
}
onAChange(a /* : string */) {
// Should be { ...state, a }, but that is unsupported by Safari.
this.setState(state => ({ a, b: state.b }));
}
onBChange(b /* : string */) {
// Should be { ...state, b }, but that is unsupported by Safari.
this.setState(state => ({ a: state.a, b }));
}
render(props /* : Field256DemoProps */, state /* : Field256DemoState */) {
const { h } = preact;
const children = handleVChildError(() => {
const a = parseField256Element('a', state.a);
const b = parseField256Element('b', state.b);
const sum = a.plus(b);
const product = a.times(b);
const aStr = a.toString(10);
const bStr = b.toString(10);
const color = s => `{\\color{${props.resultColor}}${s}}`;
const sumStr = color(sum.toString(10));
const plusItem = [
inlineMath(
`a \\oplus_{256} b = a \\ominus_{256} b = ${aStr} \\oplus ${bStr} = ${sumStr}\\text{;}`
),
];
const negationStr = color(b.toString(10));
const negationItem = [
inlineMath(`\\ominus_{256}b = b = ${negationStr}\\text{;}`),
];
const minusItem = [
inlineMath(
`a \\ominus_{256} b = a \\oplus_{256} \\ominus_{256}b = a \\oplus_{256} b = ${sumStr}\\text{;}`
),
];
const productStr = color(product.toString(10));
const timesItem = [
inlineMath(
`a \\otimes_{256} b = (${aStr} \\otimes ${bStr}) \\mathbin{\\mathrm{clmod}} 283 = ${productStr}\\text{;}`
),
];
let divItems;
if (b.equals(Field256Element.Zero)) {
divItems = [
[
'and ',
inlineMath(`{b^{-1}}_{256}`),
' and ',
inlineMath(`a \\div_{256} b`),
' are undefined.',
],
];
} else {
const bInv = Field256Element.One.dividedBy(b);
const quotient = a.dividedBy(b);
const bInvStr = bInv.toString(10);
const quotientStr = quotient.toString(10);
const coloredQuotientStr = color(quotientStr);
divItems = [
[
inlineMath(`${bStr} \\otimes_{256} ${bInvStr} = 1\\text{,}`),
' so ',
inlineMath(`{b^{-1}}_{256} = ${color(bInvStr)}\\text{;}`),
],
[
inlineMath(
`a \\oslash_{256} b = a \\otimes_{256} {b^{-1}}_{256} = (${aStr} \\otimes ${bInvStr}) \\mathbin{\\mathrm{clmod}} 283 = ${coloredQuotientStr}\\text{,}`
),
' and indeed ',
inlineMath(
`b \\otimes_{256} (a \\oslash_{256} b) = (${bStr} \\otimes ${quotientStr}) \\mathbin{\\mathrm{clmod}} 283 = ${aStr} = a\\text{.}`
),
],
];
}
return [
'Then',
h(
'ul',
null,
[plusItem, negationItem, minusItem, timesItem]
.concat(divItems)
.map(item => h('li', null, item))
),
];
});
return h(
'div',
{ class: props.containerClass },
props.header,
'Denote operations on the field with ',
inlineMath('256'),
' elements by a ',
inlineMath('{}_{256}'),
' subscript, and let ',
binaryOpInput(
state.a,
state.b,
s => this.onAChange(s),
s => this.onBChange(s),
field256Pattern,
props.inputClass
),
' ',
children
);
}
}