-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerOperator.cs
More file actions
105 lines (92 loc) · 3.35 KB
/
PowerOperator.cs
File metadata and controls
105 lines (92 loc) · 3.35 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
namespace SymSharp;
/// <summary>
/// Represents exponentiation: base ^ exponent.
/// </summary>
public class PowerOperator : IMathExpression
{
/// <summary>
/// The base expression.
/// </summary>
private readonly IMathExpression left;
/// <summary>
/// The exponent expression.
/// </summary>
private readonly IMathExpression right;
/// <summary>
/// Gets the base (left operand).
/// </summary>
public IMathExpression Left => left; // the base
/// <summary>
/// Gets the exponent (right operand).
/// </summary>
public IMathExpression Right => right; // the exponent
/// <summary>
/// Constructs a power operator with the given base and exponent.
/// </summary>
/// <param name="left">The base expression.</param>
/// <param name="right">The exponent expression.</param>
public PowerOperator(IMathExpression left, IMathExpression right)
{
this.left = left;
this.right = right;
}
/// <summary>
/// Returns a textual representation of the power expression.
/// </summary>
public override string ToString()
{
return $"({Left} ^ {Right})";
}
/// <summary>
/// Simplifies the power expression applying constant evaluation and trivial exponent rules.
/// </summary>
public IMathExpression Simplified()
{
IMathExpression simplifiedLeft = Left.Simplified();
IMathExpression simplifiedRight = Right.Simplified();
// Case: scalar base and exponent → evaluate directly
if (simplifiedLeft is Scalar b && simplifiedRight is Scalar e)
{
return new Scalar(Math.Pow(b.ToDouble(), e.ToDouble()));
}
// Case: exponent = 0 → 1
if (simplifiedRight is Scalar zeroExp && zeroExp.ToDouble() == 0.0)
{
return new Scalar(1);
}
// Case: exponent = 1 → base
if (simplifiedRight is Scalar oneExp && oneExp.ToDouble() == 1.0)
{
return simplifiedLeft;
}
return new PowerOperator(simplifiedLeft, simplifiedRight);
}
/// <summary>
/// Computes the derivative of the power expression.
/// Handles simple power rule when exponent is a scalar and the general logarithmic differentiation otherwise.
/// </summary>
/// <param name="varname">The variable name to differentiate with respect to.</param>
public IMathExpression Derivative(string varname)
{
// Power rule: d/dx (f(x)^n) = n * f(x)^(n-1) * f'(x), only if exponent is scalar
if (Right is Scalar expScalar)
{
double n = expScalar.ToDouble();
return new MultiplyOperator(
new MultiplyOperator(
new Scalar(n),
new PowerOperator(Left, new Scalar(n - 1))
),
Left.Derivative(varname)
).Simplified();
}
// General case (f(x)^g(x)) → logarithmic differentiation
return new MultiplyOperator(
new PowerOperator(Left, Right),
new AddOperator(
new MultiplyOperator(Right.Derivative(varname), new LogOperator(Left)),
new MultiplyOperator(Right, new DivideOperator(Left.Derivative(varname), Left))
)
).Simplified();
}
}