/// <summary>Converts HSL color values to Towel.Graphics.Color values.</summary> /// <param name="hue">The HSL hue component.</param> /// <param name="saturation">The HSL hue saturation.</param> /// <param name="lightness">The HSL hue lightness.</param> /// <param name="alpha">The HSL hue alpha.</param> /// <returns>The converted color value.</returns> /// <citation> /// This conversion algorithm was originally developed as part of the /// OpenTK open source project. Hoever, it has been modified since its /// addition to the Theta Framework. /// </citation> public static Color FromHsl(float hue, float saturation, float lightness, float alpha) { hue = hue * 360.0f; var C = (1.0f - Compute.AbsoluteValue(2.0f * lightness - 1.0f)) * saturation; var h = hue / 60.0f; var X = C * (1.0f - Compute.AbsoluteValue(h % 2.0f - 1.0f)); float r, g, b; if (0.0f <= h && h < 1.0f) { r = C; g = X; b = 0.0f; } else if (1.0f <= h && h < 2.0f) { r = X; g = C; b = 0.0f; } else if (2.0f <= h && h < 3.0f) { r = 0.0f; g = C; b = X; } else if (3.0f <= h && h < 4.0f) { r = 0.0f; g = X; b = C; } else if (4.0f <= h && h < 5.0f) { r = X; g = 0.0f; b = C; } else if (5.0f <= h && h < 6.0f) { r = C; g = 0.0f; b = X; } else { r = 0.0f; g = 0.0f; b = 0.0f; } var m = lightness - (C / 2.0f); return(new Color(r + m, g + m, b + m, alpha)); }
public Integer Modulus(Integer that) { Integer abs = Compute <Integer> .AbsoluteValue(that); Integer result = Compute <Integer> .AbsoluteValue(this); while (result < that) { result = result - that; } if ((this > 0 && that < 0) || (this > 0 && that < 0)) { return(-result); } else { return(result); } }
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 }
static void Main(string[] args) { Random random = new Random(); int test = 10; Console.WriteLine("You are runnning the Data Structures example."); Console.WriteLine("======================================================"); Console.WriteLine(); #region Link (aka Tuple) Console.WriteLine(" Link------------------------------------"); Console.WriteLine(); Console.WriteLine(" A \"Link\" is like a System.Tuple that implements"); Console.WriteLine(" Towel.DataStructures.DataStructure. A Link/Tuple is"); Console.WriteLine(" used when you have a small, known-sized set of objects"); Console.WriteLine(" that you want to bundle together without making a custom"); Console.WriteLine(" custom class."); Console.WriteLine(); Link link = new Link <int, int, int, int, int, int>(0, 1, 2, 3, 4, 5); Console.Write(" Traversal: "); link.Stepper(i => Console.Write(i)); Console.WriteLine(); Console.WriteLine(" Size: " + link.Size); Console.WriteLine(); #endregion #region Indexed (aka Array) Console.WriteLine(" Indexed---------------------------------"); Console.WriteLine(); Console.WriteLine(" An \"Indexed\" is just a wrapper for arrays that implements"); Console.WriteLine(" Towel.DataStructures.DataStructure. An array is used when"); Console.WriteLine(" dealing with static-sized, known-sized sets of data. Arrays"); Console.WriteLine(" can be sorted along 1 dimensions for binary searching algorithms."); Console.WriteLine(); IIndexed <int> indexed = new IndexedArray <int>(test); Console.Write(" Filling in (0-" + (test - 1) + ")..."); for (int i = 0; i < test; i++) { indexed[i] = i; } Console.WriteLine(); Console.Write(" Traversal: "); indexed.Stepper(i => Console.Write(i)); Console.WriteLine(); Console.WriteLine(" Length: " + indexed.Length); Console.WriteLine(); #endregion #region Addable (aka List) Console.WriteLine(" Addable---------------------------------"); Console.WriteLine(); Console.WriteLine(" An \"Addable\" is like an IList that implements"); Console.WriteLine(" Towel.DataStructures.DataStructure. \"AddableArray\" is"); Console.WriteLine(" the array implementation while \"AddableLinked\" is the"); Console.WriteLine(" the linked-list implementation. An Addable/List is used"); Console.WriteLine(" when dealing with an unknown quantity of data that you"); Console.WriteLine(" will likely have to enumerate/step through everything. The"); Console.WriteLine(" AddableArray shares the properties of an Indexed/Array in"); Console.WriteLine(" that it can be relateively quickly sorted along 1 dimensions"); Console.WriteLine(" for binary search algorithms."); Console.WriteLine(); // AddableArray --------------------------------------- IAddable <int> addableArray = new AddableArray <int>(test); Console.Write(" [AddableArray] Adding (0-" + (test - 1) + ")..."); for (int i = 0; i < test; i++) { addableArray.Add(i); } Console.WriteLine(); Console.Write(" [AddableArray] Traversal: "); addableArray.Stepper(i => Console.Write(i)); Console.WriteLine(); Console.WriteLine(" [AddableArray] Count: " + addableArray.Count); addableArray.Clear(); // Clears the addable Console.WriteLine(); // AddableLinked --------------------------------------- IAddable <int> addableLinked = new AddableLinked <int>(); Console.Write(" [AddableLinked] Adding (0-" + (test - 1) + ")..."); for (int i = 0; i < test; i++) { addableLinked.Add(i); } Console.WriteLine(); Console.Write(" [AddableLinked] Traversal: "); addableLinked.Stepper(i => Console.Write(i)); Console.WriteLine(); Console.WriteLine(" [AddableLinked] Count: " + addableLinked.Count); addableLinked.Clear(); // Clears the addable Console.WriteLine(); #endregion #region FirstInLastOut (aka stack) { Console.WriteLine(" FirstInLastOut---------------------------------"); Console.WriteLine(); Console.WriteLine(" An \"FirstInLastOut\" is a Stack that implements"); Console.WriteLine(" Towel.DataStructures.DataStructure. \"FirstInLastOutArray\" is"); Console.WriteLine(" the array implementation while \"FirstInLastOutLinked\" is the"); Console.WriteLine(" the linked-list implementation. A FirstInLastOut/Stack is used"); Console.WriteLine(" specifically when you need the algorithm provided by the Push"); Console.WriteLine(" and Pop functions."); Console.WriteLine(); IFirstInLastOut <int> firstInLastOutArray = new FirstInLastOutArray <int>(); Console.Write(" [FirstInLastOutArray] Pushing (0-" + (test - 1) + ")..."); for (int i = 0; i < test; i++) { firstInLastOutArray.Push(i); } Console.WriteLine(); Console.Write(" [FirstInLastOutArray] Traversal: "); firstInLastOutArray.Stepper(i => Console.Write(i)); Console.WriteLine(); Console.WriteLine(" [FirstInLastOutArray] Pop: " + firstInLastOutArray.Pop()); Console.WriteLine(" [FirstInLastOutArray] Pop: " + firstInLastOutArray.Pop()); Console.WriteLine(" [FirstInLastOutArray] Peek: " + firstInLastOutArray.Peek()); Console.WriteLine(" [FirstInLastOutArray] Pop: " + firstInLastOutArray.Pop()); Console.WriteLine(" [FirstInLastOutArray] Count: " + firstInLastOutArray.Count); firstInLastOutArray.Clear(); // Clears the firstInLastOut Console.WriteLine(); IFirstInLastOut <int> firstInLastOutLinked = new FirstInLastOutLinked <int>(); Console.Write(" [FirstInLastOutLinked] Pushing (0-" + (test - 1) + ")..."); for (int i = 0; i < test; i++) { firstInLastOutLinked.Push(i); } Console.WriteLine(); Console.Write(" [FirstInLastOutLinked] Traversal: "); firstInLastOutLinked.Stepper(i => Console.Write(i)); Console.WriteLine(); Console.WriteLine(" [FirstInLastOutLinked] Pop: " + firstInLastOutLinked.Pop()); Console.WriteLine(" [FirstInLastOutLinked] Pop: " + firstInLastOutLinked.Pop()); Console.WriteLine(" [FirstInLastOutLinked] Peek: " + firstInLastOutLinked.Peek()); Console.WriteLine(" [FirstInLastOutLinked] Pop: " + firstInLastOutLinked.Pop()); Console.WriteLine(" [FirstInLastOutLinked] Count: " + firstInLastOutLinked.Count); firstInLastOutLinked.Clear(); // Clears the firstInLastOut Console.WriteLine(); } #endregion #region FirstInFirstOut (aka Queue) { Console.WriteLine(" FirstInFirstOut---------------------------------"); Console.WriteLine(); Console.WriteLine(" An \"FirstInFirstOut\" is a Queue that implements"); Console.WriteLine(" Towel.DataStructures.DataStructure. \"FirstInFirstOutArray\" is"); Console.WriteLine(" the array implementation while \"FirstInFirstOutLinked\" is the"); Console.WriteLine(" the linked-list implementation. A FirstInFirstOut/Stack is used"); Console.WriteLine(" specifically when you need the algorithm provided by the Queue"); Console.WriteLine(" and Dequeue functions."); Console.WriteLine(); IFirstInFirstOut <int> firstInFirstOutArray = new FirstInFirstOutArray <int>(); Console.Write(" [FirstInFirstOutArray] Enqueuing (0-" + (test - 1) + ")..."); for (int i = 0; i < test; i++) { firstInFirstOutArray.Enqueue(i); } Console.WriteLine(); Console.Write(" [FirstInFirstOutArray] Traversal: "); firstInFirstOutArray.Stepper(i => Console.Write(i)); Console.WriteLine(); Console.WriteLine(" [FirstInFirstOutArray] Dequeue: " + firstInFirstOutArray.Dequeue()); Console.WriteLine(" [FirstInFirstOutArray] Dequeue: " + firstInFirstOutArray.Dequeue()); Console.WriteLine(" [FirstInFirstOutArray] Peek: " + firstInFirstOutArray.Peek()); Console.WriteLine(" [FirstInFirstOutArray] Dequeue: " + firstInFirstOutArray.Dequeue()); Console.WriteLine(" [FirstInFirstOutArray] Count: " + firstInFirstOutArray.Count); firstInFirstOutArray.Clear(); // Clears the firstInLastOut Console.WriteLine(); IFirstInFirstOut <int> firstInFirstOutLinked = new FirstInFirstOutLinked <int>(); Console.Write(" [FirstInFirstOutLinked] Enqueuing (0-" + (test - 1) + ")..."); for (int i = 0; i < test; i++) { firstInFirstOutLinked.Enqueue(i); } Console.WriteLine(); Console.Write(" [FirstInFirstOutLinked] Traversal: "); firstInFirstOutLinked.Stepper(i => Console.Write(i)); Console.WriteLine(); Console.WriteLine(" [FirstInFirstOutLinked] Pop: " + firstInFirstOutLinked.Dequeue()); Console.WriteLine(" [FirstInFirstOutLinked] Pop: " + firstInFirstOutLinked.Dequeue()); Console.WriteLine(" [FirstInFirstOutLinked] Peek: " + firstInFirstOutLinked.Peek()); Console.WriteLine(" [FirstInFirstOutLinked] Pop: " + firstInFirstOutLinked.Dequeue()); Console.WriteLine(" [FirstInFirstOutLinked] Count: " + firstInFirstOutLinked.Count); firstInFirstOutLinked.Clear(); // Clears the firstInLastOut Console.WriteLine(); } #endregion #region Heap { Console.WriteLine(" Heap---------------------------------"); Console.WriteLine(); Console.WriteLine(" An \"Heap\" is a binary tree that stores items based on priorities."); Console.WriteLine(" It implements Towel.DataStructures.DataStructure like the others."); Console.WriteLine(" It uses sifting algorithms to move nodes vertically through itself."); Console.WriteLine(" It is often the best data structure for standard priority queues."); Console.WriteLine(" \"HeapArray\" is an implementation where the tree has been flattened"); Console.WriteLine(" into an array."); Console.WriteLine(); Console.WriteLine(" Let's say the priority is how close a number is to \"5\"."); Console.WriteLine(" So \"Dequeue\" will give us the next closest value to \"5\"."); Comparison Priority(int a, int b) { int _a = Compute.AbsoluteValue(a - 5); int _b = Compute.AbsoluteValue(b - 5); Comparison comparison = Compare.Wrap(_b.CompareTo(_a)); return(comparison); } Console.WriteLine(); IHeap <int> heapArray = new HeapArray <int>(Priority); Console.Write(" [HeapArray] Enqueuing (0-" + (test - 1) + ")..."); for (int i = 0; i < test; i++) { heapArray.Enqueue(i); } Console.WriteLine(); Console.WriteLine(" [HeapArray] Dequeue: " + heapArray.Dequeue()); Console.WriteLine(" [HeapArray] Dequeue: " + heapArray.Dequeue()); Console.WriteLine(" [HeapArray] Peek: " + heapArray.Peek()); Console.WriteLine(" [HeapArray] Dequeue: " + heapArray.Dequeue()); Console.WriteLine(" [HeapArray] Count: " + heapArray.Count); heapArray.Clear(); // Clears the heapArray Console.WriteLine(); } #endregion #region Tree //Console.WriteLine(" Tree-----------------------------"); //Tree<int> tree_Map = new TreeMap<int>(0, Compute.Equal, Hash.Default); //for (int i = 1; i < test; i++) //{ // tree_Map.Add(i, i / Compute.SquareRoot(i)); //} //Console.Write(" Children of 0 (root): "); //tree_Map.Children(0, (int i) => { Console.Write(i + " "); }); //Console.WriteLine(); //Console.Write(" Children of " + ((int)System.Math.Sqrt(test) - 1) + " (root): "); //tree_Map.Children(((int)System.Math.Sqrt(test) - 1), (int i) => { Console.Write(i + " "); }); //Console.WriteLine(); //Console.Write(" Traversal: "); //tree_Map.Stepper((int i) => { Console.Write(i + " "); }); //Console.WriteLine(); //Console.WriteLine(); #endregion #region AVL Tree { Console.WriteLine(" AvlTree------------------------------------------------"); Console.WriteLine(); Console.WriteLine(" An AVL Tree is a sorted binary tree."); Console.WriteLine(" It implements Towel.DataStructures.DataStructure like the others."); Console.WriteLine(" It allows for very fast 1D ranged queries/traversals."); Console.WriteLine(" It is very similar to an Red Black tree, but uses a different sorting algorithm."); Console.WriteLine(); IAvlTree <int> avlTree = new AvlTreeLinked <int>(); Console.Write(" Adding (0-" + (test - 1) + ")..."); for (int i = 0; i < test; i++) { avlTree.Add(i); } Console.WriteLine(); Console.Write(" Traversal: "); avlTree.Stepper(i => Console.Write(i)); Console.WriteLine(); //// Note: Because the nodes in AVL Tree linked do not have //// a parent pointer, the IEnumerable "foreach" iteration //// is extremely slow and should be avoided. It requires //// a stack for it's iteration. // //Console.Write(" Traversal Foreach: "); //foreach (int i in avlTree) //{ // Console.Write(i); //} //Console.WriteLine(); int minimum = random.Next(1, test / 2); int maximum = random.Next(1, test / 2) + test / 2; Console.Write(" Ranged Traversal [" + minimum + "-" + maximum + "]: "); avlTree.Stepper(i => Console.Write(i), minimum, maximum); Console.WriteLine(); int removal = random.Next(0, test); Console.Write(" Remove(" + removal + "): "); avlTree.Remove(removal); avlTree.Stepper(i => Console.Write(i)); Console.WriteLine(); int contains = random.Next(0, test); Console.WriteLine(" Contains(" + contains + "): " + avlTree.Contains(contains)); Console.WriteLine(" Current Least: " + avlTree.CurrentLeast); Console.WriteLine(" Current Greatest: " + avlTree.CurrentGreatest); Console.WriteLine(" Count: " + avlTree.Count); avlTree.Clear(); // Clears the AVL tree Console.WriteLine(); } #endregion #region Red-Black Tree { Console.WriteLine(" Red-Black Tree------------------------------------------------"); Console.WriteLine(); Console.WriteLine(" An Red-Black Tree is a sorted binary tree."); Console.WriteLine(" It implements Towel.DataStructures.DataStructure like the others."); Console.WriteLine(" It allows for very fast 1D ranged queries/traversals."); Console.WriteLine(" It is very similar to an AVL tree, but uses a different sorting algorithm."); Console.WriteLine(); IRedBlackTree <int> redBlackTree = new RedBlackTreeLinked <int>(); Console.Write(" Adding (0-" + (test - 1) + ")..."); for (int i = 0; i < test; i++) { redBlackTree.Add(i); } Console.WriteLine(); Console.Write(" Traversal: "); redBlackTree.Stepper(i => Console.Write(i)); Console.WriteLine(); int minimum = random.Next(1, test / 2); int maximum = random.Next(1, test / 2) + test / 2; Console.Write(" Ranged Traversal [" + minimum + "-" + maximum + "]: "); redBlackTree.Stepper(i => Console.Write(i), minimum, maximum); Console.WriteLine(); int removal = random.Next(0, test); Console.Write(" Remove(" + removal + "): "); redBlackTree.Remove(removal); redBlackTree.Stepper(i => Console.Write(i)); Console.WriteLine(); int contains = random.Next(0, test); Console.WriteLine(" Contains(" + contains + "): " + redBlackTree.Contains(contains)); Console.WriteLine(" Current Least: " + redBlackTree.CurrentLeast); Console.WriteLine(" Current Greatest: " + redBlackTree.CurrentGreatest); Console.WriteLine(" Count: " + redBlackTree.Count); redBlackTree.Clear(); // Clears the Red Black tree Console.WriteLine(); } #endregion #region BTree { Console.WriteLine(" B Tree------------------------------------------------"); Console.WriteLine(); Console.WriteLine(" A B Tree is a sorted binary tree that allows multiple values to"); Console.WriteLine(" be stored per node. This makes it sort of a hybrid between a"); Console.WriteLine(" binary tree and an array. Because multiple values are stored "); Console.WriteLine(" per node, it means less nodes must be traversed to completely"); Console.WriteLine(" traverse the values in the B tree."); Console.WriteLine(); Console.WriteLine(" The generic B Tree in Towel is still in development."); Console.WriteLine(); } #endregion #region Set { Console.WriteLine(" Set------------------------------------------------"); Console.WriteLine(); Console.WriteLine(" A Set is like an Addable/List, but it does not allow duplicates. Sets are"); Console.WriteLine(" usually implemented using hash codes. Implementations with hash codes"); Console.WriteLine(" usually have very fast \"Contains\" checks to see if a value has already"); Console.WriteLine(" been added to the set."); Console.WriteLine(); ISet <int> setHashLinked = new SetHashLinked <int>(); Console.Write(" Adding (0-" + (test - 1) + ")..."); for (int i = 0; i < test; i++) { setHashLinked.Add(i); } Console.WriteLine(); Console.Write(" Traversal: "); setHashLinked.Stepper(i => Console.Write(i)); Console.WriteLine(); int a = random.Next(0, test); setHashLinked.Remove(a); Console.Write(" Remove(" + a + "): "); setHashLinked.Stepper(i => Console.Write(i)); Console.WriteLine(); int b = random.Next(0, test); Console.WriteLine(" Contains(" + b + "): " + setHashLinked.Contains(b)); Console.WriteLine(" Count: " + setHashLinked.Count); Console.WriteLine(); } #endregion #region Map (aka Dictionary) { Console.WriteLine(" Map------------------------------------------------"); Console.WriteLine(); Console.WriteLine(" A Map (aka Dictionary) is similar to a Set, but it stores two values (a "); Console.WriteLine(" key and a value). Maps do not allow duplicate keys much like Sets don't"); Console.WriteLine(" allow duplicate values. When provided with the key, the Map uses that key"); Console.WriteLine(" to look up the value that it is associated with. Thus, it allows you to "); Console.WriteLine(" \"map\" one object to another. As with Sets, Maps are usually implemented"); Console.WriteLine(" using hash codes."); Console.WriteLine(); // Note: the first generic is the value, the second is the key IMap <string, int> mapHashLinked = new MapHashLinked <string, int>(); Console.WriteLine(" Let's map each int to its word representation (ex 1 -> One)."); Console.Write(" Adding (0-" + (test - 1) + ")..."); for (int i = 0; i < test; i++) { mapHashLinked.Add(i, ((decimal)i).ToEnglishWords()); } Console.WriteLine(); Console.WriteLine(" Traversal: "); mapHashLinked.Keys(i => Console.WriteLine(" " + i + "->" + mapHashLinked[i])); Console.WriteLine(); int a = random.Next(0, test); mapHashLinked.Remove(a); Console.Write(" Remove(" + a + "): "); mapHashLinked.Keys(i => Console.Write(i)); Console.WriteLine(); int b = random.Next(0, test); Console.WriteLine(" Contains(" + b + "): " + mapHashLinked.Contains(b)); Console.WriteLine(" Count: " + mapHashLinked.Count); Console.WriteLine(); } #endregion #region OmnitreePoints { Console.WriteLine(" OmnitreePoints--------------------------------------"); Console.WriteLine(); Console.WriteLine(" An Omnitree is an ND SPT that allows for"); Console.WriteLine(" multidimensional sorting. Any time you need to look"); Console.WriteLine(" items up based on multiple fields/properties, then"); Console.WriteLine(" you might want to use an Omnitree. If you need to"); Console.WriteLine(" perform ranged queries on multiple dimensions, then"); Console.WriteLine(" the Omnitree is the data structure for you."); Console.WriteLine(); Console.WriteLine(" The \"OmnitreePoints\" stores individual points (vectors),"); Console.WriteLine(" and the \"OmnitreeBounds\" stores bounded objects (spaces)."); Console.WriteLine(); IOmnitreePoints <int, double, string, decimal> omnitree = new OmnitreePointsLinked <int, double, string, decimal>( // This is a location delegate. (how to locate the item along each dimension) (int index, out double a, out string b, out decimal c) => { a = index; b = index.ToString(); c = index; }); Console.Write(" Adding (0-" + (test - 1) + ")..."); for (int i = 0; i < test; i++) { omnitree.Add(i); } Console.WriteLine(); Console.Write(" Traversal: "); omnitree.Stepper(i => Console.Write(i)); Console.WriteLine(); int minimumXZ = random.Next(1, test / 2); int maximumXZ = random.Next(1, test / 2) + test / 2; string minimumY = minimumXZ.ToString(); string maximumY = maximumXZ.ToString(); Console.Write(" Spacial Traversal [" + "(" + minimumXZ + ", \"" + minimumY + "\", " + minimumXZ + ")->" + "(" + maximumXZ + ", \"" + maximumY + "\", " + maximumXZ + ")]: "); omnitree.Stepper(i => Console.Write(i), minimumXZ, maximumXZ, minimumY, maximumY, minimumXZ, maximumXZ); Console.WriteLine(); // Note: this "look up" is just a very narrow spacial query that (since we know the data) // wil only give us one result. int lookUp = random.Next(0, test); string lookUpToString = lookUp.ToString(); Console.Write(" Look Up (" + lookUp + ", \"" + lookUpToString + "\", " + lookUp + "): "); omnitree.Stepper(i => Console.Write(i), lookUp, lookUp, lookUp.ToString(), lookUp.ToString(), lookUp, lookUp); Console.WriteLine(); // Ignoring dimensions on traversals example. // If you want to ignore a column on a traversal, you can do so like this: omnitree.Stepper(i => { /*Do Nothing*/ }, lookUp, lookUp, Omnitree.Bound <string> .None, Omnitree.Bound <string> .None, Omnitree.Bound <decimal> .None, Omnitree.Bound <decimal> .None); Console.Write(" Counting Items In a Space [" + "(" + minimumXZ + ", \"" + minimumY + "\", " + minimumXZ + ")->" + "(" + maximumXZ + ", \"" + maximumY + "\", " + maximumXZ + ")]: "); omnitree.CountSubSpace( minimumXZ, maximumXZ, minimumY, maximumY, minimumXZ, maximumXZ); Console.WriteLine(); int removalMinimum = random.Next(1, test / 2); int removalMaximum = random.Next(1, test / 2) + test / 2; string removalMinimumY = removalMinimum.ToString(); string removalMaximumY = removalMaximum.ToString(); Console.Write(" Remove (" + removalMinimum + "-" + removalMaximum + "): "); omnitree.Remove( removalMinimum, removalMaximum, removalMinimumY, removalMaximumY, removalMinimum, removalMaximum); omnitree.Stepper(i => Console.Write(i)); Console.WriteLine(); Console.WriteLine(" Dimensions: " + omnitree.Dimensions); Console.WriteLine(" Count: " + omnitree.Count); omnitree.Clear(); // Clears the Omnitree Console.WriteLine(); } #endregion #region OmnitreeBounds { Console.WriteLine(" OmnitreeBounds--------------------------------------"); Console.WriteLine(); Console.WriteLine(" An Omnitree is an ND SPT that allows for"); Console.WriteLine(" multidimensional sorting. Any time you need to look"); Console.WriteLine(" items up based on multiple fields/properties, then"); Console.WriteLine(" you might want to use an Omnitree. If you need to"); Console.WriteLine(" perform ranged queries on multiple dimensions, then"); Console.WriteLine(" the Omnitree is the data structure for you."); Console.WriteLine(); Console.WriteLine(" The \"OmnitreePoints\" stores individual points (vectors),"); Console.WriteLine(" and the \"OmnitreeBounds\" stores bounded objects (spaces)."); Console.WriteLine(); IOmnitreeBounds <int, double, string, decimal> omnitree = new OmnitreeBoundsLinked <int, double, string, decimal>( // This is a location delegate. (how to locate the item along each dimension) (int index, out double min1, out double max1, out string min2, out string max2, out decimal min3, out decimal max3) => { string indexToString = index.ToString(); min1 = index; max1 = index; min2 = indexToString; max2 = indexToString; min3 = index; max3 = index; }); Console.Write(" Adding (0-" + (test - 1) + ")..."); for (int i = 0; i < test; i++) { omnitree.Add(i); } Console.WriteLine(); Console.Write(" Traversal: "); omnitree.Stepper(i => Console.Write(i)); Console.WriteLine(); int minimumXZ = random.Next(1, test / 2); int maximumXZ = random.Next(1, test / 2) + test / 2; string minimumY = minimumXZ.ToString(); string maximumY = maximumXZ.ToString(); Console.Write(" Spacial Traversal [" + "(" + minimumXZ + ", \"" + minimumY + "\", " + minimumXZ + ")->" + "(" + maximumXZ + ", \"" + maximumY + "\", " + maximumXZ + ")]: "); omnitree.StepperOverlapped(i => Console.Write(i), minimumXZ, maximumXZ, minimumY, maximumY, minimumXZ, maximumXZ); Console.WriteLine(); // Note: this "look up" is just a very narrow spacial query that (since we know the data) // wil only give us one result. int lookUpXZ = random.Next(0, test); string lookUpY = lookUpXZ.ToString(); Console.Write(" Look Up (" + lookUpXZ + ", \"" + lookUpY + "\", " + lookUpXZ + "): "); omnitree.StepperOverlapped(i => Console.Write(i), lookUpXZ, lookUpXZ, lookUpY, lookUpY, lookUpXZ, lookUpXZ); Console.WriteLine(); // Ignoring dimensions on traversals example. // If you want to ignore a dimension on a traversal, you can do so like this: omnitree.StepperOverlapped(i => { /*Do Nothing*/ }, lookUpXZ, lookUpXZ, // The "None" means there is no bound, so all values are valid Omnitree.Bound <string> .None, Omnitree.Bound <string> .None, Omnitree.Bound <decimal> .None, Omnitree.Bound <decimal> .None); Console.Write(" Counting Items In a Space [" + "(" + minimumXZ + ", \"" + minimumY + "\", " + minimumXZ + ")->" + "(" + maximumXZ + ", \"" + maximumY + "\", " + maximumXZ + ")]: " + omnitree.CountSubSpaceOverlapped( minimumXZ, maximumXZ, minimumY, maximumY, minimumXZ, maximumXZ)); Console.WriteLine(); int removalMinimumXZ = random.Next(1, test / 2); int removalMaximumXZ = random.Next(1, test / 2) + test / 2; string removalMinimumY = removalMinimumXZ.ToString(); string removalMaximumY = removalMaximumXZ.ToString(); Console.Write(" Remove (" + removalMinimumXZ + "-" + removalMaximumXZ + "): "); omnitree.RemoveOverlapped( removalMinimumXZ, removalMaximumXZ, removalMinimumY, removalMaximumY, removalMinimumXZ, removalMaximumXZ); omnitree.Stepper(i => Console.Write(i)); Console.WriteLine(); Console.WriteLine(" Dimensions: " + omnitree.Dimensions); Console.WriteLine(" Count: " + omnitree.Count); omnitree.Clear(); // Clears the Omnitree Console.WriteLine(); } #endregion #region KD Tree { Console.WriteLine(" KD Tree------------------------------------------------"); Console.WriteLine(); Console.WriteLine(" A KD Tree binary tree that stores points sorted along along an"); Console.WriteLine(" arbitrary number of dimensions. So it performs multidimensional"); Console.WriteLine(" sorting similar to the Omnitree (Quadtree/Octree) in Towel, but"); Console.WriteLine(" it uses a completely different algorithm and format."); Console.WriteLine(); Console.WriteLine(" The generic KD Tree in Towel is still in development."); Console.WriteLine(); } #endregion #region Graph { Console.WriteLine(" Graph------------------------------------------------"); Console.WriteLine(); Console.WriteLine(" A Graph is a data structure of nodes and edges. Nodes are values"); Console.WriteLine(" and edges are connections between those values. Graphs are often"); Console.WriteLine(" used to model real world data such as maps, and are often used in"); Console.WriteLine(" path finding algoritms. See the \"Algorithms\" example for path"); Console.WriteLine(" finding examples. This is just an example of how to make a graph."); Console.WriteLine(" A \"GraphSetOmnitree\" is an implementation where nodes are stored."); Console.WriteLine(" in a Set and edges are stored in an Omnitree (aka Quadtree)."); Console.WriteLine(); IGraph <int> graphSetOmnitree = new GraphSetOmnitree <int>(); Console.WriteLine(" Adding Nodes (0-" + (test - 1) + ")..."); for (int i = 0; i < test; i++) { graphSetOmnitree.Add(i); } int edgesPerNode = 3; Console.WriteLine(" Adding Random Edges (0-3 per node)..."); for (int i = 0; i < test; i++) { // lets use a heap to randomize the edges using random priorities IHeap <(int, int)> heap = new HeapArray <(int, int)>((x, y) => Compare.Wrap(x.Item2.CompareTo(y.Item2))); for (int j = 0; j < test; j++) { if (j != i) { heap.Enqueue((j, random.Next())); } } // dequeue some random edges from the heap and add them to the graph int randomEdgeCount = random.Next(edgesPerNode + 1); for (int j = 0; j < randomEdgeCount; j++) { graphSetOmnitree.Add(i, heap.Dequeue().Item1); } } Console.Write(" Nodes (Traversal): "); graphSetOmnitree.Stepper(i => Console.Write(i)); Console.WriteLine(); Console.WriteLine(" Edges (Traversal): "); graphSetOmnitree.Stepper((from, to) => Console.WriteLine(" " + from + "->" + to)); Console.WriteLine(); int a = random.Next(0, test); Console.Write(" Neighbors (" + a + "):"); graphSetOmnitree.Neighbors(a, i => Console.Write(" " + i)); Console.WriteLine(); int b = random.Next(0, test / 2); int c = random.Next(test / 2, test); Console.WriteLine(" Are Adjacent (" + b + ", " + c + "): " + graphSetOmnitree.Adjacent(b, c)); Console.WriteLine(" Node Count: " + graphSetOmnitree.NodeCount); Console.WriteLine(" Edge Count: " + graphSetOmnitree.EdgeCount); graphSetOmnitree.Clear(); // Clears the graph Console.WriteLine(); } #endregion #region Trie { Console.WriteLine(" Trie------------------------------------------------"); Console.WriteLine(); Console.WriteLine(" A Trie is a tree where portions of the data are stored in each node"); Console.WriteLine(" such that when you traverse the tree to a leaf, you have read the contents"); Console.WriteLine(" of that leaf along the way. Because of this, a Trie allows for its values"); Console.WriteLine(" to share data, which is a form of compression. So a Trie may be used to save"); Console.WriteLine(" memory. A trie may also be a very useful tool in pattern matching, because it"); Console.WriteLine(" it allows for culling based are portions of the data."); Console.WriteLine(); Console.WriteLine(" The generic Trie in Towel is still in development."); Console.WriteLine(); } #endregion Console.WriteLine("============================================"); Console.WriteLine("Examples Complete..."); Console.ReadLine(); }
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(); }