本文整理汇总了C#中ISymbolicExpressionTreeNode.GetSubtree方法的典型用法代码示例。如果您正苦于以下问题:C# ISymbolicExpressionTreeNode.GetSubtree方法的具体用法?C# ISymbolicExpressionTreeNode.GetSubtree怎么用?C# ISymbolicExpressionTreeNode.GetSubtree使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ISymbolicExpressionTreeNode
的用法示例。
在下文中一共展示了ISymbolicExpressionTreeNode.GetSubtree方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FormatOnlyExpression
public string FormatOnlyExpression(ISymbolicExpressionTreeNode expressionNode) {
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine(" for " + CurrentIndexVariable + " = 1:1:rows");
stringBuilder.AppendLine(" estimated(" + CurrentIndexVariable + ") = " + FormatRecursively(expressionNode.GetSubtree(0)) + ";");
stringBuilder.AppendLine(" end;");
return stringBuilder.ToString();
}
示例2: RenderNode
public static void RenderNode(TextWriter writer, ISymbolicExpressionTreeNode node, string prefix) {
string label = node.ToString();
writer.Write(label);
if (node.SubtreeCount > 0) {
var padding = prefix + new string(' ', label.Length);
for (int i = 0; i != node.SubtreeCount; ++i) {
char connector, extender = ' ';
if (i == 0) {
if (node.SubtreeCount > 1) {
connector = RenderChars.JunctionDown;
extender = RenderChars.VerticalLine;
} else {
connector = RenderChars.HorizontalLine;
extender = ' ';
}
} else {
writer.Write(padding);
if (i == node.SubtreeCount - 1) {
connector = RenderChars.CornerRight;
extender = ' ';
} else {
connector = RenderChars.JunctionRight;
extender = RenderChars.VerticalLine;
}
}
writer.Write(string.Concat(connector, RenderChars.HorizontalLine));
var newPrefix = string.Concat(padding, extender, ' ');
RenderNode(writer, node.GetSubtree(i), newPrefix);
}
} else
writer.WriteLine();
}
示例3: Match
///<summary>
/// Finds the longest common subsequence in quadratic time and linear space
/// Variant of:
/// D. S. Hirschberg. A linear space algorithm for or computing maximal common subsequences. 1975.
/// http://dl.acm.org/citation.cfm?id=360861
/// </summary>
/// <returns>Number of pairs that were matched</returns>
public static int Match(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b, ISymbolicExpressionTreeNodeSimilarityComparer comp) {
if (!comp.Equals(a, b)) return 0;
int m = a.SubtreeCount;
int n = b.SubtreeCount;
if (m == 0 || n == 0) return 1;
var matrix = new int[m + 1, n + 1];
for (int i = 1; i <= m; ++i) {
var ai = a.GetSubtree(i - 1);
for (int j = 1; j <= n; ++j) {
var bj = b.GetSubtree(j - 1);
int match = Match(ai, bj, comp);
matrix[i, j] = Math.Max(Math.Max(matrix[i, j - 1], matrix[i - 1, j]), matrix[i - 1, j - 1] + match);
}
}
return matrix[m, n] + 1;
}
示例4: Modify
/// <summary>
/// Remove, Replace or Insert subtrees
/// </summary>
/// <param name="tree">The symbolic expression tree</param>
/// <param name="parent">The insertion point (ie, the parent node who will receive a new child)</param>
/// <param name="oldChild">The subtree to be replaced</param>
/// <param name="newChild">The replacement subtree</param>
/// <param name="removeSubtree">Flag used to indicate if whole subtrees should be removed (default behavior), or just the subtree root</param>
private void Modify(ISymbolicExpressionTree tree, ISymbolicExpressionTreeNode parent,
ISymbolicExpressionTreeNode oldChild, ISymbolicExpressionTreeNode newChild, bool removeSubtree = true) {
if (oldChild == null && newChild == null)
throw new ArgumentNullException("Cannot deduce operation type from the arguments. Please provide non null operands.");
if (oldChild == null) {
// insertion operation
parent.AddSubtree(newChild);
newChild.Parent = parent;
} else if (newChild == null) {
// removal operation
parent.RemoveSubtree(parent.IndexOfSubtree(oldChild));
if (!removeSubtree) {
for (int i = oldChild.SubtreeCount - 1; i >= 0; --i) {
var subtree = oldChild.GetSubtree(i);
oldChild.RemoveSubtree(i);
parent.AddSubtree(subtree);
}
}
} else {
// replacement operation
var replacementIndex = parent.IndexOfSubtree(oldChild);
parent.RemoveSubtree(replacementIndex);
parent.InsertSubtree(replacementIndex, newChild);
newChild.Parent = parent;
if (changedNodes.ContainsKey(oldChild)) {
changedNodes.Add(newChild, changedNodes[oldChild]); // so that on double click the original node is restored
changedNodes.Remove(oldChild);
} else {
changedNodes.Add(newChild, oldChild);
}
}
treeState = IsValid(tree) ? TreeState.Valid : TreeState.Invalid;
switch (treeState) {
case TreeState.Valid:
this.grpViewHost.Enabled = true;
UpdateModel(Content.Model.SymbolicExpressionTree);
break;
case TreeState.Invalid:
this.grpViewHost.Enabled = false;
break;
}
}
示例5: FormatRecursively
private string FormatRecursively(ISymbolicExpressionTreeNode node) {
StringBuilder strBuilder = new StringBuilder();
currentLag = 0;
FormatBegin(node, strBuilder);
if (node.SubtreeCount > 0) {
strBuilder.Append(FormatRecursively(node.GetSubtree(0)));
}
int i = 1;
foreach (SymbolicExpressionTreeNode subTree in node.Subtrees.Skip(1)) {
FormatSep(node, strBuilder, i);
// format the whole subtree
strBuilder.Append(FormatRecursively(subTree));
i++;
}
FormatEnd(node, strBuilder);
return strBuilder.ToString();
}
示例6: FormatSubtraction
private void FormatSubtraction(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
strBuilder.Append("Subtract[");
FormatRecursively(node.GetSubtree(0), strBuilder);
strBuilder.Append(", Times[-1");
foreach (var t in node.Subtrees) {
strBuilder.Append(",");
FormatRecursively(t, strBuilder);
}
strBuilder.Append("]]");
}
示例7: FormatAverage
private void FormatAverage(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
// mean function needs a list of values
strBuilder.Append("Mean[{");
FormatRecursively(node.GetSubtree(0), strBuilder);
for (int i = 1; i < node.SubtreeCount; i++) {
strBuilder.Append(",");
FormatRecursively(node.GetSubtree(i), strBuilder);
}
strBuilder.Append("}]");
}
示例8: FormatIf
private void FormatIf(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
strBuilder.Append("If[Greater[");
FormatRecursively(node.GetSubtree(0), strBuilder);
strBuilder.Append(", 0], ");
FormatRecursively(node.GetSubtree(1), strBuilder);
strBuilder.Append(", ");
FormatRecursively(node.GetSubtree(2), strBuilder);
strBuilder.Append("]");
}
示例9: FormatRecursively
private string FormatRecursively(ISymbolicExpressionTreeNode node) {
ISymbol symbol = node.Symbol;
StringBuilder stringBuilder = new StringBuilder();
if (symbol is ProgramRootSymbol) {
stringBuilder.AppendLine(FormatRecursively(node.GetSubtree(0)));
} else if (symbol is StartSymbol)
return FormatRecursively(node.GetSubtree(0));
else if (symbol is Addition) {
stringBuilder.Append("(");
for (int i = 0; i < node.SubtreeCount; i++) {
if (i > 0) stringBuilder.Append("+");
stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
}
stringBuilder.Append(")");
} else if (symbol is Average) {
stringBuilder.Append("(1/");
stringBuilder.Append(node.SubtreeCount);
stringBuilder.Append(")*(");
for (int i = 0; i < node.SubtreeCount; i++) {
if (i > 0) stringBuilder.Append("+");
stringBuilder.Append("(");
stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
stringBuilder.Append(")");
}
stringBuilder.Append(")");
} else if (symbol is Constant) {
ConstantTreeNode constantTreeNode = node as ConstantTreeNode;
stringBuilder.Append(constantTreeNode.Value.ToString(CultureInfo.InvariantCulture));
} else if (symbol is Cosine) {
stringBuilder.Append("COS(");
stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
stringBuilder.Append(")");
} else if (symbol is Division) {
if (node.SubtreeCount == 1) {
stringBuilder.Append("1/");
stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
} else {
stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
stringBuilder.Append("/(");
for (int i = 1; i < node.SubtreeCount; i++) {
if (i > 1) stringBuilder.Append("*");
stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
}
stringBuilder.Append(")");
}
} else if (symbol is Exponential) {
stringBuilder.Append("EXP(");
stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
stringBuilder.Append(")");
} else if (symbol is Square) {
stringBuilder.Append("POWER(");
stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
stringBuilder.Append(",2)");
} else if (symbol is SquareRoot) {
stringBuilder.Append("SQRT(");
stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
stringBuilder.Append(")");
} else if (symbol is Logarithm) {
stringBuilder.Append("LN(");
stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
stringBuilder.Append(")");
} else if (symbol is Multiplication) {
for (int i = 0; i < node.SubtreeCount; i++) {
if (i > 0) stringBuilder.Append("*");
stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
}
} else if (symbol is Sine) {
stringBuilder.Append("SIN(");
stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
stringBuilder.Append(")");
} else if (symbol is Subtraction) {
stringBuilder.Append("(");
if (node.SubtreeCount == 1) {
stringBuilder.Append("-");
stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
} else {
stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
for (int i = 1; i < node.SubtreeCount; i++) {
stringBuilder.Append("-");
stringBuilder.Append(FormatRecursively(node.GetSubtree(i)));
}
}
stringBuilder.Append(")");
} else if (symbol is Tangent) {
stringBuilder.Append("TAN(");
stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
stringBuilder.Append(")");
} else if (symbol is Variable) {
VariableTreeNode variableTreeNode = node as VariableTreeNode;
stringBuilder.Append(variableTreeNode.Weight.ToString(CultureInfo.InvariantCulture));
stringBuilder.Append("*");
stringBuilder.Append(GetColumnToVariableName(variableTreeNode.VariableName));// + LagToString(currentLag));
} else if (symbol is Power) {
stringBuilder.Append("POWER(");
stringBuilder.Append(FormatRecursively(node.GetSubtree(0)));
stringBuilder.Append(",ROUND(");
stringBuilder.Append(FormatRecursively(node.GetSubtree(1)));
stringBuilder.Append(",0))");
//.........这里部分代码省略.........
示例10: MakeFraction
// MakeFraction, MakeProduct and MakeSum take two already simplified trees and create a new simplified tree
private ISymbolicExpressionTreeNode MakeFraction(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {
if (IsConstant(a) && IsConstant(b)) {
// fold constants
return MakeConstant(((ConstantTreeNode)a).Value / ((ConstantTreeNode)b).Value);
} if (IsConstant(a) && !((ConstantTreeNode)a).Value.IsAlmost(1.0)) {
return MakeFraction(MakeConstant(1.0), MakeProduct(b, Invert(a)));
} else if (IsVariable(a) && IsConstant(b)) {
// merge constant values into variable weights
var constB = ((ConstantTreeNode)b).Value;
((VariableTreeNode)a).Weight /= constB;
return a;
} else if (IsVariable(a) && IsVariable(b) && AreSameVariable(a, b)) {
// cancel variables
var aVar = a as VariableTreeNode;
var bVar = b as VariableTreeNode;
return MakeConstant(aVar.Weight / bVar.Weight);
} else if (IsAddition(a) && IsConstant(b)) {
return a.Subtrees
.Select(x => GetSimplifiedTree(x))
.Select(x => MakeFraction(x, b))
.Aggregate((c, d) => MakeSum(c, d));
} else if (IsMultiplication(a) && IsConstant(b)) {
return MakeProduct(a, Invert(b));
} else if (IsDivision(a) && IsConstant(b)) {
// (a1 / a2) / c => (a1 / (a2 * c))
return MakeFraction(a.GetSubtree(0), MakeProduct(a.GetSubtree(1), b));
} else if (IsDivision(a) && IsDivision(b)) {
// (a1 / a2) / (b1 / b2) =>
return MakeFraction(MakeProduct(a.GetSubtree(0), b.GetSubtree(1)), MakeProduct(a.GetSubtree(1), b.GetSubtree(0)));
} else if (IsDivision(a)) {
// (a1 / a2) / b => (a1 / (a2 * b))
return MakeFraction(a.GetSubtree(0), MakeProduct(a.GetSubtree(1), b));
} else if (IsDivision(b)) {
// a / (b1 / b2) => (a * b2) / b1
return MakeFraction(MakeProduct(a, b.GetSubtree(1)), b.GetSubtree(0));
} else {
var div = divSymbol.CreateTreeNode();
div.AddSubtree(a);
div.AddSubtree(b);
return div;
}
}
示例11: InterpretComparison
public static string InterpretComparison(string compSy, ISymbolicExpressionTreeNode node) {
ISymbolicExpressionTreeNode lhs = null, rhs = null;
if (node.SubtreeCount != 2)
throw new ArgumentException(string.Format("Expected exactly two children in {0}.", node.Symbol), "node");
lhs = node.GetSubtree(0);
rhs = node.GetSubtree(1);
return Interpret(lhs) + compSy + Interpret(rhs);
}
示例12: FormatRecursively
private void FormatRecursively(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
if (node.SubtreeCount > 1) {
var token = GetToken(node.Symbol);
if (token == "+" || token == "-" || token == "OR" || token == "XOR") {
strBuilder.Append("(");
FormatRecursively(node.Subtrees.First(), strBuilder);
foreach (var subtree in node.Subtrees.Skip(1)) {
strBuilder.Append(" ").Append(token).Append(" ");
FormatRecursively(subtree, strBuilder);
}
strBuilder.Append(")");
} else if (token == "*" || token == "/" || token == "AND") {
strBuilder.Append("(");
FormatRecursively(node.Subtrees.First(), strBuilder);
foreach (var subtree in node.Subtrees.Skip(1)) {
strBuilder.Append(" ").Append(token).Append(" ");
FormatRecursively(subtree, strBuilder);
}
strBuilder.Append(")");
}
} else if (node.SubtreeCount == 1) {
var token = GetToken(node.Symbol);
if (token == "-" || token == "NOT") {
strBuilder.Append("(").Append(token).Append("(");
FormatRecursively(node.GetSubtree(0), strBuilder);
strBuilder.Append("))");
} else if (token == "/") {
strBuilder.Append("1/");
FormatRecursively(node.GetSubtree(0), strBuilder);
} else if (token == "+" || token == "*") {
FormatRecursively(node.GetSubtree(0), strBuilder);
} else {
// function
strBuilder.Append(token).Append("(");
FormatRecursively(node.GetSubtree(0), strBuilder);
strBuilder.Append(")");
}
} else {
// no subtrees
if (node.Symbol is Variable) {
var varNode = node as VariableTreeNode;
if (!varNode.Weight.IsAlmost(1.0)) {
strBuilder.Append("(");
strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", varNode.Weight);
strBuilder.Append("*");
}
if (varNode.VariableName.Contains("'")) {
strBuilder.AppendFormat("\"{0}\"", varNode.VariableName);
} else {
strBuilder.AppendFormat("'{0}'", varNode.VariableName);
}
if (!varNode.Weight.IsAlmost(1.0)) {
strBuilder.Append(")");
}
} else if (node.Symbol is Constant) {
var constNode = node as ConstantTreeNode;
if (constNode.Value >= 0.0)
strBuilder.AppendFormat(CultureInfo.InvariantCulture, "{0}", constNode.Value);
else
strBuilder.AppendFormat(CultureInfo.InvariantCulture, "({0})", constNode.Value); // (-1)
}
}
}
示例13: InterpretStat
internal static string InterpretStat(ISymbolicExpressionTreeNode node) {
// must only have one sub-tree
Contract.Assert(node.SubtreeCount == 1);
return Interpret(node.GetSubtree(0)) + " ;" + Environment.NewLine;
}
示例14: FormatDivision
private void FormatDivision(ISymbolicExpressionTreeNode node, StringBuilder strBuilder) {
if (node.SubtreeCount == 1) {
strBuilder.Append("Divide[1, ");
FormatRecursively(node.GetSubtree(0), strBuilder);
strBuilder.Append("]");
} else {
strBuilder.Append("Divide[");
FormatRecursively(node.GetSubtree(0), strBuilder);
strBuilder.Append(", Times[");
FormatRecursively(node.GetSubtree(1), strBuilder);
for (int i = 2; i < node.SubtreeCount; i++) {
strBuilder.Append(",");
FormatRecursively(node.GetSubtree(i), strBuilder);
}
strBuilder.Append("]]");
}
}
示例15: SwitchNodeWithReplacementNode
private void SwitchNodeWithReplacementNode(ISymbolicExpressionTreeNode parent, int subTreeIndex) {
ISymbolicExpressionTreeNode subTree = parent.GetSubtree(subTreeIndex);
if (foldedNodes.ContainsKey(subTree)) {
parent.RemoveSubtree(subTreeIndex);
var replacementNode = foldedNodes[subTree];
parent.InsertSubtree(subTreeIndex, replacementNode);
// exchange key and value
foldedNodes.Remove(subTree);
foldedNodes.Add(replacementNode, subTree);
}
}