- Timestamp:
- 02/15/11 13:36:45 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/SymbolicSimplifier.cs
r5461 r5465 42 42 private Logarithm logSymbol = new Logarithm(); 43 43 private Exponential expSymbol = new Exponential(); 44 private Root rootSymbol = new Root(); 45 private Power powSymbol = new Power(); 44 46 private Sine sineSymbol = new Sine(); 45 47 private Cosine cosineSymbol = new Cosine(); … … 122 124 private bool IsExp(SymbolicExpressionTreeNode node) { 123 125 return node.Symbol is Exponential; 126 } 127 private bool IsRoot(SymbolicExpressionTreeNode node) { 128 return node.Symbol is Root; 129 } 130 private bool IsPower(SymbolicExpressionTreeNode node) { 131 return node.Symbol is Power; 124 132 } 125 133 // trigonometric … … 195 203 } else if (IsExp(original)) { 196 204 return SimplifyExp(original); 205 } else if (IsRoot(original)) { 206 return SimplifyRoot(original); 207 } else if (IsPower(original)) { 208 return SimplifyPower(original); 197 209 } else if (IsSine(original)) { 198 210 return SimplifySine(original); … … 347 359 return MakeLog(GetSimplifiedTree(original.SubTrees[0])); 348 360 } 349 361 private SymbolicExpressionTreeNode SimplifyRoot(SymbolicExpressionTreeNode original) { 362 return MakeRoot(GetSimplifiedTree(original.SubTrees[0]), GetSimplifiedTree(original.SubTrees[1])); 363 } 364 365 private SymbolicExpressionTreeNode SimplifyPower(SymbolicExpressionTreeNode original) { 366 return MakePower(GetSimplifiedTree(original.SubTrees[0]), GetSimplifiedTree(original.SubTrees[1])); 367 } 350 368 #endregion 351 352 353 369 354 370 #region low level tree restructuring … … 462 478 463 479 private SymbolicExpressionTreeNode MakeSine(SymbolicExpressionTreeNode node) { 464 // todo implement more transformation rules465 480 if (IsConstant(node)) { 466 481 var constT = node as ConstantTreeNode; … … 473 488 } 474 489 private SymbolicExpressionTreeNode MakeTangent(SymbolicExpressionTreeNode node) { 475 // todo implement more transformation rules476 490 if (IsConstant(node)) { 477 491 var constT = node as ConstantTreeNode; … … 484 498 } 485 499 private SymbolicExpressionTreeNode MakeCosine(SymbolicExpressionTreeNode node) { 486 // todo implement more transformation rules487 500 if (IsConstant(node)) { 488 501 var constT = node as ConstantTreeNode; … … 495 508 } 496 509 private SymbolicExpressionTreeNode MakeExp(SymbolicExpressionTreeNode node) { 497 // todo implement more transformation rules498 510 if (IsConstant(node)) { 499 511 var constT = node as ConstantTreeNode; … … 501 513 } else if (IsLog(node)) { 502 514 return node.SubTrees[0]; 515 } else if (IsAddition(node)) { 516 return node.SubTrees.Select(s => MakeExp(s)).Aggregate((s, t) => MakeProduct(s, t)); 517 } else if (IsSubtraction(node)) { 518 return node.SubTrees.Select(s => MakeExp(s)).Aggregate((s, t) => MakeProduct(s, Negate(t))); 503 519 } else { 504 520 var expNode = expSymbol.CreateTreeNode(); … … 508 524 } 509 525 private SymbolicExpressionTreeNode MakeLog(SymbolicExpressionTreeNode node) { 510 // todo implement more transformation rules511 526 if (IsConstant(node)) { 512 527 var constT = node as ConstantTreeNode; … … 526 541 logNode.AddSubTree(node); 527 542 return logNode; 543 } 544 } 545 private SymbolicExpressionTreeNode MakeRoot(SymbolicExpressionTreeNode a, SymbolicExpressionTreeNode b) { 546 if (IsConstant(a) && IsConstant(b)) { 547 var constA = a as ConstantTreeNode; 548 var constB = b as ConstantTreeNode; 549 return MakeConstant(Math.Pow(constA.Value, 1.0 / Math.Round(constB.Value))); 550 } else if (IsConstant(b)) { 551 var constB = b as ConstantTreeNode; 552 var constBValue = Math.Round(constB.Value); 553 if (constBValue.IsAlmost(1.0)) { 554 return a; 555 } else if (constBValue.IsAlmost(0.0)) { 556 return MakeConstant(1.0); 557 } else if (constBValue.IsAlmost(-1.0)) { 558 return MakeFraction(MakeConstant(1.0), a); 559 } else if (constBValue < 0) { 560 var rootNode = rootSymbol.CreateTreeNode(); 561 rootNode.AddSubTree(a); 562 rootNode.AddSubTree(MakeConstant(-1.0 * constBValue)); 563 return MakeFraction(MakeConstant(1.0), rootNode); 564 } else { 565 var rootNode = rootSymbol.CreateTreeNode(); 566 rootNode.AddSubTree(a); 567 rootNode.AddSubTree(MakeConstant(constBValue)); 568 return rootNode; 569 } 570 } else { 571 var rootNode = rootSymbol.CreateTreeNode(); 572 rootNode.AddSubTree(a); 573 rootNode.AddSubTree(b); 574 return rootNode; 575 } 576 } 577 private SymbolicExpressionTreeNode MakePower(SymbolicExpressionTreeNode a, SymbolicExpressionTreeNode b) { 578 if (IsConstant(a) && IsConstant(b)) { 579 var constA = a as ConstantTreeNode; 580 var constB = b as ConstantTreeNode; 581 return MakeConstant(Math.Pow(constA.Value, Math.Round(constB.Value))); 582 } else if (IsConstant(b)) { 583 var constB = b as ConstantTreeNode; 584 double exponent = Math.Round(constB.Value); 585 if (exponent.IsAlmost(0.0)) { 586 return MakeConstant(1.0); 587 } else if (exponent.IsAlmost(1.0)) { 588 return a; 589 } else if (exponent.IsAlmost(-1.0)) { 590 return MakeFraction(MakeConstant(1.0), a); 591 } else if (exponent < 0) { 592 var powNode = powSymbol.CreateTreeNode(); 593 powNode.AddSubTree(a); 594 powNode.AddSubTree(MakeConstant(-1.0 * exponent)); 595 return MakeFraction(MakeConstant(1.0), powNode); 596 } else { 597 var powNode = powSymbol.CreateTreeNode(); 598 powNode.AddSubTree(a); 599 powNode.AddSubTree(MakeConstant(exponent)); 600 return powNode; 601 } 602 } else { 603 var powNode = powSymbol.CreateTreeNode(); 604 powNode.AddSubTree(a); 605 powNode.AddSubTree(b); 606 return powNode; 528 607 } 529 608 }
Note: See TracChangeset
for help on using the changeset viewer.