@Override
public Expression apply() {
Expression dd = p.discriminant();
ExpressionContext.getInstance().addExpression(new Equality(Numbers.constant("D"), dd));
dd = dd.evaluate();
ExpressionContext.getInstance().addExpression(new Equality(Numbers.constant("D"), dd));
Expression a = p.getCoefficient(2);
Expression b = p.getCoefficient(1);
Expression c = p.getCoefficient(0);
NaturalNumber two = Naturals.getInstance().create(2);
if (dd instanceof ANumber) {
ANumber d = (ANumber) dd;
if (d.isNegative()) {
return new EmptySet();
} else if (d.isZero()) {
return new FiniteSet(new Division(new Negation(b),
new Multiplication(two, a)).evaluate());
}
}
Expression sd = new Sqrt(dd);
Expression x1 = new Division(
new Addition(new Negation(b), sd),
new Multiplication(two, a));
Expression x2 = new Division(
new Subtraction(new Negation(b), sd),
new Multiplication(two, a));
ExpressionContext.getInstance().addExpression(new Equality(Numbers.variable("x", 1), x1));
ExpressionContext.getInstance().addExpression(new Equality(Numbers.variable("x", 2), x2));
x1 = x1.evaluate();
x2 = x2.evaluate();
ExpressionContext.getInstance().addExpression(new Equality(Numbers.variable("x", 1), x1));
ExpressionContext.getInstance().addExpression(new Equality(Numbers.variable("x", 2), x2));
return new FiniteSet(x1, x2);
}