Anda di halaman 1dari 4

27/03/2019 Towel - CodeProject

Towel
Zachary Patten, 27 Mar 2019

Throw in the towel.

Download latest Repository Archive


Download local copy

"It's a tough galaxy. If you want to survive, you've gotta know... where your towel is." - Ford Prefect

Towel is a C# .NET Standard libary intended to add much needed functionality that is missing in C# as well as redesign some aspects
to bring the language up to modern standards.

Status
Azure Pipelines succeeded
Build

Many features are coded and working, but Towel is still under heavy development. It should be ready for its first release later this year.

Mathematics
Towel has fast generic mathematics functions.

Fundamental Operations

T Add<T>(T a, T b);
T Negate<T>(T a);
T Subtract<T>(T a, T b);
T Multiply<T>(T a, T b);
T Divide<T>(T a, T b);
T Modulo<T>(T a, T b);

More Numeric Mathematics

void FactorPrimes<T>(T a, Step<T> step);


T Factorial<T>(T a);
T LinearInterpolation<T>(T x, T x0, T x1, T y0, T y1);
T LeastCommonMultiple<T>(T a, T b, params T[] c);
T GreatestCommonFactor<T>(T a, T b, params T[] c);

Statistics
https://www.codeproject.com/Articles/1308693/Towel?display=Print 1/4
27/03/2019 Towel - CodeProject

T Mean<T>(T a, params T[] b);


T Median<T>(params T[] values);
Heap<Link<T, int>> Mode<T>(T a, params T[] b);
void Range<T>(out T minimum, out T maximum, Stepper<T> stepper);
T[] Quantiles<T>(int quantiles, Stepper<T> stepper);
T GeometricMean<T>(Stepper<T> stepper);
T Variance<T>(Stepper<T> stepper);
T StandardDeviation<T>(Stepper<T> stepper);
T MeanDeviation<T>(Stepper<T> stepper);

Vectors

Vector<T> V1 = new Vector<T>(params T[] vector);


Vector<T> V2 = new Vector<T>(params T[] vector);
Vector<T> V3;
T scalar;

V3 = -V1; // Negate
V3 = V1 + V2; // Add
V3 = V1 - V2; // Subtract
V3 = V1 * scalar; // Multiply
V3 = V1 / scalar; // Divide
scalar = V1.DotProduct(V2); // Dot Product
V3 = V1.CrossProduct(V2); // Cross Product
V1.Magnitude; // Magnitude
V3 = V1.Normalize(); // Normalize
bool equal = V1 == V2; // Equal

Matrices

Matrix<T> M1 = new Matrix<T>(int rows, int columns);


Matrix<T> M2 = new Matrix<T>(int rows, int columns);
Matrix<T> M3;
Vector<T> V2 = new Vector<T>(params T[] vector);
Vector<T> V3;
T scalar;

M3 = -M1; // Negate
M1 + M2; // Add
M3 = M1 - M2; // Subtract
M3 = M1 * M2; // Multiply
V3 = M1 * V2; // Multiply (vector)
M3 = M1 * scalar; // Multiply (scalar)
M3 = M1 / scalar; // Divide
M3 = M1 ^ 3; // Power
scalar = M1.Determinent(); // Determinent
M3 = M1.Minor(int row, int column); // Minor
M3 = M1.Echelon(); // Echelon Form (REF)
M3 = M1.ReducedEchelon(); // Reduced Echelon Form (RREF)
M3 = M1.Inverse(); // Inverse
M1.DecomposeLowerUpper(ref M2, ref M3); // Lower Upper Decomposition
bool equal = M1 == M2; // Equal

Symbolic Mathematics

// From Linq Expression


Expression<Func<double, double>> exp1 = (x) => 2 * (x / 7);
Symbolics.Expression symExp1 = Symbolics.Parse(exp1);
Symbolics.Expression simplified = symExp1.Simplify(); // Simplification
symExp1.Substitute("x", 5); // Variable Subsitition

// From String
Symbolics.Expression symExp2 = Symbolics.Parse("2 * (x / 7)");
Symbolics.Expression simplified = symExp2.Simplify(); // Simplification
symExp2.Substitute("x", 5); // Variable Subsitition

https://www.codeproject.com/Articles/1308693/Towel?display=Print 2/4
27/03/2019 Towel - CodeProject

Measurement Mathematics
Towel has measurement types to provide type-safe measurement mathematics with automatic unit conversion. Never make a unit
conversion mistake again, and add a layer of mathematical validation to your code.

// Automatic Unit Conversion Example:


Angle<double> angle1 = new Angle<double>(90d, Angle.Units.Degrees);
Angle<double> angle2 = new Angle<double>(.5d, Angle.Units.Turns);
Angle<double> result1 = angle1 + angle2; // 270°
// When you perform mathematical operations on measurements, any necessary unit conversions will
// be automatically performed by the relative measurement type (in this case "Angle<T>").

// Type Safeness Example:


Length<double> length1 = new Length<double>(2d, Length.Units.Yards);
object result2 = angle1 + length1; // WILL NOT COMPILE!!!
// The type safe-ness of the measurement types prevents the miss-use of the measurements. You cannot
// add "Length<T>" to "Angle<T>" because that is mathematically invalid (no operator exists).

Data Structures
Towel has many useful data structures. Especially the Omnitree, which is an SPT that works on any number of dimensions. It can
be used to make a quadtree, octree, or SPTs with higher dimensions.

Heap<T> heap;
AvlTree<T> avlTree;
RedBlackTree<T> redBlackTree;
Omnitree<T, A1, A2, ... AX> omnitree; // (Quadtree, Octree, ...)
Tree<T> tree;
Graph<T> graph;

Algorithms
// Sorting
Sort<T>.Shuffle(...);
Sort<T>.Bubble(...);
Sort<T>.Selection(...);
Sort<T>.Insertion(...);
Sort<T>.Quick(...);
Sort<T>.Merge(...);
Sort<T>.Heap(...);
Sort<T>.OddEven(...);

// Path Finding
Search<T>.Graph<Math>.Astar(...);
Search<T>.Graph<Math>.Greedy(...);

Extensions
// System.Random extensions to generate more random types
string NextString(this Random random, int length);
char NextChar(this Random random);
decimal NextDecimal(this Random random);
DateTime DateTime(this Random random);
TimeSpan TimeSpan(this Random random);
long NextLong(this Random random);

// Type conversion to string as appears in C# source code


string ConvertToCsharpSource(this Type type);

https://www.codeproject.com/Articles/1308693/Towel?display=Print 3/4
27/03/2019 Towel - CodeProject
// decimal conversion to English words
string ToWords(this decimal @decimal);

Developer(s)
Zachary Patten

Howdy! I'm Zachary Patten, and Towel has been a 5+ year long project of mine to make the most advanced, easy-to-use coding
library around. Data structures, mathematics, mearsurements, algorithms, extensions... Towel has something for every project out
there. Currently, I only work on Towel in my free time, but feel free to contact me if you have questions and I will respond when I am
able. :)

License
This article, along with any associated source code and files, is licensed under The MIT License

About the Author


Zachary Patten
United States

Name: Zachary Patten


College: Kansas State University
Hobbies: Game Programming, Algorithm Analysis, and Framework Development

Comments and Discussions


0 messages have been posted for this article Visit https://www.codeproject.com/Articles/1308693/Towel to post and view
comments on this article, or click here to get a print view with messages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile Article Copyright 2019 by Zachary Patten
Web01 | 2.8.190306.1 | Last Updated 27 Mar 2019 Everything else Copyright © CodeProject, 1999-2019

https://www.codeproject.com/Articles/1308693/Towel?display=Print 4/4