@khanacademy/kmath 0.4.7 → 2.0.0
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/dist/es/index.js +72 -56
- package/dist/es/index.js.map +1 -1
- package/dist/index.js +95 -79
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/es/index.js
CHANGED
|
@@ -4,14 +4,18 @@ import { approximateEqual, approximateDeepEqual } from '@khanacademy/perseus-cor
|
|
|
4
4
|
import $ from 'jquery';
|
|
5
5
|
|
|
6
6
|
// This file is processed by a Rollup plugin (replace) to inject the production
|
|
7
|
+
// version number during the release build.
|
|
8
|
+
// In dev, you'll never see the version number.
|
|
9
|
+
|
|
7
10
|
const libName = "@khanacademy/kmath";
|
|
8
|
-
const libVersion = "0.
|
|
11
|
+
const libVersion = "2.0.0";
|
|
9
12
|
addLibraryVersionToPerseusDebug(libName, libVersion);
|
|
10
13
|
|
|
11
14
|
/**
|
|
12
15
|
* Number Utils
|
|
13
16
|
* A number is a js-number, e.g. 5.12
|
|
14
17
|
*/
|
|
18
|
+
|
|
15
19
|
const DEFAULT_TOLERANCE = 1e-9;
|
|
16
20
|
|
|
17
21
|
// TODO: Should this just be Number.Epsilon
|
|
@@ -101,14 +105,14 @@ var number = /*#__PURE__*/Object.freeze({
|
|
|
101
105
|
__proto__: null,
|
|
102
106
|
DEFAULT_TOLERANCE: DEFAULT_TOLERANCE,
|
|
103
107
|
EPSILON: EPSILON,
|
|
104
|
-
|
|
108
|
+
ceilTo: ceilTo$2,
|
|
105
109
|
equal: equal$4,
|
|
106
|
-
|
|
110
|
+
floorTo: floorTo$2,
|
|
111
|
+
is: is$2,
|
|
107
112
|
isInteger: isInteger,
|
|
108
113
|
round: round$2,
|
|
109
114
|
roundTo: roundTo$2,
|
|
110
|
-
|
|
111
|
-
ceilTo: ceilTo$2,
|
|
115
|
+
sign: sign$1,
|
|
112
116
|
toFraction: toFraction
|
|
113
117
|
});
|
|
114
118
|
|
|
@@ -116,6 +120,7 @@ var number = /*#__PURE__*/Object.freeze({
|
|
|
116
120
|
* Vector Utils
|
|
117
121
|
* A vector is an array of numbers e.g. [0, 3, 4].
|
|
118
122
|
*/
|
|
123
|
+
|
|
119
124
|
function arraySum(array) {
|
|
120
125
|
return array.reduce((memo, arg) => memo + arg, 0);
|
|
121
126
|
}
|
|
@@ -301,32 +306,32 @@ function ceilTo$1(vec, increment) {
|
|
|
301
306
|
|
|
302
307
|
var vector$1 = /*#__PURE__*/Object.freeze({
|
|
303
308
|
__proto__: null,
|
|
304
|
-
zip: zip,
|
|
305
|
-
map: map,
|
|
306
|
-
is: is$1,
|
|
307
|
-
normalize: normalize,
|
|
308
|
-
length: length,
|
|
309
|
-
dot: dot,
|
|
310
309
|
add: add$1,
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
310
|
+
angleDeg: angleDeg,
|
|
311
|
+
angleRad: angleRad,
|
|
312
|
+
cartFromPolarDeg: cartFromPolarDeg$1,
|
|
313
|
+
cartFromPolarRad: cartFromPolarRad$1,
|
|
314
|
+
ceilTo: ceilTo$1,
|
|
315
315
|
codirectional: codirectional,
|
|
316
316
|
collinear: collinear$1,
|
|
317
|
-
|
|
317
|
+
dot: dot,
|
|
318
|
+
equal: equal$3,
|
|
319
|
+
floorTo: floorTo$1,
|
|
320
|
+
is: is$1,
|
|
321
|
+
length: length,
|
|
322
|
+
map: map,
|
|
323
|
+
negate: negate,
|
|
324
|
+
normalize: normalize,
|
|
318
325
|
polarDegFromCart: polarDegFromCart$1,
|
|
319
|
-
|
|
320
|
-
cartFromPolarDeg: cartFromPolarDeg$1,
|
|
321
|
-
rotateRad: rotateRad$1,
|
|
322
|
-
rotateDeg: rotateDeg$1,
|
|
323
|
-
angleRad: angleRad,
|
|
324
|
-
angleDeg: angleDeg,
|
|
326
|
+
polarRadFromCart: polarRadFromCart$1,
|
|
325
327
|
projection: projection,
|
|
328
|
+
rotateDeg: rotateDeg$1,
|
|
329
|
+
rotateRad: rotateRad$1,
|
|
326
330
|
round: round$1,
|
|
327
331
|
roundTo: roundTo$1,
|
|
328
|
-
|
|
329
|
-
|
|
332
|
+
scale: scale,
|
|
333
|
+
subtract: subtract,
|
|
334
|
+
zip: zip
|
|
330
335
|
});
|
|
331
336
|
|
|
332
337
|
/**
|
|
@@ -334,6 +339,7 @@ var vector$1 = /*#__PURE__*/Object.freeze({
|
|
|
334
339
|
* A point is an array of two numbers e.g. [0, 0].
|
|
335
340
|
*/
|
|
336
341
|
|
|
342
|
+
|
|
337
343
|
// A point, in 2D, 3D, or nD space.
|
|
338
344
|
|
|
339
345
|
// Rotate point (around origin unless a center is specified)
|
|
@@ -419,31 +425,32 @@ const ceilTo = ceilTo$1;
|
|
|
419
425
|
|
|
420
426
|
var point = /*#__PURE__*/Object.freeze({
|
|
421
427
|
__proto__: null,
|
|
422
|
-
rotateRad: rotateRad,
|
|
423
|
-
rotateDeg: rotateDeg,
|
|
424
|
-
distanceToPoint: distanceToPoint$1,
|
|
425
|
-
distanceToLine: distanceToLine,
|
|
426
|
-
reflectOverLine: reflectOverLine,
|
|
427
|
-
compare: compare,
|
|
428
|
-
is: is,
|
|
429
428
|
addVector: addVector,
|
|
430
429
|
addVectors: addVectors,
|
|
431
|
-
|
|
430
|
+
cartFromPolarDeg: cartFromPolarDeg,
|
|
431
|
+
cartFromPolarRad: cartFromPolarRad,
|
|
432
|
+
ceilTo: ceilTo,
|
|
433
|
+
compare: compare,
|
|
434
|
+
distanceToLine: distanceToLine,
|
|
435
|
+
distanceToPoint: distanceToPoint$1,
|
|
432
436
|
equal: equal$2,
|
|
433
|
-
|
|
437
|
+
floorTo: floorTo,
|
|
438
|
+
is: is,
|
|
434
439
|
polarDegFromCart: polarDegFromCart,
|
|
435
|
-
|
|
436
|
-
|
|
440
|
+
polarRadFromCart: polarRadFromCart,
|
|
441
|
+
reflectOverLine: reflectOverLine,
|
|
442
|
+
rotateDeg: rotateDeg,
|
|
443
|
+
rotateRad: rotateRad,
|
|
437
444
|
round: round,
|
|
438
445
|
roundTo: roundTo,
|
|
439
|
-
|
|
440
|
-
ceilTo: ceilTo
|
|
446
|
+
subtractVector: subtractVector
|
|
441
447
|
});
|
|
442
448
|
|
|
443
449
|
/**
|
|
444
450
|
* Line Utils
|
|
445
451
|
* A line is an array of two points e.g. [[-5, 0], [5, 0]].
|
|
446
452
|
*/
|
|
453
|
+
|
|
447
454
|
function distanceToPoint(line, point$1) {
|
|
448
455
|
return distanceToLine(point$1, line);
|
|
449
456
|
}
|
|
@@ -475,9 +482,9 @@ function equal$1(line1, line2, tolerance) {
|
|
|
475
482
|
var line = /*#__PURE__*/Object.freeze({
|
|
476
483
|
__proto__: null,
|
|
477
484
|
distanceToPoint: distanceToPoint,
|
|
478
|
-
|
|
485
|
+
equal: equal$1,
|
|
479
486
|
midpoint: midpoint,
|
|
480
|
-
|
|
487
|
+
reflectPoint: reflectPoint
|
|
481
488
|
});
|
|
482
489
|
|
|
483
490
|
/**
|
|
@@ -486,6 +493,7 @@ var line = /*#__PURE__*/Object.freeze({
|
|
|
486
493
|
* For example, [[0, 0], [1, 0]] is the ray starting at the origin and
|
|
487
494
|
* traveling along the positive x-axis.
|
|
488
495
|
*/
|
|
496
|
+
|
|
489
497
|
function equal(ray1, ray2, tolerance) {
|
|
490
498
|
// Compare the directions of the rays
|
|
491
499
|
const v1 = subtract(ray1[1], ray1[0]);
|
|
@@ -532,6 +540,8 @@ const KhanMath = {
|
|
|
532
540
|
let mod;
|
|
533
541
|
a = Math.abs(a);
|
|
534
542
|
b = Math.abs(b);
|
|
543
|
+
|
|
544
|
+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
535
545
|
while (b) {
|
|
536
546
|
mod = a % b;
|
|
537
547
|
a = b;
|
|
@@ -555,6 +565,7 @@ const KhanMath = {
|
|
|
555
565
|
return false;
|
|
556
566
|
}
|
|
557
567
|
if (n < 101) {
|
|
568
|
+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
558
569
|
return !!$.grep(KhanMath.primes, function (p, i) {
|
|
559
570
|
return Math.abs(p - n) <= 0.5;
|
|
560
571
|
}).length;
|
|
@@ -718,7 +729,9 @@ const KhanMath = {
|
|
|
718
729
|
if (format === "mixed") {
|
|
719
730
|
const modulus = numerator % denominator;
|
|
720
731
|
const integer = (numerator - modulus) / denominator;
|
|
721
|
-
return sign + (
|
|
732
|
+
return sign + (
|
|
733
|
+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
734
|
+
integer ? integer + " " : "") + modulus + "/" + denominator;
|
|
722
735
|
} // otherwise proper, improper, or fraction
|
|
723
736
|
return sign + numerator + "/" + denominator;
|
|
724
737
|
}
|
|
@@ -738,6 +751,7 @@ function add(a, b) {
|
|
|
738
751
|
* A collection of geomtry-related utility functions
|
|
739
752
|
*/
|
|
740
753
|
|
|
754
|
+
|
|
741
755
|
// This should really be a readonly tuple of [number, number]
|
|
742
756
|
|
|
743
757
|
// Given a number, return whether it is positive (1), negative (-1), or zero (0)
|
|
@@ -992,26 +1006,27 @@ function getLineIntersectionString(firstPoints, secondPoints) {
|
|
|
992
1006
|
|
|
993
1007
|
var geometry = /*#__PURE__*/Object.freeze({
|
|
994
1008
|
__proto__: null,
|
|
995
|
-
|
|
1009
|
+
angleMeasures: angleMeasures,
|
|
1010
|
+
canonicalSineCoefficients: canonicalSineCoefficients,
|
|
996
1011
|
ccw: ccw,
|
|
1012
|
+
clockwise: clockwise,
|
|
997
1013
|
collinear: collinear,
|
|
1014
|
+
getLineEquation: getLineEquation,
|
|
1015
|
+
getLineIntersection: getLineIntersection,
|
|
1016
|
+
getLineIntersectionString: getLineIntersectionString,
|
|
998
1017
|
intersects: intersects,
|
|
1018
|
+
lawOfCosines: lawOfCosines,
|
|
1019
|
+
magnitude: magnitude,
|
|
999
1020
|
polygonSidesIntersect: polygonSidesIntersect,
|
|
1000
|
-
vector: vector,
|
|
1001
1021
|
reverseVector: reverseVector,
|
|
1002
|
-
clockwise: clockwise,
|
|
1003
|
-
magnitude: magnitude,
|
|
1004
|
-
angleMeasures: angleMeasures,
|
|
1005
|
-
similar: similar,
|
|
1006
|
-
lawOfCosines: lawOfCosines,
|
|
1007
|
-
canonicalSineCoefficients: canonicalSineCoefficients,
|
|
1008
1022
|
rotate: rotate,
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1023
|
+
sign: sign,
|
|
1024
|
+
similar: similar,
|
|
1025
|
+
vector: vector
|
|
1012
1026
|
});
|
|
1013
1027
|
|
|
1014
1028
|
// This file contains helper functions for working with angles.
|
|
1029
|
+
|
|
1015
1030
|
function convertDegreesToRadians(degrees) {
|
|
1016
1031
|
return degrees / 180 * Math.PI;
|
|
1017
1032
|
}
|
|
@@ -1041,6 +1056,7 @@ function polar(r, th) {
|
|
|
1041
1056
|
const getAngleFromVertex = (point, vertex) => {
|
|
1042
1057
|
const x = point[0] - vertex[0];
|
|
1043
1058
|
const y = point[1] - vertex[1];
|
|
1059
|
+
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
|
|
1044
1060
|
if (!x && !y) {
|
|
1045
1061
|
return 0;
|
|
1046
1062
|
}
|
|
@@ -1072,12 +1088,12 @@ const getClockwiseAngle = (coords, allowReflexAngles = false) => {
|
|
|
1072
1088
|
|
|
1073
1089
|
var angles = /*#__PURE__*/Object.freeze({
|
|
1074
1090
|
__proto__: null,
|
|
1091
|
+
calculateAngleInDegrees: calculateAngleInDegrees,
|
|
1075
1092
|
convertDegreesToRadians: convertDegreesToRadians,
|
|
1076
1093
|
convertRadiansToDegrees: convertRadiansToDegrees,
|
|
1077
|
-
calculateAngleInDegrees: calculateAngleInDegrees,
|
|
1078
|
-
polar: polar,
|
|
1079
1094
|
getAngleFromVertex: getAngleFromVertex,
|
|
1080
|
-
getClockwiseAngle: getClockwiseAngle
|
|
1095
|
+
getClockwiseAngle: getClockwiseAngle,
|
|
1096
|
+
polar: polar
|
|
1081
1097
|
});
|
|
1082
1098
|
|
|
1083
1099
|
// TODO: there's another, very similar getSinusoidCoefficients function
|
|
@@ -1114,8 +1130,8 @@ function getQuadraticCoefficients(coords) {
|
|
|
1114
1130
|
|
|
1115
1131
|
var coefficients = /*#__PURE__*/Object.freeze({
|
|
1116
1132
|
__proto__: null,
|
|
1117
|
-
|
|
1118
|
-
|
|
1133
|
+
getQuadraticCoefficients: getQuadraticCoefficients,
|
|
1134
|
+
getSinusoidCoefficients: getSinusoidCoefficients
|
|
1119
1135
|
});
|
|
1120
1136
|
|
|
1121
1137
|
export { KhanMath, angles, coefficients, geometry, libVersion, line, number, point, ray, sum, vector$1 as vector };
|