- Timestamp:
- 05/07/19 15:38:16 (6 years ago)
- Location:
- branches/2994-AutoDiffForIntervals
- Files:
-
- 17 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2994-AutoDiffForIntervals
- Property svn:mergeinfo changed
-
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic
- Property svn:mergeinfo changed
/trunk/HeuristicLab.Problems.DataAnalysis.Symbolic merged: 16839,16858,16868,16905
- Property svn:mergeinfo changed
-
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/DerivativeCalculator.cs
r16739 r16911 143 143 var f = (ISymbolicExpressionTreeNode)branch.Clone(); 144 144 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) 146 146 } 147 147 if (branch.Symbol is Cube) { -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/LinearModelToTreeConverter.cs
r16565 r16911 38 38 double @const = 0) { 39 39 40 if (factorCoefficients.Length == 0 && coefficients.Length == 0) throw new ArgumentException(); 40 if (factorCoefficients.Length == 0 && coefficients.Length == 0 && @const==0) throw new ArgumentException(); 41 42 // Combine both trees 43 ISymbolicExpressionTreeNode add = (new Addition()).CreateTreeNode(); 41 44 42 45 // Create tree for double variables 43 ISymbolicExpressionTree tree = null;44 46 if (coefficients.Length > 0) { 45 tree = CreateTree(variableNames, new int[variableNames.Length], coefficients, @const); 46 if (factorCoefficients.Length == 0) return tree; 47 var varTree = CreateTree(variableNames, new int[variableNames.Length], coefficients); 48 foreach (var varNode in varTree.IterateNodesPrefix().OfType<VariableTreeNode>()) 49 add.AddSubtree(varNode); 47 50 } 48 51 49 52 // Create tree for string variables 50 ISymbolicExpressionTree factorTree = null;51 53 if (factorCoefficients.Length > 0) { 52 factorTree = CreateTree(factors, factorCoefficients, @const); 53 if (tree == null) return factorTree; 54 var factorTree = CreateTree(factors, factorCoefficients); 55 foreach (var binFactorNode in factorTree.IterateNodesPrefix().OfType<BinaryFactorVariableTreeNode>()) 56 add.AddSubtree(binFactorNode); 54 57 } 55 58 56 // Combine both trees 57 ISymbolicExpressionTreeNode add = tree.Root.GetSubtree(0).GetSubtree(0); 58 foreach (var binFactorNode in factorTree.IterateNodesPrefix().OfType<BinaryFactorVariableTreeNode>()) 59 add.InsertSubtree(add.SubtreeCount - 1, binFactorNode); 59 if (@const!=0.0) { 60 ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode(); 61 cNode.Value = @const; 62 add.AddSubtree(cNode); 63 } 64 65 ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode()); 66 ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode(); 67 tree.Root.AddSubtree(startNode); 68 startNode.AddSubtree(add); 60 69 return tree; 61 62 throw new ArgumentException();63 70 } 64 71 -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeToAutoDiffTermConverter.cs
r16656 r16911 92 92 ); 93 93 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 94 101 #endregion 95 102 … … 250 257 } 251 258 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))); 254 260 } 255 261 if (node.Symbol is Sine) { -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionCSharpFormatter.cs
r16829 r16911 140 140 FormatPower(node, strBuilder, "3"); 141 141 } else if (node.Symbol is CubeRoot) { 142 FormatPower(node, strBuilder, "1.0/3"); 142 strBuilder.Append("Cbrt("); 143 FormatRecursively(node.GetSubtree(0), strBuilder); 144 strBuilder.Append(")"); 143 145 } else if (node.Symbol is Power) { 144 146 FormatFunction(node, "Math.Pow", strBuilder); … … 252 254 strBuilder.AppendLine("public static class Model {"); 253 255 GenerateAverageSource(strBuilder); 256 GenerateCbrtSource(strBuilder); 254 257 GenerateIfThenElseSource(strBuilder); 255 258 GenerateFactorSource(strBuilder); … … 293 296 strBuilder.AppendLine("private static double Average(params double[] values) {"); 294 297 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);"); 295 303 strBuilder.AppendLine("}"); 296 304 } -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionExcelFormatter.cs
r16829 r16911 133 133 stringBuilder.Append(")"); 134 134 } 135 stringBuilder.Append(") ");135 stringBuilder.Append("))"); 136 136 } else if (symbol is Constant) { 137 137 ConstantTreeNode constantTreeNode = node as ConstantTreeNode; … … 144 144 stringBuilder.Append($"POWER({FormatRecursively(node.GetSubtree(0))}, 3)"); 145 145 } else if (symbol is CubeRoot) { 146 stringBuilder.Append($"POWER({FormatRecursively(node.GetSubtree(0))}, 1/3)"); 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))"); 147 148 } else if (symbol is Division) { 148 149 if (node.SubtreeCount == 1) { -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionLatexFormatter.cs
r16829 r16911 138 138 strBuilder.Append(@"\left("); 139 139 } else if (node.Symbol is CubeRoot) { 140 strBuilder.Append(@"\ left(");140 strBuilder.Append(@"\operatorname{cbrt}\left("); 141 141 } else if (node.Symbol is Sine) { 142 142 strBuilder.Append(@"\sin \left( "); … … 420 420 strBuilder.Append(@"\right)^3"); 421 421 } else if (node.Symbol is CubeRoot) { 422 strBuilder.Append(@"\right) ^\frac{1}{3}");422 strBuilder.Append(@"\right)"); 423 423 } else if (node.Symbol is Sine) { 424 424 strBuilder.Append(@" \right) "); -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMATLABFormatter.cs
r16829 r16911 188 188 stringBuilder.Append(").^3"); 189 189 } else if (symbol is CubeRoot) { 190 stringBuilder.Append(" (");191 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 192 stringBuilder.Append(" ).^(1/3)");190 stringBuilder.Append("NTHROOT("); 191 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 192 stringBuilder.Append(", 3)"); 193 193 } else if (symbol is GreaterThan) { 194 194 stringBuilder.Append("(("); -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionMathematicaFormatter.cs
r16829 r16911 115 115 FormatPower(node, strBuilder, "3"); 116 116 } else if (node.Symbol is CubeRoot) { 117 FormatPower(node, strBuilder, "1/3"); 117 strBuilder.Append("CubeRoot["); 118 FormatRecursively(node.GetSubtree(0), strBuilder); 119 strBuilder.Append("]"); 118 120 } else if (node.Symbol is Power) { 119 121 FormatFunction(node, "Power", strBuilder); -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Formatters/SymbolicDataAnalysisExpressionSmalltalkFormatter.cs
r16829 r16911 92 92 stringBuilder.Append(FormatPower(node.GetSubtree(0), "3")); 93 93 } else if (symbol is CubeRoot) { 94 stringBuilder.Append(FormatPower(node.GetSubtree(0), "(1/3)")); 94 stringBuilder.Append(FormatRecursively(node.GetSubtree(0))); 95 stringBuilder.Append(" cbrt"); 95 96 } else if (symbol is Division) { 96 97 if (node.SubtreeCount == 1) { -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/BatchOperations.cs
r16656 r16911 101 101 public static void CubeRoot(double[] a, double[] b) { 102 102 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); 104 104 } 105 105 -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionCompiledTreeInterpreter.cs
r16668 r16911 247 247 case OpCodes.CubeRoot: { 248 248 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))); 250 252 } 251 253 case OpCodes.Root: { … … 514 516 var x1 = MakeExpr(node.GetSubtree(0), variableIndices, row, columns); 515 517 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, 518 520 Expression.Add( 519 521 Expression.Constant(1.0), -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs
r16670 r16911 336 336 case OpCodes.CubeRoot: { 337 337 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 338 346 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0 / 3.0); 339 347 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); 340 354 return; 341 355 } -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeInterpreter.cs
r16670 r16911 232 232 } 233 233 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); 235 236 } 236 237 case OpCodes.Root: { -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeLinearInterpreter.cs
r16656 r16911 245 245 instr.value = Math.Sqrt(code[instr.childIndex].value); 246 246 } 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); 248 249 } else if (instr.opCode == OpCodes.Root) { 249 250 double x = code[instr.childIndex].value; -
branches/2994-AutoDiffForIntervals/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Selectors/DiversitySelector.cs
r16623 r16911 100 100 #region Events 101 101 private void RegisterParameterEventHandlers() { 102 SelectorParameter.ValueChanged += new EventHandler(SelectorParameter_ValueChanged); 103 CopySelected.ValueChanged += new EventHandler(CopySelected_ValueChanged); 102 SelectorParameter.ValueChanged += SelectorParameter_ValueChanged; 103 CopySelectedParameter.ValueChanged += CopySelectedParameter_ValueChanged; 104 CopySelected.ValueChanged += CopySelected_ValueChanged; 105 } 106 107 private void CopySelectedParameter_ValueChanged(object sender, EventArgs e) { 108 if (CopySelected.Value != true) { 109 CopySelected.Value = true; 110 } 111 CopySelected.ValueChanged += CopySelected_ValueChanged; 104 112 } 105 113
Note: See TracChangeset
for help on using the changeset viewer.