Permalink
Browse files

Increased support for TeX symbols returned by MathWidget

Summary:
On iOS, the MathWidget returns some symbols that KAS can't understand. I've added
support for a few of those symbols that might be relevant, namely the asterisk sign for
multiplication, the \neq sign for inequality, and the hyperbolic trig functions (these
probably aren't necessary, but weren't difficult to add). This doesn't cover _everything_
that MathWidget can return, but it probably covers everything we care about across the site.

Test Plan: See test.html.

Reviewers: alex, jack

Reviewed By: jack

Subscribers: jack

Differential Revision: http://phabricator.khanacademy.org/D11329
  • Loading branch information...
1 parent 5eb5fc6 commit 736a4955dc53772b00d0b580352612a435bc0522 @crm416 crm416 committed Jul 22, 2014
Showing with 329 additions and 136 deletions.
  1. +141 −67 kas.js
  2. +59 −5 src/nodes.js
  3. +10 −0 src/parser-generator.js
  4. +82 −62 src/parser.js
  5. +37 −2 test.html
View
208 kas.js
@@ -660,136 +660,156 @@ case 7:return "*"
break;
case 8:return "*"
break;
-case 9:return "/"
+case 9:return "*"
break;
-case 10:return "-"
+case 10:return "/"
break;
case 11:return "-"
break;
-case 12:return "+"
+case 12:return "-"
break;
-case 13:return "^"
+case 13:return "+"
break;
-case 14:return "("
+case 14:return "^"
break;
-case 15:return ")"
+case 15:return "("
break;
-case 16:return "("
+case 16:return ")"
break;
-case 17:return ")"
+case 17:return "("
break;
-case 18:return "{"
+case 18:return ")"
break;
-case 19:return "}"
+case 19:return "{"
break;
-case 20:return "{"
+case 20:return "}"
break;
-case 21:return "}"
+case 21:return "{"
break;
-case 22:return "_"
+case 22:return "}"
break;
-case 23:return "|"
+case 23:return "_"
break;
-case 24:return "LEFT|"
+case 24:return "|"
break;
-case 25:return "RIGHT|"
+case 25:return "LEFT|"
break;
-case 26:return "!"
+case 26:return "RIGHT|"
break;
-case 27:return "SIGN"
+case 27:return "!"
break;
-case 28:yy_.yytext = "<="; return "SIGN"
+case 28:return "SIGN"
break;
-case 29:yy_.yytext = ">="; return "SIGN"
+case 29:yy_.yytext = "<="; return "SIGN"
break;
-case 30:yy_.yytext = "<="; return "SIGN"
+case 30:yy_.yytext = ">="; return "SIGN"
break;
-case 31:yy_.yytext = ">="; return "SIGN"
+case 31:yy_.yytext = "<="; return "SIGN"
break;
-case 32:yy_.yytext = "<>"; return "SIGN"
+case 32:yy_.yytext = ">="; return "SIGN"
break;
case 33:yy_.yytext = "<>"; return "SIGN"
break;
case 34:yy_.yytext = "<>"; return "SIGN"
break;
-case 35:yy_.yytext = "<="; return "SIGN"
+case 35:yy_.yytext = "<>"; return "SIGN"
break;
-case 36:yy_.yytext = ">="; return "SIGN"
+case 36:yy_.yytext = "<>"; return "SIGN"
break;
-case 37:return "FRAC"
+case 37:yy_.yytext = "<="; return "SIGN"
break;
-case 38:return "FRAC"
+case 38:yy_.yytext = ">="; return "SIGN"
break;
-case 39:return "sqrt"
+case 39:return "FRAC"
break;
-case 40:return "abs"
+case 40:return "FRAC"
break;
-case 41:return "ln"
+case 41:return "sqrt"
break;
-case 42:return "log"
+case 42:return "abs"
break;
-case 43:return "TRIG"
+case 43:return "ln"
break;
-case 44:return "TRIG"
+case 44:return "log"
break;
-case 45:yy_.yytext = "sin"; return "TRIG"
+case 45:return "TRIG"
break;
-case 46:yy_.yytext = "cos"; return "TRIG"
+case 46:return "TRIG"
break;
-case 47:yy_.yytext = "tan"; return "TRIG"
+case 47:return "TRIG"
break;
-case 48:yy_.yytext = "csc"; return "TRIG"
+case 48:return "TRIG"
break;
-case 49:yy_.yytext = "sec"; return "TRIG"
+case 49:yy_.yytext = "sin"; return "TRIG"
break;
-case 50:yy_.yytext = "cot"; return "TRIG"
+case 50:yy_.yytext = "cos"; return "TRIG"
break;
-case 51:yy_.yytext = "arcsin"; return "TRIG"
+case 51:yy_.yytext = "tan"; return "TRIG"
break;
-case 52:yy_.yytext = "arccos"; return "TRIG"
+case 52:yy_.yytext = "csc"; return "TRIG"
break;
-case 53:yy_.yytext = "arctan"; return "TRIG"
+case 53:yy_.yytext = "sec"; return "TRIG"
break;
-case 54:yy_.yytext = "arccsc"; return "TRIG"
+case 54:yy_.yytext = "cot"; return "TRIG"
break;
-case 55:yy_.yytext = "arcsec"; return "TRIG"
+case 55:yy_.yytext = "arcsin"; return "TRIG"
break;
-case 56:yy_.yytext = "arccot"; return "TRIG"
+case 56:yy_.yytext = "arccos"; return "TRIG"
break;
-case 57:return "TRIGINV"
+case 57:yy_.yytext = "arctan"; return "TRIG"
break;
-case 58:return "TRIGINV"
+case 58:yy_.yytext = "arccsc"; return "TRIG"
break;
-case 59:return "CONST"
+case 59:yy_.yytext = "arcsec"; return "TRIG"
break;
-case 60:yy_.yytext = "pi"; return "CONST"
+case 60:yy_.yytext = "arccot"; return "TRIG"
break;
-case 61:yy_.yytext = "pi"; return "CONST"
+case 61:return "TRIGINV"
break;
-case 62:return "VAR"
+case 62:return "TRIGINV"
break;
-case 63:yy_.yytext = "theta"; return "VAR"
+case 63:yy_.yytext = "sinh"; return "TRIG"
break;
-case 64:yy_.yytext = "theta"; return "VAR"
+case 64:yy_.yytext = "cosh"; return "TRIG"
break;
-case 65:return "VAR"
+case 65:yy_.yytext = "tanh"; return "TRIG"
break;
-case 66:yy_.yytext = "phi"; return "VAR"
+case 66:yy_.yytext = "csch"; return "TRIG"
break;
-case 67:yy_.yytext = "phi"; return "VAR"
+case 67:yy_.yytext = "sech"; return "TRIG"
break;
-case 68:return yy.symbolLexer(yy_.yytext)
+case 68:yy_.yytext = "tanh"; return "TRIG"
break;
-case 69:return "EOF"
+case 69:return "CONST"
break;
-case 70:return "INVALID"
+case 70:yy_.yytext = "pi"; return "CONST"
break;
-case 71:console.log(yy_.yytext);
+case 71:yy_.yytext = "pi"; return "CONST"
+break;
+case 72:return "VAR"
+break;
+case 73:yy_.yytext = "theta"; return "VAR"
+break;
+case 74:yy_.yytext = "theta"; return "VAR"
+break;
+case 75:return "VAR"
+break;
+case 76:yy_.yytext = "phi"; return "VAR"
+break;
+case 77:yy_.yytext = "phi"; return "VAR"
+break;
+case 78:return yy.symbolLexer(yy_.yytext)
+break;
+case 79:return "EOF"
+break;
+case 80:return "INVALID"
+break;
+case 81:console.log(yy_.yytext);
break;
}
},
-rules: [/^(?:\s+)/,/^(?:\\space)/,/^(?:\\ )/,/^(?:[0-9]+\.?)/,/^(?:([0-9]+)?\.[0-9]+)/,/^(?:\*\*)/,/^(?:\*)/,/^(?:\\cdot|·)/,/^(?:\\times|×)/,/^(?:\/)/,/^(?:-)/,/^(?:−)/,/^(?:\+)/,/^(?:\^)/,/^(?:\()/,/^(?:\))/,/^(?:\\left\()/,/^(?:\\right\))/,/^(?:\{)/,/^(?:\})/,/^(?:\\left\{)/,/^(?:\\right\})/,/^(?:_)/,/^(?:\|)/,/^(?:\\left\|)/,/^(?:\\right\|)/,/^(?:\!)/,/^(?:<=|>=|<>|<|>|=)/,/^(?:\\le)/,/^(?:\\ge)/,/^(?:\\leq)/,/^(?:\\geq)/,/^(?:=\/=)/,/^(?:\\ne)/,/^(?:≠)/,/^(?:≤)/,/^(?:≥)/,/^(?:\\frac)/,/^(?:\\dfrac)/,/^(?:sqrt|\\sqrt)/,/^(?:abs|\\abs)/,/^(?:ln|\\ln)/,/^(?:log|\\log)/,/^(?:sin|cos|tan)/,/^(?:csc|sec|cot)/,/^(?:\\sin)/,/^(?:\\cos)/,/^(?:\\tan)/,/^(?:\\csc)/,/^(?:\\sec)/,/^(?:\\cot)/,/^(?:\\arcsin)/,/^(?:\\arccos)/,/^(?:\\arctan)/,/^(?:\\arccsc)/,/^(?:\\arcsec)/,/^(?:\\arccot)/,/^(?:arcsin|arccos|arctan)/,/^(?:arccsc|arcsec|arccot)/,/^(?:pi)/,/^(?:π)/,/^(?:\\pi)/,/^(?:theta)/,/^(?:θ)/,/^(?:\\theta)/,/^(?:phi)/,/^(?:φ)/,/^(?:\\phi)/,/^(?:[a-zA-Z])/,/^(?:$)/,/^(?:.)/,/^(?:.)/],
-conditions: {"INITIAL":{"rules":[0,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],"inclusive":true}}
+rules: [/^(?:\s+)/,/^(?:\\space)/,/^(?:\\ )/,/^(?:[0-9]+\.?)/,/^(?:([0-9]+)?\.[0-9]+)/,/^(?:\*\*)/,/^(?:\*)/,/^(?:\\cdot|·)/,/^(?:\\times|×)/,/^(?:\\ast)/,/^(?:\/)/,/^(?:-)/,/^(?:−)/,/^(?:\+)/,/^(?:\^)/,/^(?:\()/,/^(?:\))/,/^(?:\\left\()/,/^(?:\\right\))/,/^(?:\{)/,/^(?:\})/,/^(?:\\left\{)/,/^(?:\\right\})/,/^(?:_)/,/^(?:\|)/,/^(?:\\left\|)/,/^(?:\\right\|)/,/^(?:\!)/,/^(?:<=|>=|<>|<|>|=)/,/^(?:\\le)/,/^(?:\\ge)/,/^(?:\\leq)/,/^(?:\\geq)/,/^(?:=\/=)/,/^(?:\\ne)/,/^(?:\\neq)/,/^(?:≠)/,/^(?:≤)/,/^(?:≥)/,/^(?:\\frac)/,/^(?:\\dfrac)/,/^(?:sqrt|\\sqrt)/,/^(?:abs|\\abs)/,/^(?:ln|\\ln)/,/^(?:log|\\log)/,/^(?:sin|cos|tan)/,/^(?:csc|sec|cot)/,/^(?:sinh|cosh|tanh)/,/^(?:csch|sech|coth)/,/^(?:\\sin)/,/^(?:\\cos)/,/^(?:\\tan)/,/^(?:\\csc)/,/^(?:\\sec)/,/^(?:\\cot)/,/^(?:\\arcsin)/,/^(?:\\arccos)/,/^(?:\\arctan)/,/^(?:\\arccsc)/,/^(?:\\arcsec)/,/^(?:\\arccot)/,/^(?:arcsin|arccos|arctan)/,/^(?:arccsc|arcsec|arccot)/,/^(?:\\sinh)/,/^(?:\\cosh)/,/^(?:\\tanh)/,/^(?:\\csch)/,/^(?:\\sech)/,/^(?:\\coth)/,/^(?:pi)/,/^(?:π)/,/^(?:\\pi)/,/^(?:theta)/,/^(?:θ)/,/^(?:\\theta)/,/^(?:phi)/,/^(?:φ)/,/^(?:\\phi)/,/^(?:[a-zA-Z])/,/^(?:$)/,/^(?:.)/,/^(?:.)/],
+conditions: {"INITIAL":{"rules":[0,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],"inclusive":true}}
};
return lexer;
})();
@@ -2248,15 +2268,15 @@ _.extend(Pow.prototype, {
var decimalsInBase = pow.base.getDecimalPlaces();
var root = new Pow(pow.base, new Rational(1, exp.d));
var decimalsInRoot = root.collect().getDecimalPlaces();
-
+
if (decimalsInRoot > decimalsInBase) {
// Collecting over this denominator would result in an
// imprecise float, so avoid doing so.
var newBase = new Pow(pow.base, new Int(exp.n)).collect();
return new Pow(newBase, new Rational(1, exp.d));
}
}
-
+
// e.g. 4^1.5 -> 8
return pow.base.raiseToThe(pow.exp, options);
} else {
@@ -2572,7 +2592,53 @@ _.extend(Trig.prototype, {
arccot: {
eval: function(arg) { return Math.atan(1 / arg); },
tex: "\\operatorname{arccot}"
- }
+ },
+ sinh: {
+ eval: function(arg) {
+ return (Math.exp(arg) - Math.exp(-arg)) / 2;
+ },
+ tex: "\\sinh",
+ expand: function() { return this; }
+ },
+ cosh: {
+ eval: function(arg) {
+ return (Math.exp(arg) + Math.exp(-arg)) / 2;
+ },
+ tex: "\\cosh",
+ expand: function() { return this; }
+ },
+ tanh: {
+ eval: function(arg) {
+ return (Math.exp(arg) - Math.exp(-arg)) / (Math.exp(arg) + Math.exp(-arg));
+ },
+ tex: "\\tanh",
+ expand: function() {
+ return Mul.handleDivide(Trig.sinh(this.arg), Trig.cosh(this.arg));
+ }
+ },
+ csch: {
+ eval: function(arg) { return 2 / (Math.exp(arg) - Math.exp(-arg)); },
+ tex: "\\csch",
+ expand: function() {
+ return Mul.handleDivide(Num.One, Trig.sinh(this.arg));
+ }
+ },
+ sech: {
+ eval: function(arg) { return 2 / (Math.exp(arg) + Math.exp(-arg)); },
+ tex: "\\sech",
+ expand: function() {
+ return Mul.handleDivide(Num.One, Trig.cosh(this.arg));
+ }
+ },
+ coth: {
+ eval: function(arg) {
+ return (Math.exp(arg) + Math.exp(-arg)) / (Math.exp(arg) - Math.exp(-arg));
+ },
+ tex: "\\coth",
+ expand: function() {
+ return Mul.handleDivide(Trig.cosh(this.arg), Trig.sinh(this.arg));
+ }
+ },
},
isEven: function() {
@@ -2694,6 +2760,14 @@ _.extend(Trig, {
cos: function(arg) {
return new Trig("cos", arg);
+ },
+
+ sinh: function(arg) {
+ return new Trig("sinh", arg);
+ },
+
+ cosh: function(arg) {
+ return new Trig("cosh", arg);
}
});
@@ -3350,7 +3424,7 @@ _.extend(Float.prototype, {
return Float.toDecimalPlaces(
this.n + num.eval(),
Math.max(this.getDecimalPlaces(), num.getDecimalPlaces())
- );
+ );
} else {
return new Float(this.n + num.eval()).collect();
}
@@ -3361,7 +3435,7 @@ _.extend(Float.prototype, {
return Float.toDecimalPlaces(
this.n * num.eval(),
this.getDecimalPlaces() + num.getDecimalPlaces()
- );
+ );
} else {
return new Float(this.n * num.eval()).collect();
}
View
64 src/nodes.js
@@ -1443,15 +1443,15 @@ _.extend(Pow.prototype, {
var decimalsInBase = pow.base.getDecimalPlaces();
var root = new Pow(pow.base, new Rational(1, exp.d));
var decimalsInRoot = root.collect().getDecimalPlaces();
-
+
if (decimalsInRoot > decimalsInBase) {
// Collecting over this denominator would result in an
// imprecise float, so avoid doing so.
var newBase = new Pow(pow.base, new Int(exp.n)).collect();
return new Pow(newBase, new Rational(1, exp.d));
}
}
-
+
// e.g. 4^1.5 -> 8
return pow.base.raiseToThe(pow.exp, options);
} else {
@@ -1767,7 +1767,53 @@ _.extend(Trig.prototype, {
arccot: {
eval: function(arg) { return Math.atan(1 / arg); },
tex: "\\operatorname{arccot}"
- }
+ },
+ sinh: {
+ eval: function(arg) {
+ return (Math.exp(arg) - Math.exp(-arg)) / 2;
+ },
+ tex: "\\sinh",
+ expand: function() { return this; }
+ },
+ cosh: {
+ eval: function(arg) {
+ return (Math.exp(arg) + Math.exp(-arg)) / 2;
+ },
+ tex: "\\cosh",
+ expand: function() { return this; }
+ },
+ tanh: {
+ eval: function(arg) {
+ return (Math.exp(arg) - Math.exp(-arg)) / (Math.exp(arg) + Math.exp(-arg));
+ },
+ tex: "\\tanh",
+ expand: function() {
+ return Mul.handleDivide(Trig.sinh(this.arg), Trig.cosh(this.arg));
+ }
+ },
+ csch: {
+ eval: function(arg) { return 2 / (Math.exp(arg) - Math.exp(-arg)); },
+ tex: "\\csch",
+ expand: function() {
+ return Mul.handleDivide(Num.One, Trig.sinh(this.arg));
+ }
+ },
+ sech: {
+ eval: function(arg) { return 2 / (Math.exp(arg) + Math.exp(-arg)); },
+ tex: "\\sech",
+ expand: function() {
+ return Mul.handleDivide(Num.One, Trig.cosh(this.arg));
+ }
+ },
+ coth: {
+ eval: function(arg) {
+ return (Math.exp(arg) + Math.exp(-arg)) / (Math.exp(arg) - Math.exp(-arg));
+ },
+ tex: "\\coth",
+ expand: function() {
+ return Mul.handleDivide(Trig.cosh(this.arg), Trig.sinh(this.arg));
+ }
+ },
},
isEven: function() {
@@ -1889,6 +1935,14 @@ _.extend(Trig, {
cos: function(arg) {
return new Trig("cos", arg);
+ },
+
+ sinh: function(arg) {
+ return new Trig("sinh", arg);
+ },
+
+ cosh: function(arg) {
+ return new Trig("cosh", arg);
}
});
@@ -2545,7 +2599,7 @@ _.extend(Float.prototype, {
return Float.toDecimalPlaces(
this.n + num.eval(),
Math.max(this.getDecimalPlaces(), num.getDecimalPlaces())
- );
+ );
} else {
return new Float(this.n + num.eval()).collect();
}
@@ -2556,7 +2610,7 @@ _.extend(Float.prototype, {
return Float.toDecimalPlaces(
this.n * num.eval(),
this.getDecimalPlaces() + num.getDecimalPlaces()
- );
+ );
} else {
return new Float(this.n * num.eval()).collect();
}
View
10 src/parser-generator.js
@@ -16,6 +16,7 @@ var grammar = {
["\\*", "return \"*\""],
["\\\\cdot|\u00b7", "return \"*\""],
["\\\\times|\u00d7", "return \"*\""],
+ ["\\\\ast", "return \"*\""],
["\\/", "return \"/\""],
["-", "return \"-\""],
["\u2212", "return \"-\""], // minus
@@ -41,6 +42,7 @@ var grammar = {
["\\\\geq", "yytext = \">=\"; return \"SIGN\""],
["=\\/=", "yytext = \"<>\"; return \"SIGN\""],
["\\\\ne", "yytext = \"<>\"; return \"SIGN\""],
+ ["\\\\neq", "yytext = \"<>\"; return \"SIGN\""],
["\u2260", "yytext = \"<>\"; return \"SIGN\""], // ne
["\u2264", "yytext = \"<=\"; return \"SIGN\""], // le
["\u2265", "yytext = \">=\"; return \"SIGN\""], // ge
@@ -52,6 +54,8 @@ var grammar = {
["log|\\\\log", "return \"log\""],
["sin|cos|tan", "return \"TRIG\""],
["csc|sec|cot", "return \"TRIG\""],
+ ["sinh|cosh|tanh", "return \"TRIG\""],
+ ["csch|sech|coth", "return \"TRIG\""],
["\\\\sin", "yytext = \"sin\"; return \"TRIG\""],
["\\\\cos", "yytext = \"cos\"; return \"TRIG\""],
["\\\\tan", "yytext = \"tan\"; return \"TRIG\""],
@@ -66,6 +70,12 @@ var grammar = {
["\\\\arccot", "yytext = \"arccot\"; return \"TRIG\""],
["arcsin|arccos|arctan","return \"TRIGINV\""],
["arccsc|arcsec|arccot","return \"TRIGINV\""],
+ ["\\\\sinh", "yytext = \"sinh\"; return \"TRIG\""],
+ ["\\\\cosh", "yytext = \"cosh\"; return \"TRIG\""],
+ ["\\\\tanh", "yytext = \"tanh\"; return \"TRIG\""],
+ ["\\\\csch", "yytext = \"csch\"; return \"TRIG\""],
+ ["\\\\sech", "yytext = \"sech\"; return \"TRIG\""],
+ ["\\\\coth", "yytext = \"tanh\"; return \"TRIG\""],
["pi", "return \"CONST\""],
["\u03C0", "yytext = \"pi\"; return \"CONST\""], // pi
["\\\\pi", "yytext = \"pi\"; return \"CONST\""],
View
144 src/parser.js
@@ -659,136 +659,156 @@ case 7:return "*"
break;
case 8:return "*"
break;
-case 9:return "/"
+case 9:return "*"
break;
-case 10:return "-"
+case 10:return "/"
break;
case 11:return "-"
break;
-case 12:return "+"
+case 12:return "-"
break;
-case 13:return "^"
+case 13:return "+"
break;
-case 14:return "("
+case 14:return "^"
break;
-case 15:return ")"
+case 15:return "("
break;
-case 16:return "("
+case 16:return ")"
break;
-case 17:return ")"
+case 17:return "("
break;
-case 18:return "{"
+case 18:return ")"
break;
-case 19:return "}"
+case 19:return "{"
break;
-case 20:return "{"
+case 20:return "}"
break;
-case 21:return "}"
+case 21:return "{"
break;
-case 22:return "_"
+case 22:return "}"
break;
-case 23:return "|"
+case 23:return "_"
break;
-case 24:return "LEFT|"
+case 24:return "|"
break;
-case 25:return "RIGHT|"
+case 25:return "LEFT|"
break;
-case 26:return "!"
+case 26:return "RIGHT|"
break;
-case 27:return "SIGN"
+case 27:return "!"
break;
-case 28:yy_.yytext = "<="; return "SIGN"
+case 28:return "SIGN"
break;
-case 29:yy_.yytext = ">="; return "SIGN"
+case 29:yy_.yytext = "<="; return "SIGN"
break;
-case 30:yy_.yytext = "<="; return "SIGN"
+case 30:yy_.yytext = ">="; return "SIGN"
break;
-case 31:yy_.yytext = ">="; return "SIGN"
+case 31:yy_.yytext = "<="; return "SIGN"
break;
-case 32:yy_.yytext = "<>"; return "SIGN"
+case 32:yy_.yytext = ">="; return "SIGN"
break;
case 33:yy_.yytext = "<>"; return "SIGN"
break;
case 34:yy_.yytext = "<>"; return "SIGN"
break;
-case 35:yy_.yytext = "<="; return "SIGN"
+case 35:yy_.yytext = "<>"; return "SIGN"
break;
-case 36:yy_.yytext = ">="; return "SIGN"
+case 36:yy_.yytext = "<>"; return "SIGN"
break;
-case 37:return "FRAC"
+case 37:yy_.yytext = "<="; return "SIGN"
break;
-case 38:return "FRAC"
+case 38:yy_.yytext = ">="; return "SIGN"
break;
-case 39:return "sqrt"
+case 39:return "FRAC"
break;
-case 40:return "abs"
+case 40:return "FRAC"
break;
-case 41:return "ln"
+case 41:return "sqrt"
break;
-case 42:return "log"
+case 42:return "abs"
break;
-case 43:return "TRIG"
+case 43:return "ln"
break;
-case 44:return "TRIG"
+case 44:return "log"
break;
-case 45:yy_.yytext = "sin"; return "TRIG"
+case 45:return "TRIG"
break;
-case 46:yy_.yytext = "cos"; return "TRIG"
+case 46:return "TRIG"
break;
-case 47:yy_.yytext = "tan"; return "TRIG"
+case 47:return "TRIG"
break;
-case 48:yy_.yytext = "csc"; return "TRIG"
+case 48:return "TRIG"
break;
-case 49:yy_.yytext = "sec"; return "TRIG"
+case 49:yy_.yytext = "sin"; return "TRIG"
break;
-case 50:yy_.yytext = "cot"; return "TRIG"
+case 50:yy_.yytext = "cos"; return "TRIG"
break;
-case 51:yy_.yytext = "arcsin"; return "TRIG"
+case 51:yy_.yytext = "tan"; return "TRIG"
break;
-case 52:yy_.yytext = "arccos"; return "TRIG"
+case 52:yy_.yytext = "csc"; return "TRIG"
break;
-case 53:yy_.yytext = "arctan"; return "TRIG"
+case 53:yy_.yytext = "sec"; return "TRIG"
break;
-case 54:yy_.yytext = "arccsc"; return "TRIG"
+case 54:yy_.yytext = "cot"; return "TRIG"
break;
-case 55:yy_.yytext = "arcsec"; return "TRIG"
+case 55:yy_.yytext = "arcsin"; return "TRIG"
break;
-case 56:yy_.yytext = "arccot"; return "TRIG"
+case 56:yy_.yytext = "arccos"; return "TRIG"
break;
-case 57:return "TRIGINV"
+case 57:yy_.yytext = "arctan"; return "TRIG"
break;
-case 58:return "TRIGINV"
+case 58:yy_.yytext = "arccsc"; return "TRIG"
break;
-case 59:return "CONST"
+case 59:yy_.yytext = "arcsec"; return "TRIG"
break;
-case 60:yy_.yytext = "pi"; return "CONST"
+case 60:yy_.yytext = "arccot"; return "TRIG"
break;
-case 61:yy_.yytext = "pi"; return "CONST"
+case 61:return "TRIGINV"
break;
-case 62:return "VAR"
+case 62:return "TRIGINV"
break;
-case 63:yy_.yytext = "theta"; return "VAR"
+case 63:yy_.yytext = "sinh"; return "TRIG"
break;
-case 64:yy_.yytext = "theta"; return "VAR"
+case 64:yy_.yytext = "cosh"; return "TRIG"
break;
-case 65:return "VAR"
+case 65:yy_.yytext = "tanh"; return "TRIG"
break;
-case 66:yy_.yytext = "phi"; return "VAR"
+case 66:yy_.yytext = "csch"; return "TRIG"
break;
-case 67:yy_.yytext = "phi"; return "VAR"
+case 67:yy_.yytext = "sech"; return "TRIG"
break;
-case 68:return yy.symbolLexer(yy_.yytext)
+case 68:yy_.yytext = "tanh"; return "TRIG"
break;
-case 69:return "EOF"
+case 69:return "CONST"
break;
-case 70:return "INVALID"
+case 70:yy_.yytext = "pi"; return "CONST"
break;
-case 71:console.log(yy_.yytext);
+case 71:yy_.yytext = "pi"; return "CONST"
+break;
+case 72:return "VAR"
+break;
+case 73:yy_.yytext = "theta"; return "VAR"
+break;
+case 74:yy_.yytext = "theta"; return "VAR"
+break;
+case 75:return "VAR"
+break;
+case 76:yy_.yytext = "phi"; return "VAR"
+break;
+case 77:yy_.yytext = "phi"; return "VAR"
+break;
+case 78:return yy.symbolLexer(yy_.yytext)
+break;
+case 79:return "EOF"
+break;
+case 80:return "INVALID"
+break;
+case 81:console.log(yy_.yytext);
break;
}
},
-rules: [/^(?:\s+)/,/^(?:\\space)/,/^(?:\\ )/,/^(?:[0-9]+\.?)/,/^(?:([0-9]+)?\.[0-9]+)/,/^(?:\*\*)/,/^(?:\*)/,/^(?:\\cdot|·)/,/^(?:\\times|×)/,/^(?:\/)/,/^(?:-)/,/^(?:−)/,/^(?:\+)/,/^(?:\^)/,/^(?:\()/,/^(?:\))/,/^(?:\\left\()/,/^(?:\\right\))/,/^(?:\{)/,/^(?:\})/,/^(?:\\left\{)/,/^(?:\\right\})/,/^(?:_)/,/^(?:\|)/,/^(?:\\left\|)/,/^(?:\\right\|)/,/^(?:\!)/,/^(?:<=|>=|<>|<|>|=)/,/^(?:\\le)/,/^(?:\\ge)/,/^(?:\\leq)/,/^(?:\\geq)/,/^(?:=\/=)/,/^(?:\\ne)/,/^(?:≠)/,/^(?:≤)/,/^(?:≥)/,/^(?:\\frac)/,/^(?:\\dfrac)/,/^(?:sqrt|\\sqrt)/,/^(?:abs|\\abs)/,/^(?:ln|\\ln)/,/^(?:log|\\log)/,/^(?:sin|cos|tan)/,/^(?:csc|sec|cot)/,/^(?:\\sin)/,/^(?:\\cos)/,/^(?:\\tan)/,/^(?:\\csc)/,/^(?:\\sec)/,/^(?:\\cot)/,/^(?:\\arcsin)/,/^(?:\\arccos)/,/^(?:\\arctan)/,/^(?:\\arccsc)/,/^(?:\\arcsec)/,/^(?:\\arccot)/,/^(?:arcsin|arccos|arctan)/,/^(?:arccsc|arcsec|arccot)/,/^(?:pi)/,/^(?:π)/,/^(?:\\pi)/,/^(?:theta)/,/^(?:θ)/,/^(?:\\theta)/,/^(?:phi)/,/^(?:φ)/,/^(?:\\phi)/,/^(?:[a-zA-Z])/,/^(?:$)/,/^(?:.)/,/^(?:.)/],
-conditions: {"INITIAL":{"rules":[0,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],"inclusive":true}}
+rules: [/^(?:\s+)/,/^(?:\\space)/,/^(?:\\ )/,/^(?:[0-9]+\.?)/,/^(?:([0-9]+)?\.[0-9]+)/,/^(?:\*\*)/,/^(?:\*)/,/^(?:\\cdot|·)/,/^(?:\\times|×)/,/^(?:\\ast)/,/^(?:\/)/,/^(?:-)/,/^(?:−)/,/^(?:\+)/,/^(?:\^)/,/^(?:\()/,/^(?:\))/,/^(?:\\left\()/,/^(?:\\right\))/,/^(?:\{)/,/^(?:\})/,/^(?:\\left\{)/,/^(?:\\right\})/,/^(?:_)/,/^(?:\|)/,/^(?:\\left\|)/,/^(?:\\right\|)/,/^(?:\!)/,/^(?:<=|>=|<>|<|>|=)/,/^(?:\\le)/,/^(?:\\ge)/,/^(?:\\leq)/,/^(?:\\geq)/,/^(?:=\/=)/,/^(?:\\ne)/,/^(?:\\neq)/,/^(?:≠)/,/^(?:≤)/,/^(?:≥)/,/^(?:\\frac)/,/^(?:\\dfrac)/,/^(?:sqrt|\\sqrt)/,/^(?:abs|\\abs)/,/^(?:ln|\\ln)/,/^(?:log|\\log)/,/^(?:sin|cos|tan)/,/^(?:csc|sec|cot)/,/^(?:sinh|cosh|tanh)/,/^(?:csch|sech|coth)/,/^(?:\\sin)/,/^(?:\\cos)/,/^(?:\\tan)/,/^(?:\\csc)/,/^(?:\\sec)/,/^(?:\\cot)/,/^(?:\\arcsin)/,/^(?:\\arccos)/,/^(?:\\arctan)/,/^(?:\\arccsc)/,/^(?:\\arcsec)/,/^(?:\\arccot)/,/^(?:arcsin|arccos|arctan)/,/^(?:arccsc|arcsec|arccot)/,/^(?:\\sinh)/,/^(?:\\cosh)/,/^(?:\\tanh)/,/^(?:\\csch)/,/^(?:\\sech)/,/^(?:\\coth)/,/^(?:pi)/,/^(?:π)/,/^(?:\\pi)/,/^(?:theta)/,/^(?:θ)/,/^(?:\\theta)/,/^(?:phi)/,/^(?:φ)/,/^(?:\\phi)/,/^(?:[a-zA-Z])/,/^(?:$)/,/^(?:.)/,/^(?:.)/],
+conditions: {"INITIAL":{"rules":[0,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],"inclusive":true}}
};
return lexer;
})();
View
39 test.html
@@ -7,6 +7,7 @@
<!-- Include QUnit -->
<link rel="stylesheet" href="node_modules/qunit/support/qunit/qunit/qunit.css" type="text/css" media="screen">
<script src="node_modules/qunit/support/qunit/qunit/qunit.js"></script>
+ <script src="node_modules/qunit/support/qunit/addons/close-enough/qunit-close-enough.js"></script>
<!-- Include Underscore -->
<script src="node_modules/underscore/underscore.js"></script>
@@ -43,12 +44,12 @@ <h2 id="qunit-userAgent"></h2>
var print = function(input, expected, options) {
var message = input + " parses as " + expected;
- strictEqual(parse(input, options).print(), expected, message);
+ QUnit.close(parse(input, options).print(), expected, 1e-9, message);
};
test("empty", function() {
print("", "");
- })
+ });
test("positive and negative primitives", function() {
print("0", "0");
@@ -191,6 +192,7 @@ <h2 id="qunit-userAgent"></h2>
print("a\u00d7b", "a*b");
print("a\\cdotb", "a*b");
print("a\\timesb", "a*b");
+ print("a\\astb", "a*b");
})
test("division", function() {
@@ -351,6 +353,12 @@ <h2 id="qunit-userAgent"></h2>
print("\\sin(\\theta)", "sin(theta)");
});
+ test("hyperbolic functions", function() {
+ print("sinh xy", "sinh(x*y)");
+ print("1/(sinhx)^2", "sinh(x)^(-2)");
+ print("\\sinh(\\theta)", "sinh(theta)");
+ });
+
test("formulas", function() {
print("mx+b", "m*x+b");
print("v^2/r", "v^(2)*r^(-1)");
@@ -383,6 +391,7 @@ <h2 id="qunit-userAgent"></h2>
print("1<>2", "1<>2");
print("1=/=2", "1<>2");
print("1\\ne2", "1<>2");
+ print("1\\neq2", "1<>2");
print("a\u2260b", "a<>b");
print("a\u2264b", "a<=b");
print("a\u2265b", "a>=b");
@@ -435,6 +444,7 @@ <h2 id="qunit-userAgent"></h2>
repr("f(x+y)", "Mul(Var(f),Add(Var(x),Var(y)))");
repr("f(x+y)", "Func(f,Add(Var(x),Var(y)))", {functions: ["f"]});
repr("sin(theta)", "Trig(sin,Var(theta))");
+ repr("tanh(theta)", "Trig(tanh,Var(theta))");
// verify that negative signs get folded into numbers
repr("-x*3", "Mul(Var(x),-3)");
@@ -565,6 +575,11 @@ <h2 id="qunit-userAgent"></h2>
tex("sin^2 x + cos^2 x = 1", "\\sin^{2}(x)+\\cos^{2}(x) = 1");
});
+ test("hyperbolic functions", function() {
+ tex("sinhx", "\\sinh(x)");
+ tex("sinh^2 x", "\\sinh^{2}(x)");
+ });
+
test("multiplication with numbers", function() {
tex("4*10", "4 \\cdot 10");
tex("10^5", "10^{5}");
@@ -693,6 +708,12 @@ <h2 id="qunit-userAgent"></h2>
val("log_2 2", 1);
});
+ test("hyperbolic expressions", function() {
+ val("cosh(0.2)", 1.0200667556190757);
+ val("coth(0.2)", 5.06648956343947);
+ val("csch(3 * 2)", 0.004957534813479361);
+ });
+
test("variable expressions", function() {
val("x", 3, {x: 3});
val("x^2", 9, {x: 3});
@@ -946,6 +967,11 @@ <h2 id="qunit-userAgent"></h2>
comp("arcsec x", "arccos (1/x)");
comp("arccot x", "arctan (1/x)");
+ // Reciprocal hyperbolic trig functions
+ comp("csch x", "1/sinh x");
+ comp("sech x", "1/cosh x");
+ comp("coth x", "1/tanh x");
+
// Make sure trig functions that are the same for all integer values
// are not the same
comp("-2sin(pi x) + 4", "4", false);
@@ -1229,6 +1255,15 @@ <h2 id="qunit-userAgent"></h2>
expand("cot(x)", "cos(x)/sin(x)");
});
+ test("expand hyperbolic functions", function() {
+ expand("sinh(x)", "sinh(x)");
+ expand("cosh(x)", "cosh(x)");
+ expand("tanh(x)", "sinh(x)/cosh(x)");
+ expand("csch(x)", "1/sinh(x)");
+ expand("sech(x)", "1/cosh(x)");
+ expand("coth(x)", "cosh(x)/sinh(x)");
+ });
+
var collect = function(input, reference) {
var actual = parse(input).collect().normalize().print();
var expected = parse(reference).normalize().print();

0 comments on commit 736a495

Please sign in to comment.