[poincare] Replace complex constructors by named constructors

Change-Id: I6aad82edfb1bd243c4537a48888655608b90eeb5
This commit is contained in:
Émilie Feral
2017-02-14 17:05:18 +01:00
parent 7cbfd37925
commit dc7a629dfa
32 changed files with 96 additions and 82 deletions

View File

@@ -17,7 +17,7 @@ void Function::setDisplayDerivative(bool display) {
}
float Function::approximateDerivative(float x, Poincare::Context * context) const {
Poincare::Complex abscissa = Poincare::Complex(x);
Poincare::Complex abscissa = Poincare::Complex::Float(x);
Poincare::Expression * args[2] = {m_expression, &abscissa};
Poincare::Derivative derivative = Poincare::Derivative();
derivative.setArgument(args, 2, true);

View File

@@ -72,7 +72,7 @@ void Function::setActive(bool active) {
float Function::evaluateAtAbscissa(float x, Poincare::Context * context) const {
Poincare::Symbol xSymbol = Poincare::Symbol(symbol());
Poincare::Complex e = Poincare::Complex(x);
Poincare::Complex e = Poincare::Complex::Float(x);
context->setExpressionForSymbolName(&e, &xSymbol);
return m_expression->approximate(*context);
}

View File

@@ -2,12 +2,15 @@
#define POINCARE_COMPLEX_H
#include <poincare/leaf_expression.h>
#include <math.h>
namespace Poincare {
class Complex : public LeafExpression {
public:
Complex(float a, float b = 0.0f, bool polar = false);
static Complex Float(float x);
static Complex Cartesian(float a, float b);
static Complex Polar(float r, float theta);
Complex(const char * integralPart, int integralPartLength, bool integralNegative,
const char * fractionalPart, int fractionalPartLength,
const char * exponent, int exponentLength, bool exponentNegative);
@@ -36,6 +39,7 @@ public:
return numberOfSignificantDigits + 7;
}
private:
Complex(float a, float b);
constexpr static int k_numberOfSignificantDigits = 7;
ExpressionLayout * privateCreateLayout(FloatDisplayMode floatDisplayMode) const override;
Expression * privateEvaluate(Context& context, AngleUnit angleUnit) const override;

View File

@@ -47,10 +47,10 @@ Expression * AbsoluteValue::privateEvaluate(Context& context, AngleUnit angleUni
assert(evaluation->type() == Type::Matrix || evaluation->type() == Type::Complex);
if (evaluation->type() == Type::Matrix) {
delete evaluation;
return new Complex(NAN);
return new Complex(Complex::Float(NAN));
}
float absVal = ((Complex *)evaluation)->absoluteValue();
Complex * result = new Complex(absVal);
Complex * result = new Complex(Complex::Float(absVal));
delete evaluation;
return result;
}

View File

@@ -37,7 +37,7 @@ bool Addition::isCommutative() const {
}
Expression * Addition::evaluateOnComplex(Complex * c, Complex * d, Context& context, AngleUnit angleUnit) const {
return new Complex(c->a()+ d->a(), c->b() + d->b());
return new Complex(Complex::Cartesian(c->a()+ d->a(), c->b() + d->b()));
}
}

View File

@@ -69,7 +69,7 @@ Expression * BinaryOperation::evaluateOnMatrixAndComplex(Matrix * m, Complex * c
Expression * evaluation = m->operand(i)->evaluate(context, angleUnit);
assert(evaluation->type() == Type::Matrix || evaluation->type() == Type::Complex);
if (evaluation->type() == Type::Matrix) {
operands[i] = new Complex(NAN);
operands[i] = new Complex(Complex::Float(NAN));
delete evaluation;
continue;
}
@@ -94,7 +94,7 @@ Expression * BinaryOperation::evaluateOnMatrices(Matrix * m, Matrix * n, Context
assert(mEvaluation->type() == Type::Matrix || mEvaluation->type() == Type::Complex);
assert(nEvaluation->type() == Type::Matrix || nEvaluation->type() == Type::Complex);
if (mEvaluation->type() == Type::Matrix ||nEvaluation->type() == Type::Matrix) {
operands[i] = new Complex(NAN);
operands[i] = new Complex(Complex::Float(NAN));
delete mEvaluation;
delete nEvaluation;
continue;

View File

@@ -12,14 +12,16 @@ extern "C" {
namespace Poincare {
Complex::Complex(float a, float b, bool polar) :
m_a(a),
m_b(b)
{
if (polar) {
m_a = a * cosf(b);
m_b = a * sinf(b);
}
Complex Complex::Float(float x) {
return Complex(x,0.0f);
}
Complex Complex::Cartesian(float a, float b) {
return Complex(a,b);
}
Complex Complex::Polar(float r, float th) {
return Complex(r*cosf(th),r*sinf(th));
}
static inline float setSign(float f, bool negative) {
@@ -55,7 +57,7 @@ Complex::Complex(const char * integralPart, int integralPartLength, bool integra
}
Expression * Complex::clone() const {
return new Complex(m_a, m_b);
return new Complex(Cartesian(m_a, m_b));
}
float Complex::privateApproximate(Context& context, AngleUnit angleUnit) const {
@@ -136,6 +138,12 @@ int Complex::convertFloatToText(float f, char * buffer, int bufferSize,
return requiredLength;
}
Complex::Complex(float a, float b) :
m_a(a),
m_b(b)
{
}
int Complex::convertComplexToText(char * buffer, int bufferSize, FloatDisplayMode displayMode) const {
assert(displayMode != FloatDisplayMode::Default);
int numberOfChars = 0;

View File

@@ -40,9 +40,9 @@ Expression * Cosine::privateEvaluate(Context& context, AngleUnit angleUnit) cons
assert(evaluation->type() == Type::Matrix || evaluation->type() == Type::Complex);
if (evaluation->type() == Type::Matrix) {
delete evaluation;
return new Complex(NAN);
return new Complex(Complex::Float(NAN));
}
Expression * arg = new Complex(-((Complex *)evaluation)->b(), ((Complex *)evaluation)->a());
Expression * arg = new Complex(Complex::Cartesian(-((Complex *)evaluation)->b(), ((Complex *)evaluation)->a()));
Function * cosh = new HyperbolicCosine();
cosh->setArgument(&arg, 1, true);
delete evaluation;

View File

@@ -33,7 +33,7 @@ float Derivative::privateApproximate(Context& context, AngleUnit angleUnit) cons
VariableContext xContext = VariableContext('x', &context);
Symbol xSymbol = Symbol('x');
float x = m_args[1]->approximate(context, angleUnit);
Complex e = Complex(x);
Complex e = Complex::Float(x);
xContext.setExpressionForSymbolName(&e, &xSymbol);
float functionValue = m_args[0]->approximate(xContext, angleUnit);
@@ -108,10 +108,10 @@ float Derivative::privateApproximate(Context& context, AngleUnit angleUnit) cons
float Derivative::growthRateAroundAbscissa(float x, float h, VariableContext xContext, AngleUnit angleUnit) const {
Symbol xSymbol = Symbol('x');
Complex e = Complex(x + h, 0.0f);
Complex e = Complex::Float(x + h);
xContext.setExpressionForSymbolName(&e, &xSymbol);
float expressionPlus = m_args[0]->approximate(xContext, angleUnit);
e = Complex(x-h);
e = Complex::Float(x-h);
xContext.setExpressionForSymbolName(&e, &xSymbol);
float expressionMinus = m_args[0]->approximate(xContext, angleUnit);
return (expressionPlus - expressionMinus)/(2*h);
@@ -119,13 +119,13 @@ float Derivative::growthRateAroundAbscissa(float x, float h, VariableContext xCo
float Derivative::approximateDerivate2(float x, float h, VariableContext xContext, AngleUnit angleUnit) const {
Symbol xSymbol = Symbol('x');
Complex e = Complex(x + h);
Complex e = Complex::Float(x + h);
xContext.setExpressionForSymbolName(&e, &xSymbol);
float expressionPlus = m_args[0]->approximate(xContext, angleUnit);
e = Complex(x);
e = Complex::Float(x);
xContext.setExpressionForSymbolName(&e, &xSymbol);
float expression = m_args[0]->approximate(xContext, angleUnit);
e = Complex(x-h);
e = Complex::Float(x-h);
xContext.setExpressionForSymbolName(&e, &xSymbol);
float expressionMinus = m_args[0]->approximate(xContext, angleUnit);
return expressionPlus - 2.0f*expression + expressionMinus;

View File

@@ -121,7 +121,7 @@ number:
exp:
number { $$ = $1; }
| ICOMPLEX { $$ = new Poincare::Complex(0.0f, 1.0f); }
| ICOMPLEX { $$ = new Poincare::Complex(Poincare::Complex::Cartesian(0.0f, 1.0f)); }
| SYMBOL { $$ = new Poincare::Symbol($1); }
| exp PLUS exp { Poincare::Expression * terms[2] = {$1,$3}; $$ = new Poincare::Addition(terms, false); }
| exp MINUS exp { Poincare::Expression * terms[2] = {$1,$3}; $$ = new Poincare::Subtraction(terms, false); }

View File

@@ -31,7 +31,7 @@ Expression::Type Fraction::type() const {
Expression * Fraction::evaluateOnComplex(Complex * c, Complex * d, Context& context, AngleUnit angleUnit) const {
float norm = d->a()*d->a() + d->b()*d->b();
return new Complex((c->a()*d->a()+c->b()*d->b())/norm, (d->a()*c->b()-c->a()*d->b())/norm);
return new Complex(Complex::Cartesian((c->a()*d->a()+c->b()*d->b())/norm, (d->a()*c->b()-c->a()*d->b())/norm));
}
Expression * Fraction::evaluateOnComplexAndMatrix(Complex * c, Matrix * m, Context& context, AngleUnit angleUnit) const {
@@ -44,7 +44,7 @@ Expression * Fraction::evaluateOnMatrices(Matrix * m, Matrix * n, Context& conte
}
/* TODO: implement matrix fraction
if (n->det() == 0) {
return new Complex(NAN);
return new Complex(Complex::Float(NAN));
}
result = new Product(m, n->inv(), false);
return result;*/

View File

@@ -95,7 +95,7 @@ int Function::numberOfOperands() const {
Expression * Function::privateEvaluate(Context& context, AngleUnit angleUnit) const {
assert(angleUnit != AngleUnit::Default);
/* Default function evaluation works for reel function */
return new Complex(approximate(context, angleUnit));
return new Complex(Complex::Float(approximate(context, angleUnit)));
}
}

View File

@@ -6,8 +6,8 @@
namespace Poincare {
GlobalContext::GlobalContext() :
m_pi(Complex(M_PI)),
m_e(Complex(M_E))
m_pi(Complex::Float(M_PI)),
m_e(Complex::Float(M_E))
{
for (int i = 0; i < k_maxNumberOfScalarExpressions; i++) {
m_expressions[i] = nullptr;
@@ -15,7 +15,7 @@ GlobalContext::GlobalContext() :
}
Complex * GlobalContext::defaultExpression() {
static Complex * defaultExpression = new Complex(0.0f);
static Complex * defaultExpression = new Complex(Complex::Float(0.0f));
return defaultExpression;
}

View File

@@ -40,10 +40,10 @@ Expression * HyperbolicCosine::privateEvaluate(Context& context, AngleUnit angle
assert(evaluation->type() == Type::Matrix || evaluation->type() == Type::Complex);
if (evaluation->type() == Type::Matrix) {
delete evaluation;
return new Complex(NAN);
return new Complex(Complex::Float(NAN));
}
Expression * arguments[2];
arguments[0] = new Complex(M_E);
arguments[0] = new Complex(Complex::Float(M_E));
arguments[1] = evaluation;
Expression * exp1 = new Power(arguments, true);
arguments[1] = new Opposite(evaluation, true);
@@ -57,7 +57,7 @@ Expression * HyperbolicCosine::privateEvaluate(Context& context, AngleUnit angle
delete exp1;
delete exp2;
arguments[0] = sum;
arguments[1] = new Complex(2.0f);
arguments[1] = new Complex(Complex::Float(2.0f));
Expression * result = new Fraction(arguments, true);
delete arguments[1];
delete arguments[0];

View File

@@ -40,10 +40,10 @@ Expression * HyperbolicSine::privateEvaluate(Context& context, AngleUnit angleUn
assert(evaluation->type() == Type::Matrix || evaluation->type() == Type::Complex);
if (evaluation->type() == Type::Matrix) {
delete evaluation;
return new Complex(NAN);
return new Complex(Complex::Float(NAN));
}
Expression * arguments[2];
arguments[0] = new Complex(M_E);
arguments[0] = new Complex(Complex::Float(M_E));
arguments[1] = evaluation;
Expression * exp1 = new Power(arguments, true);
arguments[1] = new Opposite(evaluation, true);
@@ -57,7 +57,7 @@ Expression * HyperbolicSine::privateEvaluate(Context& context, AngleUnit angleUn
delete exp1;
delete exp2;
arguments[0] = sub;
arguments[1] = new Complex(2.0f);
arguments[1] = new Complex(Complex::Float(2.0f));
Expression * result = new Fraction(arguments, true);
delete arguments[1];
delete arguments[0];

View File

@@ -40,7 +40,7 @@ Expression * HyperbolicTangent::privateEvaluate(Context& context, AngleUnit angl
assert(evaluation->type() == Type::Matrix || evaluation->type() == Type::Complex);
if (evaluation->type() == Type::Matrix) {
delete evaluation;
return new Complex(NAN);
return new Complex(Complex::Float(NAN));
}
Expression * arguments[2];
arguments[0] = new HyperbolicSine();

View File

@@ -313,7 +313,7 @@ float Integer::privateApproximate(Context& context, AngleUnit angleUnit) const {
Expression * Integer::privateEvaluate(Context& context, AngleUnit angleUnit) const {
assert(angleUnit != AngleUnit::Default);
return new Complex(approximate(context, angleUnit));
return new Complex(Complex::Float(approximate(context, angleUnit)));
}
Expression::Type Integer::type() const {

View File

@@ -56,7 +56,7 @@ ExpressionLayout * Integral::privateCreateLayout(FloatDisplayMode floatDisplayMo
}
float Integral::functionValueAtAbscissa(float x, VariableContext xContext, AngleUnit angleUnit) const {
Complex e = Complex(x);
Complex e = Complex::Float(x);
Symbol xSymbol = Symbol('x');
xContext.setExpressionForSymbolName(&e, &xSymbol);
return m_args[0]->approximate(xContext, angleUnit);

View File

@@ -25,7 +25,7 @@ Matrix::Matrix(Expression ** newOperands, int numberOfOperands, int numberOfColu
}
Complex * Matrix::defaultExpression() {
static Complex * defaultExpression = new Complex(0.0f);
static Complex * defaultExpression = new Complex(Complex::Float(0.0f));
return defaultExpression;
}
@@ -65,7 +65,7 @@ Expression * Matrix::privateEvaluate(Context& context, AngleUnit angleUnit) cons
assert(operands[i]->type() == Type::Matrix || operands[i]->type() == Type::Complex);
if (operands[i]->type() == Type::Matrix) {
delete operands[i];
operands[i] = new Complex(NAN);
operands[i] = new Complex(Complex::Float(NAN));
continue;
}
}

View File

@@ -35,7 +35,7 @@ MatrixData::MatrixData(Expression ** newOperands, int numberOfOperands, int numb
}
Complex * MatrixData::defaultExpression() {
static Complex * defaultExpression = new Complex(0.0f);
static Complex * defaultExpression = new Complex(Complex::Float(0.0f));
return defaultExpression;
}

View File

@@ -37,7 +37,7 @@ Expression * Multiplication::cloneWithDifferentOperands(Expression** newOperands
}
Expression * Multiplication::evaluateOnComplex(Complex * c, Complex * d, Context& context, AngleUnit angleUnit) const {
return new Complex(c->a()*d->a()-c->b()*d->b(), c->b()*d->a() + c->a()*d->b());
return new Complex(Complex::Cartesian(c->a()*d->a()-c->b()*d->b(), c->b()*d->a() + c->a()*d->b()));
}
Expression * Multiplication::evaluateOnMatrices(Matrix * m, Matrix * n, Context& context, AngleUnit angleUnit) const {
@@ -55,7 +55,7 @@ Expression * Multiplication::evaluateOnMatrices(Matrix * m, Matrix * n, Context&
assert(mEvaluation->type() == Type::Matrix || mEvaluation->type() == Type::Complex);
assert(nEvaluation->type() == Type::Matrix || nEvaluation->type() == Type::Complex);
if (mEvaluation->type() == Type::Matrix ||nEvaluation->type() == Type::Matrix) {
operands[i] = new Complex(NAN);
operands[i] = new Complex(Complex::Float(NAN));
delete mEvaluation;
delete nEvaluation;
continue;
@@ -65,7 +65,7 @@ Expression * Multiplication::evaluateOnMatrices(Matrix * m, Matrix * n, Context&
delete mEvaluation;
delete nEvaluation;
}
operands[i*n->numberOfColumns()+j] = new Complex(a, b);
operands[i*n->numberOfColumns()+j] = new Complex(Complex::Cartesian(a, b));
}
}
return new Matrix(operands, m->numberOfRows() * n->numberOfColumns(), m->numberOfRows(), n->numberOfColumns(), false);

View File

@@ -48,12 +48,12 @@ Expression * NthRoot::privateEvaluate(Context& context, AngleUnit angleUnit) con
if (baseEvaluation->type() == Type::Matrix || indexEvaluation->type() == Type::Matrix) {
delete baseEvaluation;
delete indexEvaluation;
return new Complex(NAN);
return new Complex(Complex::Float(NAN));
}
Expression * operands[2];
operands[0] = baseEvaluation;
Expression * operandChildren[2];
operandChildren[0] = new Complex(1.0f);
operandChildren[0] = new Complex(Complex::Float(1.0f));
operandChildren[1] = indexEvaluation;
Expression * fraction = new Fraction(operandChildren, true);
operands[1] = fraction->evaluate(context, angleUnit);

View File

@@ -44,7 +44,7 @@ Expression * Opposite::privateEvaluate(Context& context, AngleUnit angleUnit) co
}
Expression * result = nullptr;
if (operandEvalutation->type() == Type::Complex) {
result = new Complex(-((Complex *)operandEvalutation)->a(), -((Complex *)operandEvalutation)->b());
result = new Complex(Complex::Cartesian(-((Complex *)operandEvalutation)->a(), -((Complex *)operandEvalutation)->b()));
}
if (operandEvalutation->type() == Type::Matrix) {
result = evaluateOnMatrix((Matrix *)operandEvalutation, context, angleUnit);
@@ -84,11 +84,11 @@ Expression * Opposite::evaluateOnMatrix(Matrix * m, Context& context, AngleUnit
Expression * evaluation = m->operand(i)->evaluate(context, angleUnit);
assert(evaluation->type() == Type::Matrix || evaluation->type() == Type::Complex);
if (evaluation->type() == Type::Matrix) {
operands[i] = new Complex(NAN);
operands[i] = new Complex(Complex::Float(NAN));
delete evaluation;
continue;
}
operands[i] = new Complex(-((Complex *)evaluation)->a(), -((Complex *)evaluation)->b());
operands[i] = new Complex(Complex::Cartesian(-((Complex *)evaluation)->a(), -((Complex *)evaluation)->b()));
delete evaluation;
}
return new Matrix(operands, m->numberOfRows() * m->numberOfColumns(), m->numberOfColumns(), m->numberOfRows(), false);

View File

@@ -37,17 +37,17 @@ Expression * Power::evaluateOnComplex(Complex * c, Complex * d, Context& context
if (d->b() != 0.0f) {
/* First case c and d is complex */
if (c->b() != 0.0f || c->a() <= 0) {
return new Complex(NAN);
return new Complex(Complex::Float(NAN));
}
/* Second case only d is complex */
float radius = powf(c->a(), d->a());
float theta = d->b()*logf(c->a());
return new Complex(radius, theta, true);
return new Complex(Complex::Polar(radius, theta));
}
/* Third case only c is complex */
float radius = powf(c->r(), d->a());
float theta = d->a()*c->th();
return new Complex(radius, theta, true);
return new Complex(Complex::Polar(radius, theta));
}
Expression * Power::evaluateOnMatrixAndComplex(Matrix * m, Complex * c, Context& context, AngleUnit angleUnit) const {
@@ -59,7 +59,7 @@ Expression * Power::evaluateOnMatrixAndComplex(Matrix * m, Complex * c, Context&
}
// TODO: return identity matrix if i == 0
int power = c->approximate(context, angleUnit);
Expression * result = new Complex(1);
Expression * result = new Complex(Complex::Float(1.0f));
for (int k = 0; k < power; k++) {
Expression * operands[2];
operands[0] = result;

View File

@@ -40,7 +40,7 @@ float Product::privateApproximate(Context& context, AngleUnit angleUnit) const {
int end = m_args[2]->approximate(context, angleUnit);
float result = 1.0f;
for (int i = start; i <= end; i++) {
Complex iExpression = Complex(i);
Complex iExpression = Complex::Float(i);
nContext.setExpressionForSymbolName(&iExpression, &nSymbol);
result = result*m_args[0]->approximate(nContext, angleUnit);
}
@@ -60,14 +60,15 @@ Expression * Product::privateEvaluate(Context& context, AngleUnit angleUnit) con
float start = m_args[1]->approximate(context, angleUnit);
float end = m_args[2]->approximate(context, angleUnit);
if (isnan(start) || isnan(end)) {
return new Complex(NAN);
return new Complex(Complex::Float(NAN));
}
VariableContext nContext = VariableContext('n', &context);
Symbol nSymbol = Symbol('n');
Expression * result = new Complex(1);
Expression * result = new Complex(Complex::Float(1.0f));
for (int i = (int)start; i <= (int)end; i++) {
Complex iExpression = Complex(i);
nContext.setExpressionForSymbolName(&iExpression, &nSymbol);
Complex * iExpression = new Complex(Complex::Float(i));
nContext.setExpressionForSymbolName(iExpression, &nSymbol);
delete iExpression;
Expression * operands[2];
operands[0] = result;
operands[1] = m_args[0]->evaluate(nContext, angleUnit);

View File

@@ -41,15 +41,15 @@ Expression * Sine::privateEvaluate(Context& context, AngleUnit angleUnit) const
assert(evaluation->type() == Type::Matrix || evaluation->type() == Type::Complex);
if (evaluation->type() == Type::Matrix) {
delete evaluation;
return new Complex(NAN);
return new Complex(Complex::Float(NAN));
}
Expression * arg = new Complex(-((Complex *)evaluation)->b(), ((Complex *)evaluation)->a());
Expression * arg = new Complex(Complex::Cartesian(-((Complex *)evaluation)->b(), ((Complex *)evaluation)->a()));
Function * sinh = new HyperbolicSine();
sinh->setArgument(&arg, 1, true);
delete evaluation;
delete arg;
Expression * args[2];
args[0] = new Complex(0.0f, -1.0f);
args[0] = new Complex(Complex::Cartesian(0.0f, -1.0f));
args[1] = sinh;
Multiplication * result = new Multiplication(args, true);
delete args[0];

View File

@@ -43,11 +43,11 @@ Expression * SquareRoot::privateEvaluate(Context& context, AngleUnit angleUnit)
assert(evaluation->type() == Type::Matrix || evaluation->type() == Type::Complex);
if (evaluation->type() == Type::Matrix) {
delete evaluation;
return new Complex(NAN);
return new Complex(Complex::Float(NAN));
}
Expression * operands[2];
operands[0] = evaluation;
operands[1] = new Complex(0.5f);
operands[1] = new Complex(Complex::Float(0.5f));
Expression * power = new Power(operands, true);
Expression * newResult = power->evaluate(context, angleUnit);
delete evaluation;

View File

@@ -38,7 +38,7 @@ ExpressionLayout * Subtraction::privateCreateLayout(FloatDisplayMode floatDispla
}
Expression * Subtraction::evaluateOnComplex(Complex * c, Complex * d, Context& context, AngleUnit angleUnit) const {
return new Complex(c->a() - d->a(), c->b() - d->b());
return new Complex(Complex::Cartesian(c->a() - d->a(), c->b() - d->b()));
}
Expression * Subtraction::evaluateOnComplexAndMatrix(Complex * c, Matrix * m, Context& context, AngleUnit angleUnit) const {

View File

@@ -40,7 +40,7 @@ float Sum::privateApproximate(Context& context, AngleUnit angleUnit) const {
int end = m_args[2]->approximate(context, angleUnit);
float result = 0.0f;
for (int i = start; i <= end; i++) {
Complex iExpression = Complex(i);
Complex iExpression = Complex::Float(i);
nContext.setExpressionForSymbolName(&iExpression, &nSymbol);
result += m_args[0]->approximate(nContext, angleUnit);
}
@@ -60,14 +60,15 @@ Expression * Sum::privateEvaluate(Context& context, AngleUnit angleUnit) const {
float start = m_args[1]->approximate(context, angleUnit);
float end = m_args[2]->approximate(context, angleUnit);
if (isnan(start) || isnan(end)) {
return new Complex(NAN);
return new Complex(Complex::Float(NAN));
}
VariableContext nContext = VariableContext('n', &context);
Symbol nSymbol = Symbol('n');
Expression * result = new Complex(0.0f);
Expression * result = new Complex(Complex::Float(0.0f));
for (int i = (int)start; i <= (int)end; i++) {
Complex iExpression = Complex(i);
nContext.setExpressionForSymbolName(&iExpression, &nSymbol);
Complex * iExpression = new Complex(Complex::Float(i));
nContext.setExpressionForSymbolName(iExpression, &nSymbol);
delete iExpression;
Expression * operands[2];
operands[0] = result;
operands[1] = m_args[0]->evaluate(nContext, angleUnit);

View File

@@ -42,7 +42,7 @@ Expression * Tangent::privateEvaluate(Context& context, AngleUnit angleUnit) con
assert(evaluation->type() == Type::Matrix || evaluation->type() == Type::Complex);
if (evaluation->type() == Type::Matrix) {
delete evaluation;
return new Complex(NAN);
return new Complex(Complex::Float(NAN));
}
Expression * arguments[2];
arguments[0] = new Sine();

View File

@@ -6,7 +6,7 @@ namespace Poincare {
VariableContext::VariableContext(char name, Context * parentContext) :
m_name(name),
m_value(Complex(0.0f)),
m_value(Complex::Float(0.0f)),
m_parentContext(parentContext)
{
}
@@ -15,7 +15,7 @@ void VariableContext::setExpressionForSymbolName(Expression * expression, const
if (symbol->name() == m_name) {
assert(expression->type() == Expression::Type::Complex);
/* WARNING: We assume that the evaluation of expression is a reel */
m_value = Complex(expression->approximate(*m_parentContext, Preferences::sharedPreferences()->angleUnit()));
m_value = Complex::Float(expression->approximate(*m_parentContext, Preferences::sharedPreferences()->angleUnit()));
} else {
m_parentContext->setExpressionForSymbolName(expression, symbol);
}

View File

@@ -58,27 +58,27 @@ QUIZ_CASE(poincare_complex_to_text) {
char result13[20] = {'1','.','2',Ion::Charset::Exponent,'2',0};
assert(strcmp(buffer3, result13) == 0);
Complex(1, 2).writeTextInBuffer(buffer, 14);
Complex::Cartesian(1.0f, 2.0f).writeTextInBuffer(buffer, 14);
char text1[14] = {'1','+', '2', '*', Ion::Charset::IComplex, 0};
assert(strcmp(buffer, text1) == 0);
Complex(-1.3, 2.444).writeTextInBuffer(buffer, 14);
Complex::Cartesian(-1.3f, 2.444f).writeTextInBuffer(buffer, 14);
char text2[14] = {'-','1','.','3','+', '2','.','4','4','4', '*', Ion::Charset::IComplex, 0};
assert(strcmp(buffer, text2) == 0);
Complex(-1.3, -2.444).writeTextInBuffer(buffer, 14);
Complex::Cartesian(-1.3f, -2.444f).writeTextInBuffer(buffer, 14);
char text3[14] = {'-','1','.','3','-', '2','.','4','4','4', '*', Ion::Charset::IComplex, 0};
assert(strcmp(buffer, text3) == 0);
}
QUIZ_CASE(poincare_complex_approximate) {
GlobalContext globalContext;
Expression * a = new Complex(123.456f);
Expression * a = new Complex(Complex::Float(123.456f));
assert(a->approximate(globalContext) == 123.456f);
delete a;
}
QUIZ_CASE(poincare_complex_evaluate) {
GlobalContext globalContext;
Expression * a = new Complex(123.456f);
Expression * a = new Complex(Complex::Float(123.456f));
Expression * e = a->evaluate(globalContext);
assert(e->approximate(globalContext) == 123.456f);
delete a;
@@ -86,12 +86,12 @@ QUIZ_CASE(poincare_complex_evaluate) {
}
QUIZ_CASE(poincare_complex_constructor) {
Complex * a = new Complex(2.0f, 3.0f, false);
Complex * a = new Complex(Complex::Cartesian(2.0f, 3.0f));
assert(a->a() == 2.0f && a->b() == 3.0f);
assert(a->r() == 3.60555124f && a->th() == 0.982793748f);
delete a;
a = new Complex(3.60555124f, 0.982793748f, true);
a = new Complex(Complex::Polar(3.60555124f, 0.982793748f));
assert(a->a() == 2.0f && a->b() == 3.0f);
delete a;
}