perseus-custom 0.0.1
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/.eslintrc.js +11 -0
- package/CHANGELOG.md +31 -0
- package/LICENSE.txt +21 -0
- package/README.md +94 -0
- package/experimenter.html +75 -0
- package/package.json +38 -0
- package/src/__genfiles__/parser.js +840 -0
- package/src/__genfiles__/unitparser.js +679 -0
- package/src/__tests__/checking-form_test.js +77 -0
- package/src/__tests__/comparing_test.js +325 -0
- package/src/__tests__/compilation_test.js +91 -0
- package/src/__tests__/evaluating_test.js +75 -0
- package/src/__tests__/index_test.js +367 -0
- package/src/__tests__/parsing_test.js +487 -0
- package/src/__tests__/rendering_test.js +273 -0
- package/src/__tests__/transforming_test.js +329 -0
- package/src/__tests__/units_test.js +192 -0
- package/src/compare.js +70 -0
- package/src/index.js +2 -0
- package/src/nodes.js +3507 -0
- package/src/parser-generator.js +215 -0
- package/src/unitvalue.jison +161 -0
package/.eslintrc.js
ADDED
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# @khanacademy/kas
|
|
2
|
+
|
|
3
|
+
## 0.2.7
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- f567f660: Update the eslint config to look at both the package.json for the package and the one from the root
|
|
8
|
+
|
|
9
|
+
## 0.2.6
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- bf180fe1: Fix our use of import/no-extraneous-dependencies
|
|
14
|
+
|
|
15
|
+
## 0.2.5
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- 98d283ff: Fix storybook
|
|
20
|
+
|
|
21
|
+
## 0.2.4
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- a15b0e86: Add 'perseus-build-settings' as a dev dep to packages that were missing it
|
|
26
|
+
|
|
27
|
+
## 0.2.3
|
|
28
|
+
|
|
29
|
+
### Patch Changes
|
|
30
|
+
|
|
31
|
+
- 570c5800: Move KAS into khan/perseus repo
|
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2014 Khan Academy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
KAS
|
|
2
|
+
===
|
|
3
|
+
|
|
4
|
+
A lightweight JavaScript CAS (Computer Algebra System) for comparing expressions and equations.
|
|
5
|
+
It is used throughout [Khan Academy](https://khanacademy.org)'s interactive exercises.
|
|
6
|
+
|
|
7
|
+
What can it do?
|
|
8
|
+
---------------
|
|
9
|
+
|
|
10
|
+
It can parse plain text math, LaTeX, or a mix of both:
|
|
11
|
+
|
|
12
|
+
```js
|
|
13
|
+
var expr = KAS.parse("3x \\frac{42}{42} sin^2y").expr;
|
|
14
|
+
expr.print();
|
|
15
|
+
// "3*x*42/42*sin(y)^(2)"
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
It can evaluate expressions:
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
var expr = KAS.parse("(x^2+y^2)^.5").expr;
|
|
22
|
+
expr.eval({x: 3, y: 4});
|
|
23
|
+
// 5
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
It can compare expressions and equations:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
var expr1 = KAS.parse("(1-x)(-1-6x)").expr;
|
|
30
|
+
var expr2 = KAS.parse("(6x+1)(x-1)").expr;
|
|
31
|
+
KAS.compare(expr1, expr2).equal;
|
|
32
|
+
// true
|
|
33
|
+
|
|
34
|
+
var eq1 = KAS.parse("2w+50/w=25").expr;
|
|
35
|
+
var eq2 = KAS.parse("w(12.5-w)=25").expr;
|
|
36
|
+
KAS.compare(eq1, eq2).equal;
|
|
37
|
+
// true
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
It can perform basic transforms that always simplify an expression:
|
|
41
|
+
|
|
42
|
+
```js
|
|
43
|
+
var expr = KAS.parse("1+1+x+x+x+y").expr;
|
|
44
|
+
expr.collect().print();
|
|
45
|
+
// "2+3*x+y"
|
|
46
|
+
|
|
47
|
+
var expr = KAS.parse("b^(2*y*log_b x)").expr;
|
|
48
|
+
expr.collect().print();
|
|
49
|
+
// "x^(2*y)"
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
It can perform non-simplifying transforms on an expression:
|
|
53
|
+
|
|
54
|
+
```js
|
|
55
|
+
var expr = KAS.parse("ab(c+d)e^f").expr;
|
|
56
|
+
expr.print();
|
|
57
|
+
// "a*b*(c+d)*e^(f)"
|
|
58
|
+
expr.expand().print();
|
|
59
|
+
// "a*b*e^(f)*c+a*b*e^(f)*d"
|
|
60
|
+
expr.expand().factor().print();
|
|
61
|
+
// "a*b*e^(f)*(c+d)"
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
It can combine the above abilities to perform powerful simplification:
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
var expr = KAS.parse("((nx^5)^5)/(n^-2x^2)^-3").expr;
|
|
68
|
+
expr.print();
|
|
69
|
+
// "(n*x^(5))^(5)*(n^(-2)*x^(2))^(-1*-3)"
|
|
70
|
+
expr.simplify().print();
|
|
71
|
+
// "n^(-1)*x^(31)"
|
|
72
|
+
|
|
73
|
+
var expr = KAS.parse("(15np-25mp)/(15p^2-5p)+(20mp+10p^2)/(15p^2-5p)").expr;
|
|
74
|
+
expr.print();
|
|
75
|
+
// "(15*n*p+-25*m*p)*(15*p^(2)+-5*p)^(-1)+(20*m*p+10*p^(2))*(15*p^(2)+-5*p)^(-1)"
|
|
76
|
+
expr.simplify().print();
|
|
77
|
+
// "(-1+3*p)^(-1)*(3*n+-1*m+2*p)"
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
How to build the library
|
|
81
|
+
------------------------
|
|
82
|
+
npm install
|
|
83
|
+
npm run build
|
|
84
|
+
|
|
85
|
+
How to build the parser
|
|
86
|
+
-----------------------
|
|
87
|
+
First, make any changes in `src/parser-generator.js`
|
|
88
|
+
|
|
89
|
+
npm install
|
|
90
|
+
npm run build:parser
|
|
91
|
+
|
|
92
|
+
License
|
|
93
|
+
-------
|
|
94
|
+
[MIT License](http://opensource.org/licenses/MIT)
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>KAS Experimenter</title>
|
|
5
|
+
<!-- Include Underscore -->
|
|
6
|
+
<script src="node_modules/underscore/underscore.js"></script>
|
|
7
|
+
<!-- Include KAS -->
|
|
8
|
+
<script src="src/parser.js"></script>
|
|
9
|
+
<script src="src/nodes.js"></script>
|
|
10
|
+
<script src="src/compare.js"></script>
|
|
11
|
+
<style>
|
|
12
|
+
* {
|
|
13
|
+
font-family: "Courier New"
|
|
14
|
+
}
|
|
15
|
+
body {
|
|
16
|
+
font-size: 28px;
|
|
17
|
+
}
|
|
18
|
+
#input {
|
|
19
|
+
width: 100%;
|
|
20
|
+
font-size: 72px;
|
|
21
|
+
}
|
|
22
|
+
.label {
|
|
23
|
+
display: inline-block;
|
|
24
|
+
margin-right:30px;
|
|
25
|
+
width:400px;
|
|
26
|
+
vertical-align: top;
|
|
27
|
+
}
|
|
28
|
+
.value {
|
|
29
|
+
display: inline-block;
|
|
30
|
+
}
|
|
31
|
+
.code {
|
|
32
|
+
font-size: 12px;
|
|
33
|
+
}
|
|
34
|
+
</style>
|
|
35
|
+
</head>
|
|
36
|
+
<body>
|
|
37
|
+
<input type="text" id="input"></input>
|
|
38
|
+
<div id="output"></div>
|
|
39
|
+
<script>
|
|
40
|
+
window.onload = function() {
|
|
41
|
+
var input = document.getElementById("input");
|
|
42
|
+
var out = document.getElementById("output");
|
|
43
|
+
|
|
44
|
+
if ("oninput" in input) {
|
|
45
|
+
input.addEventListener("input", reprocess, false);
|
|
46
|
+
} else {
|
|
47
|
+
input.attachEvent("onkeyup", reprocess);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function addInfo(el, label, value) {
|
|
51
|
+
el.innerHTML += "<div>"
|
|
52
|
+
+ "<div class='label'>" + label + "</div>"
|
|
53
|
+
+ "<div class='value'>" + value + "</div>"
|
|
54
|
+
+ "</div>";
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function reprocess() {
|
|
58
|
+
out.innerHTML = "";
|
|
59
|
+
var parsed = KAS.parse(input.value, {}).expr;
|
|
60
|
+
if (input.value === "") { return; }
|
|
61
|
+
if (parsed === undefined) { return; }
|
|
62
|
+
addInfo(out, "AST Representation:", parsed.repr());
|
|
63
|
+
addInfo(out, "Printed Representation:", parsed.normalize().print());
|
|
64
|
+
addInfo(out, "TeX Representation:", parsed.tex());
|
|
65
|
+
addInfo(out, "Simplified?", parsed.isSimplified() ? "Yes" : "No");
|
|
66
|
+
addInfo(out, "Simplified", parsed.simplify().normalize().print());
|
|
67
|
+
addInfo(out, "JSON Representation:", "<pre class='code'>"
|
|
68
|
+
+ JSON.stringify(parsed, null, 2)
|
|
69
|
+
+ "</pre>");
|
|
70
|
+
addInfo(out, "Compiled", parsed.compile().toString());
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
</script>
|
|
74
|
+
</body>
|
|
75
|
+
</html>
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "perseus-custom",
|
|
3
|
+
"description": "A lightweight JavaScript CAS for comparing expressions and equations.",
|
|
4
|
+
"author": "Khan Academy",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"version": "0.0.1",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
},
|
|
15
|
+
"module": "dist/es/index.js",
|
|
16
|
+
"main": "dist/index.js",
|
|
17
|
+
"source": "src/index.js",
|
|
18
|
+
"scripts": {
|
|
19
|
+
"gen:parsers": "node src/parser-generator.js",
|
|
20
|
+
"test": "bash -c 'yarn --silent --cwd \"../..\" test ${@:0} $($([[ ${@: -1} = -* ]] || [[ ${@: -1} = bash ]]) && echo $PWD)'"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"jison": "0.4.15",
|
|
25
|
+
"perseus-build-settings": "^0.0.5",
|
|
26
|
+
"underscore": "1.4.4"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"underscore": "1.4.4"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"parsing",
|
|
33
|
+
"equation",
|
|
34
|
+
"expression",
|
|
35
|
+
"algebra",
|
|
36
|
+
"symbolic"
|
|
37
|
+
]
|
|
38
|
+
}
|