Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var binary = {
  2.     "+"  : function(left, right) {return left + right},
  3.     "-"  : function(left, right) {return left - right},
  4.     "*"  : function(left, right) {return left * right},
  5.     "/"  : function(left, right) {return left / right}
  6. };
  7.  
  8. var unary = {
  9.     "negate" : function(expr) {return -expr},
  10.     "sin" : function(expr) {return Math.sin(expr)},
  11.     "cos" : function(expr) {return Math.cos(expr)}
  12. };
  13.  
  14. function Const(value) {
  15.     this.value = value;
  16. }
  17.  
  18. Const.prototype.evaluate = function() {
  19.     return this.value;
  20. };
  21.  
  22. Const.prototype.toString = function() {
  23.     return this.value.toString();
  24. };
  25.  
  26. Const.prototype.diff = function() {
  27.     return new Const(0);
  28. };
  29.  
  30. Const.prototype.simplify = function() {
  31.     return new Const(this.value);
  32. };
  33.  
  34. function Variable(name) {
  35.     this.name = name;
  36. }
  37.  
  38. Variable.prototype.evaluate = function() {
  39.     switch (this.name) {
  40.         case "x" :
  41.             return arguments[0];
  42.         case "y" :
  43.             return arguments[1];
  44.         default :
  45.             return arguments[2];
  46.     }
  47. };
  48.  
  49. Variable.prototype.toString = function() {
  50.     return this.name;
  51. };
  52.  
  53. Variable.prototype.diff = function(diffName) {
  54.     if (this.name == diffName) {
  55.         return new Const(1);
  56.     } else {
  57.         return new Const(0);
  58.     }
  59. };
  60.  
  61. Variable.prototype.simplify = function() {
  62.     return this;
  63. };
  64.  
  65. function UnaryOperation(operation, expr) {
  66.     this.operation = operation;
  67.     this.expr = expr;
  68. }
  69.  
  70. UnaryOperation.prototype.evaluate = function() {
  71.     return unary[this.operation](this.expr.evaluate.apply(this.expr, arguments));
  72. };
  73.  
  74. UnaryOperation.prototype.toString = function() {
  75.     return this.expr.toString() + " " + this.operation;
  76. };
  77.  
  78. function Negate(expr) {
  79.     UnaryOperation.call(this, "negate", expr);
  80. }
  81.  
  82. Negate.prototype = Object.create(UnaryOperation.prototype);;
  83.  
  84. Negate.prototype.diff = function(name) {
  85.     return new Negate(this.expr.diff(name));
  86. };
  87.  
  88. Negate.prototype.simplify = function() {
  89.     var newExpr = this.expr.simplify();
  90.  
  91.     if (newExpr instanceof Const) {
  92.         return new Const(-newExpr.evaluate.apply(newExpr, arguments));
  93.     }
  94.     return new Negate(newExpr);
  95. };
  96.  
  97. function Sin(expr) {
  98.     UnaryOperation.call(this, "sin", expr);
  99. }
  100.  
  101. Sin.prototype = Object.create(UnaryOperation.prototype);;
  102.  
  103. Sin.prototype.diff = function(name) {
  104.     return new Multiply(new Cos(this.expr), this.expr.diff(name));
  105. };
  106.  
  107. Sin.prototype.simplify = function() {
  108.     var newExpr = this.expr.simplify();
  109.  
  110.     if (newExpr instanceof Const) {
  111.         return new Const(new Sin(newExpr).evaluate.apply(newExpr, arguments));
  112.     }
  113.  
  114.     return new Sin(newExpr);
  115. };
  116.  
  117. function Cos(expr) {
  118.     UnaryOperation.call(this, "cos", expr);
  119. }
  120.  
  121. Cos.prototype = Object.create(UnaryOperation.prototype);;
  122.  
  123. Cos.prototype.diff = function(name) {
  124.     return new Multiply(new Negate(new Sin(this.expr)), this.expr.diff(name));
  125. };
  126.  
  127. Cos.prototype.simplify = function() {
  128.     var newExpr = this.expr.simplify();
  129.  
  130.     if (newExpr instanceof Const) {
  131.         return new Const(new Cos(newExpr).evaluate.apply(newExpr, arguments));
  132.     }
  133.  
  134.     return new Cos(newExpr);
  135. };
  136.  
  137. function BinaryOperation(operation, left, right) {
  138.     this.operation = operation;
  139.     this.left = left;
  140.     this.right = right;
  141. }
  142.  
  143. BinaryOperation.prototype.evaluate = function() {
  144.     return binary[this.operation](this.left.evaluate.apply(this.left, arguments),
  145.         this.right.evaluate.apply(this.right, arguments));
  146. };
  147.  
  148. BinaryOperation.prototype.toString = function() {
  149.     return this.left.toString() + " " + this.right.toString() + " " + this.operation;
  150. };
  151.  
  152. function Add(left, right) {
  153.     BinaryOperation.call(this, "+", left, right);
  154. }
  155.  
  156. Add.prototype = Object.create(BinaryOperation.prototype);;
  157.  
  158. Add.prototype.diff = function(name) {
  159.     return new Add(this.left.diff(name), this.right.diff(name));
  160. };
  161.  
  162. Add.prototype.simplify = function() {
  163.     var newLeft = this.left.simplify();
  164.     var newRight = this.right.simplify();
  165.  
  166.     if ((newLeft instanceof Const) && (newRight instanceof Const)) {
  167.         return new Const(newLeft.evaluate() + newRight.evaluate());
  168.     }
  169.  
  170.     if (newLeft.toString() == "0") {
  171.         return newRight;
  172.     }
  173.     if (newRight.toString() == "0") {
  174.         return newLeft;
  175.     }
  176.     return new Add(newLeft, newRight);
  177. };
  178.  
  179. function Subtract(left, right) {
  180.     BinaryOperation.call(this, "-", left, right);
  181. }
  182.  
  183. Subtract.prototype = Object.create(BinaryOperation.prototype);;
  184.  
  185. Subtract.prototype.diff = function(name) {
  186.     return new Subtract(this.left.diff(name), this.right.diff(name));
  187. };
  188.  
  189. Subtract.prototype.simplify = function() {
  190.     var newLeft = this.left.simplify();
  191.     var newRight = this.right.simplify();
  192.  
  193.     if ((newLeft instanceof Const) && (newRight instanceof Const)) {
  194.         return new Const(newLeft.evaluate() - newRight.evaluate());
  195.     }
  196.  
  197.     if (newLeft.toString() == "0") {
  198.         return new Negate(newRight).simplify();
  199.     }
  200.  
  201.     if (newRight.toString() == "0") {
  202.         return newLeft;
  203.     }
  204.  
  205.     return new Subtract(newLeft, newRight);
  206. };
  207.  
  208. function Multiply(left, right) {
  209.     BinaryOperation.call(this, "*", left, right);
  210. }
  211.  
  212. Multiply.prototype = Object.create(BinaryOperation.prototype);;
  213.  
  214. Multiply.prototype.diff = function(name) {
  215.     return new Add(
  216.         new Multiply(this.left.diff(name), this.right),
  217.         new Multiply(this.left, this.right.diff(name))
  218.     )
  219. };
  220.  
  221. Multiply.prototype.simplify = function() {
  222.     var newLeft = this.left.simplify();
  223.     var newRight = this.right.simplify();
  224.  
  225.     if ((newLeft instanceof Const) && (newRight instanceof Const)) {
  226.         return new Const(newLeft.evaluate() * newRight.evaluate());
  227.     }
  228.  
  229.     if (newLeft.toString() == "0" || newRight.toString() == "0") {
  230.         return new Const(0);
  231.     }
  232.     if (newLeft.toString() == "1") {
  233.         return newRight;
  234.     }
  235.     if (newRight.toString() == "1") {
  236.         return newLeft;
  237.     }
  238.     return new Multiply(newLeft, newRight);
  239. };
  240.  
  241. function Divide(left, right) {
  242.     BinaryOperation.call(this, "/", left, right);
  243. }
  244.  
  245. Divide.prototype = Object.create(BinaryOperation.prototype);;
  246.  
  247. Divide.prototype.diff = function(name) {
  248.     return new Divide(
  249.         new Subtract(
  250.             new Multiply(this.left.diff(name), this.right),
  251.             new Multiply(this.left, this.right.diff(name))
  252.         ),
  253.         new Multiply(this.right, this.right)
  254.     )
  255. };
  256.  
  257. Divide.prototype.simplify = function() {
  258.     var newLeft = this.left.simplify();
  259.     var newRight = this.right.simplify();
  260.  
  261.     if ((newLeft instanceof Const) && (newRight instanceof Const)) {
  262.         return new Const(newLeft.evaluate() / newRight.evaluate());
  263.     }
  264.  
  265.     if (newLeft.toString() == "0") {
  266.         return new Const(0);
  267.     }
  268.     if (newRight.toString() == "1") {
  269.         return newLeft;
  270.     }
  271.     return new Divide(newLeft, newRight);
  272. };
  273.  
  274. var binaryObjects = {
  275.     "+" : Add,
  276.     "-" : Subtract,
  277.     "*" : Multiply,
  278.     "/" : Divide
  279. };
  280.  
  281. var unaryObjects = {
  282.     "negate" : Negate,
  283.     "sin" : Sin,
  284.     "cos" : Cos
  285. };
  286.  
  287. function parse(mainString) {
  288.     mainString += ' ';
  289.     var it = 0, temp = "";
  290.     var stack = [];
  291.  
  292.     while (it != mainString.length) {
  293.         if (mainString[it] == ' ') {
  294.             if (temp == "") {
  295.                 it++;
  296.                 continue;
  297.             }
  298.             if (temp in binary) {
  299.                 var right = stack.pop();
  300.                 var left = stack.pop();
  301.  
  302.                 stack.push(new binaryObjects[temp](left, right));
  303.             } else {
  304.                 if (temp in unary) {
  305.                     stack.push(new unaryObjects[temp](stack.pop()));
  306.                 } else {
  307.                     if (parseInt(temp)) {
  308.                         stack.push(new Const(parseInt(temp)));
  309.                     } else {
  310.                         stack.push(new Variable(temp));
  311.                     }
  312.                 }
  313.             }
  314.  
  315.             temp = "";
  316.         } else {
  317.             temp += mainString[it];
  318.         }
  319.         it++;
  320.     }
  321.     return stack.pop();
  322. }
  323. //
  324. //var expr =  new Divide(new Negate(new Variable('x')), new Const(2));
  325. //println(expr.evaluate(2, 0, 0));
  326. //
  327. //println(parse('3 y -').diff('x').simplify().toString());
  328. //
  329. //println(parse('x 0 +').simplify().toString());
  330.  
  331. //println(parse("1 cos").diff("x").simplify().toString());
  332. //var expr = (new Add(new Variable('x'), new Const(2))).diff('x');
  333. //println(expr.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement