Add multiplication rules.

Change-Id: I5e91414e8c6ad45e0ff2d19fc7c5dc3914fa247a
This commit is contained in:
Felix Raimundo
2016-04-11 16:38:58 +02:00
parent def625cca1
commit 3f8c05ad7e
4 changed files with 25 additions and 2 deletions

View File

@@ -3,3 +3,5 @@ Addition(Integer.a,Integer.b)->$AddIntegers(a,b);
Addition(Integer.a,Integer.b,c*)->Addition($AddIntegers(a,b),c*);
Product(Product(a*),b*)->Product(a*,b*);
Product(Integer[0],a*)->Integer[0];
Product(Integer.a,Integer.b)->$MultiplyIntegers(a,b);
Product(Integer.a,Integer.b,c*)->Product($MultiplyIntegers(a,b),c*);

View File

@@ -8,8 +8,22 @@ Expression * SimplificationGenerator::AddIntegers(Expression ** parameters, int
Integer * result = new Integer((native_int_t)0);
for (int i=0; i<numberOfParameters; i++) {
assert(parameters[i]->type() == Expression::Type::Integer);
// FIXME: get rid of this operator overloading.
*result = *result + *(Integer *)parameters[i];
*result = result->add(*(Integer *)parameters[i], false);
/* Note We have to delete the parameters as they have been cloned before and
* we are the last ones to use them here. */
delete parameters[i];
}
return result;
}
Expression * SimplificationGenerator::MultiplyIntegers(Expression ** parameters, int numberOfParameters) {
Integer * result = new Integer((native_int_t)1);
for (int i=0; i<numberOfParameters; i++) {
assert(parameters[i]->type() == Expression::Type::Integer);
// FIXME: get rid of this operator overloading, there are to many stars.
*result = *result * *(Integer *)parameters[i];
/* Note We have to delete the parameters as they have been cloned before and
* we are the last ones to use them here. */
delete parameters[i];
}
return result;

View File

@@ -6,6 +6,7 @@
class SimplificationGenerator {
public:
static Expression * AddIntegers(Expression ** parameters, int numberOfParameters);
static Expression * MultiplyIntegers(Expression ** parameters, int numberOfParameters);
};
#endif

View File

@@ -5,6 +5,12 @@
QUIZ_CASE(poincare_simplify_product_by_zero) {
assert(simplifies_to("3*0", "0"));
assert(simplifies_to("foo*0", "0"));
assert(simplifies_to("0*3", "0"));
assert(simplifies_to("0*foo", "0"));
assert(simplifies_to("3*5", "15"));
assert(simplifies_to("8*6", "48"));
assert(simplifies_to("3*(5+4)", "27"));
}