Skip to content

Commit

Permalink
#262 - Add 'Log' simplification rules
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed May 16, 2020
1 parent 16d0867 commit 4833368
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
21 changes: 19 additions & 2 deletions xFunc.Maths/Analyzers/Simplifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,13 @@ public virtual IExpression Analyze(Div exp)
[ExcludeFromCodeCoverage]
public virtual IExpression Analyze(Exp exp)
{
return AnalyzeUnary(exp);
exp = AnalyzeUnary(exp);

// exp(ln(y)) -> y
if (exp.Argument is Ln ln)
return ln.Argument;

return exp;
}

/// <summary>
Expand Down Expand Up @@ -681,7 +687,18 @@ public virtual IExpression Analyze(Pow exp)
if (exp.Right.Equals(one))
return exp.Left;

return exp;
return (exp.Left, exp.Right) switch
{
// x ^ log(x, y) -> y
(var left, Log log) when left.Equals(log.Left) => log.Right,
// e ^ ln(y) -> y
(Variable variable, Ln ln) when variable.Name == "e" => ln.Argument,
// 10 ^ lg(y) -> y
(Number number, Lg lg) when MathExtensions.Equals(number.Value, 10) => lg.Argument,
// 2 ^ lb(y) -> y
(Number number, Lb lb) when MathExtensions.Equals(number.Value, 2) => lb.Argument,
_ => exp,
};
}

/// <summary>
Expand Down
11 changes: 11 additions & 0 deletions xFunc.Maths/MathExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using System.Globalization;
using System.Numerics;
using System.Runtime.CompilerServices;
using xFunc.Maths.Expressions;
using xFunc.Maths.Resources;

Expand Down Expand Up @@ -406,5 +407,15 @@ public static double Not(this double value)

return ~(int)value;
}

/// <summary>
/// Determines whether the specified number is equal to the current number.
/// </summary>
/// <param name="left">The current number.</param>
/// <param name="right">The number to compare with the current number.</param>
/// <returns><c>true</c> if the specified number is equal to the current number; otherwise, <c>false</c>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Equals(double left, double right) =>
left.Equals(right) || Math.Abs(left - right) < Epsilon;
}
}
61 changes: 61 additions & 0 deletions xFunc.Tests/Analyzers/SimplifierTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,50 @@ public void PowerOne()
SimpleTest(pow, expected);
}

[Fact]
public void PowLog()
{
var pow = new Pow(
new Number(30),
new Log(new Number(30), new Variable("x")));
var expected = new Variable("x");

SimpleTest(pow, expected);
}

[Fact]
public void PowLg()
{
var pow = new Pow(
new Number(10),
new Lg(new Variable("x")));
var expected = new Variable("x");

SimpleTest(pow, expected);
}

[Fact]
public void PowLn()
{
var pow = new Pow(
new Variable("e"),
new Ln(new Variable("x")));
var expected = new Variable("x");

SimpleTest(pow, expected);
}

[Fact]
public void PowLb()
{
var pow = new Pow(
new Number(2),
new Lb(new Variable("x")));
var expected = new Variable("x");

SimpleTest(pow, expected);
}

[Fact]
public void RootOne()
{
Expand All @@ -964,6 +1008,23 @@ public void Root()
SimpleTest(root, root);
}

[Fact]
public void Exp()
{
var exp = new Exp(new Number(30));

SimpleTest(exp, exp);
}

[Fact]
public void ExpLn()
{
var exp = new Exp(new Ln(new Number(30)));
var expected = new Number(30);

SimpleTest(exp, expected);
}

[Fact]
public void Log()
{
Expand Down

0 comments on commit 4833368

Please sign in to comment.