Permalink
Browse files

yeah

  • Loading branch information...
1 parent 6186578 commit 73cf4b0627bde29f57cfa40cb26278e13569bb78 @charliesome committed Apr 1, 2012
Showing with 565 additions and 443 deletions.
  1. +10 −443 ast.js
  2. +36 −0 ast/addition.js
  3. +19 −0 ast/cos.js
  4. +104 −0 ast/division.js
  5. +21 −0 ast/e.js
  6. +17 −0 ast/function.js
  7. +33 −0 ast/log_natural.js
  8. +95 −0 ast/multiplication.js
  9. +37 −0 ast/negation.js
  10. +27 −0 ast/number.js
  11. +60 −0 ast/power.js
  12. +19 −0 ast/sin.js
  13. +33 −0 ast/subtraction.js
  14. +19 −0 ast/tan.js
  15. +21 −0 ast/x.js
  16. +14 −0 index.html
View
453 ast.js

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,36 @@
+AST.Addition = function(left, right) {
+ this.left = left;
+ this.right = right;
+};
+
+AST.Addition.prototype = new AST.BinaryOperation();
+AST.Addition.prototype.constructor = AST.Addition;
+
+AST.Addition.prototype.precedence = 1;
+
+AST.Addition.prototype.operator = "+";
+
+AST.Addition.prototype.derive = function() {
+ return new AST.Addition(this.left.derive(), this.right.derive());
+};
+
+AST.Addition.prototype.simplify = function() {
+ var left = this.left.simplify();
+ var right = this.right.simplify();
+ if(left.identical(right)) {
+ return new AST.Multiplication(new AST.Number(2), left).simplify();
+ }
+ if(left instanceof AST.Number && left.number == 0) {
+ return right;
+ }
+ if(right instanceof AST.Number && right.number == 0) {
+ return left;
+ }
+ if(left instanceof AST.Number && right instanceof AST.Number) {
+ return new AST.Number(left.number + right.number);
+ }
+ if(right instanceof AST.Negation) {
+ return new AST.Subtraction(left, right.node).simplify();
+ }
+ return new AST.Addition(left, right);
+};
View
@@ -0,0 +1,19 @@
+AST.Cos = function Cos(node) {
+ this.node = node;
+};
+
+AST.Cos.prototype = new AST.Function();
+AST.Cos.prototype.constructor = AST.Cos;
+
+AST.Cos.prototype.name = "cos";
+
+AST.Cos.prototype.nameTeX = "\\cos";
+
+AST.Cos.prototype.derive = function() {
+ return new AST.Multiplication(new AST.Negation(new AST.Sin(this.node)), this.node.derive());
+};
+
+AST.Cos.prototype.simplify = function() {
+ var node = this.node.simplify();
+ return new AST.Cos(node);
+};
View
@@ -0,0 +1,104 @@
+AST.Division = function(left, right) {
+ this.left = left;
+ this.right = right;
+};
+
+AST.Division.prototype = new AST.BinaryOperation();
+AST.Division.prototype.constructor = AST.Division;
+
+AST.Division.prototype.precedence = 0;
+
+AST.Division.prototype.operator = "/";
+
+AST.Division.prototype.operatorTeX = "\\over";
+
+AST.Division.prototype.derive = function() {
+ return new AST.Division(new AST.Subtraction(new AST.Multiplication(this.left.derive(), this.right), new AST.Multiplication(this.left, this.right.derive())), new AST.Power(this.right, new AST.Number(2)));
+};
+
+AST.Division.prototype.simplify = function() {
+ var left = this.left.simplify();
+ var right = this.right.simplify();
+
+ function gcd(a, b) {
+ if(b == 0) {
+ return a;
+ } else {
+ return gcd(b, a % b);
+ }
+ }
+
+ if((left instanceof AST.Number) && (right instanceof AST.Number)) {
+ var factor = gcd(left.number, right.number);
+ if(right.number == factor) {
+ return new AST.Number(left.number / factor);
+ } else {
+ return new AST.Division(new AST.Number(left.number / factor), new AST.Number(right.number / factor));
+ }
+ }
+
+ if(left instanceof AST.Number) {
+ if(left.number == 0) {
+ return new AST.Number(0);
+ }
+ }
+
+ if(right instanceof AST.Number) {
+ if(right.number == 1) {
+ return left;
+ }
+ }
+
+ if((left instanceof AST.Negation) && (right instanceof AST.Negation)) {
+ return new AST.Division(left.node, right.node).simplify();
+ }
+
+ if(left instanceof AST.Negation) {
+ return new AST.Negation(new AST.Division(left.node, right)).simplify();
+ }
+
+ if(right instanceof AST.Negation) {
+ return new AST.Negation(new AST.Division(left, right.node)).simplify();
+ }
+
+ return this.removeCommonFactors();
+};
+
+AST.Division.prototype.removeCommonFactors = function() {
+ var left = this.left.simplify();
+ var right = this.right.simplify();
+
+ var top_muls = []
+ var bottom_muls = [];
+ for(var top = left; top instanceof AST.Multiplication; top = top.right) {
+ top_muls.push(top.left);
+ }
+ top_muls.push(top);
+
+ for(var bottom = right; bottom instanceof AST.Multiplication; bottom = bottom.right) {
+ bottom_muls.push(bottom.left);
+ }
+ bottom_muls.push(bottom);
+
+ for(var i = 0; i < top_muls.length; i++) {
+ for(var j = 0; j < bottom_muls.length; j++) {
+ if(top_muls[i] instanceof AST.Power && top_muls[i].right instanceof AST.Number && top_muls[i].left.identical(bottom_muls[j])) {
+ top_muls[i] = new AST.Power(top_muls[i].left, new AST.Number(top_muls[i].right.number - 1)).simplify();
+ bottom_muls[j] = new AST.Number(1);
+ }
+ if(top_muls[i].identical(bottom_muls[j])) {
+ top_muls[i] = new AST.Number(1);
+ bottom_muls[j] = new AST.Number(1);
+ }
+ }
+ }
+
+ var top = top_muls.reduceRight(function(b, a) { return new AST.Multiplication(b, a); }).simplify();
+ var bottom = bottom_muls.reduceRight(function(b, a) { return new AST.Multiplication(b, a); }).simplify();
+
+ if(bottom instanceof AST.Number && bottom.number == 1) {
+ return top;
+ }
+
+ return new AST.Division(top, bottom);
+};
View
@@ -0,0 +1,21 @@
+AST.E = function() { };
+
+AST.E.prototype.derive = function() {
+ return new AST.Number(0);
+};
+
+AST.E.prototype.simplify = function() {
+ return this;
+};
+
+AST.E.prototype.toString = function() {
+ return "e";
+};
+
+AST.E.prototype.toTeX = function() {
+ return "e";
+};
+
+AST.E.prototype.identical = function(other) {
+ return other instanceof AST.E;
+};
View
@@ -0,0 +1,17 @@
+AST.Function = function() { };
+
+AST.Function.prototype.toString = function() {
+ return this.name + "(" + this.node.toString() + ")";
+};
+
+AST.Function.prototype.toTeX = function() {
+ return (this.nameTeX || this.name) + "\\left({" + this.node.toTeX() + "}\\right)";
+};
+
+AST.Function.prototype.toTeXWithPower = function(power) {
+ return (this.nameTeX || this.name) + "^{" + power + "}\\left({" + this.node.toTeX() + "}\\right)";
+};
+
+AST.Function.prototype.identical = function(other) {
+ return (other instanceof this.constructor) && this.node.identical(other.node);
+};
View
@@ -0,0 +1,33 @@
+AST.LogNatural = function(node) {
+ this.node = node;
+};
+
+AST.LogNatural.prototype.derive = function() {
+ return new AST.Division(this.node.derive(), this.node);
+};
+
+AST.LogNatural.prototype.simplify = function() {
+ var node = this.node.simplify();
+ if((node instanceof AST.Number) && node.number == 1) {
+ return new AST.Number(0);
+ }
+ if(node instanceof AST.E) {
+ return new AST.Number(1);
+ }
+ if((node instanceof AST.Power) && (node.left instanceof AST.E)) {
+ return node.right;
+ }
+ return new AST.LogNatural(node);
+};
+
+AST.LogNatural.prototype.toString = function() {
+ return "ln(" + this.node.toString() + ")";
+};
+
+AST.LogNatural.prototype.toTeX = function() {
+ return "\\log_{e}{\\left(" + this.node.toTeX() + "\\right)}";
+};
+
+AST.LogNatural.prototype.identical = function(other) {
+ return other instanceof AST.LogNatural && this.node.identical(other.node);
+};
View
@@ -0,0 +1,95 @@
+AST.Multiplication = function(left, right) {
+ this.left = left;
+ this.right = right;
+};
+
+AST.Multiplication.prototype = new AST.BinaryOperation();
+AST.Multiplication.prototype.constructor = AST.Multiplication;
+
+AST.Multiplication.prototype.precedence = 2;
+
+AST.Multiplication.prototype.operator = "*";
+
+AST.Multiplication.prototype.operatorTeX = "\\cdot";
+
+AST.Multiplication.prototype.derive = function() {
+ return new AST.Addition(new AST.Multiplication(this.left.derive(), this.right), new AST.Multiplication(this.left, this.right.derive()));
+};
+
+AST.Multiplication.prototype.simplify = function() {
+ var left = this.left.simplify();
+ var right = this.right.simplify();
+ if(right instanceof AST.Number) {
+ var tmp = left;
+ left = right;
+ right = tmp;
+ }
+ if(left instanceof AST.Power && left.right instanceof AST.Number) {
+ if(left.left.identical(right)) {
+ return new AST.Power(right, new AST.Number(left.right.number + 1));
+ }
+ if(right instanceof AST.Power && right.right instanceof AST.Number) {
+ if(right.left.identical(left.left)) {
+ return new AST.Power(left.left, new AST.Number(right.right.number + left.right.number));
+ }
+ }
+ }
+ if(right instanceof AST.Power && right.right instanceof AST.Number) {
+ if(right.left.identical(left)) {
+ return new AST.Power(left, new AST.Number(right.right.number + 1));
+ }
+ }
+ if(left.identical(right)) {
+ return new AST.Power(left, new AST.Number(2));
+ }
+ if(left instanceof AST.Number && left.number == 0) {
+ return new AST.Number(0);
+ }
+ if(right instanceof AST.Number && right.number == 0) {
+ return new AST.Number(0);
+ }
+ if(left instanceof AST.Number && left.number == 1) {
+ return right;
+ }
+ if(right instanceof AST.Number && right.number == 1) {
+ return left;
+ }
+ if((left instanceof AST.Negation) && (right instanceof AST.Negation)) {
+ return new AST.Multiplication(left.node, right.node).simplify();
+ }
+ if(left instanceof AST.Negation) {
+ return new AST.Negation(new AST.Multiplication(left.node, right)).simplify();
+ }
+ if(right instanceof AST.Negation) {
+ return new AST.Negation(new AST.Multiplication(left, right.node)).simplify();
+ }
+ if(left instanceof AST.Number && right instanceof AST.Number) {
+ return new AST.Number(left.number * right.number);
+ }
+ if(left instanceof AST.Multiplication) {
+ var lr = left.right;
+ left = left.left;
+ return new AST.Multiplication(left, new AST.Multiplication(lr, right)).simplify();
+ }
+ if(left instanceof AST.Division && right instanceof AST.Division) {
+ return new AST.Division(new AST.Multiplication(left.left, right.left), new AST.Multiplication(left.right, right.right)).simplify();
+ }
+ if(left instanceof AST.Division) {
+ return new AST.Division(new AST.Multiplication(left.left, right), left.right).simplify();
+ }
+ if(right instanceof AST.Division) {
+ return new AST.Division(new AST.Multiplication(left, right.left), right.right).simplify();
+ }
+
+ return new AST.Multiplication(left, right);
+};
+
+AST.Multiplication.prototype.toTeX = function() {
+ if((this.left instanceof AST.Number) && !this.right.precedence) {
+ return this.left.toTeX() + this.right.toTeX();
+ }
+ if((this.right instanceof AST.Number) && !this.left.precedence) {
+ return this.right.toTeX() + this.left.toTeX();
+ }
+ return AST.BinaryOperation.prototype.toTeX.call(this);
+};
View
@@ -0,0 +1,37 @@
+AST.Negation = function(node) {
+ this.node = node;
+};
+
+AST.Negation.prototype.derive = function() {
+ return new AST.Negation(this.node.derive());
+};
+
+AST.Negation.prototype.simplify = function() {
+ if(this.node instanceof AST.Negation) {
+ return this.node.simplify();
+ }
+ if(this.node instanceof AST.Number) {
+ return new AST.Number(-this.node.number);
+ }
+ return new AST.Negation(this.node.simplify());
+};
+
+AST.Negation.prototype.toString = function() {
+ if(!this.node.precedence) {
+ return "-" + this.node;
+ } else {
+ return "-(" + this.node + ")";
+ }
+};
+
+AST.Negation.prototype.toTeX = function() {
+ if(this.node instanceof AST.Addition || this.node instanceof AST.Subtraction) {
+ return "-\\left({" + this.node.toTeX() + "}\\right)";
+ } else {
+ return "-{" + this.node.toTeX() + "}";
+ }
+};
+
+AST.Negation.prototype.identical = function(other) {
+ return other instanceof AST.Negation && this.node.identical(other.node);
+};
Oops, something went wrong.

0 comments on commit 73cf4b0

Please sign in to comment.