Skip to content

Commit

Permalink
#362 - Differentiation of x^x returns wrong expression
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed Oct 6, 2020
1 parent 7b06ed2 commit d4e6363
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
27 changes: 19 additions & 8 deletions xFunc.Maths/Analyzers/Differentiator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,17 @@ public override IExpression Analyze(Number exp)
/// </returns>
public override IExpression Analyze(Pow exp)
{
if (!Helpers.HasVariable(exp, Variable))
return new Number(0);
var hasVariableInLeft = Helpers.HasVariable(exp.Left, Variable);
var hasVariableInRight = Helpers.HasVariable(exp.Right, Variable);

if (Helpers.HasVariable(exp.Left, Variable))
if (hasVariableInLeft && hasVariableInRight)
{
return new Mul(
exp,
Analyze(new Mul(exp.Right, new Ln(Variable))));
}

if (hasVariableInLeft)
{
var sub = new Sub(exp.Right.Clone(), new Number(1));
var inv = new Pow(exp.Left.Clone(), sub);
Expand All @@ -315,12 +322,16 @@ public override IExpression Analyze(Pow exp)
return mul2;
}

// if (Helpers.HasVar(exp.Right, variable))
var ln = new Ln(exp.Left.Clone());
var mul3 = new Mul(ln, exp.Clone());
var mul4 = new Mul(mul3, exp.Right.Clone().Analyze(this));
if (hasVariableInRight)
{
var ln = new Ln(exp.Left.Clone());
var mul3 = new Mul(ln, exp.Clone());
var mul4 = new Mul(mul3, exp.Right.Clone().Analyze(this));

return mul4;
return mul4;
}

return new Number(0);
}

/// <summary>
Expand Down
46 changes: 46 additions & 0 deletions xFunc.Tests/Analyzers/DifferentiatorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,52 @@ public void PowDerivativeTest4()
Assert.Equal(expected, deriv);
}

[Fact(DisplayName = "x ^ x")]
public void PowXbyX()
{
var exp = new Pow(Variable.X, Variable.X);
var result = Differentiate(exp);
var expected = new Mul(
new Pow(Variable.X, Variable.X),
new Add(
new Mul(new Number(1), new Ln(Variable.X)),
new Mul(Variable.X, new Div(new Number(1), Variable.X)))
);

Assert.Equal(expected, result);
}

[Fact(DisplayName = "x ^ 2x")]
public void PowXby2X()
{
var exp = new Pow(Variable.X, new Mul(new Number(2), Variable.X));
var result = Differentiate(exp);
var expected = new Mul(
new Pow(Variable.X, new Mul(new Number(2), Variable.X)),
new Add(
new Mul(new Mul(new Number(2), new Number(1)), new Ln(Variable.X)),
new Mul(new Mul(new Number(2), Variable.X), new Div(new Number(1), Variable.X)))
);

Assert.Equal(expected, result);
}

[Fact(DisplayName = "x ^ sin(x)")]
public void PowXbySinX()
{
var exp = new Pow(Variable.X, new Sin(Variable.X));
var result = Differentiate(exp);
var expected = new Mul(
new Pow(Variable.X, new Sin(Variable.X)),
new Add(
new Mul(new Mul(new Cos(Variable.X), new Number(1)), new Ln(Variable.X)),
new Mul(new Sin(Variable.X), new Div(new Number(1), Variable.X))
)
);

Assert.Equal(expected, result);
}

[Fact]
public void PowPartialDerivativeTest1()
{
Expand Down

0 comments on commit d4e6363

Please sign in to comment.