-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddOperator.cs
More file actions
122 lines (107 loc) · 4.17 KB
/
AddOperator.cs
File metadata and controls
122 lines (107 loc) · 4.17 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
namespace SymSharp;
/// <summary>
/// Represents the binary addition operator between two expressions.
/// </summary>
public class AddOperator : IBinaryOperator
{
/// <summary>
/// The left operand.
/// </summary>
private readonly IMathExpression left;
/// <summary>
/// The right operand.
/// </summary>
private readonly IMathExpression right;
/// <summary>
/// Gets the left operand.
/// </summary>
public IMathExpression Left => left;
/// <summary>
/// Gets the right operand.
/// </summary>
public IMathExpression Right => right;
/// <summary>
/// Constructs an addition operator with the specified operands.
/// </summary>
/// <param name="left">Left operand.</param>
/// <param name="right">Right operand.</param>
public AddOperator(IMathExpression left, IMathExpression right)
{
this.left = left;
this.right = right;
}
/// <summary>
/// Returns a textual representation of the addition.
/// </summary>
public override string ToString()
{
return $"({Left} + {Right})";
}
/// <summary>
/// Simplifies the addition expression by applying algebraic and constant-folding rules.
/// </summary>
public IMathExpression Simplified()
{
IMathExpression simplifiedLeft = Left.Simplified();
IMathExpression simplifiedRight = Right.Simplified();
// Case: both scalars → collapse into one scalar
if (simplifiedLeft is Scalar leftScalar && simplifiedRight is Scalar rightScalar)
{
return new Scalar(leftScalar.ToDouble() + rightScalar.ToDouble());
}
// Case: x + 0 → x
if (simplifiedRight is Scalar zeroRight && zeroRight.ToDouble() == 0.0)
{
return simplifiedLeft;
}
if (simplifiedLeft is Scalar zeroLeft && zeroLeft.ToDouble() == 0.0)
{
return simplifiedRight;
}
// Case: x + x → 2 * x
if (simplifiedLeft.ToString() == simplifiedRight.ToString())
{
return new MultiplyOperator(new Scalar(2), simplifiedLeft).Simplified();
}
// Case: (a * x) + (b * x) → (a+b) * x
if (simplifiedLeft is MultiplyOperator ml && simplifiedRight is MultiplyOperator mr)
{
if (ml.Left is Scalar sl && mr.Left is Scalar sr &&
ml.Right.ToString() == mr.Right.ToString())
{
return new MultiplyOperator(new Scalar(sl.ToDouble() + sr.ToDouble()), ml.Right).Simplified();
}
if (ml.Right is Scalar slr && mr.Right is Scalar srr &&
ml.Left.ToString() == mr.Left.ToString())
{
return new MultiplyOperator(new Scalar(slr.ToDouble() + srr.ToDouble()), ml.Left).Simplified();
}
}
// Case: (a * x) + x → (a+1) * x
if (simplifiedLeft is MultiplyOperator ml2 && simplifiedRight is Variable vr &&
ml2.Right.ToString() == vr.ToString() && ml2.Left is Scalar sl2)
{
return new MultiplyOperator(new Scalar(sl2.ToDouble() + 1), vr).Simplified();
}
if (simplifiedRight is MultiplyOperator mr2 && simplifiedLeft is Variable vl &&
mr2.Right.ToString() == vl.ToString() && mr2.Left is Scalar sr2)
{
return new MultiplyOperator(new Scalar(sr2.ToDouble() + 1), vl).Simplified();
}
// Case: reorder Scalar + Variable → Variable + Scalar
if (simplifiedLeft is Scalar && simplifiedRight is Variable)
{
return new AddOperator(simplifiedRight, simplifiedLeft);
}
// Default: keep as is
return new AddOperator(simplifiedLeft, simplifiedRight);
}
/// <summary>
/// The derivative of a sum is the sum of derivatives.
/// </summary>
/// <param name="varname">The variable name to differentiate with respect to.</param>
public IMathExpression Derivative(string varname)
{
return new AddOperator(Left.Derivative(varname), Right.Derivative(varname)).Simplified();
}
}