[14843] | 1 | #region License Information
|
---|
| 2 |
|
---|
| 3 | /* HeuristicLab
|
---|
[17180] | 4 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[14843] | 5 | *
|
---|
| 6 | * This file is part of HeuristicLab.
|
---|
| 7 | *
|
---|
| 8 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 9 | * it under the terms of the GNU General Public License as published by
|
---|
| 10 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 11 | * (at your option) any later version.
|
---|
| 12 | *
|
---|
| 13 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 16 | * GNU General Public License for more details.
|
---|
| 17 | *
|
---|
| 18 | * You should have received a copy of the GNU General Public License
|
---|
| 19 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | #endregion
|
---|
| 23 |
|
---|
| 24 | using System;
|
---|
| 25 | using System.Collections.Generic;
|
---|
| 26 | using System.Linq;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
| 28 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// Simplifier for symbolic expressions
|
---|
| 33 | /// </summary>
|
---|
| 34 | public class TreeSimplifier {
|
---|
[14949] | 35 | private static readonly Addition addSymbol = new Addition();
|
---|
| 36 | private static readonly Multiplication mulSymbol = new Multiplication();
|
---|
| 37 | private static readonly Division divSymbol = new Division();
|
---|
[18146] | 38 | private static readonly Number numberSymbol = new Number();
|
---|
[16356] | 39 | private static readonly Absolute absSymbol = new Absolute();
|
---|
[14949] | 40 | private static readonly Logarithm logSymbol = new Logarithm();
|
---|
| 41 | private static readonly Exponential expSymbol = new Exponential();
|
---|
| 42 | private static readonly Root rootSymbol = new Root();
|
---|
| 43 | private static readonly Square sqrSymbol = new Square();
|
---|
| 44 | private static readonly SquareRoot sqrtSymbol = new SquareRoot();
|
---|
[16360] | 45 | private static readonly AnalyticQuotient aqSymbol = new AnalyticQuotient();
|
---|
[16356] | 46 | private static readonly Cube cubeSymbol = new Cube();
|
---|
| 47 | private static readonly CubeRoot cubeRootSymbol = new CubeRoot();
|
---|
[14949] | 48 | private static readonly Power powSymbol = new Power();
|
---|
| 49 | private static readonly Sine sineSymbol = new Sine();
|
---|
| 50 | private static readonly Cosine cosineSymbol = new Cosine();
|
---|
| 51 | private static readonly Tangent tanSymbol = new Tangent();
|
---|
| 52 | private static readonly IfThenElse ifThenElseSymbol = new IfThenElse();
|
---|
| 53 | private static readonly And andSymbol = new And();
|
---|
| 54 | private static readonly Or orSymbol = new Or();
|
---|
| 55 | private static readonly Not notSymbol = new Not();
|
---|
| 56 | private static readonly GreaterThan gtSymbol = new GreaterThan();
|
---|
| 57 | private static readonly LessThan ltSymbol = new LessThan();
|
---|
| 58 | private static readonly Integral integralSymbol = new Integral();
|
---|
| 59 | private static readonly LaggedVariable laggedVariableSymbol = new LaggedVariable();
|
---|
| 60 | private static readonly TimeLag timeLagSymbol = new TimeLag();
|
---|
[14843] | 61 |
|
---|
[14949] | 62 | [Obsolete("Use static method TreeSimplifier.Simplify instead")]
|
---|
| 63 | public TreeSimplifier() { }
|
---|
| 64 |
|
---|
| 65 | public static ISymbolicExpressionTree Simplify(ISymbolicExpressionTree originalTree) {
|
---|
[14843] | 66 | var clone = (ISymbolicExpressionTreeNode)originalTree.Root.Clone();
|
---|
| 67 | // macro expand (initially no argument trees)
|
---|
| 68 | var macroExpandedTree = MacroExpand(clone, clone.GetSubtree(0), new List<ISymbolicExpressionTreeNode>());
|
---|
| 69 | ISymbolicExpressionTreeNode rootNode = (new ProgramRootSymbol()).CreateTreeNode();
|
---|
| 70 | rootNode.AddSubtree(GetSimplifiedTree(macroExpandedTree));
|
---|
| 71 |
|
---|
| 72 | #if DEBUG
|
---|
| 73 | // check that each node is only referenced once
|
---|
| 74 | var nodes = rootNode.IterateNodesPrefix().ToArray();
|
---|
[14949] | 75 | foreach (var n in nodes) if (nodes.Count(ni => ni == n) > 1) throw new InvalidOperationException();
|
---|
[14843] | 76 | #endif
|
---|
| 77 | return new SymbolicExpressionTree(rootNode);
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | // the argumentTrees list contains already expanded trees used as arguments for invocations
|
---|
[14949] | 81 | private static ISymbolicExpressionTreeNode MacroExpand(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode node,
|
---|
[14843] | 82 | IList<ISymbolicExpressionTreeNode> argumentTrees) {
|
---|
[18146] | 83 | var subtrees = new List<ISymbolicExpressionTreeNode>(node.Subtrees);
|
---|
[14843] | 84 | while (node.SubtreeCount > 0) node.RemoveSubtree(0);
|
---|
| 85 | if (node.Symbol is InvokeFunction) {
|
---|
| 86 | var invokeSym = node.Symbol as InvokeFunction;
|
---|
| 87 | var defunNode = FindFunctionDefinition(root, invokeSym.FunctionName);
|
---|
| 88 | var macroExpandedArguments = new List<ISymbolicExpressionTreeNode>();
|
---|
| 89 | foreach (var subtree in subtrees) {
|
---|
| 90 | macroExpandedArguments.Add(MacroExpand(root, subtree, argumentTrees));
|
---|
| 91 | }
|
---|
| 92 | return MacroExpand(root, defunNode, macroExpandedArguments);
|
---|
| 93 | } else if (node.Symbol is Argument) {
|
---|
| 94 | var argSym = node.Symbol as Argument;
|
---|
| 95 | // return the correct argument sub-tree (already macro-expanded)
|
---|
| 96 | return (SymbolicExpressionTreeNode)argumentTrees[argSym.ArgumentIndex].Clone();
|
---|
| 97 | } else {
|
---|
| 98 | // recursive application
|
---|
| 99 | foreach (var subtree in subtrees) {
|
---|
| 100 | node.AddSubtree(MacroExpand(root, subtree, argumentTrees));
|
---|
| 101 | }
|
---|
| 102 | return node;
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
[14949] | 106 | private static ISymbolicExpressionTreeNode FindFunctionDefinition(ISymbolicExpressionTreeNode root, string functionName) {
|
---|
[14843] | 107 | foreach (var subtree in root.Subtrees.OfType<DefunTreeNode>()) {
|
---|
| 108 | if (subtree.FunctionName == functionName) return subtree.GetSubtree(0);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | throw new ArgumentException("Definition of function " + functionName + " not found.");
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | #region symbol predicates
|
---|
| 115 |
|
---|
| 116 | // arithmetic
|
---|
[14949] | 117 | private static bool IsDivision(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 118 | return node.Symbol is Division;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
[14949] | 121 | private static bool IsMultiplication(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 122 | return node.Symbol is Multiplication;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
[14949] | 125 | private static bool IsSubtraction(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 126 | return node.Symbol is Subtraction;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[14949] | 129 | private static bool IsAddition(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 130 | return node.Symbol is Addition;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[14949] | 133 | private static bool IsAverage(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 134 | return node.Symbol is Average;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[16356] | 137 | private static bool IsAbsolute(ISymbolicExpressionTreeNode node) {
|
---|
| 138 | return node.Symbol is Absolute;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[14843] | 141 | // exponential
|
---|
[14949] | 142 | private static bool IsLog(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 143 | return node.Symbol is Logarithm;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[14949] | 146 | private static bool IsExp(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 147 | return node.Symbol is Exponential;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[14949] | 150 | private static bool IsRoot(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 151 | return node.Symbol is Root;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[14949] | 154 | private static bool IsSquare(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 155 | return node.Symbol is Square;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[14949] | 158 | private static bool IsSquareRoot(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 159 | return node.Symbol is SquareRoot;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[16356] | 162 | private static bool IsCube(ISymbolicExpressionTreeNode node) {
|
---|
| 163 | return node.Symbol is Cube;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | private static bool IsCubeRoot(ISymbolicExpressionTreeNode node) {
|
---|
| 167 | return node.Symbol is CubeRoot;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[14949] | 170 | private static bool IsPower(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 171 | return node.Symbol is Power;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | // trigonometric
|
---|
[14949] | 175 | private static bool IsSine(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 176 | return node.Symbol is Sine;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[14949] | 179 | private static bool IsCosine(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 180 | return node.Symbol is Cosine;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[14949] | 183 | private static bool IsTangent(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 184 | return node.Symbol is Tangent;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[16356] | 187 | private static bool IsAnalyticalQuotient(ISymbolicExpressionTreeNode node) {
|
---|
[16360] | 188 | return node.Symbol is AnalyticQuotient;
|
---|
[16356] | 189 | }
|
---|
| 190 |
|
---|
[14843] | 191 | // boolean
|
---|
[14949] | 192 | private static bool IsIfThenElse(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 193 | return node.Symbol is IfThenElse;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[14949] | 196 | private static bool IsAnd(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 197 | return node.Symbol is And;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[14949] | 200 | private static bool IsOr(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 201 | return node.Symbol is Or;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
[14949] | 204 | private static bool IsNot(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 205 | return node.Symbol is Not;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | // comparison
|
---|
[14949] | 209 | private static bool IsGreaterThan(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 210 | return node.Symbol is GreaterThan;
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[14949] | 213 | private static bool IsLessThan(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 214 | return node.Symbol is LessThan;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[14949] | 217 | private static bool IsBoolean(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 218 | return
|
---|
| 219 | node.Symbol is GreaterThan ||
|
---|
| 220 | node.Symbol is LessThan ||
|
---|
| 221 | node.Symbol is And ||
|
---|
| 222 | node.Symbol is Or;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | // terminals
|
---|
[14949] | 226 | private static bool IsVariable(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 227 | return node.Symbol is Variable;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[14949] | 230 | private static bool IsVariableBase(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 231 | return node is VariableTreeNodeBase;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[14949] | 234 | private static bool IsFactor(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 235 | return node is FactorVariableTreeNode;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[14949] | 238 | private static bool IsBinFactor(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 239 | return node is BinaryFactorVariableTreeNode;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
[18146] | 242 | private static bool IsNumber(ISymbolicExpressionTreeNode node) {
|
---|
| 243 | return node.Symbol is Number;
|
---|
| 244 | }
|
---|
[14949] | 245 | private static bool IsConstant(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 246 | return node.Symbol is Constant;
|
---|
| 247 | }
|
---|
[18146] | 248 | private static bool IsConstantOrNumber(ISymbolicExpressionTreeNode node) {
|
---|
| 249 | return node is INumericTreeNode;
|
---|
| 250 | }
|
---|
[14843] | 251 |
|
---|
[14949] | 252 | private static bool IsTimeLag(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 253 | return node.Symbol is TimeLag;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[14949] | 256 | private static bool IsIntegral(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 257 | return node.Symbol is Integral;
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | #endregion
|
---|
| 261 |
|
---|
| 262 | /// <summary>
|
---|
| 263 | /// Creates a new simplified tree
|
---|
| 264 | /// </summary>
|
---|
| 265 | /// <param name="original"></param>
|
---|
| 266 | /// <returns></returns>
|
---|
[14949] | 267 | public static ISymbolicExpressionTreeNode GetSimplifiedTree(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 268 | if (IsConstantOrNumber(original) || IsVariableBase(original)) {
|
---|
[14843] | 269 | return (ISymbolicExpressionTreeNode)original.Clone();
|
---|
[16356] | 270 | } else if (IsAbsolute(original)) {
|
---|
| 271 | return SimplifyAbsolute(original);
|
---|
[14843] | 272 | } else if (IsAddition(original)) {
|
---|
| 273 | return SimplifyAddition(original);
|
---|
| 274 | } else if (IsSubtraction(original)) {
|
---|
| 275 | return SimplifySubtraction(original);
|
---|
| 276 | } else if (IsMultiplication(original)) {
|
---|
| 277 | return SimplifyMultiplication(original);
|
---|
| 278 | } else if (IsDivision(original)) {
|
---|
| 279 | return SimplifyDivision(original);
|
---|
| 280 | } else if (IsAverage(original)) {
|
---|
| 281 | return SimplifyAverage(original);
|
---|
| 282 | } else if (IsLog(original)) {
|
---|
| 283 | return SimplifyLog(original);
|
---|
| 284 | } else if (IsExp(original)) {
|
---|
| 285 | return SimplifyExp(original);
|
---|
| 286 | } else if (IsSquare(original)) {
|
---|
| 287 | return SimplifySquare(original);
|
---|
| 288 | } else if (IsSquareRoot(original)) {
|
---|
| 289 | return SimplifySquareRoot(original);
|
---|
[16356] | 290 | } else if (IsCube(original)) {
|
---|
| 291 | return SimplifyCube(original);
|
---|
| 292 | } else if (IsCubeRoot(original)) {
|
---|
| 293 | return SimplifyCubeRoot(original);
|
---|
[14843] | 294 | } else if (IsPower(original)) {
|
---|
| 295 | return SimplifyPower(original);
|
---|
| 296 | } else if (IsRoot(original)) {
|
---|
| 297 | return SimplifyRoot(original);
|
---|
| 298 | } else if (IsSine(original)) {
|
---|
| 299 | return SimplifySine(original);
|
---|
| 300 | } else if (IsCosine(original)) {
|
---|
| 301 | return SimplifyCosine(original);
|
---|
| 302 | } else if (IsTangent(original)) {
|
---|
| 303 | return SimplifyTangent(original);
|
---|
[16356] | 304 | } else if (IsAnalyticalQuotient(original)) {
|
---|
| 305 | return SimplifyAnalyticalQuotient(original);
|
---|
[14843] | 306 | } else if (IsIfThenElse(original)) {
|
---|
| 307 | return SimplifyIfThenElse(original);
|
---|
| 308 | } else if (IsGreaterThan(original)) {
|
---|
| 309 | return SimplifyGreaterThan(original);
|
---|
| 310 | } else if (IsLessThan(original)) {
|
---|
| 311 | return SimplifyLessThan(original);
|
---|
| 312 | } else if (IsAnd(original)) {
|
---|
| 313 | return SimplifyAnd(original);
|
---|
| 314 | } else if (IsOr(original)) {
|
---|
| 315 | return SimplifyOr(original);
|
---|
| 316 | } else if (IsNot(original)) {
|
---|
| 317 | return SimplifyNot(original);
|
---|
| 318 | } else if (IsTimeLag(original)) {
|
---|
| 319 | return SimplifyTimeLag(original);
|
---|
| 320 | } else if (IsIntegral(original)) {
|
---|
| 321 | return SimplifyIntegral(original);
|
---|
| 322 | } else {
|
---|
| 323 | return SimplifyAny(original);
|
---|
| 324 | }
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | #region specific simplification routines
|
---|
| 328 |
|
---|
[14949] | 329 | private static ISymbolicExpressionTreeNode SimplifyAny(ISymbolicExpressionTreeNode original) {
|
---|
[14843] | 330 | // can't simplify this function but simplify all subtrees
|
---|
[18146] | 331 | var subtrees = new List<ISymbolicExpressionTreeNode>(original.Subtrees);
|
---|
[14843] | 332 | while (original.Subtrees.Count() > 0) original.RemoveSubtree(0);
|
---|
| 333 | var clone = (SymbolicExpressionTreeNode)original.Clone();
|
---|
[18146] | 334 | var simplifiedSubtrees = new List<ISymbolicExpressionTreeNode>();
|
---|
[14843] | 335 | foreach (var subtree in subtrees) {
|
---|
| 336 | simplifiedSubtrees.Add(GetSimplifiedTree(subtree));
|
---|
| 337 | original.AddSubtree(subtree);
|
---|
| 338 | }
|
---|
| 339 | foreach (var simplifiedSubtree in simplifiedSubtrees) {
|
---|
| 340 | clone.AddSubtree(simplifiedSubtree);
|
---|
| 341 | }
|
---|
[18146] | 342 | if (simplifiedSubtrees.TrueForAll(IsNumber)) {
|
---|
| 343 | FoldNumbers(clone);
|
---|
[14843] | 344 | }
|
---|
| 345 | return clone;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
[18146] | 348 | private static ISymbolicExpressionTreeNode FoldNumbers(ISymbolicExpressionTreeNode original) {
|
---|
| 349 | // TODO not implemented
|
---|
[14843] | 350 | return original;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
[14949] | 353 | private static ISymbolicExpressionTreeNode SimplifyAverage(ISymbolicExpressionTreeNode original) {
|
---|
[14843] | 354 | if (original.Subtrees.Count() == 1) {
|
---|
| 355 | return GetSimplifiedTree(original.GetSubtree(0));
|
---|
| 356 | } else {
|
---|
| 357 | // simplify expressions x0..xn
|
---|
| 358 | // make sum(x0..xn) / n
|
---|
| 359 | var sum = original.Subtrees
|
---|
| 360 | .Select(GetSimplifiedTree)
|
---|
[18146] | 361 | .Aggregate(Sum);
|
---|
| 362 | return Fraction(sum, Number(original.Subtrees.Count()));
|
---|
[14843] | 363 | }
|
---|
| 364 | }
|
---|
| 365 |
|
---|
[14949] | 366 | private static ISymbolicExpressionTreeNode SimplifyDivision(ISymbolicExpressionTreeNode original) {
|
---|
[14843] | 367 | if (original.Subtrees.Count() == 1) {
|
---|
| 368 | return Invert(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
| 369 | } else {
|
---|
| 370 | // simplify expressions x0..xn
|
---|
| 371 | // make multiplication (x0 * 1/(x1 * x1 * .. * xn))
|
---|
| 372 | var first = original.GetSubtree(0);
|
---|
| 373 | var second = original.GetSubtree(1);
|
---|
| 374 | var remaining = original.Subtrees.Skip(2);
|
---|
| 375 | return
|
---|
[18146] | 376 | Product(GetSimplifiedTree(first),
|
---|
| 377 | Invert(remaining.Aggregate(GetSimplifiedTree(second), (a, b) => Product(a, GetSimplifiedTree(b)))));
|
---|
[14843] | 378 | }
|
---|
| 379 | }
|
---|
| 380 |
|
---|
[14949] | 381 | private static ISymbolicExpressionTreeNode SimplifyMultiplication(ISymbolicExpressionTreeNode original) {
|
---|
[14843] | 382 | if (original.Subtrees.Count() == 1) {
|
---|
| 383 | return GetSimplifiedTree(original.GetSubtree(0));
|
---|
| 384 | } else {
|
---|
| 385 | return original.Subtrees
|
---|
| 386 | .Select(GetSimplifiedTree)
|
---|
[18146] | 387 | .Aggregate(Product);
|
---|
[14843] | 388 | }
|
---|
| 389 | }
|
---|
| 390 |
|
---|
[14949] | 391 | private static ISymbolicExpressionTreeNode SimplifySubtraction(ISymbolicExpressionTreeNode original) {
|
---|
[14843] | 392 | if (original.Subtrees.Count() == 1) {
|
---|
| 393 | return Negate(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
| 394 | } else {
|
---|
| 395 | // simplify expressions x0..xn
|
---|
| 396 | // make addition (x0,-x1..-xn)
|
---|
| 397 | var first = original.Subtrees.First();
|
---|
| 398 | var remaining = original.Subtrees.Skip(1);
|
---|
[18146] | 399 | return remaining.Aggregate(GetSimplifiedTree(first), (a, b) => Sum(a, Negate(GetSimplifiedTree(b))));
|
---|
[14843] | 400 | }
|
---|
| 401 | }
|
---|
| 402 |
|
---|
[14949] | 403 | private static ISymbolicExpressionTreeNode SimplifyAddition(ISymbolicExpressionTreeNode original) {
|
---|
[14843] | 404 | if (original.Subtrees.Count() == 1) {
|
---|
| 405 | return GetSimplifiedTree(original.GetSubtree(0));
|
---|
| 406 | } else {
|
---|
| 407 | // simplify expression x0..xn
|
---|
| 408 | // make addition (x0..xn)
|
---|
| 409 | return original.Subtrees
|
---|
| 410 | .Select(GetSimplifiedTree)
|
---|
[18146] | 411 | .Aggregate(Sum);
|
---|
[14843] | 412 | }
|
---|
| 413 | }
|
---|
| 414 |
|
---|
[16356] | 415 | private static ISymbolicExpressionTreeNode SimplifyAbsolute(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 416 | return Abs(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
[16356] | 417 | }
|
---|
| 418 |
|
---|
[14949] | 419 | private static ISymbolicExpressionTreeNode SimplifyNot(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 420 | return Not(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
[14843] | 421 | }
|
---|
| 422 |
|
---|
[14949] | 423 | private static ISymbolicExpressionTreeNode SimplifyOr(ISymbolicExpressionTreeNode original) {
|
---|
[14843] | 424 | return original.Subtrees
|
---|
| 425 | .Select(GetSimplifiedTree)
|
---|
[18146] | 426 | .Aggregate(Or);
|
---|
[14843] | 427 | }
|
---|
| 428 |
|
---|
[14949] | 429 | private static ISymbolicExpressionTreeNode SimplifyAnd(ISymbolicExpressionTreeNode original) {
|
---|
[14843] | 430 | return original.Subtrees
|
---|
| 431 | .Select(GetSimplifiedTree)
|
---|
[18146] | 432 | .Aggregate(And);
|
---|
[14843] | 433 | }
|
---|
| 434 |
|
---|
[14949] | 435 | private static ISymbolicExpressionTreeNode SimplifyLessThan(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 436 | return LessThan(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
|
---|
[14843] | 437 | }
|
---|
| 438 |
|
---|
[14949] | 439 | private static ISymbolicExpressionTreeNode SimplifyGreaterThan(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 440 | return GreaterThan(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
|
---|
[14843] | 441 | }
|
---|
| 442 |
|
---|
[14949] | 443 | private static ISymbolicExpressionTreeNode SimplifyIfThenElse(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 444 | return IfThenElse(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)),
|
---|
[14843] | 445 | GetSimplifiedTree(original.GetSubtree(2)));
|
---|
| 446 | }
|
---|
| 447 |
|
---|
[14949] | 448 | private static ISymbolicExpressionTreeNode SimplifyTangent(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 449 | return Tangent(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
[14843] | 450 | }
|
---|
| 451 |
|
---|
[14949] | 452 | private static ISymbolicExpressionTreeNode SimplifyCosine(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 453 | return Cosine(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
[14843] | 454 | }
|
---|
| 455 |
|
---|
[14949] | 456 | private static ISymbolicExpressionTreeNode SimplifySine(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 457 | return Sine(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
[14843] | 458 | }
|
---|
| 459 |
|
---|
[14949] | 460 | private static ISymbolicExpressionTreeNode SimplifyExp(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 461 | return Exp(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
[14843] | 462 | }
|
---|
| 463 |
|
---|
[14949] | 464 | private static ISymbolicExpressionTreeNode SimplifySquare(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 465 | return Square(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
[14843] | 466 | }
|
---|
| 467 |
|
---|
[14949] | 468 | private static ISymbolicExpressionTreeNode SimplifySquareRoot(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 469 | return SquareRoot(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
[14843] | 470 | }
|
---|
[16356] | 471 | private static ISymbolicExpressionTreeNode SimplifyCube(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 472 | return Cube(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
[16356] | 473 | }
|
---|
[14843] | 474 |
|
---|
[16356] | 475 | private static ISymbolicExpressionTreeNode SimplifyCubeRoot(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 476 | return CubeRoot(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
[16356] | 477 | }
|
---|
| 478 |
|
---|
[14949] | 479 | private static ISymbolicExpressionTreeNode SimplifyLog(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 480 | return Log(GetSimplifiedTree(original.GetSubtree(0)));
|
---|
[14843] | 481 | }
|
---|
| 482 |
|
---|
[14949] | 483 | private static ISymbolicExpressionTreeNode SimplifyRoot(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 484 | return Root(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
|
---|
[14843] | 485 | }
|
---|
| 486 |
|
---|
[14949] | 487 | private static ISymbolicExpressionTreeNode SimplifyPower(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 488 | return Power(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
|
---|
[14843] | 489 | }
|
---|
| 490 |
|
---|
[16356] | 491 | private static ISymbolicExpressionTreeNode SimplifyAnalyticalQuotient(ISymbolicExpressionTreeNode original) {
|
---|
[18146] | 492 | return AQ(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)));
|
---|
[16356] | 493 | }
|
---|
| 494 |
|
---|
[14949] | 495 | private static ISymbolicExpressionTreeNode SimplifyTimeLag(ISymbolicExpressionTreeNode original) {
|
---|
[14843] | 496 | var laggedTreeNode = original as ILaggedTreeNode;
|
---|
| 497 | var simplifiedSubtree = GetSimplifiedTree(original.GetSubtree(0));
|
---|
| 498 | if (!ContainsVariableCondition(simplifiedSubtree)) {
|
---|
| 499 | return AddLagToDynamicNodes(simplifiedSubtree, laggedTreeNode.Lag);
|
---|
| 500 | } else {
|
---|
[18146] | 501 | return Lag(simplifiedSubtree, laggedTreeNode.Lag);
|
---|
[14843] | 502 | }
|
---|
| 503 | }
|
---|
| 504 |
|
---|
[14949] | 505 | private static ISymbolicExpressionTreeNode SimplifyIntegral(ISymbolicExpressionTreeNode original) {
|
---|
[14843] | 506 | var laggedTreeNode = original as ILaggedTreeNode;
|
---|
| 507 | var simplifiedSubtree = GetSimplifiedTree(original.GetSubtree(0));
|
---|
[18146] | 508 | if (IsConstantOrNumber(simplifiedSubtree)) {
|
---|
| 509 | return GetSimplifiedTree(Product(simplifiedSubtree, Number(-laggedTreeNode.Lag)));
|
---|
[14843] | 510 | } else {
|
---|
[18146] | 511 | return Integral(simplifiedSubtree, laggedTreeNode.Lag);
|
---|
[14843] | 512 | }
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 | #endregion
|
---|
| 516 |
|
---|
| 517 | #region low level tree restructuring
|
---|
| 518 |
|
---|
[18146] | 519 | private static ISymbolicExpressionTreeNode Lag(ISymbolicExpressionTreeNode subtree, int lag) {
|
---|
[14843] | 520 | if (lag == 0) return subtree;
|
---|
[18146] | 521 | if (IsConstantOrNumber(subtree)) return subtree;
|
---|
[14843] | 522 | var lagNode = (LaggedTreeNode)timeLagSymbol.CreateTreeNode();
|
---|
| 523 | lagNode.Lag = lag;
|
---|
| 524 | lagNode.AddSubtree(subtree);
|
---|
| 525 | return lagNode;
|
---|
| 526 | }
|
---|
| 527 |
|
---|
[18146] | 528 | private static ISymbolicExpressionTreeNode Integral(ISymbolicExpressionTreeNode subtree, int lag) {
|
---|
[14843] | 529 | if (lag == 0) return subtree;
|
---|
| 530 | else if (lag == -1 || lag == 1) {
|
---|
[18146] | 531 | return Sum(subtree, AddLagToDynamicNodes((ISymbolicExpressionTreeNode)subtree.Clone(), lag));
|
---|
[14843] | 532 | } else {
|
---|
| 533 | var node = (LaggedTreeNode)integralSymbol.CreateTreeNode();
|
---|
| 534 | node.Lag = lag;
|
---|
| 535 | node.AddSubtree(subtree);
|
---|
| 536 | return node;
|
---|
| 537 | }
|
---|
| 538 | }
|
---|
| 539 |
|
---|
[18146] | 540 | private static ISymbolicExpressionTreeNode Not(ISymbolicExpressionTreeNode t) {
|
---|
| 541 | if (IsNumber(t)) {
|
---|
| 542 | var numNode = t as NumberTreeNode;
|
---|
| 543 | if (numNode.Value > 0) return Number(-1.0);
|
---|
| 544 | else return Number(1.0);
|
---|
[14843] | 545 | } else if (IsNot(t)) {
|
---|
| 546 | return t.GetSubtree(0);
|
---|
| 547 | } else if (!IsBoolean(t)) {
|
---|
| 548 | var gtNode = gtSymbol.CreateTreeNode();
|
---|
| 549 | gtNode.AddSubtree(t);
|
---|
[18146] | 550 | gtNode.AddSubtree(Number(0.0));
|
---|
[14843] | 551 | var notNode = notSymbol.CreateTreeNode();
|
---|
| 552 | notNode.AddSubtree(gtNode);
|
---|
| 553 | return notNode;
|
---|
| 554 | } else {
|
---|
| 555 | var notNode = notSymbol.CreateTreeNode();
|
---|
| 556 | notNode.AddSubtree(t);
|
---|
| 557 | return notNode;
|
---|
| 558 | }
|
---|
| 559 | }
|
---|
| 560 |
|
---|
[18146] | 561 | private static ISymbolicExpressionTreeNode Or(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
| 562 | if (IsNumber(a) && IsNumber(b)) {
|
---|
| 563 | var aNode = a as NumberTreeNode;
|
---|
| 564 | var bNode = b as NumberTreeNode;
|
---|
| 565 | if (aNode.Value > 0.0 || bNode.Value > 0.0) {
|
---|
| 566 | return Number(1.0);
|
---|
[14843] | 567 | } else {
|
---|
[18146] | 568 | return Number(-1.0);
|
---|
[14843] | 569 | }
|
---|
[18146] | 570 | } else if (IsNumber(a)) {
|
---|
| 571 | return Or(b, a);
|
---|
| 572 | } else if (IsNumber(b)) {
|
---|
| 573 | var bNode = b as NumberTreeNode;
|
---|
| 574 | if (bNode.Value > 0.0) {
|
---|
[14843] | 575 | // boolean expression is necessarily true
|
---|
[18146] | 576 | return Number(1.0);
|
---|
[14843] | 577 | } else {
|
---|
[18146] | 578 | // the value has no effect on the result of the boolean condition so we can drop the number
|
---|
[14843] | 579 | var orNode = orSymbol.CreateTreeNode();
|
---|
| 580 | orNode.AddSubtree(a);
|
---|
| 581 | return orNode;
|
---|
| 582 | }
|
---|
| 583 | } else {
|
---|
| 584 | var orNode = orSymbol.CreateTreeNode();
|
---|
| 585 | orNode.AddSubtree(a);
|
---|
| 586 | orNode.AddSubtree(b);
|
---|
| 587 | return orNode;
|
---|
| 588 | }
|
---|
| 589 | }
|
---|
| 590 |
|
---|
[18146] | 591 | private static ISymbolicExpressionTreeNode And(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
| 592 | if (IsNumber(a) && IsNumber(b)) {
|
---|
| 593 | var aNode = a as NumberTreeNode;
|
---|
| 594 | var bNode = b as NumberTreeNode;
|
---|
| 595 | if (aNode.Value > 0.0 && bNode.Value > 0.0) {
|
---|
| 596 | return Number(1.0);
|
---|
[14843] | 597 | } else {
|
---|
[18146] | 598 | return Number(-1.0);
|
---|
[14843] | 599 | }
|
---|
[18146] | 600 | } else if (IsNumber(a)) {
|
---|
| 601 | return And(b, a);
|
---|
| 602 | } else if (IsNumber(b)) {
|
---|
| 603 | var bNode = b as NumberTreeNode;
|
---|
| 604 | if (bNode.Value > 0.0) {
|
---|
| 605 | // the value has no effect on the result of the boolean condition so we can drop the number
|
---|
[14843] | 606 | var andNode = andSymbol.CreateTreeNode();
|
---|
| 607 | andNode.AddSubtree(a);
|
---|
| 608 | return andNode;
|
---|
| 609 | } else {
|
---|
| 610 | // boolean expression is necessarily false
|
---|
[18146] | 611 | return Number(-1.0);
|
---|
[14843] | 612 | }
|
---|
| 613 | } else {
|
---|
| 614 | var andNode = andSymbol.CreateTreeNode();
|
---|
| 615 | andNode.AddSubtree(a);
|
---|
| 616 | andNode.AddSubtree(b);
|
---|
| 617 | return andNode;
|
---|
| 618 | }
|
---|
| 619 | }
|
---|
| 620 |
|
---|
[18146] | 621 | private static ISymbolicExpressionTreeNode LessThan(ISymbolicExpressionTreeNode leftSide,
|
---|
[14843] | 622 | ISymbolicExpressionTreeNode rightSide) {
|
---|
[18146] | 623 | if (IsNumber(leftSide) && IsNumber(rightSide)) {
|
---|
| 624 | var lsNode = leftSide as NumberTreeNode;
|
---|
| 625 | var rsNode = rightSide as NumberTreeNode;
|
---|
| 626 | if (lsNode.Value < rsNode.Value) return Number(1.0);
|
---|
| 627 | else return Number(-1.0);
|
---|
[14843] | 628 | } else {
|
---|
| 629 | var ltNode = ltSymbol.CreateTreeNode();
|
---|
| 630 | ltNode.AddSubtree(leftSide);
|
---|
| 631 | ltNode.AddSubtree(rightSide);
|
---|
| 632 | return ltNode;
|
---|
| 633 | }
|
---|
| 634 | }
|
---|
| 635 |
|
---|
[18146] | 636 | private static ISymbolicExpressionTreeNode GreaterThan(ISymbolicExpressionTreeNode leftSide,
|
---|
[14843] | 637 | ISymbolicExpressionTreeNode rightSide) {
|
---|
[18146] | 638 | if (IsNumber(leftSide) && IsNumber(rightSide)) {
|
---|
| 639 | var lsNode = leftSide as NumberTreeNode;
|
---|
| 640 | var rsNode = rightSide as NumberTreeNode;
|
---|
| 641 | if (lsNode.Value > rsNode.Value) return Number(1.0);
|
---|
| 642 | else return Number(-1.0);
|
---|
[14843] | 643 | } else {
|
---|
| 644 | var gtNode = gtSymbol.CreateTreeNode();
|
---|
| 645 | gtNode.AddSubtree(leftSide);
|
---|
| 646 | gtNode.AddSubtree(rightSide);
|
---|
| 647 | return gtNode;
|
---|
| 648 | }
|
---|
| 649 | }
|
---|
| 650 |
|
---|
[18146] | 651 | private static ISymbolicExpressionTreeNode IfThenElse(ISymbolicExpressionTreeNode condition,
|
---|
[14843] | 652 | ISymbolicExpressionTreeNode trueBranch, ISymbolicExpressionTreeNode falseBranch) {
|
---|
[18146] | 653 | if (IsConstantOrNumber(condition)) {
|
---|
| 654 | var condNode = condition as INumericTreeNode;
|
---|
| 655 | if (condNode.Value > 0.0) return trueBranch;
|
---|
[14843] | 656 | else return falseBranch;
|
---|
| 657 | } else {
|
---|
| 658 | var ifNode = ifThenElseSymbol.CreateTreeNode();
|
---|
| 659 | if (IsBoolean(condition)) {
|
---|
| 660 | ifNode.AddSubtree(condition);
|
---|
| 661 | } else {
|
---|
| 662 | var gtNode = gtSymbol.CreateTreeNode();
|
---|
| 663 | gtNode.AddSubtree(condition);
|
---|
[18146] | 664 | gtNode.AddSubtree(Number(0.0));
|
---|
[14843] | 665 | ifNode.AddSubtree(gtNode);
|
---|
| 666 | }
|
---|
| 667 | ifNode.AddSubtree(trueBranch);
|
---|
| 668 | ifNode.AddSubtree(falseBranch);
|
---|
| 669 | return ifNode;
|
---|
| 670 | }
|
---|
| 671 | }
|
---|
| 672 |
|
---|
[18146] | 673 | private static ISymbolicExpressionTreeNode Sine(ISymbolicExpressionTreeNode node) {
|
---|
| 674 | if (IsNumber(node)) {
|
---|
| 675 | var numNode = node as NumberTreeNode;
|
---|
| 676 | return Number(Math.Sin(numNode.Value));
|
---|
[14843] | 677 | } else if (IsFactor(node)) {
|
---|
| 678 | var factor = node as FactorVariableTreeNode;
|
---|
[18146] | 679 | return Factor(factor.Symbol, factor.VariableName, factor.Weights.Select(Math.Sin));
|
---|
[14843] | 680 | } else if (IsBinFactor(node)) {
|
---|
| 681 | var binFactor = node as BinaryFactorVariableTreeNode;
|
---|
[18146] | 682 | return BinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Sin(binFactor.Weight));
|
---|
[14843] | 683 | } else {
|
---|
| 684 | var sineNode = sineSymbol.CreateTreeNode();
|
---|
| 685 | sineNode.AddSubtree(node);
|
---|
| 686 | return sineNode;
|
---|
| 687 | }
|
---|
| 688 | }
|
---|
| 689 |
|
---|
[18146] | 690 | private static ISymbolicExpressionTreeNode Tangent(ISymbolicExpressionTreeNode node) {
|
---|
| 691 | if (IsNumber(node)) {
|
---|
| 692 | var numNode = node as NumberTreeNode;
|
---|
| 693 | return Number(Math.Tan(numNode.Value));
|
---|
[14843] | 694 | } else if (IsFactor(node)) {
|
---|
| 695 | var factor = node as FactorVariableTreeNode;
|
---|
[18146] | 696 | return Factor(factor.Symbol, factor.VariableName, factor.Weights.Select(Math.Tan));
|
---|
[14843] | 697 | } else if (IsBinFactor(node)) {
|
---|
| 698 | var binFactor = node as BinaryFactorVariableTreeNode;
|
---|
[18146] | 699 | return BinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Tan(binFactor.Weight));
|
---|
[14843] | 700 | } else {
|
---|
| 701 | var tanNode = tanSymbol.CreateTreeNode();
|
---|
| 702 | tanNode.AddSubtree(node);
|
---|
| 703 | return tanNode;
|
---|
| 704 | }
|
---|
| 705 | }
|
---|
| 706 |
|
---|
[18146] | 707 | private static ISymbolicExpressionTreeNode Cosine(ISymbolicExpressionTreeNode node) {
|
---|
| 708 | if (IsNumber(node)) {
|
---|
| 709 | var numNode = node as NumberTreeNode;
|
---|
| 710 | return Number(Math.Cos(numNode.Value));
|
---|
[14843] | 711 | } else if (IsFactor(node)) {
|
---|
| 712 | var factor = node as FactorVariableTreeNode;
|
---|
[18146] | 713 | return Factor(factor.Symbol, factor.VariableName, factor.Weights.Select(Math.Cos));
|
---|
[14843] | 714 | } else if (IsBinFactor(node)) {
|
---|
| 715 | var binFactor = node as BinaryFactorVariableTreeNode;
|
---|
| 716 | // cos(0) = 1 see similar case for Exp(binfactor)
|
---|
[18146] | 717 | return Sum(BinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Cos(binFactor.Weight) - 1),
|
---|
| 718 | Number(1.0));
|
---|
[14843] | 719 | } else {
|
---|
| 720 | var cosNode = cosineSymbol.CreateTreeNode();
|
---|
| 721 | cosNode.AddSubtree(node);
|
---|
| 722 | return cosNode;
|
---|
| 723 | }
|
---|
| 724 | }
|
---|
| 725 |
|
---|
[18146] | 726 | private static ISymbolicExpressionTreeNode Exp(ISymbolicExpressionTreeNode node) {
|
---|
| 727 | if (IsNumber(node)) {
|
---|
| 728 | var numNode = node as NumberTreeNode;
|
---|
| 729 | return Number(Math.Exp(numNode.Value));
|
---|
[14843] | 730 | } else if (IsFactor(node)) {
|
---|
| 731 | var factNode = node as FactorVariableTreeNode;
|
---|
[18146] | 732 | return Factor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Exp(w)));
|
---|
[14843] | 733 | } else if (IsBinFactor(node)) {
|
---|
| 734 | // exp( binfactor w val=a) = if(val=a) exp(w) else exp(0) = binfactor( (exp(w) - 1) val a) + 1
|
---|
| 735 | var binFactor = node as BinaryFactorVariableTreeNode;
|
---|
| 736 | return
|
---|
[18146] | 737 | Sum(BinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Exp(binFactor.Weight) - 1), Number(1.0));
|
---|
[14843] | 738 | } else if (IsLog(node)) {
|
---|
| 739 | return node.GetSubtree(0);
|
---|
| 740 | } else if (IsAddition(node)) {
|
---|
[18146] | 741 | return node.Subtrees.Select(s => Exp(s)).Aggregate((s, t) => Product(s, t));
|
---|
[14843] | 742 | } else if (IsSubtraction(node)) {
|
---|
[18146] | 743 | return node.Subtrees.Select(s => Exp(s)).Aggregate((s, t) => Product(s, Negate(t)));
|
---|
[14843] | 744 | } else {
|
---|
| 745 | var expNode = expSymbol.CreateTreeNode();
|
---|
| 746 | expNode.AddSubtree(node);
|
---|
| 747 | return expNode;
|
---|
| 748 | }
|
---|
| 749 | }
|
---|
[18146] | 750 | private static ISymbolicExpressionTreeNode Log(ISymbolicExpressionTreeNode node) {
|
---|
| 751 | if (IsNumber(node)) {
|
---|
| 752 | var numNode = node as NumberTreeNode;
|
---|
| 753 | return Number(Math.Log(numNode.Value));
|
---|
[14843] | 754 | } else if (IsFactor(node)) {
|
---|
| 755 | var factNode = node as FactorVariableTreeNode;
|
---|
[18146] | 756 | return Factor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Log(w)));
|
---|
[14843] | 757 | } else if (IsExp(node)) {
|
---|
| 758 | return node.GetSubtree(0);
|
---|
| 759 | } else if (IsSquareRoot(node)) {
|
---|
[18146] | 760 | return Fraction(Log(node.GetSubtree(0)), Number(2.0));
|
---|
[14843] | 761 | } else {
|
---|
| 762 | var logNode = logSymbol.CreateTreeNode();
|
---|
| 763 | logNode.AddSubtree(node);
|
---|
| 764 | return logNode;
|
---|
| 765 | }
|
---|
| 766 | }
|
---|
| 767 |
|
---|
[18146] | 768 | private static ISymbolicExpressionTreeNode Square(ISymbolicExpressionTreeNode node) {
|
---|
| 769 | if (IsNumber(node)) {
|
---|
| 770 | var numNode = node as NumberTreeNode;
|
---|
| 771 | return Number(numNode.Value * numNode.Value);
|
---|
[14843] | 772 | } else if (IsFactor(node)) {
|
---|
| 773 | var factNode = node as FactorVariableTreeNode;
|
---|
[18146] | 774 | return Factor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => w * w));
|
---|
[14843] | 775 | } else if (IsBinFactor(node)) {
|
---|
| 776 | var binFactor = node as BinaryFactorVariableTreeNode;
|
---|
[18146] | 777 | return BinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, binFactor.Weight * binFactor.Weight);
|
---|
[14843] | 778 | } else if (IsSquareRoot(node)) {
|
---|
| 779 | return node.GetSubtree(0);
|
---|
[16356] | 780 | } else if (IsMultiplication(node)) {
|
---|
| 781 | // sqr( x * y ) = sqr(x) * sqr(y)
|
---|
| 782 | var mulNode = mulSymbol.CreateTreeNode();
|
---|
| 783 | foreach (var subtree in node.Subtrees) {
|
---|
[18146] | 784 | mulNode.AddSubtree(Square(subtree));
|
---|
[16356] | 785 | }
|
---|
| 786 | return mulNode;
|
---|
| 787 | } else if (IsAbsolute(node)) {
|
---|
[18146] | 788 | return Square(node.GetSubtree(0)); // sqr(abs(x)) = sqr(x)
|
---|
[16356] | 789 | } else if (IsExp(node)) {
|
---|
[18146] | 790 | return Exp(Product(node.GetSubtree(0), Number(2.0))); // sqr(exp(x)) = exp(2x)
|
---|
[17963] | 791 | } else if (IsSquare(node)) {
|
---|
[18146] | 792 | return Power(node.GetSubtree(0), Number(4));
|
---|
[16356] | 793 | } else if (IsCube(node)) {
|
---|
[18146] | 794 | return Power(node.GetSubtree(0), Number(6));
|
---|
[14843] | 795 | } else {
|
---|
| 796 | var sqrNode = sqrSymbol.CreateTreeNode();
|
---|
| 797 | sqrNode.AddSubtree(node);
|
---|
| 798 | return sqrNode;
|
---|
| 799 | }
|
---|
| 800 | }
|
---|
| 801 |
|
---|
[18146] | 802 | private static ISymbolicExpressionTreeNode Cube(ISymbolicExpressionTreeNode node) {
|
---|
| 803 | if (IsNumber(node)) {
|
---|
| 804 | var numNode = node as NumberTreeNode;
|
---|
| 805 | return Number(numNode.Value * numNode.Value * numNode.Value);
|
---|
[16356] | 806 | } else if (IsFactor(node)) {
|
---|
| 807 | var factNode = node as FactorVariableTreeNode;
|
---|
[18146] | 808 | return Factor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => w * w * w));
|
---|
[16356] | 809 | } else if (IsBinFactor(node)) {
|
---|
| 810 | var binFactor = node as BinaryFactorVariableTreeNode;
|
---|
[18146] | 811 | return BinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, binFactor.Weight * binFactor.Weight * binFactor.Weight);
|
---|
[16356] | 812 | } else if (IsCubeRoot(node)) {
|
---|
| 813 | return node.GetSubtree(0); // NOTE: not really accurate because cuberoot(x) with negative x is evaluated to NaN and after this simplification we evaluate as x
|
---|
| 814 | } else if (IsExp(node)) {
|
---|
[18146] | 815 | return Exp(Product(node.GetSubtree(0), Number(3)));
|
---|
[16356] | 816 | } else if (IsSquare(node)) {
|
---|
[18146] | 817 | return Power(node.GetSubtree(0), Number(6));
|
---|
[17963] | 818 | } else if (IsCube(node)) {
|
---|
[18146] | 819 | return Power(node.GetSubtree(0), Number(9));
|
---|
[16356] | 820 | } else {
|
---|
| 821 | var cubeNode = cubeSymbol.CreateTreeNode();
|
---|
| 822 | cubeNode.AddSubtree(node);
|
---|
| 823 | return cubeNode;
|
---|
| 824 | }
|
---|
| 825 | }
|
---|
| 826 |
|
---|
[18146] | 827 | private static ISymbolicExpressionTreeNode Abs(ISymbolicExpressionTreeNode node) {
|
---|
| 828 | if (IsNumber(node)) {
|
---|
| 829 | var numNode = node as NumberTreeNode;
|
---|
| 830 | return Number(Math.Abs(numNode.Value));
|
---|
[16356] | 831 | } else if (IsFactor(node)) {
|
---|
| 832 | var factNode = node as FactorVariableTreeNode;
|
---|
[18146] | 833 | return Factor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Abs(w)));
|
---|
[16356] | 834 | } else if (IsBinFactor(node)) {
|
---|
| 835 | var binFactor = node as BinaryFactorVariableTreeNode;
|
---|
[18146] | 836 | return BinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Abs(binFactor.Weight));
|
---|
[16356] | 837 | } else if (IsSquare(node) || IsExp(node) || IsSquareRoot(node) || IsCubeRoot(node)) {
|
---|
| 838 | return node; // abs(sqr(x)) = sqr(x), abs(exp(x)) = exp(x) ...
|
---|
| 839 | } else if (IsMultiplication(node)) {
|
---|
| 840 | var mul = mulSymbol.CreateTreeNode();
|
---|
| 841 | foreach (var st in node.Subtrees) {
|
---|
[18146] | 842 | mul.AddSubtree(Abs(st));
|
---|
[16356] | 843 | }
|
---|
| 844 | return mul;
|
---|
| 845 | } else if (IsDivision(node)) {
|
---|
| 846 | var div = divSymbol.CreateTreeNode();
|
---|
| 847 | foreach (var st in node.Subtrees) {
|
---|
[18146] | 848 | div.AddSubtree(Abs(st));
|
---|
[16356] | 849 | }
|
---|
| 850 | return div;
|
---|
| 851 | } else {
|
---|
| 852 | var absNode = absSymbol.CreateTreeNode();
|
---|
| 853 | absNode.AddSubtree(node);
|
---|
| 854 | return absNode;
|
---|
| 855 | }
|
---|
| 856 | }
|
---|
| 857 |
|
---|
| 858 | // constant folding only
|
---|
[18146] | 859 | private static ISymbolicExpressionTreeNode AQ(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
| 860 | if (IsNumber(b)) {
|
---|
| 861 | var nNode = b as NumberTreeNode;
|
---|
| 862 | return Fraction(a, Number(Math.Sqrt(1.0 + nNode.Value * nNode.Value)));
|
---|
[16356] | 863 | } else if (IsFactor(b)) {
|
---|
| 864 | var factNode = b as FactorVariableTreeNode;
|
---|
[18146] | 865 | return Fraction(a, Factor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Sqrt(1.0 + w * w))));
|
---|
[16356] | 866 | } else if (IsBinFactor(b)) {
|
---|
| 867 | var binFactor = b as BinaryFactorVariableTreeNode;
|
---|
[18146] | 868 | return Fraction(a, BinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Sqrt(1.0 + binFactor.Weight * binFactor.Weight)));
|
---|
[16356] | 869 | } else {
|
---|
| 870 | var aqNode = aqSymbol.CreateTreeNode();
|
---|
| 871 | aqNode.AddSubtree(a);
|
---|
| 872 | aqNode.AddSubtree(b);
|
---|
| 873 | return aqNode;
|
---|
| 874 | }
|
---|
| 875 | }
|
---|
| 876 |
|
---|
[18146] | 877 | private static ISymbolicExpressionTreeNode SquareRoot(ISymbolicExpressionTreeNode node) {
|
---|
| 878 | if (IsNumber(node)) {
|
---|
| 879 | var numNode = node as NumberTreeNode;
|
---|
| 880 | return Number(Math.Sqrt(numNode.Value));
|
---|
[14843] | 881 | } else if (IsFactor(node)) {
|
---|
| 882 | var factNode = node as FactorVariableTreeNode;
|
---|
[18146] | 883 | return Factor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Sqrt(w)));
|
---|
[14843] | 884 | } else if (IsBinFactor(node)) {
|
---|
| 885 | var binFactor = node as BinaryFactorVariableTreeNode;
|
---|
[18146] | 886 | return BinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Sqrt(binFactor.Weight));
|
---|
[14843] | 887 | } else if (IsSquare(node)) {
|
---|
[16356] | 888 | return node.GetSubtree(0); // NOTE: not really accurate because sqrt(x) with negative x is evaluated to NaN and after this simplification we evaluate as x
|
---|
[14843] | 889 | } else {
|
---|
| 890 | var sqrtNode = sqrtSymbol.CreateTreeNode();
|
---|
| 891 | sqrtNode.AddSubtree(node);
|
---|
| 892 | return sqrtNode;
|
---|
| 893 | }
|
---|
| 894 | }
|
---|
| 895 |
|
---|
[18146] | 896 | private static ISymbolicExpressionTreeNode CubeRoot(ISymbolicExpressionTreeNode node) {
|
---|
| 897 | if (IsNumber(node)) {
|
---|
| 898 | var numNode = node as NumberTreeNode;
|
---|
| 899 | return Number(Math.Pow(numNode.Value, 1.0 / 3.0));
|
---|
[16356] | 900 | } else if (IsFactor(node)) {
|
---|
| 901 | var factNode = node as FactorVariableTreeNode;
|
---|
[18146] | 902 | return Factor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Pow(w, 1.0 / 3.0)));
|
---|
[16356] | 903 | } else if (IsBinFactor(node)) {
|
---|
| 904 | var binFactor = node as BinaryFactorVariableTreeNode;
|
---|
[18146] | 905 | return BinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Sqrt(Math.Pow(binFactor.Weight, 1.0 / 3.0)));
|
---|
[16356] | 906 | } else if (IsCube(node)) {
|
---|
| 907 | return node.GetSubtree(0);
|
---|
| 908 | } else {
|
---|
| 909 | var cubeRootNode = cubeRootSymbol.CreateTreeNode();
|
---|
| 910 | cubeRootNode.AddSubtree(node);
|
---|
| 911 | return cubeRootNode;
|
---|
| 912 | }
|
---|
| 913 | }
|
---|
| 914 |
|
---|
[18146] | 915 | private static ISymbolicExpressionTreeNode Root(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
| 916 | if (IsNumber(a) && IsNumber(b)) {
|
---|
| 917 | var aNode = a as NumberTreeNode;
|
---|
| 918 | var bNode = b as NumberTreeNode;
|
---|
| 919 | return Number(Math.Pow(aNode.Value, 1.0 / Math.Round(bNode.Value)));
|
---|
| 920 | } else if (IsFactor(a) && IsNumber(b)) {
|
---|
[14843] | 921 | var factNode = a as FactorVariableTreeNode;
|
---|
[18146] | 922 | var bNode = b as NumberTreeNode;
|
---|
| 923 | return Factor(factNode.Symbol, factNode.VariableName,
|
---|
| 924 | factNode.Weights.Select(w => Math.Pow(w, 1.0 / Math.Round(bNode.Value))));
|
---|
| 925 | } else if (IsBinFactor(a) && IsNumber(b)) {
|
---|
[14843] | 926 | var binFactor = a as BinaryFactorVariableTreeNode;
|
---|
[18146] | 927 | var bNode = b as NumberTreeNode;
|
---|
| 928 | return BinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Pow(binFactor.Weight, 1.0 / Math.Round(bNode.Value)));
|
---|
| 929 | } else if (IsNumber(a) && IsFactor(b)) {
|
---|
| 930 | var aNode = a as NumberTreeNode;
|
---|
[14843] | 931 | var factNode = b as FactorVariableTreeNode;
|
---|
[18146] | 932 | return Factor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Pow(aNode.Value, 1.0 / Math.Round(w))));
|
---|
| 933 | } else if (IsNumber(a) && IsBinFactor(b)) {
|
---|
| 934 | var aNode = a as NumberTreeNode;
|
---|
[14843] | 935 | var factNode = b as BinaryFactorVariableTreeNode;
|
---|
[18146] | 936 | return BinFactor(factNode.Symbol, factNode.VariableName, factNode.VariableValue, Math.Pow(aNode.Value, 1.0 / Math.Round(factNode.Weight)));
|
---|
[14843] | 937 | } else if (IsFactor(a) && IsFactor(b) && AreSameTypeAndVariable(a, b)) {
|
---|
| 938 | var node0 = a as FactorVariableTreeNode;
|
---|
| 939 | var node1 = b as FactorVariableTreeNode;
|
---|
[18146] | 940 | return Factor(node0.Symbol, node0.VariableName, node0.Weights.Zip(node1.Weights, (u, v) => Math.Pow(u, 1.0 / Math.Round(v))));
|
---|
| 941 | } else if (IsNumber(b)) {
|
---|
| 942 | var bNode = b as NumberTreeNode;
|
---|
| 943 | var bVal = Math.Round(bNode.Value);
|
---|
| 944 | if (bVal == 1.0) {
|
---|
[17820] | 945 | // root(a, 1) => a
|
---|
[14843] | 946 | return a;
|
---|
[18146] | 947 | } else if (bVal == 0.0) {
|
---|
[17820] | 948 | // root(a, 0) is not defined
|
---|
[18146] | 949 | return Number(double.NaN);
|
---|
| 950 | } else if (bVal == -1.0) {
|
---|
[17820] | 951 | // root(a, -1) => a^(-1/1) => 1/a
|
---|
[18146] | 952 | return Fraction(Number(1.0), a);
|
---|
| 953 | } else if (bVal < 0) {
|
---|
[17820] | 954 | // root(a, -b) => a^(-1/b) => (1/a)^(1/b) => root(1, b) / root(a, b) => 1 / root(a, b)
|
---|
[14843] | 955 | var rootNode = rootSymbol.CreateTreeNode();
|
---|
| 956 | rootNode.AddSubtree(a);
|
---|
[18146] | 957 | rootNode.AddSubtree(Number(-1.0 * bVal));
|
---|
| 958 | return Fraction(Number(1.0), rootNode);
|
---|
[14843] | 959 | } else {
|
---|
| 960 | var rootNode = rootSymbol.CreateTreeNode();
|
---|
| 961 | rootNode.AddSubtree(a);
|
---|
[18146] | 962 | rootNode.AddSubtree(Number(bVal));
|
---|
[14843] | 963 | return rootNode;
|
---|
| 964 | }
|
---|
| 965 | } else {
|
---|
| 966 | var rootNode = rootSymbol.CreateTreeNode();
|
---|
| 967 | rootNode.AddSubtree(a);
|
---|
| 968 | rootNode.AddSubtree(b);
|
---|
| 969 | return rootNode;
|
---|
| 970 | }
|
---|
| 971 | }
|
---|
| 972 |
|
---|
| 973 |
|
---|
[18146] | 974 | private static ISymbolicExpressionTreeNode Power(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
| 975 | if (IsNumber(a) && IsNumber(b)) {
|
---|
| 976 | var aNode = a as NumberTreeNode;
|
---|
| 977 | var bNode = b as NumberTreeNode;
|
---|
| 978 | return Number(Math.Pow(aNode.Value, Math.Round(bNode.Value)));
|
---|
| 979 | } else if (IsFactor(a) && IsNumber(b)) {
|
---|
[14843] | 980 | var factNode = a as FactorVariableTreeNode;
|
---|
[18146] | 981 | var bNode = b as NumberTreeNode;
|
---|
| 982 | return Factor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Pow(w, Math.Round(bNode.Value))));
|
---|
| 983 | } else if (IsBinFactor(a) && IsNumber(b)) {
|
---|
[14843] | 984 | var binFactor = a as BinaryFactorVariableTreeNode;
|
---|
[18146] | 985 | var bNode = b as NumberTreeNode;
|
---|
| 986 | return BinFactor(binFactor.Symbol, binFactor.VariableName, binFactor.VariableValue, Math.Pow(binFactor.Weight, Math.Round(bNode.Value)));
|
---|
| 987 | } else if (IsNumber(a) && IsFactor(b)) {
|
---|
| 988 | var aNode = a as NumberTreeNode;
|
---|
[14843] | 989 | var factNode = b as FactorVariableTreeNode;
|
---|
[18146] | 990 | return Factor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => Math.Pow(aNode.Value, Math.Round(w))));
|
---|
| 991 | } else if (IsNumber(a) && IsBinFactor(b)) {
|
---|
| 992 | var aNode = a as NumberTreeNode;
|
---|
[14843] | 993 | var factNode = b as BinaryFactorVariableTreeNode;
|
---|
[18146] | 994 | return BinFactor(factNode.Symbol, factNode.VariableName, factNode.VariableValue, Math.Pow(aNode.Value, Math.Round(factNode.Weight)));
|
---|
[14843] | 995 | } else if (IsFactor(a) && IsFactor(b) && AreSameTypeAndVariable(a, b)) {
|
---|
| 996 | var node0 = a as FactorVariableTreeNode;
|
---|
| 997 | var node1 = b as FactorVariableTreeNode;
|
---|
[18146] | 998 | return Factor(node0.Symbol, node0.VariableName, node0.Weights.Zip(node1.Weights, (u, v) => Math.Pow(u, Math.Round(v))));
|
---|
| 999 | } else if (IsNumber(b)) {
|
---|
| 1000 | var bNode = b as NumberTreeNode;
|
---|
| 1001 | double exponent = Math.Round(bNode.Value);
|
---|
[17797] | 1002 | if (exponent == 0.0) {
|
---|
[17820] | 1003 | // a^0 => 1
|
---|
[18146] | 1004 | return Number(1.0);
|
---|
[17797] | 1005 | } else if (exponent == 1.0) {
|
---|
[17820] | 1006 | // a^1 => a
|
---|
[14843] | 1007 | return a;
|
---|
[17797] | 1008 | } else if (exponent == -1.0) {
|
---|
[17820] | 1009 | // a^-1 => 1/a
|
---|
[18146] | 1010 | return Fraction(Number(1.0), a);
|
---|
[14843] | 1011 | } else if (exponent < 0) {
|
---|
[17820] | 1012 | // a^-b => (1/a)^b => 1/(a^b)
|
---|
[14843] | 1013 | var powNode = powSymbol.CreateTreeNode();
|
---|
| 1014 | powNode.AddSubtree(a);
|
---|
[18146] | 1015 | powNode.AddSubtree(Number(-1.0 * exponent));
|
---|
| 1016 | return Fraction(Number(1.0), powNode);
|
---|
[14843] | 1017 | } else {
|
---|
| 1018 | var powNode = powSymbol.CreateTreeNode();
|
---|
| 1019 | powNode.AddSubtree(a);
|
---|
[18146] | 1020 | powNode.AddSubtree(Number(exponent));
|
---|
[14843] | 1021 | return powNode;
|
---|
| 1022 | }
|
---|
| 1023 | } else {
|
---|
| 1024 | var powNode = powSymbol.CreateTreeNode();
|
---|
| 1025 | powNode.AddSubtree(a);
|
---|
| 1026 | powNode.AddSubtree(b);
|
---|
| 1027 | return powNode;
|
---|
| 1028 | }
|
---|
| 1029 | }
|
---|
| 1030 |
|
---|
| 1031 |
|
---|
[18146] | 1032 | // Fraction, Product and Sum take two already simplified trees and create a new simplified tree
|
---|
| 1033 | private static ISymbolicExpressionTreeNode Fraction(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
| 1034 | if (IsNumber(a) && IsNumber(b)) {
|
---|
[14843] | 1035 | // fold constants
|
---|
[18146] | 1036 | return Number(((NumberTreeNode)a).Value / ((NumberTreeNode)b).Value);
|
---|
| 1037 | } else if (IsNumber(a) && ((NumberTreeNode)a).Value != 1.0) {
|
---|
[17820] | 1038 | // a / x => (a * 1/a) / (x * 1/a) => 1 / (x * 1/a)
|
---|
[18146] | 1039 | return Fraction(Number(1.0), Product(b, Invert(a)));
|
---|
| 1040 | } else if (IsVariableBase(a) && IsNumber(b)) {
|
---|
[14843] | 1041 | // merge constant values into variable weights
|
---|
[18146] | 1042 | var bVal = ((NumberTreeNode)b).Value;
|
---|
| 1043 | ((VariableTreeNodeBase)a).Weight /= bVal;
|
---|
[14843] | 1044 | return a;
|
---|
[18146] | 1045 | } else if (IsFactor(a) && IsNumber(b)) {
|
---|
[14843] | 1046 | var factNode = a as FactorVariableTreeNode;
|
---|
[18146] | 1047 | var bNode = b as NumberTreeNode;
|
---|
| 1048 | return Factor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select(w => w / bNode.Value));
|
---|
| 1049 | } else if (IsBinFactor(a) && IsNumber(b)) {
|
---|
[14843] | 1050 | var factNode = a as BinaryFactorVariableTreeNode;
|
---|
[18146] | 1051 | var bNode = b as NumberTreeNode;
|
---|
| 1052 | return BinFactor(factNode.Symbol, factNode.VariableName, factNode.VariableValue, factNode.Weight / bNode.Value);
|
---|
[14843] | 1053 | } else if (IsFactor(a) && IsFactor(b) && AreSameTypeAndVariable(a, b)) {
|
---|
| 1054 | var node0 = a as FactorVariableTreeNode;
|
---|
| 1055 | var node1 = b as FactorVariableTreeNode;
|
---|
[18146] | 1056 | return Factor(node0.Symbol, node0.VariableName, node0.Weights.Zip(node1.Weights, (u, v) => u / v));
|
---|
[14843] | 1057 | } else if (IsFactor(a) && IsBinFactor(b) && ((IVariableTreeNode)a).VariableName == ((IVariableTreeNode)b).VariableName) {
|
---|
| 1058 | var node0 = a as FactorVariableTreeNode;
|
---|
| 1059 | var node1 = b as BinaryFactorVariableTreeNode;
|
---|
| 1060 | var varValues = node0.Symbol.GetVariableValues(node0.VariableName).ToArray();
|
---|
| 1061 | var wi = Array.IndexOf(varValues, node1.VariableValue);
|
---|
| 1062 | if (wi < 0) throw new ArgumentException();
|
---|
| 1063 | var newWeighs = new double[varValues.Length];
|
---|
| 1064 | node0.Weights.CopyTo(newWeighs, 0);
|
---|
| 1065 | for (int i = 0; i < newWeighs.Length; i++)
|
---|
| 1066 | if (wi == i) newWeighs[i] /= node1.Weight;
|
---|
| 1067 | else newWeighs[i] /= 0.0;
|
---|
[18146] | 1068 | return Factor(node0.Symbol, node0.VariableName, newWeighs);
|
---|
[14843] | 1069 | } else if (IsFactor(a)) {
|
---|
[18146] | 1070 | return Fraction(Number(1.0), Product(b, Invert(a)));
|
---|
[14843] | 1071 | } else if (IsVariableBase(a) && IsVariableBase(b) && AreSameTypeAndVariable(a, b) && !IsBinFactor(b)) {
|
---|
| 1072 | // cancel variables (not allowed for bin factors because of division by zero)
|
---|
| 1073 | var aVar = a as VariableTreeNode;
|
---|
| 1074 | var bVar = b as VariableTreeNode;
|
---|
[18146] | 1075 | return Number(aVar.Weight / bVar.Weight);
|
---|
| 1076 | } else if (IsAddition(a) && IsNumber(b)) {
|
---|
[14843] | 1077 | return a.Subtrees
|
---|
| 1078 | .Select(x => GetSimplifiedTree(x))
|
---|
[18146] | 1079 | .Select(x => Fraction(x, GetSimplifiedTree(b)))
|
---|
| 1080 | .Aggregate((c, d) => Sum(c, d));
|
---|
| 1081 | } else if (IsMultiplication(a) && IsNumber(b)) {
|
---|
| 1082 | return Product(a, Invert(b));
|
---|
| 1083 | } else if (IsDivision(a) && IsNumber(b)) {
|
---|
[14843] | 1084 | // (a1 / a2) / c => (a1 / (a2 * c))
|
---|
[18146] | 1085 | return Fraction(a.GetSubtree(0), Product(a.GetSubtree(1), b));
|
---|
[14843] | 1086 | } else if (IsDivision(a) && IsDivision(b)) {
|
---|
| 1087 | // (a1 / a2) / (b1 / b2) =>
|
---|
[18146] | 1088 | return Fraction(Product(a.GetSubtree(0), b.GetSubtree(1)), Product(a.GetSubtree(1), b.GetSubtree(0)));
|
---|
[14843] | 1089 | } else if (IsDivision(a)) {
|
---|
| 1090 | // (a1 / a2) / b => (a1 / (a2 * b))
|
---|
[18146] | 1091 | return Fraction(a.GetSubtree(0), Product(a.GetSubtree(1), b));
|
---|
[14843] | 1092 | } else if (IsDivision(b)) {
|
---|
| 1093 | // a / (b1 / b2) => (a * b2) / b1
|
---|
[18146] | 1094 | return Fraction(Product(a, b.GetSubtree(1)), b.GetSubtree(0));
|
---|
[16356] | 1095 | } else if (IsAnalyticalQuotient(a)) {
|
---|
[18146] | 1096 | return AQ(a.GetSubtree(0), Product(a.GetSubtree(1), b));
|
---|
[14843] | 1097 | } else {
|
---|
| 1098 | var div = divSymbol.CreateTreeNode();
|
---|
| 1099 | div.AddSubtree(a);
|
---|
| 1100 | div.AddSubtree(b);
|
---|
| 1101 | return div;
|
---|
| 1102 | }
|
---|
| 1103 | }
|
---|
| 1104 |
|
---|
[18146] | 1105 | private static ISymbolicExpressionTreeNode Sum(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
| 1106 | if (IsNumber(a) && IsNumber(b)) {
|
---|
[14843] | 1107 | // fold constants
|
---|
[18146] | 1108 | ((NumberTreeNode)a).Value += ((NumberTreeNode)b).Value;
|
---|
[14843] | 1109 | return a;
|
---|
[18146] | 1110 | } else if (IsNumber(a)) {
|
---|
[14843] | 1111 | // c + x => x + c
|
---|
| 1112 | // b is not constant => make sure constant is on the right
|
---|
[18146] | 1113 | return Sum(b, a);
|
---|
| 1114 | } else if (IsNumber(b) && ((NumberTreeNode)b).Value == 0.0) {
|
---|
[14843] | 1115 | // x + 0 => x
|
---|
| 1116 | return a;
|
---|
[18146] | 1117 | } else if (IsFactor(a) && IsNumber(b)) {
|
---|
[14843] | 1118 | var factNode = a as FactorVariableTreeNode;
|
---|
[18146] | 1119 | var bNode = b as NumberTreeNode;
|
---|
| 1120 | return Factor(factNode.Symbol, factNode.VariableName, factNode.Weights.Select((w) => w + bNode.Value));
|
---|
[14843] | 1121 | } else if (IsFactor(a) && IsFactor(b) && AreSameTypeAndVariable(a, b)) {
|
---|
| 1122 | var node0 = a as FactorVariableTreeNode;
|
---|
| 1123 | var node1 = b as FactorVariableTreeNode;
|
---|
[18146] | 1124 | return Factor(node0.Symbol, node0.VariableName, node0.Weights.Zip(node1.Weights, (u, v) => u + v));
|
---|
[14843] | 1125 | } else if (IsBinFactor(a) && IsFactor(b)) {
|
---|
[18146] | 1126 | return Sum(b, a);
|
---|
[14843] | 1127 | } else if (IsFactor(a) && IsBinFactor(b) &&
|
---|
| 1128 | ((IVariableTreeNode)a).VariableName == ((IVariableTreeNode)b).VariableName) {
|
---|
| 1129 | var node0 = a as FactorVariableTreeNode;
|
---|
| 1130 | var node1 = b as BinaryFactorVariableTreeNode;
|
---|
| 1131 | var varValues = node0.Symbol.GetVariableValues(node0.VariableName).ToArray();
|
---|
| 1132 | var wi = Array.IndexOf(varValues, node1.VariableValue);
|
---|
| 1133 | if (wi < 0) throw new ArgumentException();
|
---|
| 1134 | var newWeighs = new double[varValues.Length];
|
---|
| 1135 | node0.Weights.CopyTo(newWeighs, 0);
|
---|
| 1136 | newWeighs[wi] += node1.Weight;
|
---|
[18146] | 1137 | return Factor(node0.Symbol, node0.VariableName, newWeighs);
|
---|
[14843] | 1138 | } else if (IsAddition(a) && IsAddition(b)) {
|
---|
| 1139 | // merge additions
|
---|
| 1140 | var add = addSymbol.CreateTreeNode();
|
---|
| 1141 | // add all sub trees except for the last
|
---|
| 1142 | for (int i = 0; i < a.Subtrees.Count() - 1; i++) add.AddSubtree(a.GetSubtree(i));
|
---|
| 1143 | for (int i = 0; i < b.Subtrees.Count() - 1; i++) add.AddSubtree(b.GetSubtree(i));
|
---|
[18146] | 1144 | if (IsNumber(a.Subtrees.Last()) && IsNumber(b.Subtrees.Last())) {
|
---|
| 1145 | add.AddSubtree(Sum(a.Subtrees.Last(), b.Subtrees.Last()));
|
---|
| 1146 | } else if (IsNumber(a.Subtrees.Last())) {
|
---|
[14843] | 1147 | add.AddSubtree(b.Subtrees.Last());
|
---|
| 1148 | add.AddSubtree(a.Subtrees.Last());
|
---|
| 1149 | } else {
|
---|
| 1150 | add.AddSubtree(a.Subtrees.Last());
|
---|
| 1151 | add.AddSubtree(b.Subtrees.Last());
|
---|
| 1152 | }
|
---|
| 1153 | MergeVariablesInSum(add);
|
---|
| 1154 | if (add.Subtrees.Count() == 1) {
|
---|
| 1155 | return add.GetSubtree(0);
|
---|
| 1156 | } else {
|
---|
| 1157 | return add;
|
---|
| 1158 | }
|
---|
| 1159 | } else if (IsAddition(b)) {
|
---|
[18146] | 1160 | return Sum(b, a);
|
---|
| 1161 | } else if (IsAddition(a) && IsNumber(b)) {
|
---|
[14843] | 1162 | // a is an addition and b is a constant => append b to a and make sure the constants are merged
|
---|
| 1163 | var add = addSymbol.CreateTreeNode();
|
---|
| 1164 | // add all sub trees except for the last
|
---|
| 1165 | for (int i = 0; i < a.Subtrees.Count() - 1; i++) add.AddSubtree(a.GetSubtree(i));
|
---|
[18146] | 1166 | if (IsNumber(a.Subtrees.Last()))
|
---|
| 1167 | add.AddSubtree(Sum(a.Subtrees.Last(), b));
|
---|
[14843] | 1168 | else {
|
---|
| 1169 | add.AddSubtree(a.Subtrees.Last());
|
---|
| 1170 | add.AddSubtree(b);
|
---|
| 1171 | }
|
---|
| 1172 | return add;
|
---|
| 1173 | } else if (IsAddition(a)) {
|
---|
| 1174 | // a is already an addition => append b
|
---|
| 1175 | var add = addSymbol.CreateTreeNode();
|
---|
| 1176 | add.AddSubtree(b);
|
---|
| 1177 | foreach (var subtree in a.Subtrees) {
|
---|
| 1178 | add.AddSubtree(subtree);
|
---|
| 1179 | }
|
---|
| 1180 | MergeVariablesInSum(add);
|
---|
| 1181 | if (add.Subtrees.Count() == 1) {
|
---|
| 1182 | return add.GetSubtree(0);
|
---|
| 1183 | } else {
|
---|
| 1184 | return add;
|
---|
| 1185 | }
|
---|
| 1186 | } else {
|
---|
| 1187 | var add = addSymbol.CreateTreeNode();
|
---|
| 1188 | add.AddSubtree(a);
|
---|
| 1189 | add.AddSubtree(b);
|
---|
| 1190 | MergeVariablesInSum(add);
|
---|
| 1191 | if (add.Subtrees.Count() == 1) {
|
---|
| 1192 | return add.GetSubtree(0);
|
---|
| 1193 | } else {
|
---|
| 1194 | return add;
|
---|
| 1195 | }
|
---|
| 1196 | }
|
---|
| 1197 | }
|
---|
| 1198 |
|
---|
| 1199 | // makes sure variable symbols in sums are combined
|
---|
[14949] | 1200 | private static void MergeVariablesInSum(ISymbolicExpressionTreeNode sum) {
|
---|
[14843] | 1201 | var subtrees = new List<ISymbolicExpressionTreeNode>(sum.Subtrees);
|
---|
| 1202 | while (sum.Subtrees.Any()) sum.RemoveSubtree(0);
|
---|
| 1203 | var groupedVarNodes = from node in subtrees.OfType<IVariableTreeNode>()
|
---|
| 1204 | where node.SubtreeCount == 0
|
---|
| 1205 | group node by GroupId(node) into g
|
---|
| 1206 | select g;
|
---|
[18146] | 1207 | var sumNumbers = (from node in subtrees.OfType<NumberTreeNode>()
|
---|
| 1208 | select node.Value).DefaultIfEmpty(0.0).Sum();
|
---|
| 1209 | var unchangedSubtrees = subtrees.Where(t => t.SubtreeCount > 0 || !(t is IVariableTreeNode) && !(t is NumberTreeNode));
|
---|
[14843] | 1210 |
|
---|
| 1211 | foreach (var variableNodeGroup in groupedVarNodes) {
|
---|
| 1212 | var firstNode = variableNodeGroup.First();
|
---|
| 1213 | if (firstNode is VariableTreeNodeBase) {
|
---|
| 1214 | var representative = firstNode as VariableTreeNodeBase;
|
---|
| 1215 | var weightSum = variableNodeGroup.Cast<VariableTreeNodeBase>().Select(t => t.Weight).Sum();
|
---|
| 1216 | representative.Weight = weightSum;
|
---|
| 1217 | sum.AddSubtree(representative);
|
---|
| 1218 | } else if (firstNode is FactorVariableTreeNode) {
|
---|
| 1219 | var representative = firstNode as FactorVariableTreeNode;
|
---|
| 1220 | foreach (var node in variableNodeGroup.Skip(1).Cast<FactorVariableTreeNode>()) {
|
---|
| 1221 | for (int j = 0; j < representative.Weights.Length; j++) {
|
---|
| 1222 | representative.Weights[j] += node.Weights[j];
|
---|
| 1223 | }
|
---|
| 1224 | }
|
---|
| 1225 | sum.AddSubtree(representative);
|
---|
| 1226 | }
|
---|
| 1227 | }
|
---|
| 1228 | foreach (var unchangedSubtree in unchangedSubtrees)
|
---|
| 1229 | sum.AddSubtree(unchangedSubtree);
|
---|
[18146] | 1230 | if (sumNumbers != 0.0) {
|
---|
| 1231 | sum.AddSubtree(Number(sumNumbers));
|
---|
[14843] | 1232 | }
|
---|
| 1233 | }
|
---|
| 1234 |
|
---|
| 1235 | // nodes referencing variables can be grouped if they have
|
---|
[14949] | 1236 | private static string GroupId(IVariableTreeNode node) {
|
---|
[18146] | 1237 | if (node is VariableTreeNode variableNode) {
|
---|
[14843] | 1238 | return "var " + variableNode.VariableName;
|
---|
[18146] | 1239 | } else if (node is BinaryFactorVariableTreeNode binaryFactorNode) {
|
---|
[14843] | 1240 | return "binfactor " + binaryFactorNode.VariableName + " " + binaryFactorNode.VariableValue;
|
---|
[18146] | 1241 | } else if (node is FactorVariableTreeNode factorNode) {
|
---|
[14843] | 1242 | return "factor " + factorNode.VariableName;
|
---|
[18146] | 1243 | } else if (node is LaggedVariableTreeNode laggedVarNode) {
|
---|
[14843] | 1244 | return "lagged " + laggedVarNode.VariableName + " " + laggedVarNode.Lag;
|
---|
| 1245 | } else {
|
---|
| 1246 | throw new NotSupportedException();
|
---|
| 1247 | }
|
---|
| 1248 | }
|
---|
| 1249 |
|
---|
| 1250 |
|
---|
[18146] | 1251 | private static ISymbolicExpressionTreeNode Product(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
| 1252 | if (IsNumber(a) && IsNumber(b)) {
|
---|
[14843] | 1253 | // fold constants
|
---|
[18146] | 1254 | return Number(((NumberTreeNode)a).Value * ((NumberTreeNode)b).Value);
|
---|
| 1255 | } else if (IsNumber(a)) {
|
---|
[14843] | 1256 | // a * $ => $ * a
|
---|
[18146] | 1257 | return Product(b, a);
|
---|
[14843] | 1258 | } else if (IsFactor(a) && IsFactor(b) && AreSameTypeAndVariable(a, b)) {
|
---|
| 1259 | var node0 = a as FactorVariableTreeNode;
|
---|
| 1260 | var node1 = b as FactorVariableTreeNode;
|
---|
[18146] | 1261 | return Factor(node0.Symbol, node0.VariableName, node0.Weights.Zip(node1.Weights, (u, v) => u * v));
|
---|
[14843] | 1262 | } else if (IsBinFactor(a) && IsBinFactor(b) && AreSameTypeAndVariable(a, b)) {
|
---|
| 1263 | var node0 = a as BinaryFactorVariableTreeNode;
|
---|
| 1264 | var node1 = b as BinaryFactorVariableTreeNode;
|
---|
[18146] | 1265 | return BinFactor(node0.Symbol, node0.VariableName, node0.VariableValue, node0.Weight * node1.Weight);
|
---|
| 1266 | } else if (IsFactor(a) && IsNumber(b)) {
|
---|
[14843] | 1267 | var node0 = a as FactorVariableTreeNode;
|
---|
[18146] | 1268 | var node1 = b as NumberTreeNode;
|
---|
| 1269 | return Factor(node0.Symbol, node0.VariableName, node0.Weights.Select(w => w * node1.Value));
|
---|
| 1270 | } else if (IsBinFactor(a) && IsNumber(b)) {
|
---|
[14843] | 1271 | var node0 = a as BinaryFactorVariableTreeNode;
|
---|
[18146] | 1272 | var node1 = b as NumberTreeNode;
|
---|
| 1273 | return BinFactor(node0.Symbol, node0.VariableName, node0.VariableValue, node0.Weight * node1.Value);
|
---|
[14843] | 1274 | } else if (IsBinFactor(a) && IsFactor(b)) {
|
---|
[18146] | 1275 | return Product(b, a);
|
---|
[14843] | 1276 | } else if (IsFactor(a) && IsBinFactor(b) &&
|
---|
| 1277 | ((IVariableTreeNode)a).VariableName == ((IVariableTreeNode)b).VariableName) {
|
---|
| 1278 | var node0 = a as FactorVariableTreeNode;
|
---|
| 1279 | var node1 = b as BinaryFactorVariableTreeNode;
|
---|
| 1280 | var varValues = node0.Symbol.GetVariableValues(node0.VariableName).ToArray();
|
---|
| 1281 | var wi = Array.IndexOf(varValues, node1.VariableValue);
|
---|
| 1282 | if (wi < 0) throw new ArgumentException();
|
---|
[18146] | 1283 | return BinFactor(node1.Symbol, node1.VariableName, node1.VariableValue, node1.Weight * node0.Weights[wi]);
|
---|
| 1284 | } else if (IsNumber(b) && ((NumberTreeNode)b).Value == 1.0) {
|
---|
[14843] | 1285 | // $ * 1.0 => $
|
---|
| 1286 | return a;
|
---|
[18146] | 1287 | } else if (IsNumber(b) && ((NumberTreeNode)b).Value == 0.0) {
|
---|
| 1288 | return Number(0);
|
---|
| 1289 | } else if (IsNumber(b) && IsVariableBase(a)) {
|
---|
[14843] | 1290 | // multiply constants into variables weights
|
---|
[18146] | 1291 | ((VariableTreeNodeBase)a).Weight *= ((NumberTreeNode)b).Value;
|
---|
[14843] | 1292 | return a;
|
---|
[18146] | 1293 | } else if (IsNumber(b) && IsAddition(a) ||
|
---|
[14843] | 1294 | IsFactor(b) && IsAddition(a) ||
|
---|
| 1295 | IsBinFactor(b) && IsAddition(a)) {
|
---|
[18146] | 1296 | // multiply numbers into additions
|
---|
| 1297 | return a.Subtrees.Select(x => Product(GetSimplifiedTree(x), GetSimplifiedTree(b))).Aggregate((c, d) => Sum(c, d));
|
---|
[14843] | 1298 | } else if (IsDivision(a) && IsDivision(b)) {
|
---|
| 1299 | // (a1 / a2) * (b1 / b2) => (a1 * b1) / (a2 * b2)
|
---|
[18146] | 1300 | return Fraction(Product(a.GetSubtree(0), b.GetSubtree(0)), Product(a.GetSubtree(1), b.GetSubtree(1)));
|
---|
[14843] | 1301 | } else if (IsDivision(a)) {
|
---|
| 1302 | // (a1 / a2) * b => (a1 * b) / a2
|
---|
[18146] | 1303 | return Fraction(Product(a.GetSubtree(0), b), a.GetSubtree(1));
|
---|
[14843] | 1304 | } else if (IsDivision(b)) {
|
---|
| 1305 | // a * (b1 / b2) => (b1 * a) / b2
|
---|
[18146] | 1306 | return Fraction(Product(b.GetSubtree(0), a), b.GetSubtree(1));
|
---|
[14843] | 1307 | } else if (IsMultiplication(a) && IsMultiplication(b)) {
|
---|
| 1308 | // merge multiplications (make sure constants are merged)
|
---|
| 1309 | var mul = mulSymbol.CreateTreeNode();
|
---|
| 1310 | for (int i = 0; i < a.Subtrees.Count(); i++) mul.AddSubtree(a.GetSubtree(i));
|
---|
| 1311 | for (int i = 0; i < b.Subtrees.Count(); i++) mul.AddSubtree(b.GetSubtree(i));
|
---|
| 1312 | MergeVariablesAndConstantsInProduct(mul);
|
---|
| 1313 | return mul;
|
---|
| 1314 | } else if (IsMultiplication(b)) {
|
---|
[18146] | 1315 | return Product(b, a);
|
---|
[14843] | 1316 | } else if (IsMultiplication(a)) {
|
---|
| 1317 | // a is already an multiplication => append b
|
---|
| 1318 | a.AddSubtree(GetSimplifiedTree(b));
|
---|
| 1319 | MergeVariablesAndConstantsInProduct(a);
|
---|
| 1320 | return a;
|
---|
[16356] | 1321 | } else if (IsAbsolute(a) && IsAbsolute(b)) {
|
---|
[18146] | 1322 | return Abs(Product(a.GetSubtree(0), b.GetSubtree(0)));
|
---|
| 1323 | } else if (IsAbsolute(a) && IsNumber(b)) {
|
---|
| 1324 | var bNode = b as NumberTreeNode;
|
---|
| 1325 | var posF = Math.Abs(bNode.Value);
|
---|
| 1326 | if (bNode.Value > 0) {
|
---|
| 1327 | return Abs(Product(a.GetSubtree(0), Number(posF)));
|
---|
[16356] | 1328 | } else {
|
---|
| 1329 | var mul = mulSymbol.CreateTreeNode();
|
---|
[18146] | 1330 | mul.AddSubtree(Abs(Product(a.GetSubtree(0), Number(posF))));
|
---|
| 1331 | mul.AddSubtree(Number(-1.0));
|
---|
[16356] | 1332 | return mul;
|
---|
| 1333 | }
|
---|
| 1334 | } else if (IsAnalyticalQuotient(a)) {
|
---|
[18146] | 1335 | return AQ(Product(a.GetSubtree(0), b), a.GetSubtree(1));
|
---|
[14843] | 1336 | } else {
|
---|
| 1337 | var mul = mulSymbol.CreateTreeNode();
|
---|
| 1338 | mul.AddSubtree(a);
|
---|
| 1339 | mul.AddSubtree(b);
|
---|
| 1340 | MergeVariablesAndConstantsInProduct(mul);
|
---|
| 1341 | return mul;
|
---|
| 1342 | }
|
---|
| 1343 | }
|
---|
| 1344 |
|
---|
| 1345 | #endregion
|
---|
| 1346 |
|
---|
| 1347 | #region helper functions
|
---|
| 1348 |
|
---|
[14949] | 1349 | private static bool ContainsVariableCondition(ISymbolicExpressionTreeNode node) {
|
---|
[14843] | 1350 | if (node.Symbol is VariableCondition) return true;
|
---|
| 1351 | foreach (var subtree in node.Subtrees)
|
---|
| 1352 | if (ContainsVariableCondition(subtree)) return true;
|
---|
| 1353 | return false;
|
---|
| 1354 | }
|
---|
| 1355 |
|
---|
[14949] | 1356 | private static ISymbolicExpressionTreeNode AddLagToDynamicNodes(ISymbolicExpressionTreeNode node, int lag) {
|
---|
[18146] | 1357 | if (node is ILaggedTreeNode laggedTreeNode)
|
---|
[14843] | 1358 | laggedTreeNode.Lag += lag;
|
---|
[18146] | 1359 | else if (node is VariableTreeNode variableNode) {
|
---|
[14843] | 1360 | var laggedVariableNode = (LaggedVariableTreeNode)laggedVariableSymbol.CreateTreeNode();
|
---|
| 1361 | laggedVariableNode.Lag = lag;
|
---|
| 1362 | laggedVariableNode.VariableName = variableNode.VariableName;
|
---|
| 1363 | return laggedVariableNode;
|
---|
[18146] | 1364 | } else if (node is VariableConditionTreeNode) {
|
---|
[14843] | 1365 | throw new NotSupportedException("Removal of time lags around variable condition symbols is not allowed.");
|
---|
| 1366 | }
|
---|
| 1367 | var subtrees = new List<ISymbolicExpressionTreeNode>(node.Subtrees);
|
---|
| 1368 | while (node.SubtreeCount > 0) node.RemoveSubtree(0);
|
---|
| 1369 | foreach (var subtree in subtrees) {
|
---|
| 1370 | node.AddSubtree(AddLagToDynamicNodes(subtree, lag));
|
---|
| 1371 | }
|
---|
| 1372 | return node;
|
---|
| 1373 | }
|
---|
| 1374 |
|
---|
[14949] | 1375 | private static bool AreSameTypeAndVariable(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
|
---|
[14843] | 1376 | return GroupId((IVariableTreeNode)a) == GroupId((IVariableTreeNode)b);
|
---|
| 1377 | }
|
---|
| 1378 |
|
---|
| 1379 | // helper to combine the constant factors in products and to combine variables (powers of 2, 3...)
|
---|
[14949] | 1380 | private static void MergeVariablesAndConstantsInProduct(ISymbolicExpressionTreeNode prod) {
|
---|
[14843] | 1381 | var subtrees = new List<ISymbolicExpressionTreeNode>(prod.Subtrees);
|
---|
| 1382 | while (prod.Subtrees.Any()) prod.RemoveSubtree(0);
|
---|
| 1383 | var groupedVarNodes = from node in subtrees.OfType<IVariableTreeNode>()
|
---|
| 1384 | where node.SubtreeCount == 0
|
---|
| 1385 | group node by GroupId(node) into g
|
---|
| 1386 | orderby g.Count()
|
---|
| 1387 | select g;
|
---|
[18146] | 1388 | var numberProduct = (from node in subtrees.OfType<VariableTreeNodeBase>()
|
---|
| 1389 | select node.Weight)
|
---|
| 1390 | .Concat(from node in subtrees.OfType<NumberTreeNode>()
|
---|
| 1391 | select node.Value)
|
---|
| 1392 | .DefaultIfEmpty(1.0)
|
---|
| 1393 | .Aggregate((c1, c2) => c1 * c2);
|
---|
[14843] | 1394 |
|
---|
| 1395 | var unchangedSubtrees = from tree in subtrees
|
---|
[18146] | 1396 | where tree.SubtreeCount > 0 || !(tree is IVariableTreeNode) && !(tree is NumberTreeNode)
|
---|
[14843] | 1397 | select tree;
|
---|
| 1398 |
|
---|
| 1399 | foreach (var variableNodeGroup in groupedVarNodes) {
|
---|
| 1400 | var firstNode = variableNodeGroup.First();
|
---|
| 1401 | if (firstNode is VariableTreeNodeBase) {
|
---|
| 1402 | var representative = (VariableTreeNodeBase)firstNode;
|
---|
| 1403 | representative.Weight = 1.0;
|
---|
| 1404 | if (variableNodeGroup.Count() > 1) {
|
---|
| 1405 | var poly = mulSymbol.CreateTreeNode();
|
---|
| 1406 | for (int p = 0; p < variableNodeGroup.Count(); p++) {
|
---|
| 1407 | poly.AddSubtree((ISymbolicExpressionTreeNode)representative.Clone());
|
---|
| 1408 | }
|
---|
| 1409 | prod.AddSubtree(poly);
|
---|
| 1410 | } else {
|
---|
| 1411 | prod.AddSubtree(representative);
|
---|
| 1412 | }
|
---|
| 1413 | } else if (firstNode is FactorVariableTreeNode) {
|
---|
| 1414 | var representative = (FactorVariableTreeNode)firstNode;
|
---|
| 1415 | foreach (var node in variableNodeGroup.Skip(1).Cast<FactorVariableTreeNode>()) {
|
---|
| 1416 | for (int j = 0; j < representative.Weights.Length; j++) {
|
---|
| 1417 | representative.Weights[j] *= node.Weights[j];
|
---|
| 1418 | }
|
---|
| 1419 | }
|
---|
| 1420 | for (int j = 0; j < representative.Weights.Length; j++) {
|
---|
[18146] | 1421 | representative.Weights[j] *= numberProduct;
|
---|
[14843] | 1422 | }
|
---|
[18146] | 1423 | numberProduct = 1.0;
|
---|
[14843] | 1424 | // if the product already contains a factor it is not necessary to multiply a constant below
|
---|
| 1425 | prod.AddSubtree(representative);
|
---|
| 1426 | }
|
---|
| 1427 | }
|
---|
| 1428 |
|
---|
| 1429 | foreach (var unchangedSubtree in unchangedSubtrees)
|
---|
| 1430 | prod.AddSubtree(unchangedSubtree);
|
---|
| 1431 |
|
---|
[18146] | 1432 | if (numberProduct != 1.0) {
|
---|
| 1433 | prod.AddSubtree(Number(numberProduct));
|
---|
[14843] | 1434 | }
|
---|
| 1435 | }
|
---|
| 1436 |
|
---|
| 1437 |
|
---|
| 1438 | /// <summary>
|
---|
| 1439 | /// x => x * -1
|
---|
| 1440 | /// Is only used in cases where it is not necessary to create new tree nodes. Manipulates x directly.
|
---|
| 1441 | /// </summary>
|
---|
| 1442 | /// <param name="x"></param>
|
---|
| 1443 | /// <returns>-x</returns>
|
---|
[14949] | 1444 | private static ISymbolicExpressionTreeNode Negate(ISymbolicExpressionTreeNode x) {
|
---|
[18146] | 1445 | if (IsNumber(x)) {
|
---|
| 1446 | ((NumberTreeNode)x).Value *= -1;
|
---|
[14843] | 1447 | } else if (IsVariableBase(x)) {
|
---|
| 1448 | var variableTree = (VariableTreeNodeBase)x;
|
---|
| 1449 | variableTree.Weight *= -1.0;
|
---|
| 1450 | } else if (IsFactor(x)) {
|
---|
| 1451 | var factorNode = (FactorVariableTreeNode)x;
|
---|
| 1452 | for (int i = 0; i < factorNode.Weights.Length; i++) factorNode.Weights[i] *= -1;
|
---|
| 1453 | } else if (IsBinFactor(x)) {
|
---|
| 1454 | var factorNode = (BinaryFactorVariableTreeNode)x;
|
---|
| 1455 | factorNode.Weight *= -1;
|
---|
| 1456 | } else if (IsAddition(x)) {
|
---|
| 1457 | // (x0 + x1 + .. + xn) * -1 => (-x0 + -x1 + .. + -xn)
|
---|
| 1458 | var subtrees = new List<ISymbolicExpressionTreeNode>(x.Subtrees);
|
---|
| 1459 | while (x.Subtrees.Any()) x.RemoveSubtree(0);
|
---|
| 1460 | foreach (var subtree in subtrees) {
|
---|
| 1461 | x.AddSubtree(Negate(subtree));
|
---|
| 1462 | }
|
---|
| 1463 | } else if (IsMultiplication(x) || IsDivision(x)) {
|
---|
| 1464 | // x0 * x1 * .. * xn * -1 => x0 * x1 * .. * -xn
|
---|
| 1465 | var lastSubTree = x.Subtrees.Last();
|
---|
| 1466 | x.RemoveSubtree(x.SubtreeCount - 1);
|
---|
| 1467 | x.AddSubtree(Negate(lastSubTree)); // last is maybe a constant, prefer to negate the constant
|
---|
| 1468 | } else {
|
---|
| 1469 | // any other function
|
---|
[18146] | 1470 | return Product(x, Number(-1));
|
---|
[14843] | 1471 | }
|
---|
| 1472 | return x;
|
---|
| 1473 | }
|
---|
| 1474 |
|
---|
| 1475 | /// <summary>
|
---|
| 1476 | /// x => 1/x
|
---|
| 1477 | /// Must create new tree nodes
|
---|
| 1478 | /// </summary>
|
---|
| 1479 | /// <param name="x"></param>
|
---|
| 1480 | /// <returns></returns>
|
---|
[14949] | 1481 | private static ISymbolicExpressionTreeNode Invert(ISymbolicExpressionTreeNode x) {
|
---|
[18146] | 1482 | if (IsNumber(x)) {
|
---|
| 1483 | return Number(1.0 / ((NumberTreeNode)x).Value);
|
---|
[14843] | 1484 | } else if (IsFactor(x)) {
|
---|
| 1485 | var factorNode = (FactorVariableTreeNode)x;
|
---|
[18146] | 1486 | return Factor(factorNode.Symbol, factorNode.VariableName, factorNode.Weights.Select(w => 1.0 / w));
|
---|
[14843] | 1487 | } else if (IsDivision(x)) {
|
---|
[18146] | 1488 | return Fraction(x.GetSubtree(1), x.GetSubtree(0));
|
---|
[14843] | 1489 | } else {
|
---|
| 1490 | // any other function
|
---|
[18146] | 1491 | return Fraction(Number(1), x);
|
---|
[14843] | 1492 | }
|
---|
| 1493 | }
|
---|
| 1494 |
|
---|
[18146] | 1495 | private static ISymbolicExpressionTreeNode Number(double value) {
|
---|
| 1496 | var numberTreeNode = (NumberTreeNode)numberSymbol.CreateTreeNode();
|
---|
| 1497 | numberTreeNode.Value = value;
|
---|
| 1498 | return numberTreeNode;
|
---|
[14843] | 1499 | }
|
---|
| 1500 |
|
---|
[18146] | 1501 | private static ISymbolicExpressionTreeNode Factor(FactorVariable sy, string variableName, IEnumerable<double> weights) {
|
---|
[14843] | 1502 | var tree = (FactorVariableTreeNode)sy.CreateTreeNode();
|
---|
| 1503 | tree.VariableName = variableName;
|
---|
| 1504 | tree.Weights = weights.ToArray();
|
---|
| 1505 | return tree;
|
---|
| 1506 | }
|
---|
[18146] | 1507 | private static ISymbolicExpressionTreeNode BinFactor(BinaryFactorVariable sy, string variableName, string variableValue, double weight) {
|
---|
[14843] | 1508 | var tree = (BinaryFactorVariableTreeNode)sy.CreateTreeNode();
|
---|
| 1509 | tree.VariableName = variableName;
|
---|
| 1510 | tree.VariableValue = variableValue;
|
---|
| 1511 | tree.Weight = weight;
|
---|
| 1512 | return tree;
|
---|
| 1513 | }
|
---|
| 1514 |
|
---|
| 1515 |
|
---|
| 1516 | #endregion
|
---|
| 1517 | }
|
---|
| 1518 | }
|
---|