public override FNode CloneOfMe() { FNodeResult Dolly = new FNodeResult(this.ParentNode, this._Func); foreach (FNode n in this._Cache) Dolly.AddChildNode(n.CloneOfMe()); return Dolly; }
private bool ItterateOnce() { // Create the cluster mapping FNode; this node does the nearest neighbor test // FNodeSet keys = new FNodeSet(); FNode n = new FNodeResult(null, new RowClusterCellFunction(this._rule, this._means)); foreach (FNode t in this._fields.Nodes) { n.AddChildNode(t.CloneOfMe()); } keys.Add("CLUSTER_ID", n); // Create the aggregate// AggregateSet set = new AggregateSet(); // Add a counter to the aggregate // set.Add(new AggregateSum(FNodeFactory.Value(1D)), "CLUSTER_ELEMENT_COUNT"); // load the aggregate with the mean aggregates // for (int i = 0; i < this._fields.Count; i++) { set.Add(new AggregateAverage(this._fields[i].CloneOfMe()), this._fields.Alias(i)); } // Load the aggregate with the variance aggregates // for (int i = 0; i < this._fields.Count; i++) { set.Add(new AggregateVarianceP(this._fields[i].CloneOfMe()), "VAR_" + this._fields.Alias(i)); } // Run the aggregate; this is basically a horse aggregate step with the cluster node mapping as the key, and averaging as the value RecordSet rs = AggregatePlan.Render(this._data, this._where, keys, set); // Need to chop up the recordset we just created // Key mean_keeper = Key.Build(this._means.Columns.Count); RecordSet means = FastReadPlan.Render(rs, Predicate.TrueForAll, mean_keeper, long.MaxValue); Key stat_keeper = new Key(0,1); // keep the id and the count for (int i = mean_keeper.Count; i < rs.Columns.Count; i++) { stat_keeper.Add(i); } this._stats = FastReadPlan.Render(rs, Predicate.TrueForAll, stat_keeper, long.MaxValue); // Check for cluster misses; cluster misses occur when no node maps to a cluster correctly // if (means.Count != this._means.Count) { this.HandleNullCluster(means); } // Compare the changes between itterations double change = this.CompareChanges(this._means, means); // Set the means to the newly calculated means // this._means = means; // Return a boolean indicating if we failed or not return change < this._exit_condition; }
public static FNode LinkAnd(FNode Left, FNode Right) { FNodeResult node = new FNodeResult(null, CellFunctionFactory.LookUp("and")); node.AddChildNode(Left); node.AddChildNode(Right); return node; }
// F(X) = power(Y, G(X)), F'(X) = LOG(Y) * power(Y, G(X)) * G'(X) private static FNode GradientOfPowerUpper(FNode Node, FNodePointer X) { // Throw an exception if X is decendant of Y, in otherwords F(X) = Power(G(X), H(X)) if (FNodeAnalysis.IsDecendent(X, Node.Children[1])) throw new Exception(string.Format("Cannot differentiate the power function with the form: Power(G(X), H(X)); G(X) cannot have a relation to X")); // LOG(Y) // FNode log_y = new FNodeResult(null, new CellFuncFVLog()); log_y.AddChildNode(Node.Children[0]); // Get Power(Y, G(X)) * LOG(Y) // FNode pow_f_dx = new FNodeResult(Node.ParentNode, new CellBinMult()); pow_f_dx.AddChildNode(Node.CloneOfMe()); pow_f_dx.AddChildNode(log_y); // Get Power(G(X), N-1) * G'(X) // FNode t = new FNodeResult(Node.ParentNode, new CellBinMult()); t.AddChildNode(pow_f_dx); t.AddChildNode(Gradient(Node.Children[1], X)); return t; }
// Main Join Functions // /// <summary> /// Allows the user to perform a join based on the equality predicate AND each predicate link via 'AND' /// </summary> /// <param name="Output"></param> /// <param name="JM"></param> /// <param name="JA"></param> /// <param name="T1"></param> /// <param name="J1"></param> /// <param name="T2"></param> /// <param name="J2"></param> /// <param name="CM"></param> public static void Join(MergeMethod JM, MergeAlgorithm JA, RecordWriter Output, FNodeSet Fields, Predicate Where, DataSet Data1, DataSet Data2, Key Equality1, Key Equality2, StaticRegister Memory1, StaticRegister Memory2) { // Do some checks first // if (Where == null) Where = Predicate.TrueForAll; if (Equality1.Count != Equality2.Count) throw new Exception("Both join keys must have the same length"); if (Equality1.Count == 0) JA = MergeAlgorithm.NestedLoop; // Nested loop; if the algorithm is nested loop and we have keys, we need to build a new where clause that has the equality predicates // FNodeResult nl_node = new FNodeResult(null, new AndMany()); nl_node.AddChildNode(Where.Node.CloneOfMe()); for (int i = 0; i < Equality1.Count; i++) { FNodeFieldRef left = new FNodeFieldRef(null, Equality1[i], Data1.Columns.ColumnAffinity(Equality1[i]), Data1.Columns.ColumnSize(Equality1[i]), Memory1); FNodeFieldRef right = new FNodeFieldRef(null, Equality2[i], Data2.Columns.ColumnAffinity(Equality2[i]), Data2.Columns.ColumnSize(Equality2[i]), Memory2); FNodeResult eq = new FNodeResult(null, new CellBoolEQ()); eq.AddChildren(left, right); nl_node.AddChildNode(eq); } Predicate nl_where = (Equality1.Count == 0 ? Where : new Predicate(nl_node)); // Switch // switch (JA) { case MergeAlgorithm.SortMerge: SortMerge(JM, Output, Fields, Where, Data1, Data2, Equality1, Equality2, Memory1, Memory2); break; case MergeAlgorithm.NestedLoop: NestedLoop(JM, Output, Fields, nl_where, Data1, Data2, Memory1, Memory2); break; default: HashTable(JM, Output, Fields, Where, Data1, Data2, Equality1, Equality2, Memory1, Memory2); break; } }
// A * 1 or 1 * A or A / 1 or A /? 1 or A % 1 -> A // A * -1 or -1 * A or A / -1 or A /? -1 or A % -1 -> -A // A * 0, 0 * A, 0 / A, 0 /? A, A /? 0, 0 % A -> 0 // A / 0, A % 0 -> null private FNode CompactMultDivMod(FNode Node) { if (Node.Affinity != FNodeAffinity.ResultNode) return Node; FNodeResult x = (Node as FNodeResult); string name = x.InnerFunction.NameSig; if (name != FunctionNames.OP_MUL && name != FunctionNames.OP_DIV && name != FunctionNames.OP_DIV2 && name != FunctionNames.OP_MOD) return Node; // A * 1 or A / 1 or A /? 1 or A % 1 // if (IsStaticOne(Node.Children[1])) { this._Tocks++; return Node.Children[0]; } // 1 * A // if (IsStaticOne(Node.Children[0]) && name == FunctionNames.OP_MUL) { this._Tocks++; return Node.Children[1]; } // A * -1 or A / -1 or A /? -1 or A % -1 // if (IsStaticMinusOne(Node.Children[1])) { this._Tocks++; FNode t = new FNodeResult(Node.ParentNode, new CellUniMinus()); t.AddChildNode(Node.Children[0]); return t; } // -1 * A // if (IsStaticMinusOne(Node.Children[0]) && name == FunctionNames.OP_MUL) { this._Tocks++; FNode t = new FNodeResult(Node.ParentNode, new CellUniMinus()); t.AddChildNode(Node.Children[1]); return t; } // Look 0 * A, 0 / A, 0 /? A, 0 % A // if (IsStaticZero(Node.Children[0])) { this._Tocks++; return new FNodeValue(Node.ParentNode, new Cell(0.00)); } // A * 0, A /? 0 // if (IsStaticZero(Node.Children[1]) && (name == FunctionNames.OP_MUL || name == FunctionNames.OP_DIV2)) { this._Tocks++; return new FNodeValue(Node.ParentNode, new Cell(0.00)); } // A / 0, A % 0 // if (IsStaticZero(Node.Children[1]) && (name == FunctionNames.OP_DIV || name == FunctionNames.OP_MOD)) { this._Tocks++; return new FNodeValue(Node.ParentNode, Cell.NULL_DOUBLE); } return Node; }
public static Predicate NotEquals(FNode Left, FNode Right) { FNodeResult node = new FNodeResult(null, CellFunctionFactory.LookUp("!=")); node.AddChildNode(Left); node.AddChildNode(Right); return new Predicate(node); }
public override RecordSet Initialize(DataSet Data, Predicate Where, FNodeSet Fields, int Clusters) { AggregateSet set = new AggregateSet(); set.Add(new AggregateSum(FNodeFactory.Value(1D)), "CLUSTER_ELEMENT_COUNT"); for (int i = 0; i < Fields.Count; i++) { set.Add(new AggregateAverage(Fields[i].CloneOfMe()), Fields.Alias(i)); } FNode rnd = new FNodeResult(null, new CellRandomInt()); rnd.AddChildNode(new FNodeValue(rnd, new Cell(this.Seed))); rnd.AddChildNode(new FNodeValue(rnd, new Cell(0))); rnd.AddChildNode(new FNodeValue(rnd, new Cell(Clusters))); FNodeSet keys = new FNodeSet(); keys.Add(rnd); RecordSet rs = AggregatePlan.Render(Data, Where, keys, set); return rs; }
// F(X) = sin(G(X)), F'(X) = cos(G(X)) * G'(X) private static FNode GradientOfSin(FNode Node, FNodePointer X) { // cos(G(X)) // FNodeResult t = new FNodeResult(Node.ParentNode, new CellFuncFVCos()); t.AddChildNode(Node.Children[0]); // 1 / G(X) // FNodeResult u = new FNodeResult(Node.ParentNode, new CellBinMult()); u.AddChildNode(t); u.AddChildNode(Gradient(t.Children[0], X)); return u; }
// F(X) = log(G(X)), F'(X) = 1 / G(X) * G'(X) private static FNode GradientOfLog(FNode Node, FNodePointer X) { // 1 / G(X) // FNodeResult t = new FNodeResult(Node.ParentNode, new CellFuncFVPower()); t.AddChildNode(Node.Children[0]); t.AddChildNode(new FNodeValue(null, new Cell(-1.00))); // 1 / G(X) // FNodeResult u = new FNodeResult(Node.ParentNode, new CellBinMult()); u.AddChildNode(t); u.AddChildNode(Gradient(t.Children[0], X)); return u; }
// F(X) = exp(G(X)), F'(X) = exp(G(X)) * G'(X), or simplified F(X) * G'(X) private static FNode GradientOfExp(FNode Node, FNodePointer X) { // F(X) * G'(X) // FNodeResult t = new FNodeResult(Node.ParentNode, new CellBinMult()); t.AddChildNode(Node.CloneOfMe()); t.AddChildNode(Gradient(Node.Children[0], X)); return t; }
// F(X) = G(X) / H(X), F'(X) = (G'(X) * H(X) - G(X) * H'(X)) / (H(X) * H(X)) private static FNode GradientOfDivide(FNode Node, FNodePointer X) { if (Debug) Comm.WriteLine("GRADIENT : DIVIDE"); // Need to handle the case where H is not a function of X // if (!FNodeAnalysis.ContainsPointerRef(Node[1], X.PointerName)) { FNode a = Gradient(Node[0], X); return FNodeFactory.Divide(a, Node[1]); } // G'(X) * H(X) // FNodeResult t = new FNodeResult(Node.ParentNode, new CellBinMult()); t.AddChildNode(Gradient(Node.Children[0], X)); t.AddChildNode(Node.Children[1]); // G(X) * H'(X) // FNodeResult u = new FNodeResult(Node.ParentNode, new CellBinMult()); u.AddChildNode(Node.Children[0]); u.AddChildNode(Gradient(Node.Children[1], X)); // G'(X) * H(X) - G(X) * H'(X) // FNodeResult v = new FNodeResult(Node.ParentNode, new CellBinMinus()); v.AddChildNode(t); v.AddChildNode(u); // H(X) * H(X) // FNodeResult w = new FNodeResult(Node.ParentNode, new CellFuncFVPower()); w.AddChildNode(Node.Children[1]); w.AddChildNode(new FNodeValue(null, new Cell(2.00))); // Final Node // FNodeResult x = new FNodeResult(Node.ParentNode, new CellBinDiv()); x.AddChildNode(v); x.AddChildNode(w); return x; }
// F(X) = G(X) * H(X), F'(X) = G'(X) * H(X) + G(X) * H'(X) private static FNode GradientOfMultiply(FNode Node, FNodePointer X) { if (Debug) Comm.WriteLine("GRADIENT : MULT"); // G'(X) * H(X) // FNodeResult t = new FNodeResult(Node.ParentNode, new CellBinMult()); t.AddChildNode(Gradient(Node.Children[0], X)); t.AddChildNode(Node.Children[1]); // G(X) * H'(X) // FNodeResult u = new FNodeResult(Node.ParentNode, new CellBinMult()); u.AddChildNode(Node.Children[0]); u.AddChildNode(Gradient(Node.Children[1], X)); // Final Node // FNodeResult v = new FNodeResult(Node.ParentNode, new CellBinPlus()); v.AddChildNode(t); v.AddChildNode(u); return v; }
// F(X) = G(X) - H(X), F'(X) = G'(X) - H'(X) private static FNode GradientOfSubtract(FNode Node, FNodePointer X) { if (Debug) Comm.WriteLine("GRADIENT : SUB"); FNodeResult t = new FNodeResult(Node.ParentNode, new CellBinMinus()); t.AddChildNode(Gradient(Node.Children[0], X)); t.AddChildNode(Gradient(Node.Children[1], X)); return t; }
// F(X) = -G(X), F'(X) = -G'(X) private static FNode GradientOfUniMinus(FNode Node, FNodePointer X) { if(Debug) Comm.WriteLine("GRADIENT : UNI_MINUS"); FNodeResult t = new FNodeResult(Node.ParentNode, new CellUniMinus()); t.AddChildNode(Gradient(Node.Children[0], X)); return t; }
public override void Extend(RecordWriter Output, DataSet Data, FNodeSet ClusterVariables, FNodeSet OtherKeepers, Predicate Where) { // Check that the ClusterVariable count matches the internal node set count // if (ClusterVariables.Count != this._fields.Count) throw new ArgumentException("The cluster variable count passed does not match the internal cluster variable count"); // Create the selectors // FNodeSet values = OtherKeepers.CloneOfMe(); FNode n = new FNodeResult(null, new RowClusterCellFunction(this._rule, this._means)); foreach (FNode t in ClusterVariables.Nodes) { n.AddChildNode(t.CloneOfMe()); } values.Add("CLUSTER_ID", n); // Run a fast select // FastReadPlan plan = new FastReadPlan(Data, Where, values, Output); }
public override Table Extend(string Dir, string Name, DataSet Data, FNodeSet ClusterVariables, FNodeSet OtherKeepers, Predicate Where) { // Check that the ClusterVariable count matches the internal node set count // if (ClusterVariables.Count != this._fields.Count) throw new ArgumentException("The cluster variable count passed does not match the internal cluster variable count"); // Create the selectors // FNodeSet values = OtherKeepers.CloneOfMe(); FNode n = new FNodeResult(null, new RowClusterCellFunction(this._rule, this._means)); foreach (FNode t in ClusterVariables.Nodes) { n.AddChildNode(t.CloneOfMe()); } values.Add("CLUSTER_ID", n); // Build a recordset // Table tablix = new Table(Dir, Name, values.Columns, Data.MaxRecords); RecordWriter w = tablix.OpenWriter(); // Run a fast select // FastReadPlan plan = new FastReadPlan(Data, Where, values, w); w.Close(); return tablix; }
// F(X) = cos(G(X)), F'(X) = -sin(G(X)) * G'(X) private static FNode GradientOfCos(FNode Node, FNodePointer X) { // sin(G(X)) // FNodeResult t = new FNodeResult(Node.ParentNode, new CellFuncFVSin()); t.AddChildNode(Node.Children[0]); // -sin(G(X)) // FNodeResult u = new FNodeResult(t, new CellUniMinus()); u.AddChildNode(t); // -sin(G(X)) * G'(X) // FNodeResult v = new FNodeResult(Node.ParentNode, new CellBinMult()); v.AddChildNode(u); v.AddChildNode(Gradient(t.Children[0], X)); return v; }
public static Predicate IsNotNull(FNode N) { FNodeResult node = new FNodeResult(null, CellFunctionFactory.LookUp("isnotnull")); node.AddChildNode(N); return new Predicate(node); }
// F(X) = cosh(G(X)), F'(X) = sinh(G(X)) * G'(X) private static FNode GradientOfCosh(FNode Node, FNodePointer X) { // sinh(G(X)) // FNodeResult t = new FNodeResult(Node.ParentNode, new CellFuncFVSinh()); t.AddChildNode(Node.Children[0]); // sinh(G(X)) * G'(X) // FNodeResult u = new FNodeResult(Node.ParentNode, new CellBinMult()); u.AddChildNode(t); u.AddChildNode(Gradient(Node.Children[0], X)); return u; }
// A + 0 or 0 + A or A - 0 -> A // 0 - A -> -A // A + -B -> A - B private FNode CompactAddSub(FNode Node) { if (Node.Affinity != FNodeAffinity.ResultNode) return Node; FNodeResult x = (Node as FNodeResult); string name = x.InnerFunction.NameSig; if (name != FunctionNames.OP_ADD && name != FunctionNames.OP_SUB) return Node; // Look for A + 0 or A - 0 -> A // if (IsStaticZero(Node.Children[1])) { this._Tocks++; return Node.Children[0]; } // Look for 0 + A -> A // if (IsStaticZero(Node.Children[0]) && name == FunctionNames.OP_ADD) { this._Tocks++; return Node.Children[1]; } // Look for 0 - A -> -A // if (IsStaticZero(Node.Children[0]) && name == FunctionNames.OP_SUB) { this._Tocks++; FNode t = new FNodeResult(Node.ParentNode, CellFunctionFactory.LookUp(FunctionNames.UNI_MINUS)); t.AddChildNode(Node.Children[1]); return t; } // Look for A + -B -> A - B // if (IsUniNegative(Node.Children[1]) && name == FunctionNames.OP_ADD) { this._Tocks++; FNode t = new FNodeResult(Node.ParentNode, CellFunctionFactory.LookUp(FunctionNames.OP_SUB)); t.AddChildNode(Node.Children[0]); t.AddChildNode(Node.Children[1].Children[0]); return t; } // Look for -A + B -> B - A // if (IsUniNegative(Node.Children[0]) && name == FunctionNames.OP_ADD) { this._Tocks++; FNode t = new FNodeResult(Node.ParentNode, CellFunctionFactory.LookUp(FunctionNames.OP_SUB)); t.AddChildNode(Node.Children[1]); t.AddChildNode(Node.Children[0].Children[0]); return t; } // Look for A - -B -> A + B // if (IsUniNegative(Node.Children[1]) && name == FunctionNames.OP_SUB) { this._Tocks++; FNode t = new FNodeResult(Node.ParentNode, CellFunctionFactory.LookUp(FunctionNames.OP_ADD)); t.AddChildNode(Node.Children[0]); t.AddChildNode(Node.Children[1].Children[0]); return t; } return Node; }
// F(X) = tanh(X), F'(X) = Power(cosh(x) , -2.00) private static FNode GradientOfTanh(FNode Node, FNodePointer X) { // cosh(G(X)) // FNodeResult t = new FNodeResult(null, new CellFuncFVCosh()); t.AddChildNode(Node.Children[0]); // power(cosh(G(x)),2) // FNodeResult u = new FNodeResult(t, new CellFuncFVPower()); u.AddChildNode(t); u.AddChildNode(new FNodeValue(null, new Cell(-2.00))); // power(cosh(G(x)),2) * G'(X) // FNodeResult v = new FNodeResult(Node.ParentNode, new CellBinMult()); v.AddChildNode(u); v.AddChildNode(Gradient(Node.Children[0], X)); return u; }
// F(X) = power(G(X), N), F'(X) = power(G(X), N - 1) * G'(X) * N, or G'(X) if N == 1, N must not be a decendant of X private static FNode GradientOfPowerLower(FNode Node, FNodePointer X) { // Throw an exception if X is decendant of N, in otherwords F(X) = Power(G(X), H(X)) if (FNodeAnalysis.IsDecendent(X, Node.Children[1])) throw new Exception(string.Format("Cannot differentiate the power function with the form: Power(G(X), H(X)); H(X) cannot have a relation to X")); // Build 'N-1' // FNode n_minus_one = new FNodeResult(Node.ParentNode, new CellBinMinus()); n_minus_one.AddChildNode(Node.Children[1]); n_minus_one.AddChildNode(new FNodeValue(null, new Cell(1.00))); // Get Power(G(X), N-1) // FNode power_gx_n_minus_one = new FNodeResult(Node.ParentNode, new CellFuncFVPower()); power_gx_n_minus_one.AddChildNode(Node.Children[0]); power_gx_n_minus_one.AddChildNode(n_minus_one); // Get Power(G(X), N-1) * G'(X) // FNode t = new FNodeResult(Node.ParentNode, new CellBinMult()); t.AddChildNode(power_gx_n_minus_one); t.AddChildNode(Gradient(Node.Children[0], X)); // Get Power(G(X), N-1) * G'(X) * N // FNode u = new FNodeResult(Node.ParentNode, new CellBinMult()); u.AddChildNode(t); u.AddChildNode(Node.Children[1]); return u; }
// Statics // public static FNode BuildParent(FNode L, CellFunction F) { FNode n = new FNodeResult(null, F); n.AddChildNode(L); return n; }