[poincare] Make Product inherits from binary operation (matrix product

are not commutative)

Change-Id: I86f4c49ea973b45605ffa0a6b28f172de2866b98
This commit is contained in:
Émilie Feral
2016-11-25 11:18:10 +01:00
parent f4b73de940
commit 487256bf04
4 changed files with 27 additions and 19 deletions

View File

@@ -1,17 +1,16 @@
#ifndef POINCARE_PRODUCT_H
#define POINCARE_PRODUCT_H
#include <poincare/commutative_operation.h>
#include <poincare/binary_operation.h>
class Product : public CommutativeOperation {
using CommutativeOperation::CommutativeOperation;
class Product : public BinaryOperation {
using BinaryOperation::BinaryOperation;
public:
Type type() const override;
float operateApproximatevelyOn(float a, float b) const override;
ExpressionLayout * createLayout() const override;
float approximate(Context& context) const override;
Expression * cloneWithDifferentOperands(Expression** newOperands,
int numnerOfOperands, bool cloneOperands = true) const override;
protected:
char operatorChar() const override;
};
#endif

View File

@@ -105,7 +105,7 @@ exp:
| SYMBOL { $$ = new Symbol($1); }
| exp PLUS exp { Expression * terms[2] = {$1,$3}; $$ = new Addition(terms, 2, false); }
| exp MINUS exp { Expression * terms[2] = {$1,$3}; $$ = new Subtraction(terms, false); }
| exp MULTIPLY exp { Expression * terms[2] = {$1,$3}; $$ = new Product(terms, 2, false); }
| exp MULTIPLY exp { Expression * terms[2] = {$1,$3}; $$ = new Product(terms, false); }
| exp DIVIDE exp { Expression * terms[2] = {$1,$3}; $$ = new Fraction(terms, false); }
| exp POW exp { Expression * terms[2] = {$1,$3}; $$ = new Power(terms, false); }
| LEFT_PARENTHESIS exp RIGHT_PARENTHESIS { $$ = new Parenthesis($2, false); }

View File

@@ -1,22 +1,31 @@
extern "C" {
#include <assert.h>
#include <stdlib.h>
}
#include <poincare/product.h>
Expression * Product::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) const {
return new Product(newOperands, numberOfOperands, cloneOperands);
}
float Product::operateApproximatevelyOn(float a, float b) const {
return a*b;
}
#include "layout/string_layout.h"
#include "layout/horizontal_layout.h"
Expression::Type Product::type() const {
return Expression::Type::Product;
}
char Product::operatorChar() const {
return '*';
ExpressionLayout * Product::createLayout() const {
ExpressionLayout** children_layouts = (ExpressionLayout **)malloc(3*sizeof(ExpressionLayout *));
children_layouts[0] = m_operands[0]->createLayout();
children_layouts[1] = new StringLayout("*", 1);
children_layouts[2] = m_operands[1]->createLayout();
return new HorizontalLayout(children_layouts, 3);
}
float Product::approximate(Context& context) const {
return m_operands[0]->approximate(context)*m_operands[1]->approximate(context);;
}
Expression * Product::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) const {
assert(numberOfOperands == 2);
assert(newOperands != nullptr);
return new Product(newOperands, cloneOperands);
}

View File

@@ -39,7 +39,7 @@ Expression * ExpressionBuilder::build(ExpressionMatch matches[]) {
case Expression::Type::Product:
/* The children do not need to be cloned as they already have been
* before. */
result = new Product(children_expressions, numberOfChildrenExpressions, false);
result = new Product(children_expressions, false);
break;
default:
assert(false);