/// <summary>Runs the Greedy search algorithm algorithm on a graph.</summary> /// <param name="start">The node to start at.</param> /// <param name="neighbors">Step function for all neigbors of a given node.</param> /// <param name="heuristic">Computes the heuristic value of a given node in a graph.</param> /// <param name="goal">Predicate for determining if we have reached the goal node.</param> /// <returns>Stepper of the shortest path or null if no path exists.</returns> public static Stepper <T> Greedy(T start, Neighbors neighbors, Heuristic heuristic, Goal goal) { // using a heap (aka priority queue) to store nodes based on their computed heuristic value Heap <Greedy_Node> fringe = new HeapArray <Greedy_Node>( // NOTE: I just reversed the order of left and right because smaller values are higher priority (Greedy_Node left, Greedy_Node right) => { return(Compute <Math> .Compare(right.Priority, left.Priority)); }); // push starting node Greedy_Node start_node = new Greedy_Node(null, start, default(Math)); fringe.Enqueue(start_node); // run the algorithm while (fringe.Count != 0) { Greedy_Node current = fringe.Dequeue(); if (goal(current.Value)) { return(Greedy_BuildPath(current)); } else { neighbors(current.Value, (T neighbor) => { Greedy_Node newNode = new Greedy_Node(current, neighbor, heuristic(neighbor)); fringe.Enqueue(newNode); }); } } return(null); // goal node was not reached (no path exists) }
/// <summary>Runs the A* search algorithm algorithm on a graph.</summary> /// <param name="start">The node to start at.</param> /// <param name="neighbors">Step function for all neigbors of a given node.</param> /// <param name="heuristic">Computes the heuristic value of a given node in a graph.</param> /// <param name="cost">Computes the cost of moving from the current node to a specific neighbor.</param> /// <param name="goal">Predicate for determining if we have reached the goal node.</param> /// <returns>Stepper of the shortest path or null if no path exists.</returns> public static Stepper <T> Astar(T start, Neighbors neighbors, Heuristic heuristic, Cost cost, Goal goal) { // using a heap (aka priority queue) to store nodes based on their computed A* f(n) value Heap <Astar_Node> fringe = new HeapArray <Astar_Node>( // NOTE: Typical A* implementations prioritize smaller values (Astar_Node left, Astar_Node right) => { Comparison comparison = Compute.Compare <Math>(right.Priority, left.Priority); return(comparison); }); // using a map (aka dictionary) to store costs from start to current nodes Map <Math, Astar_Node> computed_costs = new MapHashArray <Math, Astar_Node>(); // construct the f(n) for this A* execution Astar_function function = (T node, Astar_Node previous) => { Math previousCost = computed_costs.Get(previous); Math currentCost = cost(previous.Value, node); Math costFromStart = Compute.Add <Math>(previousCost, currentCost); Math hueristic = heuristic(node); return(Compute.Add <Math>(costFromStart, hueristic)); }; // push starting node Astar_Node start_node = new Astar_Node(null, start, default(Math)); fringe.Enqueue(start_node); computed_costs.Add(start_node, default(Math)); // run the algorithm while (fringe.Count != 0) { Astar_Node current = fringe.Dequeue(); if (goal(current.Value)) { return(Astar_BuildPath(current)); } else { neighbors(current.Value, (T neighbor) => { Astar_Node newNode = new Astar_Node(current, neighbor, function(neighbor, current)); Math costValue = Compute.Add <Math>(computed_costs.Get(current), cost(current.Value, neighbor)); computed_costs.Add(newNode, costValue); fringe.Enqueue(newNode); }); } } return(null); // goal node was not reached (no path exists) }
/// <summary>Runs the A* search algorithm algorithm on a graph.</summary> /// <typeparam name="Node">The node type of the graph being searched.</typeparam> /// <typeparam name="Numeric">The numeric to use when performing calculations.</typeparam> /// <param name="start">The node to start at.</param> /// <param name="neighbors">Step function for all neigbors of a given node.</param> /// <param name="heuristic">Computes the heuristic value of a given node in a graph.</param> /// <param name="cost">Computes the cost of moving from the current node to a specific neighbor.</param> /// <param name="goal">Predicate for determining if we have reached the goal node.</param> /// <returns>Stepper of the shortest path or null if no path exists.</returns> public static Stepper <Node> Graph <Node, Numeric>(Node start, Neighbors <Node> neighbors, Heuristic <Node, Numeric> heuristic, Cost <Node, Numeric> cost, Goal <Node> goal) { // using a heap (aka priority queue) to store nodes based on their computed A* f(n) value IHeap <AstarNode <Node, Numeric> > fringe = new HeapArray <AstarNode <Node, Numeric> >( // NOTE: Typical A* implementations prioritize smaller values (a, b) => Compute.Compare(b.Priority, a.Priority)); // push starting node fringe.Enqueue( new AstarNode <Node, Numeric>() { Previous = null, Value = start, Priority = default,
public static void TestMath() { #region math stuffs Console.WriteLine("Negate: " + (Compute <int> .Negate(7) == -7)); Console.WriteLine("Add: " + (Compute <int> .Add(7, 7) == 14)); Console.WriteLine("Subtract: " + (Compute <int> .Subtract(14, 7) == 7)); Console.WriteLine("Multiply: " + (Compute <int> .Multiply(7, 7) == 49)); Console.WriteLine("Divide: " + (Compute <int> .Divide(14, 7) == 2)); Console.WriteLine("AbsoluteValue: " + (Compute <int> .AbsoluteValue(7) == 7 && Compute <int> .AbsoluteValue(-7) == 7)); Console.WriteLine("Clamp: " + (Compute <int> .Clamp(7, 6, 8) == 7)); Console.WriteLine("Maximum: " + (Compute <int> .Maximum((Step <int> step) => { step(1); step(2); step(3); }) == 3)); Console.WriteLine("Minimum: " + (Compute <int> .Minimum((Step <int> step) => { step(1); step(2); step(3); }) == 1)); Console.WriteLine("LessThan: " + (Compute <int> .LessThan(1, 2) == true && Compute <int> .LessThan(2, 1) == false)); Console.WriteLine("GreaterThan: " + (Compute <int> .GreaterThan(2, 1) == true && Compute <int> .GreaterThan(1, 2) == false)); Console.WriteLine("Compare: " + (Compute <int> .Compare(2, 1) == Comparison.Greater && Compute <int> .Compare(1, 2) == Comparison.Less && Compute <int> .Compare(1, 1) == Comparison.Equal)); Console.WriteLine("Equate: " + (Compute <int> .Equate(2, 1) == false && Compute <int> .Equate(1, 1) == true)); Console.WriteLine("EqualsLeniency: " + (Compute <int> .Equals(2, 1, 1) == true && Compute <int> .Equals(2, 1, 0) == false && Compute <int> .Equals(1, 1, 0) == true)); #endregion }
/// <summary>Runs the A* search algorithm algorithm on a graph.</summary> /// <param name="start">The node to start at.</param> /// <param name="neighbors">Step function for all neigbors of a given node.</param> /// <param name="heuristic">Computes the heuristic value of a given node in a graph.</param> /// <param name="cost">Computes the cost of moving from the current node to a specific neighbor.</param> /// <param name="goal">Predicate for determining if we have reached the goal node.</param> /// <returns>Stepper of the shortest path or null if no path exists.</returns> public static Stepper <NODE> Graph <NODE, NUMERIC>(NODE start, Neighbors <NODE> neighbors, Heuristic <NODE, NUMERIC> heuristic, Cost <NODE, NUMERIC> cost, Goal <NODE> goal) { // using a heap (aka priority queue) to store nodes based on their computed A* f(n) value IHeap <AstarNode <NODE, NUMERIC> > fringe = new HeapArray <AstarNode <NODE, NUMERIC> >( // NOTE: Typical A* implementations prioritize smaller values (a, b) => Compute.Compare(b.Priority, a.Priority)); // push starting node fringe.Enqueue( new AstarNode <NODE, NUMERIC>( null, start, default(NUMERIC), Constant <NUMERIC> .Zero)); // run the algorithm while (fringe.Count != 0) { AstarNode <NODE, NUMERIC> current = fringe.Dequeue(); if (goal(current.Value)) { return(BuildPath(current)); } else { neighbors(current.Value, (NODE neighbor) => { NUMERIC costValue = Compute.Add(current.Cost, cost(current.Value, neighbor)); fringe.Enqueue( new AstarNode <NODE, NUMERIC>( current, neighbor, Compute.Add(heuristic(neighbor), costValue), costValue)); }); } } return(null); // goal node was not reached (no path exists) }
/// <summary>Runs the Greedy search algorithm algorithm on a graph.</summary> /// <param name="start">The node to start at.</param> /// <param name="neighbors">Step function for all neigbors of a given node.</param> /// <param name="heuristic">Computes the heuristic value of a given node in a graph.</param> /// <param name="goal">Predicate for determining if we have reached the goal node.</param> /// <returns>Stepper of the shortest path or null if no path exists.</returns> public static Stepper <NODE> Graph <NODE, NUMERIC>(NODE start, Neighbors <NODE> neighbors, Heuristic <NODE, NUMERIC> heuristic, Goal <NODE> goal) { // using a heap (aka priority queue) to store nodes based on their computed heuristic value IHeap <GreedyNode <NODE, NUMERIC> > fringe = new HeapArray <GreedyNode <NODE, NUMERIC> >( // NOTE: Typical graph search implementations prioritize smaller values (a, b) => Compute.Compare(b.Priority, a.Priority)); // push starting node fringe.Enqueue( new GreedyNode <NODE, NUMERIC>( null, start, default(NUMERIC))); // run the algorithm while (fringe.Count != 0) { GreedyNode <NODE, NUMERIC> current = fringe.Dequeue(); if (goal(current.Value)) { return(BuildPath(current)); } else { neighbors(current.Value, (NODE neighbor) => { fringe.Enqueue( new GreedyNode <NODE, NUMERIC>( current, neighbor, heuristic(neighbor))); }); } } return(null); // goal node was not reached (no path exists) }
public static void TestOmnitree1() { #region construction Omnitree.Location <Object3D, double, double, double> locate = (Object3D record, out double a, out double b, out double c) => { a = record.X; b = record.Y; c = record.Z; }; Compute <double> .Compare(0, 0); OmnitreePoints <Object3D, double, double, double> omnitree = new OmnitreePointsLinked <Object3D, double, double, double>(locate); #endregion #region random generation Console.WriteLine("Generating random data..."); Random random = new Random(0); int count = 100; Object3D[] records = new Object3D[count]; for (int i = 0; i < count; i++) { records[i] = new Object3D(i, random.NextDouble(), random.NextDouble(), random.NextDouble()); } Console.WriteLine("Generated random data."); #endregion #region adding Console.WriteLine("Building Omnitree..."); for (int i = 0; i < count; i++) { omnitree.Add(records[i]); if (i % (count / 10) == 0) { Console.WriteLine(((double)i / (double)count * 100D) + "%"); } } Console.WriteLine("OmniTree.Count: " + omnitree.Count); //Console.WriteLine("OmniTree._top.Count: " + (omnitree as OmnitreeLinked<TestObject, double>)._top.Count); int test_count = 0; omnitree.Stepper((Object3D record) => { test_count++; }); Console.WriteLine("OmniTree Stepper Count: " + test_count); #endregion #region validation SetHashArray <Object3D> setHash = new SetHashArray <Object3D>( (Object3D a, Object3D b) => { return(a.Id == b.Id); }, (Object3D a) => { return(a.Id.GetHashCode()); }); for (int i = 0; i < count; i++) { setHash.Add(records[i]); } bool validated = true; omnitree.Stepper((Object3D record) => { if (!setHash.Contains(record)) { validated = false; } }); if (validated) { Console.WriteLine("Values Validated."); } else { Console.WriteLine("Values INVALID."); } #endregion #region querying Console.WriteLine("Value Querying: "); bool query_test = false; for (int i = 0; i < count; i++) { query_test = false; double a, b, c; locate(records[i], out a, out b, out c); omnitree[a, b, c]((Object3D record) => { query_test = true; }); if (query_test == false) { Console.WriteLine("Querying INVALID on value: " + i); break; } if (i % (count / 10) == 0) { Console.WriteLine(((double)i / (double)count * 100D) + "%"); } } if (query_test == true) { Console.WriteLine("Querying Validated."); } else { Console.WriteLine("Querying INVALID."); } #endregion #region dynamic values (re-randomizing) Console.WriteLine("Moving randomized data..."); foreach (Object3D record in records) { record.X += Math.Max(0d, Math.Min(1d, (random.NextDouble() / 100D) - .5D)); record.Y += Math.Max(0d, Math.Min(1d, (random.NextDouble() / 100D) - .5D)); record.Z += Math.Max(0d, Math.Min(1d, (random.NextDouble() / 100D) - .5D)); } Console.WriteLine("Randomized data moved."); #endregion #region Updating Console.WriteLine("Updating Tree Positions..."); //// Update Method #1 omnitree.Update(); //// Update Method #2 //omnitree.Update(omnitree.Min, omnitree.Max); Console.WriteLine("Tree Positions Updated."); #endregion #region removal Console.WriteLine("Removing Values: "); for (int i = 0; i < count; i++) { int tempCount = omnitree.Count; // Removal Method #1 omnitree.Remove(records[i]); //// Removal Method #2 //omnitree.Remove(locate(records[i]), locate(records[i])); //// Removal Method #3 //omnitree.Remove(locate(records[i]), locate(records[i]), (omnitree_record step) => { return records[i].Id == step.Id; }); //// Removal Method #4 //double[] location = new double[] { locate(records[i])(0), locate(records[i])(1), locate(records[i])(2) }; //omnitree.Remove(location, location); //// Removal Method #5 //double[] location = new double[] { locate(records[i])(0), locate(records[i])(1), locate(records[i])(2) }; //omnitree.Remove(location, location, (omnitree_record step) => { return records[i].Id == step.Id; }); if (omnitree.Count != count - (i + 1)) { throw new System.Exception(); } if (i % (count / 10) == 0) { Console.WriteLine(((double)i / (double)count * 100D) + "%"); } } Console.WriteLine("Values Removed: "); Console.WriteLine("OmniTree.Count: " + omnitree.Count); //Console.WriteLine("OmniTree._top.Count: " + (omnitree as OmnitreeLinked<TestObject, double>)._top.Count); test_count = 0; omnitree.Stepper((Object3D record) => { test_count++; }); Console.WriteLine("OmniTree Stepper Count: " + test_count); #endregion Console.WriteLine(); Console.WriteLine("TEST COMPLETE"); }
static void Main(string[] args) { Console.WriteLine("You are runnning the Mathematics example."); Console.WriteLine("=========================================="); Console.WriteLine(); #region Basic Operations Console.WriteLine(" Basics----------------------------------------------"); Console.WriteLine(); // Negation Console.WriteLine(" Compute<int>.Negate(7): " + Compute <int> .Negate(7)); // Addition Console.WriteLine(" Compute<double>.Add(7, 7): " + Compute <decimal> .Add(7, 7)); // Subtraction Console.WriteLine(" Compute<float>.Subtract(14, 7): " + Compute <float> .Subtract(14, 7)); // Multiplication Console.WriteLine(" Compute<long>.Multiply(7, 7): " + Compute <long> .Multiply(7, 7)); // Division Console.WriteLine(" Compute<short>.Divide(14, 7): " + Compute <short> .Divide((short)14, (short)7)); // Absolute Value Console.WriteLine(" Compute<decimal>.AbsoluteValue(-7): " + Compute <double> .AbsoluteValue(-7)); // Clamp Console.WriteLine(" Compute<Fraction>.Clamp(-123, 7, 14): " + Compute <Fraction> .Clamp(-123, 7, 14)); // Maximum Console.WriteLine(" Compute<byte>.Maximum(1, 2, 3): " + Compute <byte> .Maximum((Step <byte> step) => { step(1); step(2); step(3); })); // Minimum Console.WriteLine(" Compute<Integer>.Minimum(1, 2, 3): " + Compute <Integer> .Minimum((Step <Integer> step) => { step(1); step(2); step(3); })); // Less Than Console.WriteLine(" Compute<Fraction128>.LessThan(1, 2): " + Compute <Fraction128> .LessThan(1, 2)); // Greater Than Console.WriteLine(" Compute<Fraction64>.GreaterThan(1, 2): " + Compute <Fraction64> .GreaterThan(1, 2)); // Compare Console.WriteLine(" Compute<Fraction32>.Compare(7, 7): " + Compute <Fraction32> .Compare(7, 7)); // Equate Console.WriteLine(" Compute<int>.Equate(7, 6): " + Compute <int> .Equate(7, 6)); // EqualsLeniency Console.WriteLine(" Compute<int>.EqualsLeniency(2, 1, 1): " + Compute <int> .Equals(2, 1, 1)); Console.WriteLine(); #endregion #region Number Theory Console.WriteLine(" Number Theory--------------------------------------"); Console.WriteLine(); // Prime Checking int prime_check = random.Next(0, 100000); Console.WriteLine(" IsPrime(" + prime_check + "): " + Compute <int> .IsPrime(prime_check)); // GCF Checking int[] gcf = new int[] { random.Next(0, 500) * 2, random.Next(0, 500) * 2, random.Next(0, 500) * 2 }; Console.WriteLine(" GCF(" + gcf[0] + ", " + gcf[1] + ", " + gcf[2] + "): " + Compute <int> .GreatestCommonFactor(gcf.Stepper())); // LCM Checking int[] lcm = new int[] { random.Next(0, 500) * 2, random.Next(0, 500) * 2, random.Next(0, 500) * 2 }; Console.WriteLine(" LCM(" + lcm[0] + ", " + lcm[1] + ", " + lcm[2] + "): " + Compute <int> .LeastCommonMultiple(lcm.Stepper())); Console.WriteLine(); #endregion #region Range Console.WriteLine(" Range---------------------------------------------"); Console.WriteLine(); Console.WriteLine(" 1D int"); { Range <int> range1 = new Range <int>(1, 7); Console.WriteLine(" range1: " + range1); Range <int> range2 = new Range <int>(4, 10); Console.WriteLine(" range2: " + range2); Range <int>[] range3 = range1 ^ range2; Console.WriteLine(" range1 ^ range2 (Complement): " + range3[0]); Range <int>[] range4 = range1 | range2; Console.WriteLine(" range1 | range2 (Union): " + range4[0]); Range <int> range5 = range1 & range2; Console.WriteLine(" range1 & range2 (Intersection): " + range5); } Console.WriteLine(); #endregion #region Angles Console.WriteLine(" Angles--------------------------------------"); Console.WriteLine(); Angle <double> angle1 = Angle <double> .Factory_Degrees(90d); Console.WriteLine(" angle1 = " + angle1); Angle <double> angle2 = Angle <double> .Factory_Turns(0.5d); Console.WriteLine(" angle2 = " + angle2); Console.WriteLine(" angle1 + angle2 = " + (angle1 + angle2)); Console.WriteLine(" angle2 - angle1 = " + (angle1 + angle2)); Console.WriteLine(" angle1 * 2 = " + (angle1 * 2)); Console.WriteLine(" angle1 / 2 = " + (angle1 / 2)); Console.WriteLine(" angle1 > angle2 = " + (angle1 > angle2)); Console.WriteLine(" angle1 == angle2 = " + (angle1 == angle2)); Console.WriteLine(" angle1 * 2 == angle2 = " + (angle1 * 2 == angle2)); Console.WriteLine(" angle1 != angle2 = " + (angle1 != angle2)); Console.WriteLine(); // examples of non-doubles Angle <float> angle10 = Angle <float> .Factory_Degrees(90f); Angle <Fraction> angle11 = Angle <Fraction> .Factory_Degrees(new Fraction("90/1")); Angle <decimal> angle12 = Angle <decimal> .Factory_Degrees(90m); #endregion #region Fraction //Console.WriteLine(" Fractions-----------------------------------"); //Console.WriteLine(); //Fraction128 fraction1 = new Fraction128(2.5); //Console.WriteLine(" fraction1 = " + fraction1); //Fraction128 fraction2 = new Fraction128(3.75); //Console.WriteLine(" fraction2 = " + fraction2); //Console.WriteLine(" fraction1 + fraction2 = " + fraction1 + fraction2); //Console.WriteLine(" fraction2 - fraction1 = " + fraction1 + fraction2); //Console.WriteLine(" fraction1 * 2 = " + fraction1 * 2); //Console.WriteLine(" fraction1 / 2 = " + fraction1 / 2); //Console.WriteLine(" fraction1 > fraction2 = " + (fraction1 > fraction2)); //Console.WriteLine(" fraction1 == fraction2 = " + (fraction1 == fraction2)); //Console.WriteLine(" fraction1 * 2 == fraction2 = " + (fraction1 * 2 == fraction2)); //Console.WriteLine(" fraction1 != fraction2 = " + (fraction1 != fraction2)); //Console.WriteLine(); #endregion #region Trigonometry Console.WriteLine(" Trigonometry -----------------------------------------"); Console.WriteLine(); Angle <double> testingAngle = Angle <double> .Factory_Degrees(90d); Console.WriteLine(" Sin(90degrees) = " + Compute <double> .Sine(testingAngle)); #endregion #region Statistics Console.WriteLine(" Statistics-----------------------------------------"); Console.WriteLine(); // Makin some random data... double mode_temp = random.NextDouble() * 100; double[] statistics_data = new double[] { random.NextDouble() * 100, mode_temp, random.NextDouble() * 100, random.NextDouble() * 100, random.NextDouble() * 100, random.NextDouble() * 100, mode_temp }; // Print the data to the console... Console.WriteLine(" data: [" + string.Format("{0:0.00}", statistics_data[0]) + ", " + string.Format("{0:0.00}", statistics_data[1]) + ", " + string.Format("{0:0.00}", statistics_data[2]) + ", " + string.Format("{0:0.00}", statistics_data[3]) + ", " + string.Format("{0:0.00}", statistics_data[4]) + ", " + string.Format("{0:0.00}", statistics_data[5]) + ", " + string.Format("{0:0.00}", statistics_data[6]) + "]"); Console.WriteLine(); // Mean Console.WriteLine(" Mean(data): " + string.Format("{0:0.00}", Compute <double> .Mean(statistics_data.Stepper()))); // Median Console.WriteLine(" Median(data): " + string.Format("{0:0.00}", Compute <double> .Median(statistics_data.Stepper()))); // Mode Console.WriteLine(" Mode(data): "); Heap <Link <double, int> > modes = Compute <double> .Mode(statistics_data.Stepper()); while (modes.Count > 0) { Link <double, int> link = modes.Dequeue(); Console.WriteLine(" Point: " + string.Format("{0:0.00}", link._1) + " Occurences: " + link._2); } Console.WriteLine(); // Geometric Mean Console.WriteLine(" Geometric Mean(data): " + string.Format("{0:0.00}", Compute <double> .GeometricMean(statistics_data.Stepper()))); // Range Range <double> range = Compute <double> .Range(statistics_data.Stepper()); Console.WriteLine(" Range(data): " + string.Format("{0:0.00}", range.Min) + "-" + string.Format("{0:0.00}", range.Max)); // Variance Console.WriteLine(" Variance(data): " + string.Format("{0:0.00}", Compute <double> .Variance(statistics_data.Stepper()))); // Standard Deviation Console.WriteLine(" Standard Deviation(data): " + string.Format("{0:0.00}", Compute <double> .StandardDeviation(statistics_data.Stepper()))); // Mean Deviation Console.WriteLine(" Mean Deviation(data): " + string.Format("{0:0.00}", Compute <double> .MeanDeviation(statistics_data.Stepper()))); Console.WriteLine(); // Quantiles //double[] quatiles = Statistics<double>.Quantiles(4, statistics_data.Stepper()); //Console.Write(" Quartiles(data):"); //foreach (double i in quatiles) // Console.Write(string.Format(" {0:0.00}", i)); //Console.WriteLine(); //Console.WriteLine(); #endregion #region Algebra Console.WriteLine(" Algebra---------------------------------------------"); Console.WriteLine(); // Prime Factorization int prime_factors = random.Next(0, 100000); Console.Write(" Prime Factors(" + prime_factors + "): "); Compute <int> .FactorPrimes(prime_factors, (int i) => { Console.Write(i + " "); }); Console.WriteLine(); Console.WriteLine(); // Logarithms int log_1 = random.Next(0, 11), log_2 = random.Next(0, 100000); Console.WriteLine(" log_" + log_1 + "(" + log_2 + "): " + string.Format("{0:0.00}", Compute <double> .Logarithm((double)log_1, (double)log_2))); Console.WriteLine(); // Summation double[] summation_values = new double[] { random.NextDouble(), random.NextDouble(), random.NextDouble(), random.NextDouble(), }; double summation = Compute <double> .Add(summation_values.Stepper()); Console.Write(" Σ (" + string.Format("{0:0.00}", summation_values[0])); for (int i = 1; i < summation_values.Length; i++) { Console.Write(", " + string.Format("{0:0.00}", summation_values[i])); } Console.WriteLine(") = " + string.Format("{0:0.00}", summation)); Console.WriteLine(); #endregion #region Combinatorics Console.WriteLine(" Combinatorics--------------------------------------"); Console.WriteLine(); // Factorials Console.WriteLine(" 7!: " + Compute <int> .Factorial(7)); Console.WriteLine(); // Combinations Console.WriteLine(" 7! / (3! * 4!): " + Compute <int> .Combinations(7, new int[] { 3, 4 })); Console.WriteLine(); // Choose Console.WriteLine(" 7 choose 2: " + Compute <int> .Choose(7, 2)); Console.WriteLine(); #endregion #region Linear Algebra Console.WriteLine(" Linear Algebra------------------------------------"); Console.WriteLine(); // Vector Construction Vector <double> V = new double[] { random.NextDouble(), random.NextDouble(), random.NextDouble(), random.NextDouble(), }; Console.WriteLine(" Vector<double> V: "); ConsoleWrite(V); Console.WriteLine(" Normalize(V): "); ConsoleWrite(V.Normalize()); // Vctor Negation Console.WriteLine(" -V: "); ConsoleWrite(-V); // Vector Addition Console.WriteLine(" V + V (aka 2V): "); ConsoleWrite(V + V); // Vector Multiplication Console.WriteLine(" V * 2: "); ConsoleWrite(V * 2); // Vector Division Console.WriteLine(" V / 2: "); ConsoleWrite(V / 2); // Vector Dot Product Console.WriteLine(" V dot V: " + Vector <double> .DotProduct(V, V)); Console.WriteLine(); // Vector Cross Product Vector <double> V3 = new double[] { random.NextDouble(), random.NextDouble(), random.NextDouble(), }; Console.WriteLine(" Vector<double> V3: "); ConsoleWrite(V3); Console.WriteLine(" V3 cross V3: "); ConsoleWrite(Vector <double> .CrossProduct(V3, V3)); // Matrix Construction Matrix <double> M = (Matrix <double>) new double[, ] { { random.NextDouble(), random.NextDouble(), random.NextDouble(), random.NextDouble() }, { random.NextDouble(), random.NextDouble(), random.NextDouble(), random.NextDouble() }, { random.NextDouble(), random.NextDouble(), random.NextDouble(), random.NextDouble() }, { random.NextDouble(), random.NextDouble(), random.NextDouble(), random.NextDouble() }, }; Console.WriteLine(" Matrix<double>.Identity(4, 4): "); ConsoleWrite(Matrix <double> .FactoryIdentity(4, 4)); Console.WriteLine(" Matrix<double> M: "); ConsoleWrite(M); // Matrix Negation Console.WriteLine(" -M: "); ConsoleWrite(-M); // Matrix Addition Console.WriteLine(" M + M (aka 2M): "); ConsoleWrite(M + M); // Matrix Subtraction Console.WriteLine(" M - M: "); ConsoleWrite(M - M); // Matrix Multiplication Console.WriteLine(" M * M (aka M ^ 2): "); ConsoleWrite(M * M); // If you have a large matrix that you want to multi-thread the multiplication, // use the function: "LinearAlgebra.Multiply_parallel". This function will // automatically parrallel the multiplication to the number of cores on your // personal computer. // Matrix Power Console.WriteLine(" M ^ 3: "); ConsoleWrite(M ^ 3); // Matrix Multiplication Console.WriteLine(" minor(M, 1, 1): "); ConsoleWrite(M.Minor(1, 1)); // Matrix Reduced Row Echelon Console.WriteLine(" rref(M): "); ConsoleWrite(Matrix <double> .ReducedEchelon(M)); // Matrix Determinant Console.WriteLine(" determinent(M): " + string.Format("{0:0.00}", Matrix <double> .Determinent(M))); Console.WriteLine(); // Matrix-Vector Multiplication Console.WriteLine(" M * V: "); ConsoleWrite(M * V); // Matrix Lower-Upper Decomposition Matrix <double> l, u; Matrix <double> .DecomposeLU(M, out l, out u); Console.WriteLine(" Lower-Upper Decomposition:"); Console.WriteLine(); Console.WriteLine(" lower(M):"); ConsoleWrite(l); Console.WriteLine(" upper(M):"); ConsoleWrite(u); // Quaternion Construction Quaternion <double> Q = new Quaternion <double>( random.NextDouble(), random.NextDouble(), random.NextDouble(), 1.0d); Console.WriteLine(" Quaternion<double> Q: "); ConsoleWrite(Q); // Quaternion Addition Console.WriteLine(" Q + Q (aka 2Q):"); ConsoleWrite(Q + Q); // Quaternion-Vector Rotation Console.WriteLine(" Q * V3 * Q':"); // Note: the vector should be normalized on the 4th component // for a proper rotation. (I did not do that) ConsoleWrite(V3.RotateBy(Q)); #endregion #region Convex Optimization //Console.WriteLine(" Convex Optimization-----------------------------------"); //Console.WriteLine(); //double[,] tableau = new double[,] //{ // { 0.0, -0.5, -3.0, -1.0, -4.0, }, // { 40.0, 1.0, 1.0, 1.0, 1.0, }, // { 10.0, -2.0, -1.0, 1.0, 1.0, }, // { 10.0, 0.0, 1.0, 0.0, -1.0, }, //}; //Console.WriteLine(" tableau (double): "); //ConsoleWrite(tableau); Console.WriteLine(); //Vector<double> simplex_result = LinearAlgebra.Simplex(ref tableau); //Console.WriteLine(" simplex(tableau): "); //ConsoleWrite(tableau); Console.WriteLine(); //Console.WriteLine(" resulting maximization: "); //ConsoleWrite(simplex_result); #endregion #region Symbolics Console.WriteLine(" Symbolics---------------------------------------"); Console.WriteLine(); Expression <Func <double, double> > expression1 = (x) => 2 * (x / 7); var syntax1 = Symbolics <double> .Parse(expression1); Console.WriteLine(" Expression 1: " + syntax1); Console.WriteLine(" Simplified: " + syntax1.Simplify()); Console.WriteLine(" Plugin(5): " + syntax1.Assign("x", 5).Simplify()); Expression <Func <double, double> > expression2 = (x) => 2 * x / 7; var syntax2 = Symbolics <double> .Parse(expression2); Console.WriteLine(" Expression 2: " + syntax2); Console.WriteLine(" Simplified: " + syntax2.Simplify()); Console.WriteLine(" Plugin(5): " + syntax2.Assign("x", 5).Simplify()); Expression <Func <double, double> > expression3 = (x) => 2 - x + 7; var syntax3 = Symbolics <double> .Parse(expression3); Console.WriteLine(" Expression 3: " + syntax3); Console.WriteLine(" Simplified: " + syntax3.Simplify()); Console.WriteLine(" Plugin(5): " + syntax3.Assign("x", 5).Simplify()); Expression <Func <double, double> > expression4 = (x) => 2 + (x - 7); var syntax4 = Symbolics <double> .Parse(expression4); Console.WriteLine(" Expression 4: " + syntax4); Console.WriteLine(" Simplified: " + syntax4.Simplify()); Console.WriteLine(" Plugin(5): " + syntax4.Assign("x", 5).Simplify()); Expression <Func <double, double, double, double> > expression5 = (x, y, z) => Compute <double> .Power(x, 3) + 2 * x * y * Compute <double> .Power(z, 2) - y * z + 1; var syntax5 = Symbolics <double> .Parse(expression5); Console.WriteLine(" Expression 5: " + syntax5); Console.WriteLine(" Simplified: " + syntax5.Simplify()); Console.WriteLine(" Plugin(x = 5): " + syntax5.Assign("x", 5).Simplify()); #endregion var test = Symbolics <double> .Parse("less(5, 10)"); Console.WriteLine(); Console.WriteLine(" Parse (string) Test: " + test); var test2 = Symbolics <double> .Parse("less(add(5, 10), 10)"); Console.WriteLine(); Console.WriteLine(" Parse (string) Test: " + test2); Console.WriteLine(" Parse (string) Test Simplify: " + test2.Simplify()); Console.WriteLine(); Console.WriteLine("================================================="); Console.WriteLine("Example Complete..."); Console.ReadLine(); }
public static bool operator !=(Angle <T> left, Angle <T> right) { return(Compute <T> .Compare(left._radians, right._radians) != Comparison.Equal); }
public static bool operator >=(Angle <T> left, Angle <T> right) { return(Compute <T> .Compare(left._radians, right._radians) == (Comparison.Greater | Comparison.Equal)); }
public static bool operator <(Angle <T> left, Angle <T> right) { return(Compute <T> .Compare(left._radians, right._radians) == Comparison.Less); }
static void Main(string[] args) { #region Delegate //Action test1 = () => { Console.WriteLine("test 1"); }; //Action test2 = () => { Console.WriteLine("test 2"); }; //Action action = test1; ////foreach (byte b in test1.Method.GetMethodBody().GetILAsByteArray()) //// Console.WriteLine(b); //// ////foreach (System.Reflection.FieldInfo field in typeof(Action).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)) //// Console.WriteLine(field); //action(); ////Console.WriteLine(typeof(Action).GetField("_target", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(action)); //IntPtr methodPtr = (IntPtr)typeof(Action).GetField("_methodPtr", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(test2); //IntPtr methodPtrAux = (IntPtr)typeof(Action).GetField("_methodPtrAux", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(test2); //object target = typeof(Action).GetField("_target", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(test2); //object methodBase = typeof(Action).GetField("_methodBase", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(test2); ////typeof(Action).GetField("_methodPtr", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(action, methodPtr); ////typeof(Action).GetField("_methodPtrAux", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(action, methodPtrAux); ////typeof(Action).GetField("_target", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(action, target); ////typeof(Action).GetField("_methodBase", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(action, methodBase); //byte[] array = test1.Method.GetMethodBody().GetILAsByteArray(); //System.Runtime.InteropServices.GCHandle pinnedArray = System.Runtime.InteropServices.GCHandle.Alloc(array, System.Runtime.InteropServices.GCHandleType.Pinned); //IntPtr pointer = pinnedArray.AddrOfPinnedObject(); //typeof(Action).GetField("_methodPtrAux", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).SetValue(action, pointer); //pinnedArray.Free(); //action(); ////Console.WriteLine(typeof(Action).GetField("_target", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(action)); //System.Action<System.String, System.Int32, System.Double> //System.Action`3[System.String, System.Int32, System.Double] //int i = 0; //foreach (string s in "Action<int, double, decimal>".Split('<', '>')) // Console.WriteLine(i++ + " "+ s); //Console.WriteLine(string.IsNullOrWhiteSpace("Action<int, double, decimal>".Split('<', '>')["Action<int, double, decimal>".Split('<', '>').Length - 1])); //Console.WriteLine(Type.GetType("System.Action`3[System.String, System.Int32, System.Double]")); //Console.WriteLine(Type.GetType("Action<int>")); //Console.WriteLine(typeof(Action<int>).Name); //Console.WriteLine(typeof(Action<float>).Name); //Console.WriteLine(typeof(Action<double>).Name); //Console.WriteLine(typeof(Action<decimal>).Name); //Console.WriteLine(typeof(System.Collections.Generic.List<int>).Name); //Console.WriteLine(typeof(System.Collections.Generic.List<float>).Name); //Console.WriteLine(typeof(System.Collections.Generic.List<double>).Name); //Console.WriteLine(typeof(System.Collections.Generic.List<decimal>).Name); //Console.WriteLine(Type.GetType("List'1")); //Action test2 = Test; //Console.WriteLine(typeof(Action).GetField("_methodPtr", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(test2)); //Console.WriteLine(typeof(Action).GetField("_methodPtrAux", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(test2)); //Console.WriteLine(typeof(Action).GetField("_target", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(test2)); //Console.WriteLine(typeof(Action).GetField("_methodBase", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public).GetValue(test2)); //Console.WriteLine(test2.Truncate().Target); //Console.WriteLine(test2.Truncate().Method.DeclaringType.Name); //Action d = (Action)Delegate.CreateDelegate(typeof(Action), test2.Method); //114 //1 //0 //0 //112 //40 //22 //0 //0 //10 //42 //System.Object _target //System.Object _methodBase //IntPtr _methodPtr //IntPtr _methodPtrAux #endregion #region Random Generators //int iterationsperrandom = 3; //Action<Random> testrandom = (Random random) => // { // for (int i = 0; i < iterationsperrandom; i++) // Console.WriteLine(i + ": " + random.Next()); // Console.WriteLine(); // }; //Arbitrary mcg_2pow59_13pow13 = Arbitrary.MultiplicativeCongruentGenerator_Modulus2power59_Multiplier13power13(); //Console.WriteLine("mcg_2pow59_13pow13 randoms:"); //testrandom(mcg_2pow59_13pow13); //Arbitrary mcg_2pow31m1_1132489760 = Arbitrary.MultiplicativeCongruentGenerator_Modulus2power31minus1_Multiplier1132489760(); //Console.WriteLine("mcg_2pow31m1_1132489760 randoms:"); //testrandom(mcg_2pow31m1_1132489760); //Arbitrary mersenneTwister = Arbitrary.MersenneTwister(); //Console.WriteLine("mersenneTwister randoms:"); //testrandom(mersenneTwister); //Arbitrary cmr32_c2_o3 = Arbitrary.CombinedMultipleRecursiveGenerator32bit_components2_order3(); //Console.WriteLine("mersenneTwister randoms:"); //testrandom(cmr32_c2_o3); //Arbitrary wh1982cmcg = Arbitrary.WichmannHills1982_CombinedMultiplicativeCongruentialGenerator(); //Console.WriteLine("mersenneTwister randoms:"); //testrandom(wh1982cmcg); //Arbitrary wh2006cmcg = Arbitrary.WichmannHills2006_CombinedMultiplicativeCongruentialGenerator(); //Console.WriteLine("mersenneTwister randoms:"); //testrandom(wh2006cmcg); //Arbitrary mwcxorsg = Arbitrary.MultiplyWithCarryXorshiftGenerator(); //Console.WriteLine("mwcxorsg randoms:"); //testrandom(mwcxorsg); #endregion #region Set Tests //{ // int iterations = int.MaxValue / 1000; // HashSet<int> validation = new HashSet<int>(); // //for (int i = 0; i < interations; i++) // // validation.Add(i); // { // HashSet<int> set0 = new HashSet<int>(); // SetHashList<int> set1 = new SetHashList<int>(); // SetHashArray<int> set2 = new SetHashArray<int>(); // for (int i = 0; i < iterations; i++) set0.Add(i); // for (int i = 0; i < iterations; i++) set1.Add(i); // for (int i = 0; i < iterations; i++) set2.Add(i); // for (int i = 0; i < iterations; i++) // validation.Add(i); // foreach (int i in set0) { validation.Remove(i); } // for (int i = 0; i < iterations; i++) // validation.Add(i); // set1.Stepper((int i) => { validation.Remove(i); }); // for (int i = 0; i < iterations; i++) // validation.Add(i); // set2.Stepper((int i) => { validation.Remove(i); }); // for (int i = 0; i < iterations; i++) set0.Contains(i); // for (int i = 0; i < iterations; i++) set1.Contains(i); // for (int i = 0; i < iterations; i++) set2.Contains(i); // for (int i = 0; i < iterations; i++) set0.Remove(i); // for (int i = 0; i < iterations; i++) set1.Remove(i); // for (int i = 0; i < iterations; i++) set2.Remove(i); // Console.WriteLine("Adding HashSet: " + Seven.Diagnostics.Performance.Time_DateTimNow(() => { for (int i = 0; i < iterations; i++) set0.Add(i); })); // Console.WriteLine("Adding Set_HashLinkedList: " + Seven.Diagnostics.Performance.Time_DateTimNow(() => { for (int i = 0; i < iterations; i++) set1.Add(i); })); // Console.WriteLine("Adding SetHash: " + Seven.Diagnostics.Performance.Time_DateTimNow(() => { for (int i = 0; i < iterations; i++) set2.Add(i); })); // for (int i = 0; i < iterations; i++) // validation.Add(i); // foreach (int i in set0) { validation.Remove(i); } // Console.WriteLine("Validate HashSet: " + (validation.Count == 0)); // for (int i = 0; i < iterations; i++) // validation.Add(i); // set1.Stepper((int i) => { validation.Remove(i); }); // Console.WriteLine("Validate Set_HashLinkedList: " + (validation.Count == 0)); // for (int i = 0; i < iterations; i++) // validation.Add(i); // set2.Stepper((int i) => { validation.Remove(i); }); // Console.WriteLine("Validate SetHas: " + (validation.Count == 0)); // Console.WriteLine("Size HashSet: " + (typeof(HashSet<int>).GetField("m_buckets", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(set0) as int[]).Length); // Console.WriteLine("Size Set_HashLinkedList: " + set1.TableSize); // Console.WriteLine("Size SetHash: " + set2.TableSize); // Console.WriteLine("Constains HashSet: " + Seven.Diagnostics.Performance.Time_DateTimNow(() => { for (int i = 0; i < iterations; i++) set0.Contains(i); })); // Console.WriteLine("Constains Set_HashLinkedList: " + Seven.Diagnostics.Performance.Time_DateTimNow(() => { for (int i = 0; i < iterations; i++) set1.Contains(i); })); // Console.WriteLine("Constains SetHash: " + Seven.Diagnostics.Performance.Time_DateTimNow(() => { for (int i = 0; i < iterations; i++) set2.Contains(i); })); // //Console.WriteLine("Removed HashSet: " + Seven.Diagnostics.Performance.Time(() => { for (int i = 0; i < iterations; i++) set0.Remove(i); })); // //Console.WriteLine("Removed Set_HashLinkedList: " + Seven.Diagnostics.Performance.Time(() => { for (int i = 0; i < iterations; i++) set1.Remove(i); })); // //Console.WriteLine("Remove SetHash: " + Seven.Diagnostics.Performance.Time(() => { for (int i = 0; i < iterations; i++) set2.Remove(i); })); // Console.WriteLine("Removed HashSet: " + Seven.Diagnostics.Performance.Time_DateTimNow(() => { for (int i = iterations - 1; i >= 0; i--) set0.Remove(i); })); // Console.WriteLine("Removed Set_HashLinkedList: " + Seven.Diagnostics.Performance.Time_DateTimNow(() => { for (int i = iterations - 1; i >= 0; i--) set1.Remove(i); })); // Console.WriteLine("Remove SetHash: " + Seven.Diagnostics.Performance.Time_DateTimNow(() => { for (int i = iterations - 1; i >= 0; i--) set2.Remove(i); })); // Console.WriteLine("Size HashSet: " + (typeof(HashSet<int>).GetField("m_buckets", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(set0) as int[]).Length); // Console.WriteLine("Size Set_HashLinkedList: " + set1.TableSize); // Console.WriteLine("Size SetHash: " + set2.TableSize); // } // Console.WriteLine(); // { // HashSet<int> set0 = new HashSet<int>(); // SetHashList<int> set1 = new SetHashList<int>(); // SetHashArray<int> set2 = new SetHashArray<int>(); // for (int i = 0; i < iterations; i++) set0.Add(i); // for (int i = 0; i < iterations; i++) set1.Add(i); // for (int i = 0; i < iterations; i++) set2.Add(i); // for (int i = 0; i < iterations; i++) // validation.Add(i); // foreach (int i in set0) { validation.Remove(i); } // for (int i = 0; i < iterations; i++) // validation.Add(i); // set1.Stepper((int i) => { validation.Remove(i); }); // for (int i = 0; i < iterations; i++) // validation.Add(i); // set2.Stepper((int i) => { validation.Remove(i); }); // for (int i = 0; i < iterations; i++) set0.Contains(i); // for (int i = 0; i < iterations; i++) set1.Contains(i); // for (int i = 0; i < iterations; i++) set2.Contains(i); // for (int i = 0; i < iterations; i++) set0.Remove(i); // for (int i = 0; i < iterations; i++) set1.Remove(i); // for (int i = 0; i < iterations; i++) set2.Remove(i); // Console.WriteLine("Adding HashSet: " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = 0; i < iterations; i++) set0.Add(i); })); // Console.WriteLine("Adding Set_HashLinkedList: " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = 0; i < iterations; i++) set1.Add(i); })); // Console.WriteLine("Adding SetHash: " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = 0; i < iterations; i++) set2.Add(i); })); // for (int i = 0; i < iterations; i++) // validation.Add(i); // foreach (int i in set0) { validation.Remove(i); } // Console.WriteLine("Validate HashSet: " + (validation.Count == 0)); // for (int i = 0; i < iterations; i++) // validation.Add(i); // set1.Stepper((int i) => { validation.Remove(i); }); // Console.WriteLine("Validate Set_HashLinkedList: " + (validation.Count == 0)); // for (int i = 0; i < iterations; i++) // validation.Add(i); // set2.Stepper((int i) => { validation.Remove(i); }); // Console.WriteLine("Validate SetHas: " + (validation.Count == 0)); // Console.WriteLine("Size HashSet: " + (typeof(HashSet<int>).GetField("m_buckets", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(set0) as int[]).Length); // Console.WriteLine("Size Set_HashLinkedList: " + set1.TableSize); // Console.WriteLine("Size SetHash: " + set2.TableSize); // Console.WriteLine("Constains HashSet: " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = 0; i < iterations; i++) set0.Contains(i); })); // Console.WriteLine("Constains Set_HashLinkedList: " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = 0; i < iterations; i++) set1.Contains(i); })); // Console.WriteLine("Constains SetHash: " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = 0; i < iterations; i++) set2.Contains(i); })); // //Console.WriteLine("Removed HashSet: " + Seven.Diagnostics.Performance.Time2(() => { for (int i = 0; i < iterations; i++) set0.Remove(i); })); // //Console.WriteLine("Removed Set_HashLinkedList: " + Seven.Diagnostics.Performance.Time2(() => { for (int i = 0; i < iterations; i++) set1.Remove(i); })); // //Console.WriteLine("Remove SetHash: " + Seven.Diagnostics.Performance.Time2(() => { for (int i = 0; i < iterations; i++) set2.Remove(i); })); // Console.WriteLine("Removed HashSet: " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = iterations - 1; i >= 0; i--) set0.Remove(i); })); // Console.WriteLine("Removed Set_HashLinkedList: " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = iterations - 1; i >= 0; i--) set1.Remove(i); })); // Console.WriteLine("Remove SetHash: " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = iterations - 1; i >= 0; i--) set2.Remove(i); })); // Console.WriteLine("Size HashSet: " + (typeof(HashSet<int>).GetField("m_buckets", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(set0) as int[]).Length); // Console.WriteLine("Size Set_HashLinkedList: " + set1.TableSize); // Console.WriteLine("Size SetHash: " + set2.TableSize); // } // Console.WriteLine(); //} #endregion #region Map/Dictionary //{ // int iterations = int.MaxValue / 10000; // HashSet<int> validation = new HashSet<int>(); // //for (int i = 0; i < interations; i++) // // validation.Add(i); // { // Dictionary<int, int> map0 = new Dictionary<int, int>(); // //MapSetHashList<int, int> map1 = new MapSetHashList<int, int>(); // MapHashLinked<int, int> map2 = new MapHashLinked<int, int>(); // MapHashArray<int, int> map3 = new MapHashArray<int, int>(); // Console.WriteLine("Adding 0: " + Seven.Diagnostics.Performance.Time(() => { for (int i = 0; i < iterations; i++) map0.Add(i, i); })); // //Console.WriteLine("Adding 1: " + Seven.Diagnostics.Performance.Time(() => { for (int i = 0; i < iterations; i++) map1.Add(i, i); })); // Console.WriteLine("Adding 2: " + Seven.Diagnostics.Performance.Time(() => { for (int i = 0; i < iterations; i++) map2.Add(i, i); })); // Console.WriteLine("Adding 3: " + Seven.Diagnostics.Performance.Time(() => { for (int i = 0; i < iterations; i++) map3.Add(i, i); })); // for (int i = 0; i < iterations; i++) // validation.Add(i); // foreach (KeyValuePair<int, int> i in map0) { validation.Remove(i.Key); } // Console.WriteLine("Validate 0: " + (validation.Count == 0)); // //for (int i = 0; i < iterations; i++) // // validation.Add(i); // ////foreach (int i in map1) { validation.Remove(i); } // //map1.Stepper((int i) => { validation.Remove(i); }); // //Console.WriteLine("Validate 1: " + (validation.Count == 0)); // for (int i = 0; i < iterations; i++) // validation.Add(i); // //foreach (int i in map1) { validation.Remove(i); } // map2.Stepper((int i) => { validation.Remove(i); }); // Console.WriteLine("Validate 2: " + (validation.Count == 0)); // for (int i = 0; i < iterations; i++) // validation.Add(i); // //foreach (int i in map1) { validation.Remove(i); } // map3.Stepper((int i) => { validation.Remove(i); }); // Console.WriteLine("Validate 3: " + (validation.Count == 0)); // int temp; // Console.WriteLine("Get 0: " + Seven.Diagnostics.Performance.Time(() => { for (int i = 0; i < iterations; i++) temp = map0[i]; })); // //Console.WriteLine("Get 1: " + Seven.Diagnostics.Performance.Time(() => { for (int i = 0; i < iterations; i++) temp = map1[i]; })); // Console.WriteLine("Get 2: " + Seven.Diagnostics.Performance.Time(() => { for (int i = 0; i < iterations; i++) temp = map2[i]; })); // Console.WriteLine("Get 3: " + Seven.Diagnostics.Performance.Time(() => { for (int i = 0; i < iterations; i++) temp = map3[i]; })); // Console.WriteLine("Removed 0: " + Seven.Diagnostics.Performance.Time(() => { for (int i = 0; i < iterations; i++) map0.Remove(i); })); // //Console.WriteLine("Removed 1: " + Seven.Diagnostics.Performance.Time(() => { for (int i = 0; i < iterations; i++) map1.Remove(i); })); // Console.WriteLine("Removed 2: " + Seven.Diagnostics.Performance.Time(() => { for (int i = 0; i < iterations; i++) map2.Remove(i); })); // Console.WriteLine("Removed 3: " + Seven.Diagnostics.Performance.Time(() => { for (int i = 0; i < iterations; i++) map3.Remove(i); })); // } //} #endregion #region Vector Test //Console.WriteLine(); //Console.WriteLine("Vector Testing-------------------------------------"); //Random random = new Random(); //const int vector_size = 4; //const int vector_iterations = int.MaxValue / 100; //Vector<double> vector_a = new Vector<double>(vector_size); //Vector<double> vector_b = new Vector<double>(vector_size); //Vector<double> vector_c; //for (int i = 0; i < vector_size; i++) //{ // vector_a[i] = random.Next(); // vector_b[i] = random.Next(); //} //Console.WriteLine("Compile 1: " + Seven.Diagnostics.Performance.Time(() => { vector_c = Vector<double>.Vector_Add(vector_a, vector_b); })); //Console.WriteLine("Compile 2: " + Seven.Diagnostics.Performance.Time(() => { vector_c = Vector<double>.Vector_Add2(vector_a, vector_b); })); //Console.WriteLine("Compile 3: " + Seven.Diagnostics.Performance.Time(() => { vector_c = Vector<double>.Vector_Add3(vector_a, vector_b); })); //Console.WriteLine("Compile 4: " + Seven.Diagnostics.Performance.Time(() => { vector_c = Vector<double>.Vector_Add4(vector_a, vector_b); })); //Console.WriteLine("Test 1: " + Seven.Diagnostics.Performance.Time(() => { // for (int i = 0; i < vector_iterations; i++) // vector_c = Vector<double>.Vector_Add(vector_a, vector_b); //})); //Console.WriteLine("Test 2: " + Seven.Diagnostics.Performance.Time(() => //{ // for (int i = 0; i < vector_iterations; i++) // vector_c = Vector<double>.Vector_Add2(vector_a, vector_b); //})); //Console.WriteLine("Test 3: " + Seven.Diagnostics.Performance.Time(() => //{ // for (int i = 0; i < vector_iterations; i++) // vector_c = Vector<double>.Vector_Add3(vector_a, vector_b); //})); //Console.WriteLine("Test 4: " + Seven.Diagnostics.Performance.Time(() => //{ // for (int i = 0; i < vector_iterations; i++) // vector_c = Vector<double>.Vector_Add4(vector_a, vector_b); //})); #endregion #region Sorting Speed //{ // int size = int.MaxValue / 1000000; // int[] dataSet = new int[size]; // for (int i = 0; i < size; i++) // dataSet[i] = i; // Console.WriteLine("Sorting Algorithms----------------------"); // Console.WriteLine(); // //Sort<int>.Shuffle(dataSet); // //Console.Write("Bubble: " + Seven.Diagnostics.Performance.Time(() => { Sort<int>.Bubble(dataSet); })); // Sort<int>.Shuffle(dataSet); // Console.WriteLine("Selection: " + Seven.Diagnostics.Performance.Time(() => { Sort<int>.Selection(dataSet); })); // Sort<int>.Shuffle(dataSet); // Console.WriteLine("Insertion: " + Seven.Diagnostics.Performance.Time(() => { Sort<int>.Insertion(dataSet); })); // Sort<int>.Shuffle(dataSet); // Console.WriteLine("Quick: " + Seven.Diagnostics.Performance.Time(() => { Sort<int>.Quick(dataSet); })); // Sort<int>.Shuffle(dataSet); // Console.WriteLine("Merge: " + Seven.Diagnostics.Performance.Time(() => { Sort<int>.Merge(dataSet); })); // Sort<int>.Shuffle(dataSet); // Console.WriteLine("Heap: " + Seven.Diagnostics.Performance.Time(() => { Sort<int>.Heap(dataSet); })); // Sort<int>.Shuffle(dataSet); // Console.WriteLine("OddEven: " + Seven.Diagnostics.Performance.Time(() => { Sort<int>.OddEven(dataSet); })); // Sort<int>.Shuffle(dataSet); // Console.WriteLine("IEnumerable: " + Seven.Diagnostics.Performance.Time(() => { dataSet.OrderBy(item => item); })); // Sort<int>.Shuffle(dataSet); // Console.WriteLine("Array.Sort: " + Seven.Diagnostics.Performance.Time(() => { Array.Sort(dataSet); })); //} #endregion #region Matrix Test //Random random = new Random(); //const int matrix_rows = 4; //const int matrix_columns = 4; //const int matrix_iterations = int.MaxValue / 100; //{ // Seven.Mathematics.Matrix<double> matrix_a = new Seven.Mathematics.Matrix<double>(matrix_rows, matrix_columns); // Seven.Mathematics.Matrix<double> matrix_b = new Seven.Mathematics.Matrix<double>(matrix_rows, matrix_columns); // Seven.Mathematics.Matrix<double> matrix_c; // matrix_c = matrix_a + matrix_b; // matrix_c = matrix_b + matrix_a; // //matrix_a = matrix_b + matrix_c; // //matrix_a = matrix_c + matrix_b; // for (int i = 0; i < matrix_rows; i++) // for (int j = 0; j < matrix_columns; j++) // { // matrix_a[i, j] = random.Next(); // matrix_b[i, j] = random.Next(); // } // Console.WriteLine("Test 1: " + Seven.Diagnostics.Performance.Time(() => // { // for (int i = 0; i < matrix_iterations; i++) // matrix_c = matrix_a + matrix_b; // })); //matrix_c = Matrix<double>.Matrix_Negate2(matrix_a); //Console.WriteLine("Test 2: " + Seven.Diagnostics.Performance.Time(() => //{ // for (int i = 0; i < matrix_iterations; i++) // matrix_c = Matrix<double>.Matrix_Negate2(matrix_a); //})); //Console.WriteLine("Test 2: " + Seven.Diagnostics.Performance.Time(() => //{ // for (int i = 0; i < matrix_iterations; i++) // Matrix<double>.Matrix_IsSymetric2(matrix_a); //})); //Console.WriteLine("Compile 1: " + Seven.Diagnostics.Performance.Time(() => { matrix_c = matrix_a + matrix_b; })); //Console.WriteLine("Test 1: " + Seven.Diagnostics.Performance.Time(() => //{ // for (int i = 0; i < matrix_iterations; i++) // matrix_c = matrix_a + matrix_b; //})); //} #endregion #region Omnitree Omnitree.Locate <TestObject, double> locate = (TestObject record) => { return((int i) => { switch (i) { case 0: return record.X; case 1: return record.Y; case 2: return record.Z; default: throw new System.Exception(); } }); }; Compute <double> .Compare(0, 0); Omnitree <TestObject, double> omnitree = new OmnitreeLinked <TestObject, double>( 3, Accessor.Get(new double[] { 0, 0, 0 }), Accessor.Get(new double[] { 1, 1, 1 }), locate, (double a, double b) => { return(a == b); }, Equate.Default, Compute <double> .Compare, (double a, double b) => { return((a + b) / 2); }); System.Collections.Generic.List <TestObject> list = new System.Collections.Generic.List <TestObject>(); System.Collections.Generic.LinkedList <TestObject> linkedlist = new System.Collections.Generic.LinkedList <TestObject>(); Random random = new Random(7); int count = 10000; TestObject[] records = new TestObject[count]; for (int i = 0; i < count; i++) { records[i] = new TestObject(i, random.NextDouble(), random.NextDouble(), random.NextDouble()); } Console.WriteLine("Testing with " + count + " records..."); Console.WriteLine(); Console.WriteLine("Adding (Omnitree): " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = 0; i < count; i++) { omnitree.Add(records[i]); } })); Console.WriteLine("Adding (List): " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = 0; i < count; i++) { list.Add(records[i]); } })); Console.WriteLine("Adding (L-List): " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = 0; i < count; i++) { linkedlist.AddLast(records[i]); } })); Console.WriteLine(); Sort <TestObject> .Shuffle(random, Accessor.Get(records), Accessor.Assign(records), 0, records.Length); Console.WriteLine("Querying Single (Omnitree): " + Seven.Diagnostics.Performance.Time_StopWatch(() => { bool query_test; for (int i = 0; i < count; i++) { query_test = false; omnitree[locate(records[i])]((TestObject record) => { query_test = true; }); //omnitree[records[i].X, records[i].Y, records[i].Z]((TestObject record) => { query_test = true; }); if (query_test == false) { throw new System.Exception(); } } })); Console.WriteLine("Querying Single (List): " + Seven.Diagnostics.Performance.Time_StopWatch(() => { bool query_test = false; for (int i = 0; i < count; i++) { foreach (TestObject record in list) { if (record.X == records[i].X && record.Y == records[i].Y && record.Z == records[i].Z) { query_test = true; break; } } if (query_test == false) { throw new System.Exception(); } } })); Console.WriteLine("Querying Single (L-List): " + Seven.Diagnostics.Performance.Time_StopWatch(() => { bool query_test = false; for (int i = 0; i < count; i++) { foreach (TestObject record in linkedlist) { if (record.X == records[i].X && record.Y == records[i].Y && record.Z == records[i].Z) { query_test = true; break; } } if (query_test == false) { throw new System.Exception(); } } })); Console.WriteLine(); int random_query_count = count / 100; double[][] random_mins = new double[random_query_count][]; for (int i = 0; i < random_query_count; i++) { random_mins[i] = new double[] { random.NextDouble(), random.NextDouble(), random.NextDouble(), } } ; double[][] random_maxes = new double[random_query_count][]; for (int i = 0; i < random_query_count; i++) { random_maxes[i] = new double[] { random.NextDouble() *((1 - random_mins[i][0]) + random_mins[i][0]), random.NextDouble() * ((1 - random_mins[i][1]) + random_mins[i][1]), random.NextDouble() * ((1 - random_mins[i][2]) + random_mins[i][2]) } } ; Console.WriteLine(random_query_count + " random range queries..."); int[] query_count_omnitree = new int[random_query_count]; int[] query_count_list = new int[random_query_count]; int[] query_count_linkedlist = new int[random_query_count]; Console.WriteLine("Querying Range (Omnitree): " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = 0; i < random_query_count; i++) { omnitree.Stepper((TestObject record) => { query_count_omnitree[i]++; }, random_mins[i], random_maxes[i]); } })); Console.WriteLine("Querying Range (List): " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = 0; i < random_query_count; i++) { foreach (TestObject record in list) { if (record.X >= random_mins[i][0] && record.X <= random_maxes[i][0] && record.Y >= random_mins[i][1] && record.Y <= random_maxes[i][1] && record.Z >= random_mins[i][2] && record.Z <= random_maxes[i][2]) { query_count_list[i]++; } } } })); Console.WriteLine("Querying Range (L-List): " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = 0; i < random_query_count; i++) { foreach (TestObject record in linkedlist) { if (record.X >= random_mins[i][0] && record.X <= random_maxes[i][0] && record.Y >= random_mins[i][1] && record.Y <= random_maxes[i][1] && record.Z >= random_mins[i][2] && record.Z <= random_maxes[i][2]) { query_count_linkedlist[i]++; } } } })); for (int i = 0; i < random_query_count; i++) { if (query_count_omnitree[i] != query_count_list[i] || query_count_list[i] != query_count_linkedlist[i]) { throw new System.Exception(); } } Console.WriteLine(); foreach (TestObject record in records) { record.X += Math.Max(0d, Math.Min(1d, (random.NextDouble() / 100D) - .5D)); record.Y += Math.Max(0d, Math.Min(1d, (random.NextDouble() / 100D) - .5D)); record.Z += Math.Max(0d, Math.Min(1d, (random.NextDouble() / 100D) - .5D)); } Console.WriteLine("Updating (Omnitree): " + Seven.Diagnostics.Performance.Time_StopWatch(() => { omnitree.Update(); })); Console.WriteLine(); Console.WriteLine("Removing Single (Omnitree): " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = 0; i < count; i++) { //// Removal Method #1 omnitree.Remove(records[i]); //// Removal Method #2 //omnitree.Remove(locate(records[i]), locate(records[i])); //// Removal Method #3 //omnitree.Remove(locate(records[i]), locate(records[i]), (TestObject step) => { return records[i].Id == step.Id; }); //// Removal Method #4 //double[] location = new double[] { locate(records[i])(0), locate(records[i])(1), locate(records[i])(2) }; //omnitree.Remove(location, location); //// Removal Method #5 //double[] location = new double[] { locate(records[i])(0), locate(records[i])(1), locate(records[i])(2) }; //omnitree.Remove(location, location, (omnitree_record step) => { return records[i].Id == step.Id; }); } })); Console.WriteLine("Removing Single (List): " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = 0; i < count; i++) { //list.Remove(records[i]); for (int j = 0; j < list.Count; j++) { if (list[j].X == records[i].X && list[j].Y == records[i].Y && list[j].Z == records[i].Z) { list.RemoveAt(j); break; } } } })); Console.WriteLine("Removing Single (L-List): " + Seven.Diagnostics.Performance.Time_StopWatch(() => { for (int i = 0; i < count; i++) { //linkedlist.Remove(records[i]); LinkedList <TestObject> temp = new LinkedList <TestObject>(); foreach (TestObject record in linkedlist) { if (record.X == records[i].X && record.Y == records[i].Y && record.Z == records[i].Z) { temp.AddLast(record); break; } } foreach (TestObject record in temp) { linkedlist.Remove(record); } } })); for (int i = 0; i < count; i++) { omnitree.Add(records[i]); list.Add(records[i]); linkedlist.AddLast(records[i]); } Console.WriteLine(); TimeSpan omnitree_remove_span = TimeSpan.Zero; for (int i = 0; i < random_query_count; i++) { System.Collections.Generic.List <TestObject> temp = new System.Collections.Generic.List <TestObject>(); omnitree[locate(records[i])]((TestObject record) => { temp.Add(record); }); omnitree_remove_span += Seven.Diagnostics.Performance.Time_StopWatch(() => { omnitree.Remove(random_mins[i], random_maxes[i]); }); foreach (TestObject record in temp) { omnitree.Add(record); } } Console.WriteLine("Removing Range (Omnitree): " + omnitree_remove_span); TimeSpan list_remove_span = TimeSpan.Zero; for (int i = 0; i < random_query_count; i++) { System.Collections.Generic.List <TestObject> temp = new System.Collections.Generic.List <TestObject>(); foreach (TestObject record in list) { if (record.X >= random_mins[i][0] && record.X <= random_maxes[i][0] && record.Y >= random_mins[i][1] && record.Y <= random_maxes[i][1] && record.Z >= random_mins[i][2] && record.Z <= random_maxes[i][2]) { temp.Add(record); } } list_remove_span += Seven.Diagnostics.Performance.Time_StopWatch(() => { list.RemoveAll((TestObject record) => { return(record.X >= random_mins[i][0] && record.X <= random_maxes[i][0] && record.Y >= random_mins[i][1] && record.Y <= random_maxes[i][1] && record.Z >= random_mins[i][2] && record.Z <= random_maxes[i][2]); }); //for (int j = 0; j < list.Count; j++) //{ // if (list[j].X >= random_mins[i][0] && list[j].X <= random_maxes[i][0] && // list[j].Y >= random_mins[i][1] && list[j].Y <= random_maxes[i][1] && // list[j].Z >= random_mins[i][2] && list[j].Z <= random_maxes[i][2]) // { // list.RemoveAll.RemoveAt(i); // } //} }); foreach (TestObject record in temp) { list.Add(record); } } Console.WriteLine("Removing Range (List): " + list_remove_span); TimeSpan linkedlist_remove_span = TimeSpan.Zero; for (int i = 0; i < random_query_count; i++) { System.Collections.Generic.List <TestObject> temp = new System.Collections.Generic.List <TestObject>(); foreach (TestObject record in linkedlist) { if (record.X >= random_mins[i][0] && record.X <= random_maxes[i][0] && record.Y >= random_mins[i][1] && record.Y <= random_maxes[i][1] && record.Z >= random_mins[i][2] && record.Z <= random_maxes[i][2]) { temp.Add(record); } } linkedlist_remove_span += Seven.Diagnostics.Performance.Time_StopWatch(() => { System.Collections.Generic.List <TestObject> temp2 = new System.Collections.Generic.List <TestObject>(); foreach (TestObject record in linkedlist) { if (record.X >= random_mins[i][0] && record.X <= random_maxes[i][0] && record.Y >= random_mins[i][1] && record.Y <= random_maxes[i][1] && record.Z >= random_mins[i][2] && record.Z <= random_maxes[i][2]) { temp2.Add(record); } } foreach (TestObject record in temp2) { linkedlist.Remove(record); } }); foreach (TestObject record in temp) { list.Add(record); } } Console.WriteLine("Removing Range (L-List): " + linkedlist_remove_span); #endregion Console.WriteLine(); Console.WriteLine("Done..."); Console.ReadLine(); } } }