Ignore:
Timestamp:
07/08/19 00:04:36 (3 years ago)
Author:
mkommend
Message:

#2915: Merged 16675, 16802, 16905, 17028 into stable.

Location:
stable
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic

  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/DerivativeCalculator.cs

    r17102 r17103  
    143143        var f = (ISymbolicExpressionTreeNode)branch.Clone();
    144144        var u = (ISymbolicExpressionTreeNode)branch.GetSubtree(0).Clone();
    145         return Product(Div(CreateConstant(1.0), Product(CreateConstant(3.0), Square(f))), Derive(u, variableName));
     145        return Product(Div(CreateConstant(1.0), Product(CreateConstant(3.0), Square(f))), Derive(u, variableName));  // 1/3 1/cbrt(f(x))^2 d/dx f(x)
    146146      }
    147147      if (branch.Symbol is Cube) {
  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToAutoDiffTermConverter.cs

    r17101 r17103  
    9292      );
    9393
     94    private static readonly Func<Term, UnaryFunc> cbrt = UnaryFunc.Factory(
     95      eval: x => x < 0 ? -Math.Pow(-x, 1.0 / 3) : Math.Pow(x, 1.0 / 3),
     96      diff: x => { var cbrt_x = x < 0 ? -Math.Pow(-x, 1.0 / 3) : Math.Pow(x, 1.0 / 3); return 1.0 / (3 * cbrt_x * cbrt_x); }
     97      );
     98
     99
     100
    94101    #endregion
    95102
     
    250257      }
    251258      if (node.Symbol is CubeRoot) {
    252         return AutoDiff.TermBuilder.Power(
    253           ConvertToAutoDiff(node.GetSubtree(0)), 1.0/3.0);
     259        return cbrt(ConvertToAutoDiff(node.GetSubtree(0)));
    254260      }
    255261      if (node.Symbol is Sine) {
  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs

    r17101 r17103  
    137137        } else if (node.Symbol is SquareRoot) {
    138138          FormatFunction(node, "Math.Sqrt", strBuilder);
     139        } else if (node.Symbol is Cube) {
     140          FormatPower(node, strBuilder, "3");
     141        } else if (node.Symbol is CubeRoot) {
     142          strBuilder.Append("Cbrt(");
     143          FormatRecursively(node.GetSubtree(0), strBuilder);
     144          strBuilder.Append(")");
    139145        } else if (node.Symbol is Power) {
    140146          FormatFunction(node, "Math.Pow", strBuilder);
    141147        } else if (node.Symbol is Root) {
    142148          FormatRoot(node, strBuilder);
     149        } else if (node.Symbol is Absolute) {
     150          FormatFunction(node, "Math.Abs", strBuilder);
     151        } else if (node.Symbol is AnalyticQuotient) {
     152          strBuilder.Append("(");
     153          FormatRecursively(node.GetSubtree(0), strBuilder);
     154          strBuilder.Append(" / Math.Sqrt(1 + Math.Pow(");
     155          FormatRecursively(node.GetSubtree(1), strBuilder);
     156          strBuilder.Append(" , 2) )");
    143157        } else {
    144158          throw new NotSupportedException("Formatting of symbol: " + node.Symbol + " not supported for C# symbolic expression tree formatter.");
     
    173187
    174188    private void FormatSquare(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
     189      FormatPower(node, strBuilder, "2");
     190    }
     191    private void FormatPower(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, string exponent) {
    175192      strBuilder.Append("Math.Pow(");
    176193      FormatRecursively(node.GetSubtree(0), strBuilder);
    177       strBuilder.Append(", 2)");
     194      strBuilder.Append($", {exponent})");
    178195    }
    179196
     
    237254      strBuilder.AppendLine("public static class Model {");
    238255      GenerateAverageSource(strBuilder);
     256      GenerateCbrtSource(strBuilder);
    239257      GenerateIfThenElseSource(strBuilder);
    240258      GenerateFactorSource(strBuilder);
     
    278296      strBuilder.AppendLine("private static double Average(params double[] values) {");
    279297      strBuilder.AppendLine("  return values.Average();");
     298      strBuilder.AppendLine("}");
     299    }
     300    private void GenerateCbrtSource(StringBuilder strBuilder) {
     301      strBuilder.AppendLine("private static double Cbrt(double x) {");
     302      strBuilder.AppendLine("  return x < 0 ? -Math.Pow(-x, 1.0 / 3.0) : Math.Pow(x, 1.0 / 3.0);");
    280303      strBuilder.AppendLine("}");
    281304    }
  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs

    r17101 r17103  
    119119        }
    120120        stringBuilder.Append(")");
     121      } else if (symbol is Absolute) {
     122        stringBuilder.Append($"ABS({FormatRecursively(node.GetSubtree(0))})");
     123      } else if (symbol is AnalyticQuotient) {
     124        stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) / SQRT(1 + POWER({FormatRecursively(node.GetSubtree(1))}, 2))");
    121125      } else if (symbol is Average) {
    122126        stringBuilder.Append("(1/(");
     
    137141        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    138142        stringBuilder.Append(")");
     143      } else if (symbol is Cube) {
     144        stringBuilder.Append($"POWER({FormatRecursively(node.GetSubtree(0))}, 3)");
     145      } else if (symbol is CubeRoot) {
     146        var arg_expr = FormatRecursively(node.GetSubtree(0));
     147        stringBuilder.Append($"IF({arg_expr} < 0, -POWER(-{arg_expr}, 1/3), POWER({arg_expr}, 1/3))");
    139148      } else if (symbol is Division) {
    140149        if (node.SubtreeCount == 1) {
  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs

    r17101 r17103  
    117117          strBuilder.Append(@" \cfrac{ ");
    118118        }
     119      } else if (node.Symbol is Absolute) {
     120        strBuilder.Append(@"\operatorname{abs} \left( ");
     121      } else if (node.Symbol is AnalyticQuotient) {
     122        strBuilder.Append(@" \frac { ");
    119123      } else if (node.Symbol is Average) {
    120124        // skip output of (1/1) if only one subtree
     
    131135      } else if (node.Symbol is SquareRoot) {
    132136        strBuilder.Append(@"\sqrt{");
     137      } else if (node.Symbol is Cube) {
     138        strBuilder.Append(@"\left(");
     139      } else if (node.Symbol is CubeRoot) {
     140        strBuilder.Append(@"\operatorname{cbrt}\left(");
    133141      } else if (node.Symbol is Sine) {
    134142        strBuilder.Append(@"\sin \left( ");
     
    289297        else
    290298          strBuilder.Append(@" }{ \cfrac{ ");
     299      } else if (node.Symbol is Absolute) {
     300        throw new InvalidOperationException();
     301      } else if (node.Symbol is AnalyticQuotient) {
     302        strBuilder.Append(@"}{\sqrt{1 + \left( ");
    291303      } else if (node.Symbol is Average) {
    292304        strBuilder.Append(@" + ");
     
    298310        throw new InvalidOperationException();
    299311      } else if (node.Symbol is SquareRoot) {
     312        throw new InvalidOperationException();
     313      } else if (node.Symbol is Cube) {
     314        throw new InvalidOperationException();
     315      } else if (node.Symbol is CubeRoot) {
    300316        throw new InvalidOperationException();
    301317      } else if (node.Symbol is Sine) {
     
    387403        for (int i = 2; i < node.SubtreeCount; i++)
    388404          strBuilder.Append(" } ");
     405      } else if (node.Symbol is Absolute) {
     406        strBuilder.Append(@" \right)");
     407      } else if (node.Symbol is AnalyticQuotient) {
     408        strBuilder.Append(@" \right)^2}}");
    389409      } else if (node.Symbol is Average) {
    390410        strBuilder.Append(@" \right) ");
     
    397417      } else if (node.Symbol is SquareRoot) {
    398418        strBuilder.Append(@"}");
     419      } else if (node.Symbol is Cube) {
     420        strBuilder.Append(@"\right)^3");
     421      } else if (node.Symbol is CubeRoot) {
     422        strBuilder.Append(@"\right)");
    399423      } else if (node.Symbol is Sine) {
    400424        strBuilder.Append(@" \right) ");
  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs

    r17101 r17103  
    126126        }
    127127        stringBuilder.Append(")");
     128      } else if (symbol is Absolute) {
     129        stringBuilder.Append($"abs({FormatRecursively(node.GetSubtree(0))})");
     130      } else if (symbol is AnalyticQuotient) {
     131        stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) / sqrt(1 + ({FormatRecursively(node.GetSubtree(1))}).^2)");
    128132      } else if (symbol is And) {
    129133        stringBuilder.Append("((");
     
    179183        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    180184        stringBuilder.Append(")");
     185      } else if (symbol is Cube) {
     186        stringBuilder.Append("(");
     187        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     188        stringBuilder.Append(").^3");
     189      } else if (symbol is CubeRoot) {
     190        stringBuilder.Append("NTHROOT(");
     191        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     192        stringBuilder.Append(", 3)");
    181193      } else if (symbol is GreaterThan) {
    182194        stringBuilder.Append("((");
  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs

    r17101 r17103  
    5656        if (node.Symbol is Addition) {
    5757          FormatFunction(node, "Plus", strBuilder);
     58        } else if (node.Symbol is Absolute) {
     59          FormatFunction(node, "Abs", strBuilder);
     60        } else if (node.Symbol is AnalyticQuotient) {
     61          strBuilder.Append("[");
     62          FormatRecursively(node.GetSubtree(0), strBuilder);
     63          strBuilder.Append("]/Sqrt[ 1 + Power[");
     64          FormatRecursively(node.GetSubtree(1), strBuilder);
     65          strBuilder.Append(", 2]]");
    5866        } else if (node.Symbol is Average) {
    5967          FormatAverage(node, strBuilder);
     
    104112        } else if (node.Symbol is SquareRoot) {
    105113          FormatFunction(node, "Sqrt", strBuilder);
     114        } else if (node.Symbol is Cube) {
     115          FormatPower(node, strBuilder, "3");
     116        } else if (node.Symbol is CubeRoot) {
     117          strBuilder.Append("CubeRoot[");
     118          FormatRecursively(node.GetSubtree(0), strBuilder);
     119          strBuilder.Append("]");
    106120        } else if (node.Symbol is Power) {
    107121          FormatFunction(node, "Power", strBuilder);
     
    205219
    206220    private void FormatSquare(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
     221      FormatPower(node, strBuilder, "2");
     222    }
     223
     224    private void FormatPower(ISymbolicExpressionTreeNode node, StringBuilder strBuilder, string exponent) {
    207225      strBuilder.Append("Power[");
    208226      FormatRecursively(node.GetSubtree(0), strBuilder);
    209       strBuilder.Append(", 2]");
     227      strBuilder.Append($", {exponent}]");
    210228    }
    211229
  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs

    r17101 r17103  
    2020#endregion
    2121
     22using System;
    2223using System.Globalization;
    2324using System.Text;
     
    6970        }
    7071        stringBuilder.Append(") ifTrue:[1] ifFalse:[-1]");
     72      } else if (symbol is Absolute) {
     73        stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) abs");
     74      } else if (symbol is AnalyticQuotient) {
     75        stringBuilder.Append($"({FormatRecursively(node.GetSubtree(0))}) / (1 + ({FormatPower(node.GetSubtree(1), "2")})) sqrt");
    7176      } else if (symbol is Average) {
    7277        stringBuilder.Append("(1/");
     
    8489        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    8590        stringBuilder.Append(" cos");
     91      } else if (symbol is Cube) {
     92        stringBuilder.Append(FormatPower(node.GetSubtree(0), "3"));
     93      } else if (symbol is CubeRoot) {
     94        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
     95        stringBuilder.Append(" cbrt");
    8696      } else if (symbol is Division) {
    8797        if (node.SubtreeCount == 1) {
     
    146156        stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
    147157        stringBuilder.Append(" sin");
     158      } else if (symbol is Square) {
     159        stringBuilder.Append(FormatPower(node.GetSubtree(0), "2"));
     160      } else if (symbol is SquareRoot) {
     161        stringBuilder.Append(FormatPower(node.GetSubtree(0), "(1/2)"));
    148162      } else if (symbol is Subtraction) {
    149163        if (node.SubtreeCount == 1) {
     
    184198    }
    185199
     200    private string FormatPower(ISymbolicExpressionTreeNode node, string exponent) {
     201      return $"(({FormatRecursively(node)}) log * {exponent}) exp ";
     202    }
     203
    186204    public override IDeepCloneable Clone(Cloner cloner) {
    187205      return new SymbolicDataAnalysisExpressionSmalltalkFormatter(this, cloner);
  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/BatchOperations.cs

    r17101 r17103  
    101101    public static void CubeRoot(double[] a, double[] b) {
    102102      for (int i = 0; i < BATCHSIZE; ++i)
    103         a[i] = Math.Pow(b[i], 1d / 3d);
     103        a[i] = b[i] < 0 ? -Math.Pow(-b[i], 1d / 3d) : Math.Pow(b[i], 1d / 3d);
    104104    }
    105105
  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionCompiledTreeInterpreter.cs

    r17101 r17103  
    247247        case OpCodes.CubeRoot: {
    248248            var arg = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
    249             return Expression.Power(arg, Expression.Constant(1.0 / 3.0));
     249            return Expression.Condition(Expression.LessThan(arg, Expression.Constant(0.0)),
     250              Expression.Negate(Expression.Power(Expression.Negate(arg), Expression.Constant(1.0 / 3.0))),
     251              Expression.Power(arg, Expression.Constant(1.0 / 3.0)));
    250252          }
    251253        case OpCodes.Root: {
     
    514516            var x1 = MakeExpr(node.GetSubtree(0), variableIndices, row, columns);
    515517            var x2 = MakeExpr(node.GetSubtree(1), variableIndices, row, columns);
    516             return Expression.Divide(x1, 
    517               Expression.Call(Sqrt, 
     518            return Expression.Divide(x1,
     519              Expression.Call(Sqrt,
    518520              Expression.Add(
    519521                Expression.Constant(1.0),
  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs

    r17101 r17103  
    336336        case OpCodes.CubeRoot: {
    337337            CompileInstructions(il, state, ds);
     338            var c1 = il.DefineLabel();
     339            var end = il.DefineLabel();
     340
     341            il.Emit(System.Reflection.Emit.OpCodes.Dup); // x
     342            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 0.0);
     343            il.Emit(System.Reflection.Emit.OpCodes.Clt); // x < 0?
     344            il.Emit(System.Reflection.Emit.OpCodes.Brfalse, c1);
     345            il.Emit(System.Reflection.Emit.OpCodes.Neg); // x = -x
    338346            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0 / 3.0);
    339347            il.Emit(System.Reflection.Emit.OpCodes.Call, power);
     348            il.Emit(System.Reflection.Emit.OpCodes.Neg); // -Math.Pow(-x, 1/3)
     349            il.Emit(System.Reflection.Emit.OpCodes.Br, end);
     350            il.MarkLabel(c1);
     351            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0 / 3.0);
     352            il.Emit(System.Reflection.Emit.OpCodes.Call, power);
     353            il.MarkLabel(end);
    340354            return;
    341355          }
     
    422436            il.Emit(System.Reflection.Emit.OpCodes.Mul); // x2*x2
    423437            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0);
    424             il.Emit(System.Reflection.Emit.OpCodes.Mul); // 1+x2*x2
     438            il.Emit(System.Reflection.Emit.OpCodes.Add); // 1+x2*x2
    425439            il.Emit(System.Reflection.Emit.OpCodes.Call, sqrt);
    426440            il.Emit(System.Reflection.Emit.OpCodes.Div);
  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs

    r17101 r17103  
    232232          }
    233233        case OpCodes.CubeRoot: {
    234             return Math.Pow(Evaluate(dataset, ref row, state), 1.0 / 3.0);
     234            var arg = Evaluate(dataset, ref row, state);
     235            return arg < 0 ? -Math.Pow(-arg, 1.0 / 3.0) : Math.Pow(arg, 1.0 / 3.0);
    235236          }
    236237        case OpCodes.Root: {
  • stable/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs

    r17101 r17103  
    245245          instr.value = Math.Sqrt(code[instr.childIndex].value);
    246246        } else if (instr.opCode == OpCodes.CubeRoot) {
    247           instr.value = Math.Pow(code[instr.childIndex].value, 1.0 / 3.0);
     247          var arg = code[instr.childIndex].value;
     248          instr.value = arg < 0 ? -Math.Pow(-arg, 1.0 / 3.0) : Math.Pow(arg, 1.0 / 3.0);
    248249        } else if (instr.opCode == OpCodes.Root) {
    249250          double x = code[instr.childIndex].value;
Note: See TracChangeset for help on using the changeset viewer.