Skip to content

Ln1Exp and other numerical optimizations #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions ComputerAlgebra/LinqCompiler/CompileExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,35 @@ protected override LinqExpr VisitPower(Power P)

protected override LinqExpr VisitCall(Call C)
{
if (C.Target.Name == "Ln")
{
var arg = C.Arguments.Single();
if (arg is Sum sum)
{
if (sum.Terms.Count() == 2)
{
var terms = sum.Terms.ToArray();
var a = terms[0];
var b = terms[1];

var (match, lnArgs) = (a, b) switch
{
(Constant c, Call f) when c.Value == 1 && f.Target.Name == "Exp" => (true, f.Arguments),
(Call f, Constant c) when c.Value == 1 && f.Target.Name == "Exp" => (true, f.Arguments),
_ => (false, null),
};

if (match)
{
var compiledArgs = lnArgs.Select(i => Visit(i)).ToArray();
return Int(C, LinqExpr.Call(
target.Module.GetFunction("Ln1Exp", compiledArgs.Select(i => i.Type).ToArray()),
compiledArgs));
}
}
}

}
LinqExpr[] args = C.Arguments.Select(i => Visit(i)).ToArray();
return Int(C, LinqExpr.Call(
target.Module.Compile(C.Target, args.Select(i => i.Type).ToArray()),
Expand Down
13 changes: 12 additions & 1 deletion ComputerAlgebra/LinqCompiler/StandardMath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ namespace ComputerAlgebra.LinqCompiler
/// </summary>
public class StandardMath
{
private static double ExpKnee = 50;
static StandardMath()
{
_knee = Math.Exp(ExpKnee);
_b = _knee - _knee * ExpKnee;
}
public static double Ln1Exp(double x) => x > 50d ? x : Math.Log(1d + Math.Exp(x));
public static double Abs(double x) { return x < 0 ? -x : x; }
public static double Sign(double x) { return x > 0 ? 1 : (x < 0 ? -1 : 0); }

Expand Down Expand Up @@ -42,7 +49,11 @@ public class StandardMath
public static double ArcCoth(double x) { return ArcTanh(1 / x); }

public static double Sqrt(double x) { return Math.Sqrt(x); }
public static double Exp(double x) { return Math.Exp(x); }

private static double _knee;
private static double _b;

public static double Exp(double x) => x > ExpKnee ? _knee * x + _b : Math.Exp(x);
public static double Ln(double x) { return Math.Log(x); }
public static double Log(double x, double b) { return Math.Log(x, b); }
public static double Pow(double x, double y) { return Math.Pow(x, y); }
Expand Down