-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathderiv2.ts
More file actions
233 lines (209 loc) · 6.21 KB
/
deriv2.ts
File metadata and controls
233 lines (209 loc) · 6.21 KB
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
export type Leaf =
| { type: "x" }
| { type: "fn"; name: string; ticks: number; body: Expr }
export type Prod = { type: "prod"; const: number; items: Leaf[] }
export type Expr = { type: "sum"; items: Prod[] }
const INITIAL: Expr = {
type: "sum",
items: [
{
type: "prod",
const: 1,
items: [
{
type: "fn",
name: "f",
ticks: 0,
body: {
type: "sum",
items: [
{
type: "prod",
const: 1,
items: [
{
type: "fn",
name: "g",
ticks: 0,
body: {
type: "sum",
items: [
{
type: "prod",
const: 1,
items: [
{
type: "x",
},
],
},
],
},
},
],
},
],
},
},
],
},
],
}
const ZERO: Expr = {
type: "sum",
items: [
{
type: "prod",
const: 0,
items: [],
},
],
}
export function multiply(sums: Expr[]): Expr {
if (sums.length == 0) {
return { type: "sum", items: [] }
}
return sums.reduce(
(a, b): Expr => ({
type: "sum",
items: a.items
.flatMap<Prod>((a): Prod[] =>
b.items.map(
(b): Prod => ({
type: "prod",
const: a.const * b.const,
items: [...a.items, ...b.items],
}),
),
)
.filter((x) => x.const != 0),
}),
)
}
export function derivLeaf(leaf: Leaf): Expr {
switch (leaf.type) {
case "x":
return {
type: "sum",
items: [{ type: "prod", const: 1, items: [] }],
}
case "fn":
return multiply([
derivExpr(leaf.body),
exprOf({ ...leaf, ticks: leaf.ticks + 1 }),
])
}
}
export function exprOf(leaf: Leaf): Expr {
return { type: "sum", items: [{ type: "prod", const: 1, items: [leaf] }] }
}
export function derivProdOne(a: Expr, b: Leaf): Expr {
return {
type: "sum",
items: [
...multiply([derivExpr(a), exprOf(b)]).items,
...multiply([derivLeaf(b), a]).items,
],
}
}
export function derivProd(prod: Prod): Expr {
if (prod.items.length == 0) {
return ZERO
} else if (prod.items.length == 1) {
return derivLeaf(prod.items[0]!)
} else if (prod.items.length == 2) {
return derivProdOne(exprOf(prod.items[0]!), prod.items[1]!)
}
return prod.items
.slice(1)
.reduce<Expr>((a, b) => derivProdOne(a, b), exprOf(prod.items[0]!))
}
export function derivExpr(sum: Expr): Expr {
return { type: "sum", items: sum.items.flatMap((x) => derivProd(x).items) }
}
export function isJustX(expr: Expr) {
return (
expr.items.length == 1
&& expr.items[0]!.const == 1
&& expr.items[0]!.items.length == 1
&& expr.items[0]!.items[0]!.type == "x"
)
}
export function strLeaf(expr: Leaf): string {
switch (expr.type) {
case "x":
return "x"
case "fn":
return (
expr.name
+ "'".repeat(expr.ticks)
+ (isJustX(expr.body) ? "" : "(" + strInner(expr.body) + ")")
)
}
}
export function strProd(expr: Prod): string {
if (expr.const == 0) {
return "0"
} else if (expr.const == 1) {
if (expr.items.length) {
return expr.items.map(strLeaf).join(" ")
} else {
return "1"
}
} else {
if (expr.items.length) {
return expr.const + " " + expr.items.map(strLeaf).join(" ")
} else {
return expr.const.toString()
}
}
}
export function strInner(expr: Expr): string {
return expr.items.map(strProd).join(" + ") || "0"
}
export function str(expr: Expr) {
return "\n " + expr.items.map(strProd).join("\n+ ")
}
export function sort(a: Leaf, b: Leaf) {
return (
a.type == "x" ? -1
: b.type == "x" ? 1
: a.name < b.name ? -1
: b.name < a.name ? 1
: a.ticks < b.ticks ? -1
: b.ticks < a.ticks ? 1
: 0
)
}
export function sortLeaf(leaf: Leaf): Leaf {
switch (leaf.type) {
case "x":
return leaf
case "fn":
return { ...leaf, body: sortExpr(leaf.body) }
}
}
export function sortProd(prod: Prod): Prod {
return {
type: "prod",
const: prod.const,
items: prod.items.map(sortLeaf).sort(sort),
}
}
export function sortProdInSum(a: Prod, b: Prod) {
return (
a.items.length == 0 ? -1
: b.items.length == 0 ? 1
: sort(a.items[0]!, b.items[0]!) || a.const - b.const
)
}
export function sortExpr(prod: Expr): Expr {
return { type: "sum", items: prod.items.map(sortProd).sort(sortProdInSum) }
}
export function deriv(expr: Expr): Expr {
return sortExpr(derivExpr(expr))
}
console.log(str(INITIAL))
console.log(str(deriv(INITIAL)))
console.log(str(deriv(deriv(INITIAL))))
console.log(str(deriv(deriv(deriv(INITIAL)))))