Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var binary = {
- "+" : function(left, right) {return left + right},
- "-" : function(left, right) {return left - right},
- "*" : function(left, right) {return left * right},
- "/" : function(left, right) {return left / right}
- };
- var unary = {
- "negate" : function(expr) {return -expr},
- "sin" : function(expr) {return Math.sin(expr)},
- "cos" : function(expr) {return Math.cos(expr)}
- };
- function Const(value) {
- this.value = value;
- }
- Const.prototype.evaluate = function() {
- return this.value;
- };
- Const.prototype.toString = function() {
- return this.value.toString();
- };
- Const.prototype.diff = function() {
- return new Const(0);
- };
- Const.prototype.simplify = function() {
- return new Const(this.value);
- };
- function Variable(name) {
- this.name = name;
- }
- Variable.prototype.evaluate = function() {
- switch (this.name) {
- case "x" :
- return arguments[0];
- case "y" :
- return arguments[1];
- default :
- return arguments[2];
- }
- };
- Variable.prototype.toString = function() {
- return this.name;
- };
- Variable.prototype.diff = function(diffName) {
- if (this.name == diffName) {
- return new Const(1);
- } else {
- return new Const(0);
- }
- };
- Variable.prototype.simplify = function() {
- return this;
- };
- function UnaryOperation(operation, expr) {
- this.operation = operation;
- this.expr = expr;
- }
- UnaryOperation.prototype.evaluate = function() {
- return unary[this.operation](this.expr.evaluate.apply(this.expr, arguments));
- };
- UnaryOperation.prototype.toString = function() {
- return this.expr.toString() + " " + this.operation;
- };
- function Negate(expr) {
- UnaryOperation.call(this, "negate", expr);
- }
- Negate.prototype = Object.create(UnaryOperation.prototype);;
- Negate.prototype.diff = function(name) {
- return new Negate(this.expr.diff(name));
- };
- Negate.prototype.simplify = function() {
- var newExpr = this.expr.simplify();
- if (newExpr instanceof Const) {
- return new Const(-newExpr.evaluate.apply(newExpr, arguments));
- }
- return new Negate(newExpr);
- };
- function Sin(expr) {
- UnaryOperation.call(this, "sin", expr);
- }
- Sin.prototype = Object.create(UnaryOperation.prototype);;
- Sin.prototype.diff = function(name) {
- return new Multiply(new Cos(this.expr), this.expr.diff(name));
- };
- Sin.prototype.simplify = function() {
- var newExpr = this.expr.simplify();
- if (newExpr instanceof Const) {
- return new Const(new Sin(newExpr).evaluate.apply(newExpr, arguments));
- }
- return new Sin(newExpr);
- };
- function Cos(expr) {
- UnaryOperation.call(this, "cos", expr);
- }
- Cos.prototype = Object.create(UnaryOperation.prototype);;
- Cos.prototype.diff = function(name) {
- return new Multiply(new Negate(new Sin(this.expr)), this.expr.diff(name));
- };
- Cos.prototype.simplify = function() {
- var newExpr = this.expr.simplify();
- if (newExpr instanceof Const) {
- return new Const(new Cos(newExpr).evaluate.apply(newExpr, arguments));
- }
- return new Cos(newExpr);
- };
- function BinaryOperation(operation, left, right) {
- this.operation = operation;
- this.left = left;
- this.right = right;
- }
- BinaryOperation.prototype.evaluate = function() {
- return binary[this.operation](this.left.evaluate.apply(this.left, arguments),
- this.right.evaluate.apply(this.right, arguments));
- };
- BinaryOperation.prototype.toString = function() {
- return this.left.toString() + " " + this.right.toString() + " " + this.operation;
- };
- function Add(left, right) {
- BinaryOperation.call(this, "+", left, right);
- }
- Add.prototype = Object.create(BinaryOperation.prototype);;
- Add.prototype.diff = function(name) {
- return new Add(this.left.diff(name), this.right.diff(name));
- };
- Add.prototype.simplify = function() {
- var newLeft = this.left.simplify();
- var newRight = this.right.simplify();
- if ((newLeft instanceof Const) && (newRight instanceof Const)) {
- return new Const(newLeft.evaluate() + newRight.evaluate());
- }
- if (newLeft.toString() == "0") {
- return newRight;
- }
- if (newRight.toString() == "0") {
- return newLeft;
- }
- return new Add(newLeft, newRight);
- };
- function Subtract(left, right) {
- BinaryOperation.call(this, "-", left, right);
- }
- Subtract.prototype = Object.create(BinaryOperation.prototype);;
- Subtract.prototype.diff = function(name) {
- return new Subtract(this.left.diff(name), this.right.diff(name));
- };
- Subtract.prototype.simplify = function() {
- var newLeft = this.left.simplify();
- var newRight = this.right.simplify();
- if ((newLeft instanceof Const) && (newRight instanceof Const)) {
- return new Const(newLeft.evaluate() - newRight.evaluate());
- }
- if (newLeft.toString() == "0") {
- return new Negate(newRight).simplify();
- }
- if (newRight.toString() == "0") {
- return newLeft;
- }
- return new Subtract(newLeft, newRight);
- };
- function Multiply(left, right) {
- BinaryOperation.call(this, "*", left, right);
- }
- Multiply.prototype = Object.create(BinaryOperation.prototype);;
- Multiply.prototype.diff = function(name) {
- return new Add(
- new Multiply(this.left.diff(name), this.right),
- new Multiply(this.left, this.right.diff(name))
- )
- };
- Multiply.prototype.simplify = function() {
- var newLeft = this.left.simplify();
- var newRight = this.right.simplify();
- if ((newLeft instanceof Const) && (newRight instanceof Const)) {
- return new Const(newLeft.evaluate() * newRight.evaluate());
- }
- if (newLeft.toString() == "0" || newRight.toString() == "0") {
- return new Const(0);
- }
- if (newLeft.toString() == "1") {
- return newRight;
- }
- if (newRight.toString() == "1") {
- return newLeft;
- }
- return new Multiply(newLeft, newRight);
- };
- function Divide(left, right) {
- BinaryOperation.call(this, "/", left, right);
- }
- Divide.prototype = Object.create(BinaryOperation.prototype);;
- Divide.prototype.diff = function(name) {
- return new Divide(
- new Subtract(
- new Multiply(this.left.diff(name), this.right),
- new Multiply(this.left, this.right.diff(name))
- ),
- new Multiply(this.right, this.right)
- )
- };
- Divide.prototype.simplify = function() {
- var newLeft = this.left.simplify();
- var newRight = this.right.simplify();
- if ((newLeft instanceof Const) && (newRight instanceof Const)) {
- return new Const(newLeft.evaluate() / newRight.evaluate());
- }
- if (newLeft.toString() == "0") {
- return new Const(0);
- }
- if (newRight.toString() == "1") {
- return newLeft;
- }
- return new Divide(newLeft, newRight);
- };
- var binaryObjects = {
- "+" : Add,
- "-" : Subtract,
- "*" : Multiply,
- "/" : Divide
- };
- var unaryObjects = {
- "negate" : Negate,
- "sin" : Sin,
- "cos" : Cos
- };
- function parse(mainString) {
- mainString += ' ';
- var it = 0, temp = "";
- var stack = [];
- while (it != mainString.length) {
- if (mainString[it] == ' ') {
- if (temp == "") {
- it++;
- continue;
- }
- if (temp in binary) {
- var right = stack.pop();
- var left = stack.pop();
- stack.push(new binaryObjects[temp](left, right));
- } else {
- if (temp in unary) {
- stack.push(new unaryObjects[temp](stack.pop()));
- } else {
- if (parseInt(temp)) {
- stack.push(new Const(parseInt(temp)));
- } else {
- stack.push(new Variable(temp));
- }
- }
- }
- temp = "";
- } else {
- temp += mainString[it];
- }
- it++;
- }
- return stack.pop();
- }
- //
- //var expr = new Divide(new Negate(new Variable('x')), new Const(2));
- //println(expr.evaluate(2, 0, 0));
- //
- //println(parse('3 y -').diff('x').simplify().toString());
- //
- //println(parse('x 0 +').simplify().toString());
- //println(parse("1 cos").diff("x").simplify().toString());
- //var expr = (new Add(new Variable('x'), new Const(2))).diff('x');
- //println(expr.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement