-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogOperator.cs
More file actions
62 lines (54 loc) · 1.78 KB
/
LogOperator.cs
File metadata and controls
62 lines (54 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
namespace SymSharp;
/// <summary>
/// Represents the natural logarithm (ln) of an inner expression.
/// </summary>
public class LogOperator : IUnaryOperator
{
/// <summary>
/// The inner expression to take the logarithm of.
/// </summary>
private readonly IMathExpression inner;
/// <summary>
/// Gets the inner expression.
/// </summary>
public IMathExpression Inner => inner;
/// <summary>
/// Constructs a natural logarithm operator wrapping the specified inner expression.
/// </summary>
/// <param name="inner">The inner expression.</param>
public LogOperator(IMathExpression inner)
{
this.inner = inner;
}
/// <summary>
/// Returns a textual representation of the logarithm.
/// </summary>
public override string ToString()
{
return $"ln({Inner})";
}
/// <summary>
/// Simplifies the logarithm, evaluating ln(constant) when possible.
/// </summary>
public IMathExpression Simplified()
{
IMathExpression simplifiedInner = Inner.Simplified();
///// I doubt we need this
// // Case: ln(constant) → evaluate directly
// if (simplifiedInner is Scalar s)
// {
// return new Scalar(Math.Log(s.ToDouble()));
// }
// Default: keep symbolic
return new LogOperator(simplifiedInner);
}
/// <summary>
/// Computes the derivative of ln(f(x)): f'(x) / f(x).
/// </summary>
/// <param name="varname">The variable name to differentiate with respect to.</param>
public IMathExpression Derivative(string varname)
{
// d/dx ln(f(x)) = f'(x) / f(x)
return new DivideOperator(Inner.Derivative(varname), Inner).Simplified();
}
}