@khanacademy/kmath 0.0.4 → 0.0.6

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 CHANGED
@@ -5,4 +5,4 @@
5
5
  *
6
6
  * We should remove this when jest is fixed.
7
7
  */
8
- module.exports = require("../../config/build/babel.config");
8
+ module.exports = require("../../config/build/babel.config.js");
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @khanacademy/kmath
2
2
 
3
+ ## 0.0.6
4
+
5
+ ### Patch Changes
6
+
7
+ - 98d283ff: Fix storybook
8
+
9
+ ## 0.0.5
10
+
11
+ ### Patch Changes
12
+
13
+ - 1c1f7717: Enhance Flow types for kmath's vector and point
14
+
3
15
  ## 0.0.4
4
16
 
5
17
  ### Patch Changes
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
- # kmath
2
-
3
- Javascript Numeric Math Utilities
1
+ <h1 align="center">
2
+ <img alt="kmath" src="logo.svg" width="50" /> <br />
3
+ kmath - Javascript Numeric Math Utilities
4
+ </h1>
4
5
 
5
6
  ## Overview
6
7
 
@@ -38,38 +39,40 @@ After cloning or downloading kmath, you can install it by running
38
39
  To play around with the available interfaces, you can load kmath
39
40
  into a Node repl:
40
41
 
41
- $ node
42
- > var kmath = require("@khanacademy/kmath");
43
- > kmath.vector.add([1, 2], [3, 4])
44
- [4, 6]
42
+ ```js
43
+ import {vector} from "@khanacademy/kmath";
44
+ vector.add([1, 2], [3, 4]) // [4, 6]
45
+ ```
45
46
 
46
47
  ## Usage overview
47
48
 
48
49
  kmath has 5 basic types:
49
50
 
50
- - number
51
- - vector
52
- - point
53
- - ray
54
- - line
51
+ - `number`
52
+ - `vector`
53
+ - `point`
54
+ - `ray`
55
+ - `line`
55
56
 
56
57
  Each has its own representation:
57
58
 
58
- - number: a js number (i.e. `5`)
59
- - vector: a js array of numbers (i.e. `[5, 6]`)
60
- - point: a js array of numbers (i.e. `[5, 6]`)
61
- - ray: a js array of two points (i.e. `[[5, 6], [1, 2]]`)
62
- - line: a js array of two points (i.e. `[[5, 6], [1, 2]]`)
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]]`)
63
64
 
64
65
  kmath functions usually take an argument of the corresponding type as
65
66
  the first parameter, and other parameters afterwards. For example, to
66
67
  rotate the point `[1, 1]` by 90 degrees around `[0, 0]`, one might use:
67
68
 
68
- kmath.point.rotateDeg([1, 1], 90, [0, 0])
69
+ ```js
70
+ point.rotateDeg([1, 1], 90, [0, 0])
71
+ ```
69
72
 
70
73
  Documentation for specific functions for each type is provided below.
71
74
 
72
- ## kmath.number
75
+ ## number
73
76
 
74
77
  #### `number.DEFAULT_TOLERANCE === 1e-9`
75
78
 
@@ -131,7 +134,7 @@ is found, `[decimal, 1]` is returned.
131
134
  `tolerance` defaults to `number.EPSILON`. `maxDenominator` defaults to
132
135
  `1000`.
133
136
 
134
- ## kmath.vector
137
+ ## vector
135
138
 
136
139
  #### `vector.is(maybeAVector, [dimension])`
137
140
 
@@ -141,22 +144,24 @@ only returns true if `maybeAVector` is a vector with dimension `dimension`.
141
144
  #### `vector.equal(v1, v2, [tolerance])`
142
145
 
143
146
  Returns true if `v1` and `v2` are equal within `tolerance`. If `tolerance`
144
- is not specified, `kmath.number.DEFAULT_TOLERANCE` is used. Each dimension
147
+ is not specified, `number.DEFAULT_TOLERANCE` is used. Each dimension
145
148
  is compared individually, rather than comparing length and direction.
146
149
 
147
150
  If `v1` and `v2` have different lengths, this function returns `false`.
148
151
 
149
- kmath.vector.equal([1, 2], [1, 3])
150
- => false
151
- kmath.vector.equal([1, 2], [1, 2, 3])
152
- => false
153
- kmath.vector.equal([1, 2], [1, 2])
154
- => true
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
+ ```
155
160
 
156
161
  #### `vector.codirectional(v1, v2, [tolerance])`
157
162
 
158
163
  Returns true if `v1` and `v2` have the same direction within `tolerance`.
159
- If `tolerance` is unspecified, `kmath.number.DEFAULT_TOLERANCE` is used.
164
+ If `tolerance` is unspecified, `number.DEFAULT_TOLERANCE` is used.
160
165
  Note that tolerance is checked after normalization.
161
166
 
162
167
  If `v1` and `v2` are different lengths, this function returns `false`,
@@ -165,14 +170,16 @@ regardless of whether they are codirectional in the existing dimensions.
165
170
  This function defines the zero-vector as trivially codirectional with
166
171
  every vector.
167
172
 
168
- kmath.vector.codirectional([1, 2], [2, 4])
169
- => true
170
- kmath.vector.codirectinoal([1, 2], [0, 0])
171
- => true
172
- kmath.vector.codirectional([1, 2], [1, 2, 0])
173
- => false
174
- kmath.vector.codirectional([1, 2], [-2, -4])
175
- => false
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
+ ```
176
183
 
177
184
  #### `vector.colinear(v1, v2, [tolerance])`
178
185
 
@@ -182,8 +189,10 @@ or `v1` and `-v2` being codirectional, or whether there is some
182
189
  `scaleFactor` such that `vector.equal(vector.scale(v1, scaleFactor), v2)`
183
190
  is true.
184
191
 
185
- kmath.vector.colinear([1, 2], [-2, -4])
186
- => true
192
+ ```js
193
+ kmath.vector.colinear([1, 2], [-2, -4])
194
+ => true
195
+ ```
187
196
 
188
197
  #### `vector.normalize(v)`
189
198
 
@@ -201,15 +210,19 @@ Returns the dot product of cartesian vectors `v1` and `v2`.
201
210
 
202
211
  Adds multiple cartesian vectors together.
203
212
 
204
- kmath.vector.add([1, 2], [3, 4])
205
- => [4, 6]
213
+ ```js
214
+ kmath.vector.add([1, 2], [3, 4])
215
+ => [4, 6]
216
+ ```
206
217
 
207
218
  #### `vector.subtract(v1, v2)`
208
219
 
209
220
  Subtracts the cartesian vector `v2` from the cartesian vector `v1`.
210
221
 
211
- kmath.vector.subtract([4, 6], [1, 2])
212
- => [3, 4]
222
+ ```js
223
+ kmath.vector.subtract([4, 6], [1, 2])
224
+ => [3, 4]
225
+ ```
213
226
 
214
227
  #### `vector.negate(v)`
215
228
 
@@ -220,46 +233,56 @@ Negates the cartesian vector `v`. This has the same effect as scaling
220
233
 
221
234
  Scales the cartesian vector `v` by `scalar`.
222
235
 
223
- kmath.vector.scale([1, 2], 2)
224
- => [2, 4]
236
+ ```js
237
+ kmath.vector.scale([1, 2], 2)
238
+ => [2, 4]
239
+ ```
225
240
 
226
241
  #### `vector.polarRadFromCart(v)`
227
242
 
228
243
  Returns a polar vector `[length, angle]` from the two-dimensional cartesian
229
244
  vector `v`, where `angle` is measured in radians.
230
245
 
231
- kmath.vector.polarRadFromCart([1, 1])
232
- => [1.4142135623730951, 0.7853981633974483]
246
+ ```js
247
+ kmath.vector.polarRadFromCart([1, 1])
248
+ => [1.4142135623730951, 0.7853981633974483]
249
+ ```
233
250
 
234
251
  #### `vector.polarDegFromCart(v)`
235
252
 
236
253
  Returns a polar vector `[length, angle]` from the two-dimensional cartesian
237
254
  vector `v`, where `angle` is measured in degrees.
238
255
 
239
- kmath.vector.polarDegFromCart([0, 2])
240
- => [2, 90]
256
+ ```js
257
+ kmath.vector.polarDegFromCart([0, 2])
258
+ => [2, 90]
259
+ ```
241
260
 
242
261
  #### `vector.cartFromPolarRad(polar)` or `vector.cartFromPolarRad(length, angle)`
243
262
 
244
263
  Returns a two-dimensional cartesian vector from the polar vector input,
245
264
  where the input angle is measured in radians.
246
265
 
247
- kmath.vector.cartFromPolarRad([2, Math.PI])
248
- => [-2, 0] // Note: a very small nonzero number is actually returned here,
249
- // due to numerical inaccuracy.
250
- kmath.vector.cartFromPolarRad(Math.pow(2, 0.5), Math.PI/4)
251
- => [1, 1]
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
+ ```
252
273
 
253
274
  #### `vector.cartFromPolarDeg(polar)` or `vector.cartFromPolarDeg(length, angle)`
254
275
 
255
276
  Returns a two-dimensional cartesian vector from the polar vector input,
256
277
  where the input angle is measured in degrees.
257
278
 
258
- kmath.vector.cartFromPolarRad([2, 90])
259
- => [-2, 0] // Note: a very small nonzero number is actually returned here,
260
- // due to numerical inaccuracy.
261
- kmath.vector.cartFromPolarRad(Math.pow(2, 0.5), 45)
262
- => [1, 1]
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
+ ```
263
286
 
264
287
  #### `vector.rotateRad(v, angle)`
265
288
 
@@ -314,12 +337,14 @@ is compared individually.
314
337
 
315
338
  If `p1` and `p2` have different lengths, this function returns `false`.
316
339
 
317
- kmath.point.equal([1, 2], [1, 3])
318
- => false
319
- kmath.point.equal([1, 2], [1, 2, 3])
320
- => false
321
- kmath.point.equal([1, 2], [1, 2])
322
- => true
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
+ ```
323
348
 
324
349
  #### `point.addVector(p, *vectors)` or `point.addVectors(p, *vectors)`
325
350
 
@@ -342,8 +367,10 @@ Returns the distance between `p` and the line `theLine`.
342
367
  For example, to find the distance from the origin to the
343
368
  `y = 5` line, one could write:
344
369
 
345
- kmath.point.distanceToLine([0, 0], [[-1, 5], [1, 5]])
346
- => 5
370
+ ```js
371
+ kmath.point.distanceToLine([0, 0], [[-1, 5], [1, 5]])
372
+ => 5
373
+ ```
347
374
 
348
375
  #### `point.reflectOverLine(p, theLine)`
349
376
 
@@ -352,8 +379,10 @@ Returns the reflection of `p` over the line `theLine`.
352
379
  For example, to reflect the origin over the line `y = 5`,
353
380
  one could write:
354
381
 
355
- kmath.point.reflectOverLine([0, 0], [[-1, 5], [1, 5]])
356
- => [0, 10]
382
+ ```js
383
+ kmath.point.reflectOverLine([0, 0], [[-1, 5], [1, 5]])
384
+ => [0, 10]
385
+ ```
357
386
 
358
387
  #### `point.compare(p1, p2, [equalityTolerance])`
359
388
 
@@ -370,38 +399,46 @@ few equalityTolerances could appear in the wrong order.
370
399
  Returns a polar point `[length, angle]` from the two-dimensional cartesian
371
400
  point `v`, where `angle` is measured in radians.
372
401
 
373
- kmath.point.polarRadFromCart([1, 1])
374
- => [1.4142135623730951, 0.7853981633974483]
402
+ ```js
403
+ kmath.point.polarRadFromCart([1, 1])
404
+ => [1.4142135623730951, 0.7853981633974483]
405
+ ```
375
406
 
376
407
  #### `point.polarDegFromCart(p)`
377
408
 
378
409
  Returns a polar point `[length, angle]` from the two-dimensional cartesian
379
410
  point `v`, where `angle` is measured in degrees.
380
411
 
381
- kmath.point.polarDegFromCart([0, 2])
382
- => [2, 90]
412
+ ```js
413
+ kmath.point.polarDegFromCart([0, 2])
414
+ => [2, 90]
415
+ ```
383
416
 
384
417
  #### `point.cartFromPolarRad(polar)` or `point.cartFromPolarRad(length, angle)`
385
418
 
386
419
  Returns a two-dimensional cartesian point from the polar point input,
387
420
  where the input angle is measured in radians.
388
421
 
389
- kmath.point.cartFromPolarRad([2, Math.PI])
390
- => [-2, 0] // Note: a very small nonzero number is actually returned here,
391
- // due to numerical inaccuracy.
392
- kmath.point.cartFromPolarRad(Math.pow(2, 0.5), Math.PI/4)
393
- => [1, 1]
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
+ ```
394
429
 
395
430
  #### `point.cartFromPolarDeg(polar)` or `point.cartFromPolarDeg(length, angle)`
396
431
 
397
432
  Returns a two-dimensional cartesian point from the polar point input,
398
433
  where the input angle is measured in degrees.
399
434
 
400
- kmath.point.cartFromPolarRad([2, 90])
401
- => [-2, 0] // Note: a very small nonzero number is actually returned here,
402
- // due to numerical inaccuracy.
403
- kmath.point.cartFromPolarRad(Math.pow(2, 0.5), 45)
404
- => [1, 1]
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
+ ```
405
442
 
406
443
  #### `point.rotateRad(p, angle, center)`
407
444
 
@@ -449,8 +486,10 @@ If unspecified, `tolerance` defaults to `kmath.number.DEFAULT_TOLERANCE`.
449
486
 
450
487
  Returns the midpoint of the line `theLine`.
451
488
 
452
- kmath.line.midpoint([[0, 5], [5, 0]])
453
- => [2.5, 2.5]
489
+ ```js
490
+ kmath.line.midpoint([[0, 5], [5, 0]])
491
+ => [2.5, 2.5]
492
+ ```
454
493
 
455
494
  #### `line.distanceToPoint(theLine, p)`
456
495
 
@@ -462,4 +501,4 @@ Returns the reflection of `p` over `theLine`.
462
501
 
463
502
  ## License
464
503
 
465
- MIT. See the LICENSE file for more information.
504
+ MIT. See the [LICENSE](../../LICENSE) file for more information.