@khanacademy/kmath 0.0.0-PR443-20230328215601

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.babelrc.js ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * HACK(somewhatabstract): Due to https://github.com/facebook/jest/issues/11741,
3
+ * we need to have this file, or updating inline snapshots can fail rather
4
+ * cryptically.
5
+ *
6
+ * We should remove this when jest is fixed.
7
+ */
8
+ module.exports = require("../../config/build/babel.config");
package/.eslintrc.js ADDED
@@ -0,0 +1,12 @@
1
+ /* eslint-disable @typescript-eslint/no-var-requires */
2
+ /* eslint-disable import/no-commonjs */
3
+ const path = require("path");
4
+
5
+ module.exports = {
6
+ rules: {
7
+ "import/no-extraneous-dependencies": [
8
+ "error",
9
+ {packageDir: [__dirname, path.join(__dirname, "../../")]},
10
+ ],
11
+ },
12
+ };
package/CHANGELOG.md ADDED
@@ -0,0 +1,37 @@
1
+ # @khanacademy/kmath
2
+
3
+ ## 0.0.0-PR443-20230328215601
4
+
5
+ ### Minor Changes
6
+
7
+ - 4ef5607d: Migrate source code to TypeScript
8
+
9
+ ## 0.0.8
10
+
11
+ ### Patch Changes
12
+
13
+ - f567f660: Update the eslint config to look at both the package.json for the package and the one from the root
14
+
15
+ ## 0.0.7
16
+
17
+ ### Patch Changes
18
+
19
+ - bf180fe1: Fix our use of import/no-extraneous-dependencies
20
+
21
+ ## 0.0.6
22
+
23
+ ### Patch Changes
24
+
25
+ - 98d283ff: Fix storybook
26
+
27
+ ## 0.0.5
28
+
29
+ ### Patch Changes
30
+
31
+ - 1c1f7717: Enhance Flow types for kmath's vector and point
32
+
33
+ ## 0.0.4
34
+
35
+ ### Patch Changes
36
+
37
+ - a46d62bf: Remove .tgz file that shouldn't be packaged
package/README.md ADDED
@@ -0,0 +1,504 @@
1
+ <h1 align="center">
2
+ <img alt="kmath" src="logo.svg" width="50" /> <br />
3
+ kmath - Javascript Numeric Math Utilities
4
+ </h1>
5
+
6
+ ## Overview
7
+
8
+ kmath is a collection of Javascript utility functions for performing
9
+ numeric (rather than algebraic) math that isn't built into Javascript,
10
+ especially geometric calculations.
11
+
12
+ For example, some computations are easy to express using vectors, but
13
+ difficult to express in terms of raw real number variables. kmath lets
14
+ you write vector-, point-, line-, or ray-based math naturally, and
15
+ also has some real number utility methods that might be useful if
16
+ you're writing a math-heavy Javascript application.
17
+
18
+ kmath emphasizes simplicity and interoperability in the interfaces
19
+ provided. All kmath interfaces use standard Javascript types, usually
20
+ numbers, arrays of numbers, or arrays of arrays of numbers. This means
21
+ interoping with other systems (other libraries, or sending kmath types
22
+ over json) is easy, but kmath doesn't have it's own object-oriented
23
+ types, which may be inconvenient (for example, in debugging, you'll
24
+ just see Arrays rather than, for example, Vectors).
25
+
26
+ kmath also focuses on being a high quality library of a few functions,
27
+ rather than a large library of many functions which are less unified.
28
+
29
+ kmath emphasizes simplicity in design over performance, when those two
30
+ are at odds. If you are writing code in an inner loop in an allocation-
31
+ sensitive environment, kmath might not be for you. Each method is
32
+ pure and allocates a new array for the return value.
33
+
34
+ ## Getting started
35
+
36
+ After cloning or downloading kmath, you can install it by running
37
+ `npm install` or `make install`.
38
+
39
+ To play around with the available interfaces, you can load kmath
40
+ into a Node repl:
41
+
42
+ ```js
43
+ import {vector} from "@khanacademy/kmath";
44
+ vector.add([1, 2], [3, 4]) // [4, 6]
45
+ ```
46
+
47
+ ## Usage overview
48
+
49
+ kmath has 5 basic types:
50
+
51
+ - `number`
52
+ - `vector`
53
+ - `point`
54
+ - `ray`
55
+ - `line`
56
+
57
+ Each has its own representation:
58
+
59
+ - `number`: a js number (i.e. `5`)
60
+ - `vector`: a js array of numbers (i.e. `[5, 6]`)
61
+ - `point`: a js array of numbers (i.e. `[5, 6]`)
62
+ - `ray`: a js array of two points (i.e. `[[5, 6], [1, 2]]`)
63
+ - `line`: a js array of two points (i.e. `[[5, 6], [1, 2]]`)
64
+
65
+ kmath functions usually take an argument of the corresponding type as
66
+ the first parameter, and other parameters afterwards. For example, to
67
+ rotate the point `[1, 1]` by 90 degrees around `[0, 0]`, one might use:
68
+
69
+ ```js
70
+ point.rotateDeg([1, 1], 90, [0, 0])
71
+ ```
72
+
73
+ Documentation for specific functions for each type is provided below.
74
+
75
+ ## number
76
+
77
+ #### `number.DEFAULT_TOLERANCE === 1e-9`
78
+
79
+ The default tolerance to kmath functions.
80
+
81
+ #### `number.EPSILON === Math.pow(2, -42)`
82
+
83
+ A small number. Not machine epsilon, but a reasonable amount of error
84
+ more than generally accrued by doing repeated floating point math.
85
+
86
+ #### `number.is(maybeANumber)`
87
+
88
+ Returns true if the argument is a javascript number.
89
+
90
+ #### `number.equal(number1, number2, [tolerance])`
91
+
92
+ Compares whether number1 and number2 are equal to each other, within
93
+ a difference of tolerance, which defaults to `number.DEFAULT_TOLERANCE`.
94
+
95
+ #### `number.sign(aNumber, [tolerance])`
96
+
97
+ Returns 0 if the number is equal to 0 within `tolerance`, or -1 if the
98
+ number is less than 0, or 1 if the number is greater than 0.
99
+
100
+ #### `number.isInteger(aNumber, tolerance)`
101
+
102
+ Returns true if `aNumber` is within `tolerance` difference of an integer.
103
+ `tolerance` defaults to `number.DEFAULT_TOLERANCE`.
104
+
105
+ #### `number.round(aNumber, precision)`
106
+
107
+ Rounds `aNumber` to `precision` decimal places.
108
+
109
+ #### `number.roundTo(aNumber, increment)`
110
+
111
+ Rounds `aNumber` to the nearest `increment`.
112
+
113
+ For example, `number.roundTo(1.4, 0.5)` would return `1.5`
114
+
115
+ #### `number.floorTo(aNumber, increment)`
116
+
117
+ Returns the nearest multiple of `increment` that is no greater than
118
+ `aNumber`.
119
+
120
+ #### `number.ceilTo(aNumber, increment)`
121
+
122
+ Returns the nearest multiple of `increment` that is no smaller than
123
+ `aNumber`.
124
+
125
+ #### `number.toFraction(decimal, [tolerance], [maxDenominator])`
126
+
127
+ Returns an array containing two elements, `[n, d]`, a numerator and
128
+ denominator representing a fraction `n/d` that is within `tolerance`
129
+ of `decimal`.
130
+
131
+ If no fraction with a denominator less than or equal to `maxDenominator`
132
+ is found, `[decimal, 1]` is returned.
133
+
134
+ `tolerance` defaults to `number.EPSILON`. `maxDenominator` defaults to
135
+ `1000`.
136
+
137
+ ## vector
138
+
139
+ #### `vector.is(maybeAVector, [dimension])`
140
+
141
+ Returns true if `maybeAVector` is an array of numbers. If `dimension` is specified,
142
+ only returns true if `maybeAVector` is a vector with dimension `dimension`.
143
+
144
+ #### `vector.equal(v1, v2, [tolerance])`
145
+
146
+ Returns true if `v1` and `v2` are equal within `tolerance`. If `tolerance`
147
+ is not specified, `number.DEFAULT_TOLERANCE` is used. Each dimension
148
+ is compared individually, rather than comparing length and direction.
149
+
150
+ If `v1` and `v2` have different lengths, this function returns `false`.
151
+
152
+ ```js
153
+ vector.equal([1, 2], [1, 3])
154
+ => false
155
+ vector.equal([1, 2], [1, 2, 3])
156
+ => false
157
+ vector.equal([1, 2], [1, 2])
158
+ => true
159
+ ```
160
+
161
+ #### `vector.codirectional(v1, v2, [tolerance])`
162
+
163
+ Returns true if `v1` and `v2` have the same direction within `tolerance`.
164
+ If `tolerance` is unspecified, `number.DEFAULT_TOLERANCE` is used.
165
+ Note that tolerance is checked after normalization.
166
+
167
+ If `v1` and `v2` are different lengths, this function returns `false`,
168
+ regardless of whether they are codirectional in the existing dimensions.
169
+
170
+ This function defines the zero-vector as trivially codirectional with
171
+ every vector.
172
+
173
+ ```js
174
+ vector.codirectional([1, 2], [2, 4])
175
+ => true
176
+ vector.codirectinoal([1, 2], [0, 0])
177
+ => true
178
+ vector.codirectional([1, 2], [1, 2, 0])
179
+ => false
180
+ vector.codirectional([1, 2], [-2, -4])
181
+ => false
182
+ ```
183
+
184
+ #### `vector.colinear(v1, v2, [tolerance])`
185
+
186
+ Returns true if `v1` and `v2` lie along the same line, regardless of
187
+ direction. This is equivalent to either `v1` and `v2` being codirectional,
188
+ or `v1` and `-v2` being codirectional, or whether there is some
189
+ `scaleFactor` such that `vector.equal(vector.scale(v1, scaleFactor), v2)`
190
+ is true.
191
+
192
+ ```js
193
+ kmath.vector.colinear([1, 2], [-2, -4])
194
+ => true
195
+ ```
196
+
197
+ #### `vector.normalize(v)`
198
+
199
+ Scales the cartesian vector `v` to be `1` unit long.
200
+
201
+ #### `vector.length(v)`
202
+
203
+ Returns the length of the cartesian vector `v`.
204
+
205
+ #### `vector.dot(v1, v2)`
206
+
207
+ Returns the dot product of cartesian vectors `v1` and `v2`.
208
+
209
+ #### `vector.add(*vectors)`
210
+
211
+ Adds multiple cartesian vectors together.
212
+
213
+ ```js
214
+ kmath.vector.add([1, 2], [3, 4])
215
+ => [4, 6]
216
+ ```
217
+
218
+ #### `vector.subtract(v1, v2)`
219
+
220
+ Subtracts the cartesian vector `v2` from the cartesian vector `v1`.
221
+
222
+ ```js
223
+ kmath.vector.subtract([4, 6], [1, 2])
224
+ => [3, 4]
225
+ ```
226
+
227
+ #### `vector.negate(v)`
228
+
229
+ Negates the cartesian vector `v`. This has the same effect as scaling
230
+ `v` by `-1` or reversing the direction of `v`.
231
+
232
+ #### `vector.scale(v, scalar)`
233
+
234
+ Scales the cartesian vector `v` by `scalar`.
235
+
236
+ ```js
237
+ kmath.vector.scale([1, 2], 2)
238
+ => [2, 4]
239
+ ```
240
+
241
+ #### `vector.polarRadFromCart(v)`
242
+
243
+ Returns a polar vector `[length, angle]` from the two-dimensional cartesian
244
+ vector `v`, where `angle` is measured in radians.
245
+
246
+ ```js
247
+ kmath.vector.polarRadFromCart([1, 1])
248
+ => [1.4142135623730951, 0.7853981633974483]
249
+ ```
250
+
251
+ #### `vector.polarDegFromCart(v)`
252
+
253
+ Returns a polar vector `[length, angle]` from the two-dimensional cartesian
254
+ vector `v`, where `angle` is measured in degrees.
255
+
256
+ ```js
257
+ kmath.vector.polarDegFromCart([0, 2])
258
+ => [2, 90]
259
+ ```
260
+
261
+ #### `vector.cartFromPolarRad(polar)` or `vector.cartFromPolarRad(length, angle)`
262
+
263
+ Returns a two-dimensional cartesian vector from the polar vector input,
264
+ where the input angle is measured in radians.
265
+
266
+ ```js
267
+ kmath.vector.cartFromPolarRad([2, Math.PI])
268
+ => [-2, 0] // Note: a very small nonzero number is actually returned here,
269
+ // due to numerical inaccuracy.
270
+ kmath.vector.cartFromPolarRad(Math.pow(2, 0.5), Math.PI/4)
271
+ => [1, 1]
272
+ ```
273
+
274
+ #### `vector.cartFromPolarDeg(polar)` or `vector.cartFromPolarDeg(length, angle)`
275
+
276
+ Returns a two-dimensional cartesian vector from the polar vector input,
277
+ where the input angle is measured in degrees.
278
+
279
+ ```js
280
+ kmath.vector.cartFromPolarRad([2, 90])
281
+ => [-2, 0] // Note: a very small nonzero number is actually returned here,
282
+ // due to numerical inaccuracy.
283
+ kmath.vector.cartFromPolarRad(Math.pow(2, 0.5), 45)
284
+ => [1, 1]
285
+ ```
286
+
287
+ #### `vector.rotateRad(v, angle)`
288
+
289
+ Returns the rotation of the cartesian vector `v` by `angle` radians.
290
+
291
+ #### `vector.rotateDeg(v, angle)`
292
+
293
+ Returns the rotation of the cartesian vector `v` by `angle` degrees.
294
+
295
+ #### `vector.angleRad(v1, v2)`
296
+
297
+ Returns the angle between the directions of cartesian vectors
298
+ `v1` and `v2` in radians.
299
+
300
+ #### `vector.angleDeg(v1, v2)`
301
+
302
+ Returns the angle between the directions of cartesian vectors
303
+ `v1` and `v2` in radians.
304
+
305
+ #### `vector.projection(v1, v2)`
306
+
307
+ Returns the projection of `v1` along the direction of `v2`
308
+
309
+ #### `vector.round(v, precision)`
310
+
311
+ Rounds each dimension of `v` to `precision` decimal places.
312
+
313
+ #### `vector.roundTo(v, increment)`
314
+
315
+ Rounds each dimension of `v` to the nearest `increment`.
316
+
317
+ #### `vector.floorTo(v, increment)`
318
+
319
+ Floors each dimension of `v` to the nearest `increment`.
320
+
321
+ #### `vector.ceilTo(v, increment)`
322
+
323
+ Ceils each dimension of `v` to the nearest `increment`.
324
+
325
+ ## kmath.point
326
+
327
+ #### `point.is(maybeAPoint, [length])`
328
+
329
+ Returns true if `maybeAPoint` is an array of numbers. If length is specified,
330
+ only returns true if `maybeAPoint` is a point of dimension `length`.
331
+
332
+ #### `point.equal(p1, p2, [tolerance])`
333
+
334
+ Returns true if `p1` and `p2` are equal within `tolerance`. If `tolerance`
335
+ is not specified, `kmath.number.DEFAULT_TOLERANCE` is used. Each dimension
336
+ is compared individually.
337
+
338
+ If `p1` and `p2` have different lengths, this function returns `false`.
339
+
340
+ ```js
341
+ kmath.point.equal([1, 2], [1, 3])
342
+ => false
343
+ kmath.point.equal([1, 2], [1, 2, 3])
344
+ => false
345
+ kmath.point.equal([1, 2], [1, 2])
346
+ => true
347
+ ```
348
+
349
+ #### `point.addVector(p, *vectors)` or `point.addVectors(p, *vectors)`
350
+
351
+ Returns the point created by adding the cartesian vectors `*vectors`
352
+ to the cartesian point `p`.
353
+
354
+ #### `point.subtractVector(p, v)`
355
+
356
+ Returns the point created by subtracting the cartesian vectors `v`
357
+ to the cartesian point `p`.
358
+
359
+ #### `point.distanceToPoint(p1, p2)`
360
+
361
+ Returns the distance between `p1` and `p2`.
362
+
363
+ #### `point.distanceToLine(p, theLine)`
364
+
365
+ Returns the distance between `p` and the line `theLine`.
366
+
367
+ For example, to find the distance from the origin to the
368
+ `y = 5` line, one could write:
369
+
370
+ ```js
371
+ kmath.point.distanceToLine([0, 0], [[-1, 5], [1, 5]])
372
+ => 5
373
+ ```
374
+
375
+ #### `point.reflectOverLine(p, theLine)`
376
+
377
+ Returns the reflection of `p` over the line `theLine`.
378
+
379
+ For example, to reflect the origin over the line `y = 5`,
380
+ one could write:
381
+
382
+ ```js
383
+ kmath.point.reflectOverLine([0, 0], [[-1, 5], [1, 5]])
384
+ => [0, 10]
385
+ ```
386
+
387
+ #### `point.compare(p1, p2, [equalityTolerance])`
388
+
389
+ Compares two points, returning -1, 0, or 1, for use with
390
+ Array.prototype.sort
391
+
392
+ Note: This technically doesn't satisfy the total-ordering
393
+ requirements of Array.prototype.sort unless equalityTolerance
394
+ is 0. In some cases very close points that compare within a
395
+ few equalityTolerances could appear in the wrong order.
396
+
397
+ #### `point.polarRadFromCart(p)`
398
+
399
+ Returns a polar point `[length, angle]` from the two-dimensional cartesian
400
+ point `v`, where `angle` is measured in radians.
401
+
402
+ ```js
403
+ kmath.point.polarRadFromCart([1, 1])
404
+ => [1.4142135623730951, 0.7853981633974483]
405
+ ```
406
+
407
+ #### `point.polarDegFromCart(p)`
408
+
409
+ Returns a polar point `[length, angle]` from the two-dimensional cartesian
410
+ point `v`, where `angle` is measured in degrees.
411
+
412
+ ```js
413
+ kmath.point.polarDegFromCart([0, 2])
414
+ => [2, 90]
415
+ ```
416
+
417
+ #### `point.cartFromPolarRad(polar)` or `point.cartFromPolarRad(length, angle)`
418
+
419
+ Returns a two-dimensional cartesian point from the polar point input,
420
+ where the input angle is measured in radians.
421
+
422
+ ```js
423
+ kmath.point.cartFromPolarRad(2, Math.PI)
424
+ => [-2, 0] // Note: a very small nonzero number is actually returned here,
425
+ // due to numerical inaccuracy.
426
+ kmath.point.cartFromPolarRad(Math.pow(2, 0.5), Math.PI/4)
427
+ => [1, 1]
428
+ ```
429
+
430
+ #### `point.cartFromPolarDeg(polar)` or `point.cartFromPolarDeg(length, angle)`
431
+
432
+ Returns a two-dimensional cartesian point from the polar point input,
433
+ where the input angle is measured in degrees.
434
+
435
+ ```js
436
+ kmath.point.cartFromPolarRad([2, 90])
437
+ => [-2, 0] // Note: a very small nonzero number is actually returned here,
438
+ // due to numerical inaccuracy.
439
+ kmath.point.cartFromPolarRad(Math.pow(2, 0.5), 45)
440
+ => [1, 1]
441
+ ```
442
+
443
+ #### `point.rotateRad(p, angle, center)`
444
+
445
+ Returns the rotation of the two-dimensional point `v` by `angle` radians
446
+ around the point `center`
447
+
448
+ #### `point.rotateDeg(p, angle, center)`
449
+
450
+ Returns the rotation of the two-dimensional point `v` by `angle` degrees
451
+ around the point `center`.
452
+
453
+ #### `point.round(p, precision)`
454
+
455
+ Rounds each dimension of `p` to `precision` decimal places.
456
+
457
+ #### `point.roundTo(p, increment)`
458
+
459
+ Rounds each dimension of `p` to the nearest `increment`.
460
+
461
+ #### `point.floorTo(p, increment)`
462
+
463
+ Floors each dimension of `p` to the nearest `increment`.
464
+
465
+ #### `point.ceilTo(p, increment)`
466
+
467
+ Ceils each dimension of `p` to the nearest `increment`.
468
+
469
+ ## kmath.ray
470
+
471
+ #### `ray.equal(r1, r2, [tolerance])`
472
+
473
+ Returns true if rays `r1` and `r2` are equal within `tolerance`.
474
+
475
+ If unspecified, `tolerance` defaults to `kmath.number.DEFAULT_TOLERANCE`.
476
+
477
+ ## kmath.line
478
+
479
+ #### `line.equal(line1, line2, [tolerance])`
480
+
481
+ Returns true if lines `line1` and `line2` are equal within `tolerance`.
482
+
483
+ If unspecified, `tolerance` defaults to `kmath.number.DEFAULT_TOLERANCE`.
484
+
485
+ #### `line.midpoint(theLine)`
486
+
487
+ Returns the midpoint of the line `theLine`.
488
+
489
+ ```js
490
+ kmath.line.midpoint([[0, 5], [5, 0]])
491
+ => [2.5, 2.5]
492
+ ```
493
+
494
+ #### `line.distanceToPoint(theLine, p)`
495
+
496
+ Returns the distance between `theLine` and point `p`.
497
+
498
+ #### `line.reflectPoint(theLine, p)`
499
+
500
+ Returns the reflection of `p` over `theLine`.
501
+
502
+ ## License
503
+
504
+ MIT. See the [LICENSE](../../LICENSE) file for more information.