Index: branches/2915-AbsoluteSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/DerivativeCalculator.cs
===================================================================
--- branches/2915-AbsoluteSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/DerivativeCalculator.cs	(revision 16240)
+++ branches/2915-AbsoluteSymbol/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/DerivativeCalculator.cs	(revision 16304)
@@ -21,4 +21,5 @@
 
 using System;
+using System.Collections.Generic;
 using System.Linq;
 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
@@ -27,18 +28,24 @@
   public static class DerivativeCalculator {
     public static ISymbolicExpressionTree Derive(ISymbolicExpressionTree tree, string variableName) {
+      if (tree.Root.SubtreeCount != 1)
+        throw new NotImplementedException("Derive is not implemented for symbolic expressions with automatically defined functions (ADF)");
+      if (tree.Root.GetSubtree(0).SubtreeCount != 1)
+        throw new NotImplementedException("Derive is not implemented for multi-variate symbolic expressions");
       var mainBranch = tree.Root.GetSubtree(0).GetSubtree(0);
       var root = new ProgramRootSymbol().CreateTreeNode();
       root.AddSubtree(new StartSymbol().CreateTreeNode());
       var dTree = TreeSimplifier.GetSimplifiedTree(Derive(mainBranch, variableName));
-      // var dTree = Derive(mainBranch, variableName);
+      //var dTree = Derive(mainBranch, variableName);
       root.GetSubtree(0).AddSubtree(dTree);
       return new SymbolicExpressionTree(root);
     }
 
-    private static Constant constantSy = new Constant();
-    private static Addition addSy = new Addition();
-    private static Subtraction subSy = new Subtraction();
-    private static Multiplication mulSy = new Multiplication();
-    private static Division divSy = new Division();
+    private static readonly Constant constantSy = new Constant();
+    private static readonly Addition addSy = new Addition();
+    private static readonly Subtraction subSy = new Subtraction();
+    private static readonly Multiplication mulSy = new Multiplication();
+    private static readonly Division divSy = new Division();
+    private static readonly Cosine cosSy = new Cosine();
+    private static readonly Square sqrSy = new Square();
 
     public static ISymbolicExpressionTreeNode Derive(ISymbolicExpressionTreeNode branch, string variableName) {
@@ -85,5 +92,7 @@
           }
           return fgPrime;
-        } else throw new ArgumentException();
+        } else
+          // multiplication with only one argument has no effect -> derive the argument
+          return Derive(branch.GetSubtree(0), variableName);
       }
       if (branch.Symbol is Division) {
@@ -95,13 +104,20 @@
           sqrNode.AddSubtree(g);
           return Div(gPrime, sqrNode);
-        } else if (branch.SubtreeCount == 2) {
+        } else {
+          // for two subtrees:
+          // (f/g)' = (f'g - fg')/g²
+
+          // if there are more than 2 subtrees
+          // div(x,y,z) is interpretered as (x/y)/z
+          // which is the same as x / (y*z)
+
+          // --> make a product of all but the first subtree and differentiate as for the 2-argument case above
           var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
-          var g = (ISymbolicExpressionTreeNode)branch.GetSubtree(1).Clone();
+          var g = Product(branch.Subtrees.Skip(1).Select(n => (ISymbolicExpressionTreeNode)n.Clone()));
           var fprime = Derive(f, variableName);
           var gprime = Derive(g, variableName);
-          var sqrNode = new Square().CreateTreeNode();
-          sqrNode.AddSubtree((ISymbolicExpressionTreeNode)branch.GetSubtree(1).Clone());
-          return Div(Subtract(Product(fprime, g), Product(f, gprime)), sqrNode);
-        } else throw new NotSupportedException();
+          var gSqr = Square(g);
+          return Div(Subtract(Product(fprime, g), Product(f, gprime)), gSqr);
+        }
       }
       if (branch.Symbol is Logarithm) {
@@ -113,9 +129,9 @@
         return Product(f, Derive(branch.GetSubtree(0), variableName));
       }
-      if(branch.Symbol is Square) {
+      if (branch.Symbol is Square) {
         var f = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
         return Product(Product(CreateConstant(2.0), f), Derive(f, variableName));
-      }     
-      if(branch.Symbol is SquareRoot) {
+      }
+      if (branch.Symbol is SquareRoot) {
         var f = (ISymbolicExpressionTreeNode)branch.Clone();
         var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
@@ -133,4 +149,10 @@
         sin.AddSubtree(u);
         return Product(CreateConstant(-1.0), Product(sin, Derive(u, variableName)));
+      }
+      if (branch.Symbol is Tangent) {
+        // tan(x)' = 1 / cos²(x)
+        var fxp = Derive(branch.GetSubtree(0), variableName);
+        var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
+        return Div(fxp, Square(Cosine(u)));
       }
       throw new NotSupportedException(string.Format("Symbol {0} is not supported.", branch.Symbol));
@@ -144,4 +166,9 @@
       return product;
     }
+    private static ISymbolicExpressionTreeNode Product(IEnumerable<ISymbolicExpressionTreeNode> fs) {
+      var product = mulSy.CreateTreeNode();
+      foreach (var f in fs) product.AddSubtree(f);
+      return product;
+    }
     private static ISymbolicExpressionTreeNode Div(ISymbolicExpressionTreeNode f, ISymbolicExpressionTreeNode g) {
       var div = divSy.CreateTreeNode();
@@ -163,5 +190,15 @@
       return sum;
     }
-                          
+    private static ISymbolicExpressionTreeNode Cosine(ISymbolicExpressionTreeNode f) {
+      var cos = cosSy.CreateTreeNode();
+      cos.AddSubtree(f);
+      return cos;
+    }
+    private static ISymbolicExpressionTreeNode Square(ISymbolicExpressionTreeNode f) {
+      var sqr = sqrSy.CreateTreeNode();
+      sqr.AddSubtree(f);
+      return sqr;
+    }
+
     private static ISymbolicExpressionTreeNode CreateConstant(double v) {
       var constNode = (ConstantTreeNode)constantSy.CreateTreeNode();
@@ -186,4 +223,5 @@
           !(n.Symbol is Sine) &&
           !(n.Symbol is Cosine) &&
+          !(n.Symbol is Tangent) &&
           !(n.Symbol is StartSymbol)
         select n).Any();
