Skip to content

Commit

Permalink
#116 - Show derivative steps
Browse files Browse the repository at this point in the history
  • Loading branch information
sys27 committed May 26, 2016
1 parent 1b3bdad commit f1fe8cc
Showing 1 changed file with 71 additions and 41 deletions.
112 changes: 71 additions & 41 deletions xFunc.Maths/Differentiator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using xFunc.Maths.Expressions;
using xFunc.Maths.Expressions.Hyperbolic;
using xFunc.Maths.Expressions.Trigonometric;
Expand All @@ -27,7 +28,9 @@ public class Differentiator : IDifferentiator
{

private ISimplifier simplifier;
private LinkedList<string> steps;
private bool simplify = true;
private bool logSteps = false;

/// <summary>
/// Initializes a new instance of the <see cref="Differentiator"/> class.
Expand All @@ -45,6 +48,8 @@ public Differentiator()
public Differentiator(ISimplifier simplifier)
{
this.simplifier = simplifier;

this.steps = new LinkedList<string>();
}

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

private IExpression _Differentiate(IExpression expression, Variable variable, ExpressionParameters parameters)
{
IExpression result = null;

var number = expression as Number;
if (number != null)
return Number(number, variable);
result = Number(number, variable);
var @var = expression as Variable;
if (@var != null)
return Variable(@var, variable);
result = Variable(@var, variable);
var deriv = expression as Derivative;
if (deriv != null)
expression = _Differentiate(deriv.Expression, deriv.Variable, parameters);

var abs = expression as Abs;
if (abs != null)
return Abs(abs, variable);
result = Abs(abs, variable);
var add = expression as Add;
if (add != null)
return Add(add, variable);
result = Add(add, variable);
var div = expression as Div;
if (div != null)
return Div(div, variable);
result = Div(div, variable);
var exp = expression as Exp;
if (exp != null)
return Exp(exp, variable);
result = Exp(exp, variable);
var ln = expression as Ln;
if (ln != null)
return Ln(ln, variable);
result = Ln(ln, variable);
var lg = expression as Lg;
if (lg != null)
return Lg(lg, variable);
result = Lg(lg, variable);
var log = expression as Log;
if (log != null)
return Log(log, variable);
result = Log(log, variable);
var mul = expression as Mul;
if (mul != null)
return Mul(mul, variable);
result = Mul(mul, variable);
var pow = expression as Pow;
if (pow != null)
return Pow(pow, variable);
result = Pow(pow, variable);
var root = expression as Root;
if (root != null)
return Root(root, variable);
result = Root(root, variable);
var sqrt = expression as Sqrt;
if (sqrt != null)
return Sqrt(sqrt, variable);
result = Sqrt(sqrt, variable);
var sub = expression as Sub;
if (sub != null)
return Sub(sub, variable);
result = Sub(sub, variable);
var minus = expression as UnaryMinus;
if (minus != null)
return UnaryMinus(minus, variable);
result = UnaryMinus(minus, variable);
var function = expression as UserFunction;
if (function != null)
return UserFunction(function, variable, parameters);
result = UserFunction(function, variable, parameters);

var sin = expression as Sin;
if (sin != null)
return Sin(sin, variable);
result = Sin(sin, variable);
var cos = expression as Cos;
if (cos != null)
return Cos(cos, variable);
result = Cos(cos, variable);
var tan = expression as Tan;
if (tan != null)
return Tan(tan, variable);
result = Tan(tan, variable);
var cot = expression as Cot;
if (cot != null)
return Cot(cot, variable);
result = Cot(cot, variable);
var sec = expression as Sec;
if (sec != null)
return Sec(sec, variable);
result = Sec(sec, variable);
var csc = expression as Csc;
if (csc != null)
return Csc(csc, variable);
result = Csc(csc, variable);
var arcsin = expression as Arcsin;
if (arcsin != null)
return Arcsin(arcsin, variable);
result = Arcsin(arcsin, variable);
var arccos = expression as Arccos;
if (arccos != null)
return Arccos(arccos, variable);
result = Arccos(arccos, variable);
var arctan = expression as Arctan;
if (arctan != null)
return Arctan(arctan, variable);
result = Arctan(arctan, variable);
var arccot = expression as Arccot;
if (arccot != null)
return Arccot(arccot, variable);
result = Arccot(arccot, variable);
var arcsec = expression as Arcsec;
if (arcsec != null)
return Arcsec(arcsec, variable);
result = Arcsec(arcsec, variable);
var arccsc = expression as Arccsc;
if (arccsc != null)
return Arccsc(arccsc, variable);
result = Arccsc(arccsc, variable);

var sinh = expression as Sinh;
if (sinh != null)
return Sinh(sinh, variable);
result = Sinh(sinh, variable);
var cosh = expression as Cosh;
if (cosh != null)
return Cosh(cosh, variable);
result = Cosh(cosh, variable);
var tanh = expression as Tanh;
if (tanh != null)
return Tanh(tanh, variable);
result = Tanh(tanh, variable);
var coth = expression as Coth;
if (coth != null)
return Coth(coth, variable);
result = Coth(coth, variable);
var sech = expression as Sech;
if (sech != null)
return Sech(sech, variable);
result = Sech(sech, variable);
var csch = expression as Csch;
if (csch != null)
return Csch(csch, variable);
result = Csch(csch, variable);
var arsinh = expression as Arsinh;
if (arsinh != null)
return Arsinh(arsinh, variable);
result = Arsinh(arsinh, variable);
var arcosh = expression as Arcosh;
if (arcosh != null)
return Arcosh(arcosh, variable);
result = Arcosh(arcosh, variable);
var artanh = expression as Artanh;
if (artanh != null)
return Artanh(artanh, variable);
result = Artanh(artanh, variable);
var arcoth = expression as Arcoth;
if (arcoth != null)
return Arcoth(arcoth, variable);
result = Arcoth(arcoth, variable);
var arsech = expression as Arsech;
if (arsech != null)
return Arsech(arsech, variable);
result = Arsech(arsech, variable);
var arcsch = expression as Arcsch;
if (arcsch != null)
return Arcsch(arcsch, variable);
result = Arcsch(arcsch, variable);

if (result == null)
throw new NotSupportedException();

LogStep(expression, result);

throw new NotSupportedException();
return result;
}

#region Common
Expand Down Expand Up @@ -919,6 +931,12 @@ protected virtual IExpression UserFunction(UserFunction expression, Variable var
return _Differentiate(func, variable, parameters);
}

protected virtual void LogStep(IExpression original, IExpression modified)
{
if (logSteps)
steps.AddLast(string.Format("{0} -> {1}", original, modified));
}

/// <summary>
/// Gets or sets the simplifier.
/// </summary>
Expand Down Expand Up @@ -953,6 +971,18 @@ public bool Simplify
}
}

public bool LogSteps
{
get
{
return logSteps;
}
set
{
logSteps = value;
}
}

}

}

0 comments on commit f1fe8cc

Please sign in to comment.