13
13
// See the License for the specific language governing permissions and
14
14
// limitations under the License.
15
15
using System ;
16
+ using System . Collections . Generic ;
16
17
using xFunc . Maths . Expressions ;
17
18
using xFunc . Maths . Expressions . Hyperbolic ;
18
19
using xFunc . Maths . Expressions . Trigonometric ;
@@ -27,7 +28,9 @@ public class Differentiator : IDifferentiator
27
28
{
28
29
29
30
private ISimplifier simplifier ;
31
+ private LinkedList < string > steps ;
30
32
private bool simplify = true ;
33
+ private bool logSteps = false ;
31
34
32
35
/// <summary>
33
36
/// Initializes a new instance of the <see cref="Differentiator"/> class.
@@ -45,6 +48,8 @@ public Differentiator()
45
48
public Differentiator ( ISimplifier simplifier )
46
49
{
47
50
this . simplifier = simplifier ;
51
+
52
+ this . steps = new LinkedList < string > ( ) ;
48
53
}
49
54
50
55
/// <summary>
@@ -105,134 +110,141 @@ private IExpression _Differentiate(IExpression expression, Variable variable)
105
110
106
111
private IExpression _Differentiate ( IExpression expression , Variable variable , ExpressionParameters parameters )
107
112
{
113
+ IExpression result = null ;
114
+
108
115
var number = expression as Number ;
109
116
if ( number != null )
110
- return Number ( number , variable ) ;
117
+ result = Number ( number , variable ) ;
111
118
var @var = expression as Variable ;
112
119
if ( @var != null )
113
- return Variable ( @var , variable ) ;
120
+ result = Variable ( @var , variable ) ;
114
121
var deriv = expression as Derivative ;
115
122
if ( deriv != null )
116
123
expression = _Differentiate ( deriv . Expression , deriv . Variable , parameters ) ;
117
124
118
125
var abs = expression as Abs ;
119
126
if ( abs != null )
120
- return Abs ( abs , variable ) ;
127
+ result = Abs ( abs , variable ) ;
121
128
var add = expression as Add ;
122
129
if ( add != null )
123
- return Add ( add , variable ) ;
130
+ result = Add ( add , variable ) ;
124
131
var div = expression as Div ;
125
132
if ( div != null )
126
- return Div ( div , variable ) ;
133
+ result = Div ( div , variable ) ;
127
134
var exp = expression as Exp ;
128
135
if ( exp != null )
129
- return Exp ( exp , variable ) ;
136
+ result = Exp ( exp , variable ) ;
130
137
var ln = expression as Ln ;
131
138
if ( ln != null )
132
- return Ln ( ln , variable ) ;
139
+ result = Ln ( ln , variable ) ;
133
140
var lg = expression as Lg ;
134
141
if ( lg != null )
135
- return Lg ( lg , variable ) ;
142
+ result = Lg ( lg , variable ) ;
136
143
var log = expression as Log ;
137
144
if ( log != null )
138
- return Log ( log , variable ) ;
145
+ result = Log ( log , variable ) ;
139
146
var mul = expression as Mul ;
140
147
if ( mul != null )
141
- return Mul ( mul , variable ) ;
148
+ result = Mul ( mul , variable ) ;
142
149
var pow = expression as Pow ;
143
150
if ( pow != null )
144
- return Pow ( pow , variable ) ;
151
+ result = Pow ( pow , variable ) ;
145
152
var root = expression as Root ;
146
153
if ( root != null )
147
- return Root ( root , variable ) ;
154
+ result = Root ( root , variable ) ;
148
155
var sqrt = expression as Sqrt ;
149
156
if ( sqrt != null )
150
- return Sqrt ( sqrt , variable ) ;
157
+ result = Sqrt ( sqrt , variable ) ;
151
158
var sub = expression as Sub ;
152
159
if ( sub != null )
153
- return Sub ( sub , variable ) ;
160
+ result = Sub ( sub , variable ) ;
154
161
var minus = expression as UnaryMinus ;
155
162
if ( minus != null )
156
- return UnaryMinus ( minus , variable ) ;
163
+ result = UnaryMinus ( minus , variable ) ;
157
164
var function = expression as UserFunction ;
158
165
if ( function != null )
159
- return UserFunction ( function , variable , parameters ) ;
166
+ result = UserFunction ( function , variable , parameters ) ;
160
167
161
168
var sin = expression as Sin ;
162
169
if ( sin != null )
163
- return Sin ( sin , variable ) ;
170
+ result = Sin ( sin , variable ) ;
164
171
var cos = expression as Cos ;
165
172
if ( cos != null )
166
- return Cos ( cos , variable ) ;
173
+ result = Cos ( cos , variable ) ;
167
174
var tan = expression as Tan ;
168
175
if ( tan != null )
169
- return Tan ( tan , variable ) ;
176
+ result = Tan ( tan , variable ) ;
170
177
var cot = expression as Cot ;
171
178
if ( cot != null )
172
- return Cot ( cot , variable ) ;
179
+ result = Cot ( cot , variable ) ;
173
180
var sec = expression as Sec ;
174
181
if ( sec != null )
175
- return Sec ( sec , variable ) ;
182
+ result = Sec ( sec , variable ) ;
176
183
var csc = expression as Csc ;
177
184
if ( csc != null )
178
- return Csc ( csc , variable ) ;
185
+ result = Csc ( csc , variable ) ;
179
186
var arcsin = expression as Arcsin ;
180
187
if ( arcsin != null )
181
- return Arcsin ( arcsin , variable ) ;
188
+ result = Arcsin ( arcsin , variable ) ;
182
189
var arccos = expression as Arccos ;
183
190
if ( arccos != null )
184
- return Arccos ( arccos , variable ) ;
191
+ result = Arccos ( arccos , variable ) ;
185
192
var arctan = expression as Arctan ;
186
193
if ( arctan != null )
187
- return Arctan ( arctan , variable ) ;
194
+ result = Arctan ( arctan , variable ) ;
188
195
var arccot = expression as Arccot ;
189
196
if ( arccot != null )
190
- return Arccot ( arccot , variable ) ;
197
+ result = Arccot ( arccot , variable ) ;
191
198
var arcsec = expression as Arcsec ;
192
199
if ( arcsec != null )
193
- return Arcsec ( arcsec , variable ) ;
200
+ result = Arcsec ( arcsec , variable ) ;
194
201
var arccsc = expression as Arccsc ;
195
202
if ( arccsc != null )
196
- return Arccsc ( arccsc , variable ) ;
203
+ result = Arccsc ( arccsc , variable ) ;
197
204
198
205
var sinh = expression as Sinh ;
199
206
if ( sinh != null )
200
- return Sinh ( sinh , variable ) ;
207
+ result = Sinh ( sinh , variable ) ;
201
208
var cosh = expression as Cosh ;
202
209
if ( cosh != null )
203
- return Cosh ( cosh , variable ) ;
210
+ result = Cosh ( cosh , variable ) ;
204
211
var tanh = expression as Tanh ;
205
212
if ( tanh != null )
206
- return Tanh ( tanh , variable ) ;
213
+ result = Tanh ( tanh , variable ) ;
207
214
var coth = expression as Coth ;
208
215
if ( coth != null )
209
- return Coth ( coth , variable ) ;
216
+ result = Coth ( coth , variable ) ;
210
217
var sech = expression as Sech ;
211
218
if ( sech != null )
212
- return Sech ( sech , variable ) ;
219
+ result = Sech ( sech , variable ) ;
213
220
var csch = expression as Csch ;
214
221
if ( csch != null )
215
- return Csch ( csch , variable ) ;
222
+ result = Csch ( csch , variable ) ;
216
223
var arsinh = expression as Arsinh ;
217
224
if ( arsinh != null )
218
- return Arsinh ( arsinh , variable ) ;
225
+ result = Arsinh ( arsinh , variable ) ;
219
226
var arcosh = expression as Arcosh ;
220
227
if ( arcosh != null )
221
- return Arcosh ( arcosh , variable ) ;
228
+ result = Arcosh ( arcosh , variable ) ;
222
229
var artanh = expression as Artanh ;
223
230
if ( artanh != null )
224
- return Artanh ( artanh , variable ) ;
231
+ result = Artanh ( artanh , variable ) ;
225
232
var arcoth = expression as Arcoth ;
226
233
if ( arcoth != null )
227
- return Arcoth ( arcoth , variable ) ;
234
+ result = Arcoth ( arcoth , variable ) ;
228
235
var arsech = expression as Arsech ;
229
236
if ( arsech != null )
230
- return Arsech ( arsech , variable ) ;
237
+ result = Arsech ( arsech , variable ) ;
231
238
var arcsch = expression as Arcsch ;
232
239
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 ) ;
234
246
235
- throw new NotSupportedException ( ) ;
247
+ return result ;
236
248
}
237
249
238
250
#region Common
@@ -919,6 +931,12 @@ protected virtual IExpression UserFunction(UserFunction expression, Variable var
919
931
return _Differentiate ( func , variable , parameters ) ;
920
932
}
921
933
934
+ protected virtual void LogStep ( IExpression original , IExpression modified )
935
+ {
936
+ if ( logSteps )
937
+ steps . AddLast ( string . Format ( "{0} -> {1}" , original , modified ) ) ;
938
+ }
939
+
922
940
/// <summary>
923
941
/// Gets or sets the simplifier.
924
942
/// </summary>
@@ -953,6 +971,18 @@ public bool Simplify
953
971
}
954
972
}
955
973
974
+ public bool LogSteps
975
+ {
976
+ get
977
+ {
978
+ return logSteps ;
979
+ }
980
+ set
981
+ {
982
+ logSteps = value ;
983
+ }
984
+ }
985
+
956
986
}
957
987
958
988
}
0 commit comments