Repository navigation
Expand file tree
/
Copy pathSubtractOperator.cs
More file actions
124 lines (108 loc) · 4.26 KB
/
Copy pathSubtractOperator.cs
File metadata and controls
124 lines (108 loc) · 4.26 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
123
124
namespace SymSharp;
/// <summary>
/// Represents binary subtraction between two expressions.
/// </summary>
public class SubtractOperator : 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 a subtraction operator with the specified operands.
/// </summary>
/// <param name="left">Left operand.</param>
/// <param name="right">Right operand.</param>
public SubtractOperator(IMathExpression left, IMathExpression right)
{
this.left = left;
this.right = right;
}
/// <summary>
/// Returns a textual representation of the subtraction.
/// </summary>
public override string ToString()
{
return $"({Left}) - ({Right})";
}
/// <summary>
/// Simplifies the subtraction 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;
}
// Case: 0 - x → -x
if (simplifiedLeft is Scalar zeroLeft && zeroLeft.ToDouble() == 0.0)
{
return new MultiplyOperator(new Scalar(-1), simplifiedRight).Simplified();
}
// Case: x - x → 0
if (simplifiedLeft.ToString() == simplifiedRight.ToString())
{
return new Scalar(0);
}
// 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(1 - sr2.ToDouble()), vl).Simplified();
}
// Case: reorder Scalar - Variable → Variable - Scalar (optional consistency)
if (simplifiedLeft is Scalar && simplifiedRight is Variable)
{
return new SubtractOperator(simplifiedRight, simplifiedLeft);
}
// Default: keep as is
return new SubtractOperator(simplifiedLeft, simplifiedRight);
}
/// <summary>
/// The derivative of a difference is the difference of derivatives.
/// </summary>
/// <param name="varname">The variable name to differentiate with respect to.</param>
public IMathExpression Derivative(string varname)
{
return new SubtractOperator(Left.Derivative(varname), Right.Derivative(varname)).Simplified();
}
}