[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [analitza/Applications/15.04] analitza: Simplify monomial scalars before reducing them
From:       Aleix Pol <aleixpol () kde ! org>
Date:       2015-03-18 15:42:38
Message-ID: E1YYG7C-0006bg-C2 () scm ! kde ! org
[Download RAW message or body]

Git commit 30b3bbb6f30c732194626ee88ca45f9b3b3b0c5a by Aleix Pol.
Committed on 18/03/2015 at 15:36.
Pushed by apol into branch 'Applications/15.04'.

Simplify monomial scalars before reducing them

BUG: 328194

M  +7    -0    analitza/expression.cpp
M  +6    -0    analitza/expression.h
M  +11   -1    analitza/polynomial.cpp
M  +2    -0    analitza/tests/analitzatest.cpp

http://commits.kde.org/analitza/30b3bbb6f30c732194626ee88ca45f9b3b3b0c5a

diff --git a/analitza/expression.cpp b/analitza/expression.cpp
index efc87bd..988a4c6 100644
--- a/analitza/expression.cpp
+++ b/analitza/expression.cpp
@@ -830,6 +830,13 @@ Object* Expression::tree()
 	return d->m_tree;
 }
 
+Object* Expression::takeTree()
+{
+	Object* ret = d->m_tree;
+	setTree(Q_NULLPTR);
+	return ret;
+}
+
 void Expression::setTree(Object* o)
 {
 	d->m_tree=o;
diff --git a/analitza/expression.h b/analitza/expression.h
index c772396..93f17eb 100644
--- a/analitza/expression.h
+++ b/analitza/expression.h
@@ -157,6 +157,12 @@ class ANALITZA_EXPORT Expression
 		 *	Returns the tree associated to this object.
 		 */
 		Object* tree();
+
+		/**
+		 *	Returns the tree associated to this object and clears the object,
+		 *	so the ownership of the tree is acquired by the caller.
+		 */
+		Object* takeTree();
 		
 		void setTree(Object* o);
 		/**
diff --git a/analitza/polynomial.cpp b/analitza/polynomial.cpp
index 83aa2c8..33320b2 100644
--- a/analitza/polynomial.cpp
+++ b/analitza/polynomial.cpp
@@ -22,6 +22,7 @@
 #include "value.h"
 #include "analitzautils.h"
 #include "operations.h"
+#include "analyzer.h"
 
 using namespace Analitza;
 
@@ -53,6 +54,15 @@ static Object* negateObject(Object* o)
 	}
 }
 
+static Object* simpExpression(Object* o)
+{
+	Analyzer a;
+	a.setExpression(Expression(o));
+	a.simplify();
+	Expression ret = a.expression();
+	return ret.takeTree();
+}
+
 bool Monomial::isScalar(const Object* o)
 {
 	return o->type()==Object::value || (o->type()==Object::vector && !AnalitzaUtils::hasVars(o));
@@ -267,7 +277,7 @@ void Polynomial::simpScalars(bool firstValue)
 	for(QList<Object*>::const_iterator i=m_scalars.constBegin(); i!=m_scalars.constEnd(); ++i) {
 		bool d=false;
 		
-		Object* aux = *i;
+		Object* aux = simpExpression(*i);
 		if(value) {
 			QString* err=0;
 			value=Operations::reduce(m_operator.operatorType(), value, aux, &err);
diff --git a/analitza/tests/analitzatest.cpp b/analitza/tests/analitzatest.cpp
index 85bc468..b8697cb 100644
--- a/analitza/tests/analitzatest.cpp
+++ b/analitza/tests/analitzatest.cpp
@@ -294,6 +294,8 @@ void AnalitzaTest::testDerivativeSimple_data()
 	QTest::newRow("log") << "log(x)" << "1/(2.30258509299*x)";
 	QTest::newRow("vector") << "vector { x, x^2 }" << "vector { 1, 2*x }";
 	QTest::newRow("exp") << "exp(x**2)" << "2*x*exp(x^2)";
+	QTest::newRow("halfx") << "(1/2)*x" << "0.5";
+	QTest::newRow("halfx2") << "1/2 x" << "-2/(2*x)^2"; //TODO: could improve the simplification
 }
 
 void AnalitzaTest::testDerivativeSimple()
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic