Commit c0ad3f03 authored by Peter Eastman's avatar Peter Eastman
Browse files

Improved display of expressions when printing them to a stream

parent 055e308a
......@@ -96,6 +96,12 @@ public:
* @param variable the variable with respect to which the derivate should be taken
*/
virtual ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const = 0;
/**
* Get whether this operation should be displayed with infix notation.
*/
virtual bool isInfixOperator() const {
return false;
}
virtual bool operator!=(const Operation& op) const {
return op.getId() != getId();
}
......@@ -264,6 +270,9 @@ public:
return args[0]+args[1];
}
ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const;
bool isInfixOperator() const {
return true;
}
};
class Operation::Subtract : public Operation {
......@@ -286,6 +295,9 @@ public:
return args[0]-args[1];
}
ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const;
bool isInfixOperator() const {
return true;
}
};
class Operation::Multiply : public Operation {
......@@ -308,6 +320,9 @@ public:
return args[0]*args[1];
}
ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const;
bool isInfixOperator() const {
return true;
}
};
class Operation::Divide : public Operation {
......@@ -330,6 +345,9 @@ public:
return args[0]/args[1];
}
ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const;
bool isInfixOperator() const {
return true;
}
};
class Operation::Power : public Operation {
......@@ -352,6 +370,9 @@ public:
return std::pow(args[0], args[1]);
}
ExpressionTreeNode differentiate(const std::vector<ExpressionTreeNode>& children, const std::vector<ExpressionTreeNode>& childDerivs, const std::string& variable) const;
bool isInfixOperator() const {
return true;
}
};
class Operation::Negate : public Operation {
......
......@@ -239,6 +239,10 @@ ExpressionProgram ParsedExpression::createProgram() const {
}
ostream& Lepton::operator<<(ostream& out, const ExpressionTreeNode& node) {
if (node.getOperation().isInfixOperator() && node.getChildren().size() == 2) {
out << "(" << node.getChildren()[0] << ")" << node.getOperation().getName() << "(" << node.getChildren()[1] << ")";
}
else {
out << node.getOperation().getName();
if (node.getChildren().size() > 0) {
out << "(";
......@@ -249,6 +253,7 @@ ostream& Lepton::operator<<(ostream& out, const ExpressionTreeNode& node) {
}
out << ")";
}
}
return out;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment