Skip to content

Commit f1fe8cc

Browse files
committed
#116 - Show derivative steps
1 parent 1b3bdad commit f1fe8cc

File tree

1 file changed

+71
-41
lines changed

1 file changed

+71
-41
lines changed

xFunc.Maths/Differentiator.cs

+71-41
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515
using System;
16+
using System.Collections.Generic;
1617
using xFunc.Maths.Expressions;
1718
using xFunc.Maths.Expressions.Hyperbolic;
1819
using xFunc.Maths.Expressions.Trigonometric;
@@ -27,7 +28,9 @@ public class Differentiator : IDifferentiator
2728
{
2829

2930
private ISimplifier simplifier;
31+
private LinkedList<string> steps;
3032
private bool simplify = true;
33+
private bool logSteps = false;
3134

3235
/// <summary>
3336
/// Initializes a new instance of the <see cref="Differentiator"/> class.
@@ -45,6 +48,8 @@ public Differentiator()
4548
public Differentiator(ISimplifier simplifier)
4649
{
4750
this.simplifier = simplifier;
51+
52+
this.steps = new LinkedList<string>();
4853
}
4954

5055
/// <summary>
@@ -105,134 +110,141 @@ private IExpression _Differentiate(IExpression expression, Variable variable)
105110

106111
private IExpression _Differentiate(IExpression expression, Variable variable, ExpressionParameters parameters)
107112
{
113+
IExpression result = null;
114+
108115
var number = expression as Number;
109116
if (number != null)
110-
return Number(number, variable);
117+
result = Number(number, variable);
111118
var @var = expression as Variable;
112119
if (@var != null)
113-
return Variable(@var, variable);
120+
result = Variable(@var, variable);
114121
var deriv = expression as Derivative;
115122
if (deriv != null)
116123
expression = _Differentiate(deriv.Expression, deriv.Variable, parameters);
117124

118125
var abs = expression as Abs;
119126
if (abs != null)
120-
return Abs(abs, variable);
127+
result = Abs(abs, variable);
121128
var add = expression as Add;
122129
if (add != null)
123-
return Add(add, variable);
130+
result = Add(add, variable);
124131
var div = expression as Div;
125132
if (div != null)
126-
return Div(div, variable);
133+
result = Div(div, variable);
127134
var exp = expression as Exp;
128135
if (exp != null)
129-
return Exp(exp, variable);
136+
result = Exp(exp, variable);
130137
var ln = expression as Ln;
131138
if (ln != null)
132-
return Ln(ln, variable);
139+
result = Ln(ln, variable);
133140
var lg = expression as Lg;
134141
if (lg != null)
135-
return Lg(lg, variable);
142+
result = Lg(lg, variable);
136143
var log = expression as Log;
137144
if (log != null)
138-
return Log(log, variable);
145+
result = Log(log, variable);
139146
var mul = expression as Mul;
140147
if (mul != null)
141-
return Mul(mul, variable);
148+
result = Mul(mul, variable);
142149
var pow = expression as Pow;
143150
if (pow != null)
144-
return Pow(pow, variable);
151+
result = Pow(pow, variable);
145152
var root = expression as Root;
146153
if (root != null)
147-
return Root(root, variable);
154+
result = Root(root, variable);
148155
var sqrt = expression as Sqrt;
149156
if (sqrt != null)
150-
return Sqrt(sqrt, variable);
157+
result = Sqrt(sqrt, variable);
151158
var sub = expression as Sub;
152159
if (sub != null)
153-
return Sub(sub, variable);
160+
result = Sub(sub, variable);
154161
var minus = expression as UnaryMinus;
155162
if (minus != null)
156-
return UnaryMinus(minus, variable);
163+
result = UnaryMinus(minus, variable);
157164
var function = expression as UserFunction;
158165
if (function != null)
159-
return UserFunction(function, variable, parameters);
166+
result = UserFunction(function, variable, parameters);
160167

161168
var sin = expression as Sin;
162169
if (sin != null)
163-
return Sin(sin, variable);
170+
result = Sin(sin, variable);
164171
var cos = expression as Cos;
165172
if (cos != null)
166-
return Cos(cos, variable);
173+
result = Cos(cos, variable);
167174
var tan = expression as Tan;
168175
if (tan != null)
169-
return Tan(tan, variable);
176+
result = Tan(tan, variable);
170177
var cot = expression as Cot;
171178
if (cot != null)
172-
return Cot(cot, variable);
179+
result = Cot(cot, variable);
173180
var sec = expression as Sec;
174181
if (sec != null)
175-
return Sec(sec, variable);
182+
result = Sec(sec, variable);
176183
var csc = expression as Csc;
177184
if (csc != null)
178-
return Csc(csc, variable);
185+
result = Csc(csc, variable);
179186
var arcsin = expression as Arcsin;
180187
if (arcsin != null)
181-
return Arcsin(arcsin, variable);
188+
result = Arcsin(arcsin, variable);
182189
var arccos = expression as Arccos;
183190
if (arccos != null)
184-
return Arccos(arccos, variable);
191+
result = Arccos(arccos, variable);
185192
var arctan = expression as Arctan;
186193
if (arctan != null)
187-
return Arctan(arctan, variable);
194+
result = Arctan(arctan, variable);
188195
var arccot = expression as Arccot;
189196
if (arccot != null)
190-
return Arccot(arccot, variable);
197+
result = Arccot(arccot, variable);
191198
var arcsec = expression as Arcsec;
192199
if (arcsec != null)
193-
return Arcsec(arcsec, variable);
200+
result = Arcsec(arcsec, variable);
194201
var arccsc = expression as Arccsc;
195202
if (arccsc != null)
196-
return Arccsc(arccsc, variable);
203+
result = Arccsc(arccsc, variable);
197204

198205
var sinh = expression as Sinh;
199206
if (sinh != null)
200-
return Sinh(sinh, variable);
207+
result = Sinh(sinh, variable);
201208
var cosh = expression as Cosh;
202209
if (cosh != null)
203-
return Cosh(cosh, variable);
210+
result = Cosh(cosh, variable);
204211
var tanh = expression as Tanh;
205212
if (tanh != null)
206-
return Tanh(tanh, variable);
213+
result = Tanh(tanh, variable);
207214
var coth = expression as Coth;
208215
if (coth != null)
209-
return Coth(coth, variable);
216+
result = Coth(coth, variable);
210217
var sech = expression as Sech;
211218
if (sech != null)
212-
return Sech(sech, variable);
219+
result = Sech(sech, variable);
213220
var csch = expression as Csch;
214221
if (csch != null)
215-
return Csch(csch, variable);
222+
result = Csch(csch, variable);
216223
var arsinh = expression as Arsinh;
217224
if (arsinh != null)
218-
return Arsinh(arsinh, variable);
225+
result = Arsinh(arsinh, variable);
219226
var arcosh = expression as Arcosh;
220227
if (arcosh != null)
221-
return Arcosh(arcosh, variable);
228+
result = Arcosh(arcosh, variable);
222229
var artanh = expression as Artanh;
223230
if (artanh != null)
224-
return Artanh(artanh, variable);
231+
result = Artanh(artanh, variable);
225232
var arcoth = expression as Arcoth;
226233
if (arcoth != null)
227-
return Arcoth(arcoth, variable);
234+
result = Arcoth(arcoth, variable);
228235
var arsech = expression as Arsech;
229236
if (arsech != null)
230-
return Arsech(arsech, variable);
237+
result = Arsech(arsech, variable);
231238
var arcsch = expression as Arcsch;
232239
if (arcsch != null)
233-
return Arcsch(arcsch, variable);
240+
result = Arcsch(arcsch, variable);
241+
242+
if (result == null)
243+
throw new NotSupportedException();
244+
245+
LogStep(expression, result);
234246

235-
throw new NotSupportedException();
247+
return result;
236248
}
237249

238250
#region Common
@@ -919,6 +931,12 @@ protected virtual IExpression UserFunction(UserFunction expression, Variable var
919931
return _Differentiate(func, variable, parameters);
920932
}
921933

934+
protected virtual void LogStep(IExpression original, IExpression modified)
935+
{
936+
if (logSteps)
937+
steps.AddLast(string.Format("{0} -> {1}", original, modified));
938+
}
939+
922940
/// <summary>
923941
/// Gets or sets the simplifier.
924942
/// </summary>
@@ -953,6 +971,18 @@ public bool Simplify
953971
}
954972
}
955973

974+
public bool LogSteps
975+
{
976+
get
977+
{
978+
return logSteps;
979+
}
980+
set
981+
{
982+
logSteps = value;
983+
}
984+
}
985+
956986
}
957987

958988
}

0 commit comments

Comments
 (0)