Commit 02fe404a authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧
Browse files

Provide a realpower command

Allows getting results such as root(-8, 1/3)=-2, with root(-8,1/3) we
get the complex root which is less fun to work with.

BUG: 402637
parent bc10467c
......@@ -40,6 +40,7 @@ set(analitza_SRCS
substituteexpression.cpp
transformation.cpp
commands/realpower.cpp
commands/listcommands.cpp
commands/vectorcommands.cpp
commands/matrixcommands.cpp
......
......@@ -37,6 +37,7 @@
#include "substituteexpression.h"
#include "expressionstream.h"
#include "matrix.h"
#include "commands/realpower.h"
#include "commands/listcommands.h"
#include "commands/vectorcommands.h"
#include "commands/matrixcommands.h"
......@@ -163,6 +164,7 @@ Analyzer::~Analyzer()
void Analyzer::registerBuiltinMethods()
{
m_builtin.insertFunction(RealPower::id, RealPower::type, new RealPower);
m_builtin.insertFunction(RangeCommand::id, RangeCommand::type, new RangeCommand);
m_builtin.insertFunction(VectorCommand::id, VectorCommand::type, new VectorCommand);
m_builtin.insertFunction(MatrixCommand::id, MatrixCommand::type, new MatrixCommand);
......
/*************************************************************************************
* Copyright (C) 2020 Aleix Pol Gonzalez <aleixpol@kde.org> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
*************************************************************************************/
#include "realpower.h"
#include <QCoreApplication>
#include "expression.h"
#include "list.h"
#include "value.h"
using namespace Analitza;
const QString RealPower::id = QStringLiteral("realpower");
const ExpressionType RealPower::type = ExpressionType(ExpressionType::Lambda)
.addParameter(ExpressionType::Value)
.addParameter(ExpressionType::Value)
.addParameter(ExpressionType::Value);
Expression RealPower::operator()(const QList<Expression>& args)
{
const auto x = args.constFirst().toReal().complexValue();
const auto r = args.constLast().toReal().complexValue();
double sign = x.real() >= 0 ? 1 : -1;
Cn reta;
reta.setValue(sign * std::pow(sign * x, r));
qDebug() << "retaaaaa" << reta.toString();
return Expression(reta);
}
/*************************************************************************************
* Copyright (C) 2020 Aleix Pol Gonzalez <aleixpol@kde.org> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
*************************************************************************************/
#ifndef REALPOWER_H
#define REALPOWER_H
#include "builtinmethods.h"
class RealPower: public Analitza::FunctionDefinition
{
public:
virtual Analitza::Expression operator()(const QList< Analitza::Expression >& args) override;
static const QString id;
static const Analitza::ExpressionType type;
};
#endif // REALPOWER_H
......@@ -59,9 +59,11 @@ Object* reduceRealReal(enum Operator::OperatorType op, Cn *oper, double a, doubl
oper->setValue(a - b);
break;
case Operator::power:
oper->setValue( b==2 ? a*a
: b<1 && b>-1 && a<0 ? pow(complex<double>(a), complex<double>(b)).real()
: pow(a, b));
if (b<1 && b>-1 && a<0) {
oper->setValue(pow(complex<double>(a), complex<double>(b)));
} else {
oper->setValue(b==2 ? a*a : pow(a, b));
}
break;
case Operator::rem:
if(Q_LIKELY(floor(b)!=0.))
......
......@@ -100,8 +100,9 @@ void AnalitzaTest::testTrivialCalculate_data()
QTest::newRow("lambda") << "(x->x+2)(2)" << Cn(4.);
QTest::newRow("lambda2") << "(x->3*x^2)(1)" << Cn(3.);
QTest::newRow("lambda3") << "(x->x*sum(t:t=0..3))(2)" << Cn(12.);
QTest::newRow("imaginarypow") << "(-4)^(1/4)" << Cn(1.);
QTest::newRow("imaginarypow") << "(-4)^(1/4)" << Cn(1, 1);
QTest::newRow("imaginaryroot") << "root(-4, 4)" << Cn(1.);
QTest::newRow("squareroot-1") << "(-1)^(1/2)" << Cn(0, 1);
//comprehension
QTest::newRow("sum.2bvars") << "sum(x*y : (x, y)=1..3)" << Cn(36.);
......
......@@ -229,6 +229,8 @@ void BuiltInTest::testCall_data()
QTest::newRow("or") << (IN QStringLiteral("or(true, justcrash(33))")) << "true";
QTest::newRow("and1") << (IN QStringLiteral("and(false, true, justcrash(33))")) << "false";
QTest::newRow("or1") << (IN QStringLiteral("or(true, false, justcrash(33))")) << "true";
QTest::newRow("img-powers") << QStringList{"realpower(-8, 1/3)"} << "-2";
}
void BuiltInTest::testCall()
......
......@@ -119,7 +119,8 @@ class ANALITZA_EXPORT Cn : public Object
/**
* @returns whether @p d is equal than this object.
*/
bool operator==(const Cn& d) const { return m_value==d.m_value && d.m_imaginaryPart==m_imaginaryPart; }
bool operator==(const Cn& d) const { return qFuzzyCompare(m_value, d.m_value)
&& qFuzzyCompare(m_imaginaryPart, d.m_imaginaryPart); }
/**
* @returns whether @p d is less than this object.
......@@ -167,7 +168,7 @@ class ANALITZA_EXPORT Cn : public Object
static Cn euler();
private:
union { double m_value; ushort m_char; };
double m_imaginaryPart;
double m_imaginaryPart = 0.;
enum ValueFormat m_format;
};
......
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