static void Main(string[] args) { Console.WriteLine("You are runnning the Mathematics example."); Console.WriteLine("=========================================="); Console.WriteLine(); #region Fraction // Fractions need some work... commenting out for now //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 Basic Operations Console.WriteLine(" Basics----------------------------------------------"); Console.WriteLine(); // Variables Fraction32 clamp_a = (Fraction32)(-123d / 9d); Fraction32 clamp_b = (Fraction32)(7d / 12d); Fraction32 clamp_c = (Fraction32)(14d / 15d); double[] summation_values = new double[] { random.NextDouble(), random.NextDouble(), random.NextDouble(), random.NextDouble(), }; // Examples Console.WriteLine(" Negate(7): " + Negate(7)); Console.WriteLine(" Add(7, 7): " + Add(7m, 7m)); 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])); } // multiple parameter add overload example (most math functions have this overload) Console.WriteLine(") = " + string.Format("{0:0.00}", Add(summation_values.ToStepper()))); Console.WriteLine(" Subtract(14, 7): " + Subtract(14f, 7f)); Console.WriteLine(" Multiply(7, 7): " + Multiply((long)7, (long)7)); Console.WriteLine(" Divide(14, 7): " + Divide((short)14, (short)7)); Console.WriteLine(" AbsoluteValue(-7): " + AbsoluteValue((double)-7)); Console.WriteLine(" Clamp(" + clamp_a + ", " + clamp_b + ", " + clamp_c + "): " + Clamp(clamp_a, clamp_b, clamp_c)); Console.WriteLine(" Maximum(1, 2, 3): " + Maximum(1, 2, 3)); Console.WriteLine(" Minimum(1, 2, 3): " + Minimum(1, 2, 3)); Console.WriteLine(" LessThan(1, 2): " + LessThan((Fraction128)1, (Fraction128)2)); Console.WriteLine(" GreaterThan(1, 2): " + GreaterThan((Fraction64)1, (Fraction64)2)); Console.WriteLine(" Compare(7, 7): " + Compare((Fraction32)7, (Fraction32)7)); Console.WriteLine(" Equate(7, 6): " + Equal(7, 6)); Console.WriteLine(" EqualsLeniency(2, 1, 1): " + EqualLeniency(2, 1, 1)); Console.WriteLine(); #endregion #region More Numeric Mathematics Console.WriteLine(" More Numeric Mathematics----------------------------"); Console.WriteLine(); // Variables int prime_check = random.Next(1, 100000); int[] gcf = new int[] { random.Next(1, 500) * 2, random.Next(1, 500) * 2, random.Next(1, 500) * 2 }; int[] lcm = new int[] { random.Next(1, 500) * 2, random.Next(1, 500) * 2, random.Next(1, 500) * 2 }; int prime_factors = random.Next(1, 100000); int check = random.Next(1, 1000); // Examples Console.WriteLine(" IsPrime(" + prime_check + "): " + IsPrime(prime_check)); Console.WriteLine(" IsNegative(" + check + "): " + IsNegative(check)); Console.WriteLine(" IsNonNegative(" + check + "): " + IsNonNegative(check)); Console.WriteLine(" IsPositive(" + check + "): " + IsPositive(check)); Console.WriteLine(" IsOdd(" + check + "): " + IsOdd(check)); Console.WriteLine(" IsEven(" + check + "): " + IsEven(check)); Console.WriteLine(" GCF(" + gcf[0] + ", " + gcf[1] + ", " + gcf[2] + "): " + GreatestCommonFactor(gcf.ToStepper())); Console.WriteLine(" LCM(" + lcm[0] + ", " + lcm[1] + ", " + lcm[2] + "): " + LeastCommonMultiple(lcm.ToStepper())); Console.Write(" Prime Factors(" + prime_factors + "): "); FactorPrimes(prime_factors)(i => Console.Write(i + " ")); Console.WriteLine(); Console.WriteLine(" 7!: " + Factorial(7)); Console.WriteLine(" 7! / (3! * 4!): " + Combinations(7, new int[] { 3, 4 })); Console.WriteLine(" 7 choose 2: " + BinomialCoefficient(7, 2)); Console.WriteLine(); #endregion #region Trigonometry Console.WriteLine(" Trigonometry -----------------------------------------"); Console.WriteLine(); double randomDouble = random.NextDouble(); Angle <double> randomAngle = new Angle <double>(randomDouble, Angle.Units.Radians); Console.WriteLine(" SinTaylorSeries(" + randomAngle + ") = " + SineTaylorSeries(randomAngle)); Console.WriteLine(" CosinTaylorSeries(" + randomAngle + ") = " + CosineTaylorSeries(randomAngle)); Console.WriteLine(); #endregion #region Statistics Console.WriteLine(" Statistics-----------------------------------------"); Console.WriteLine(); // Variables/Data double mode_temp = random.NextDouble() * 100; double[] statistics_data = new double[random.Next(5, 7)]; for (int i = 0; i < statistics_data.Length; i++) { if (i == 1 || i == statistics_data.Length - 1) { statistics_data[i] = mode_temp; } else { statistics_data[i] = random.NextDouble() * 100; } } string stat_data_string = " data: [" + string.Format("{0:0.00}", statistics_data[0]); for (int i = 1; i < statistics_data.Length; i++) { stat_data_string += ", " + string.Format("{0:0.00}", statistics_data[i]); } stat_data_string += "]"; Console.WriteLine(stat_data_string); Console.WriteLine(); // Examples Console.WriteLine(" Mean(data): " + string.Format("{0:0.00}", Mean(statistics_data.ToStepper()))); Console.WriteLine(" Median(data): " + string.Format("{0:0.00}", Median(statistics_data.ToStepper()))); // I need to fix the "Mode" function //Console.WriteLine(" Mode(data): "); //Heap<Link<double, int>> modes = 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(); Console.WriteLine(" Geometric Mean(data): " + string.Format("{0:0.00}", GeometricMean(statistics_data.ToStepper()))); Range(out double min, out double max, statistics_data.ToStepper()); Console.WriteLine(" Range(data): " + string.Format("{0:0.00}", min) + "-" + string.Format("{0:0.00}", max)); Console.WriteLine(" Variance(data): " + string.Format("{0:0.00}", Variance(statistics_data.ToStepper()))); Console.WriteLine(" Standard Deviation(data): " + string.Format("{0:0.00}", StandardDeviation(statistics_data.ToStepper()))); Console.WriteLine(" Mean Deviation(data): " + string.Format("{0:0.00}", MeanDeviation(statistics_data.ToStepper()))); double[] quatiles = Quantiles(4, statistics_data.ToStepper()); Console.Write(" Quartiles(data):"); foreach (double i in quatiles) { Console.Write(string.Format(" {0:0.00}", i)); } Console.WriteLine(); Console.WriteLine(); #endregion #region Linear Algebra Console.WriteLine(" Linear Algebra------------------------------------"); Console.WriteLine(); // Vector Construction Vector <double> V = new Vector <double>(4, i => 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 Vector <double>(3, i => 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 = new Matrix <double>(4, 4, (row, column) => 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); // 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(" ref(M): "); ConsoleWrite(Matrix <double> .Echelon(M)); // 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 = null, u = null; //Matrix<double>.DecomposeLowerUpper(M, ref l, ref u); // this method is probably bugged... working on it //Console.WriteLine(" Lower-Upper Decomposition:"); //Console.WriteLine(); //Console.WriteLine(" lower(M):"); //ConsoleWrite(l); //Console.WriteLine(" upper(M):"); //ConsoleWrite(u); // Matrix Inverse Matrix <double> inverse = M.Inverse(); Console.WriteLine(" Inverse(M):"); ConsoleWrite(inverse); // 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) => 2d * (x / 7d); var symbolicExpression1 = Symbolics.Parse(expression1); Console.WriteLine(" Expression 1: " + symbolicExpression1); Console.WriteLine(" Simplified: " + symbolicExpression1.Simplify()); Console.WriteLine(" Plugin(5): " + symbolicExpression1.Substitute("x", 5d).Simplify()); Expression <Func <double, double> > expression2 = (x) => 2d * x / 7d; var symbolicExpression2 = Symbolics.Parse(expression2); Console.WriteLine(" Expression 2: " + symbolicExpression2); Console.WriteLine(" Simplified: " + symbolicExpression2.Simplify()); Console.WriteLine(" Plugin(5): " + symbolicExpression2.Substitute("x", 5d).Simplify()); Expression <Func <double, double> > expression3 = (x) => 2d - x + 7d; var symbolicExpression3 = Symbolics.Parse(expression3); Console.WriteLine(" Expression 3: " + symbolicExpression3); Console.WriteLine(" Simplified: " + symbolicExpression3.Simplify()); Console.WriteLine(" Plugin(5): " + symbolicExpression3.Substitute("x", 5d).Simplify()); Expression <Func <double, double> > expression4 = (x) => 2d + (x - 7d); var symbolicExpression4 = Symbolics.Parse(expression4); Console.WriteLine(" Expression 4: " + symbolicExpression4); Console.WriteLine(" Simplified: " + symbolicExpression4.Simplify()); Console.WriteLine(" Plugin(5): " + symbolicExpression4.Substitute("x", 5d).Simplify()); //Expression<Func<double, double, double, double>> expression5 = (x, y, z) => Power(x, 3d) + 2d * x * y * Power(z, 2d) - y * z + 1d; //var symbolicExpression5 = Symbolics.Parse(expression5); //Console.WriteLine(" Expression 5: " + symbolicExpression5); //Console.WriteLine(" Simplified: " + symbolicExpression5.Simplify()); //Console.WriteLine(" Plugin(x = 5): " + symbolicExpression5.Substitute("x", 5d).Simplify()); var symbolicExpression6 = Symbolics.Parse <double>("2 * (7 / [x])", double.TryParse); Console.WriteLine(" Expression 6: " + symbolicExpression6); Console.WriteLine(" Simplified: " + symbolicExpression6.Simplify()); Symbolics.Expression symbolicExpression6Simplified = symbolicExpression6.Substitute("x", 9d).Simplify(); Console.WriteLine(" Plugin(x = 9): " + symbolicExpression6Simplified); var symbolicExpression7 = Symbolics.Parse <double>("10 + 8 * (7 / [x]) + 7 ^ 2", double.TryParse); Console.WriteLine(" Expression 7: " + symbolicExpression7); Console.WriteLine(" Simplified: " + symbolicExpression7.Simplify()); Console.WriteLine(" Plugin(x = 11): " + symbolicExpression7.Substitute("x", 11d).Simplify()); Console.WriteLine(); #endregion Console.WriteLine(); Console.WriteLine("================================================="); Console.WriteLine("Example Complete..."); Console.ReadLine(); }
static void Main() { Console.WriteLine("You are runnning the Mathematics example."); Console.WriteLine("=========================================="); Console.WriteLine(); #region Basic Operations Console.WriteLine(" Basics----------------------------------------------"); Console.WriteLine(); // Variables Fraction <short> clampA = new Fraction <short>(-123, 9); // -123 / 9 Fraction <short> clampB = new Fraction <short>(7, 12); // 7 / 12 Fraction <short> clampC = new Fraction <short>(14, 15); // 14 / 15 double[] values = new double[4]; Stepper <double> valueStepper = values.ToStepper(); values.Format(x => random.NextDouble()); // Examples double negation = Negation(7); Console.WriteLine(" Negate(7): " + negation); decimal addition = Addition(7m, 7m); Console.WriteLine(" Add(7, 7): " + addition); double summation = Σ(valueStepper); Console.WriteLine(" Σ (" + string.Join(", ", values.Select(x => Format(x))) + ") = " + summation); float subtraction = Subtraction(14f, 7f); Console.WriteLine(" Subtract(14, 7): " + subtraction); long multiplication = Multiplication(7L, 7L); Console.WriteLine(" Multiply(7, 7): " + multiplication); double product = Π(valueStepper); Console.WriteLine(" Π (" + string.Join(", ", values.Select(x => Format(x))) + ") = " + product); short division = Division((short)14, (short)7); Console.WriteLine(" Divide(14, 7): " + division); double absoluteValue = AbsoluteValue(-7d); Console.WriteLine(" AbsoluteValue(-7): " + absoluteValue); Fraction <short> clamp = Clamp(clampA, clampB, clampC); Console.WriteLine(" Clamp(" + clampA + ", " + clampB + ", " + clampC + "): " + clamp); int maximum = Maximum(1, 2, 3); Console.WriteLine(" Maximum(1, 2, 3): " + maximum); int minimum = Minimum(1, 2, 3); Console.WriteLine(" Minimum(1, 2, 3): " + minimum); bool lessThan = LessThan((Fraction <int>) 1, (Fraction <int>) 2); Console.WriteLine(" LessThan(1, 2): " + lessThan); bool greaterThan = GreaterThan((Fraction <int>) 1, (Fraction <int>) 2); Console.WriteLine(" GreaterThan(1, 2): " + greaterThan); CompareResult compare = Comparison((Fraction <short>) 7, (Fraction <short>) 7); Console.WriteLine(" Compare(7, 7): " + compare); bool equality = Equality(7, 6); Console.WriteLine(" Equate(7, 6): " + equality); bool equalsLeniency = EqualityLeniency(2, 1, 1); Console.WriteLine(" EqualsLeniency(2, 1, 1): " + equalsLeniency); Console.WriteLine(); #endregion #region More Numeric Mathematics Console.WriteLine(" More Numeric Mathematics----------------------------"); Console.WriteLine(); // some random ints for the examples int random1 = random.Next(1, 100000); int random2 = random.Next(1, 1000); int random3 = random.Next(1, 1000); int random4 = random.Next(1, 1000); int random5 = random.Next(1, 1000); int random6 = random.Next(1, 1000); int random7 = random.Next(6, 10); int random8 = random.Next(1, 100000); int[] randomInts1 = new int[3]; randomInts1.Format(x => random.Next(1, 500) * 2); Stepper <int> randomInts1Stepper = randomInts1.ToStepper(); int[] randomInts2 = new int[3]; randomInts2.Format(x => random.Next(1, 500) * 2); Stepper <int> randomInts2Stepper = randomInts2.ToStepper(); bool isPrime = IsPrime(random1); Console.WriteLine(" IsPrime(" + random1 + "): " + isPrime); bool isNegative = IsNegative(random2); Console.WriteLine(" IsNegative(" + random2 + "): " + isNegative); bool isNonNegative = IsNonNegative(random3); Console.WriteLine(" IsNonNegative(" + random3 + "): " + isNonNegative); bool isPositive = IsPositive(random4); Console.WriteLine(" IsPositive(" + random4 + "): " + isPositive); bool isOdd = IsOdd(random5); Console.WriteLine(" IsOdd(" + random5 + "): " + isOdd); bool isEven = IsEven(random6); Console.WriteLine(" IsEven(" + random6 + "): " + isEven); int greatestCommonFactor = GreatestCommonFactor(randomInts1Stepper); Console.WriteLine(" GCF(" + string.Join(", ", randomInts1) + "): " + greatestCommonFactor); int leastCommonMultiple = LeastCommonMultiple(randomInts2Stepper); Console.WriteLine(" LCM(" + string.Join(", ", randomInts2) + "): " + leastCommonMultiple); int factorial = Factorial(random7); Console.WriteLine(" " + random7 + "!: " + factorial); int combinations = Combinations(7, new int[] { 3, 4 }); Console.WriteLine(" 7! / (3! * 4!): " + combinations); int binomialCoefficient = BinomialCoefficient(7, 2); Console.WriteLine(" 7 choose 2: " + binomialCoefficient); Console.Write(" Prime Factors(" + random8 + "): "); FactorPrimes(random8, prime => Console.Write(prime + " ")); Console.WriteLine(); Console.WriteLine(); #endregion #region Trigonometry Console.WriteLine(" Trigonometry -----------------------------------------"); Console.WriteLine(); double randomDouble = random.NextDouble(); Angle <double> randomAngle = new Angle <double>(randomDouble, Angle.Units.Radians); double sineTaylorSeries = SineTaylorSeries(randomAngle); Console.WriteLine(" SinTaylorSeries(" + randomAngle + ") = " + Format(sineTaylorSeries)); double cosineTaylorSeries = CosineTaylorSeries(randomAngle); Console.WriteLine(" CosinTaylorSeries(" + randomAngle + ") = " + Format(cosineTaylorSeries)); Console.WriteLine(); #endregion #region Statistics Console.WriteLine(" Statistics-----------------------------------------"); Console.WriteLine(); // Data Generation double mode_temp = random.NextDouble() * 100; double[] dataArray = new double[random.Next(5, 7)]; dataArray.Format(x => random.NextDouble() * 100); // Lets copy a value in the array to ensure there is at least one // duplicate (so the "Mode" example will has something to show) dataArray[dataArray.Length - 1] = dataArray[0]; // Print Data Console.WriteLine(" data: [" + string.Join(", ", dataArray.Select(x => Format(x))) + "]"); Console.WriteLine(); Stepper <double> data = dataArray.ToStepper(); // Examples double mean = Mean(data); Console.WriteLine(" Mean(data): " + Format(mean)); double median = Median(data); Console.WriteLine(" Median(data): " + Format(median)); //// Here is an example if you just want the values in a list // //IList<double> modes = new ListArray<double>(); //Mode(data, x => modes.Add(x)); //Console.WriteLine(" Mode(data): " + // (modes.Count > 0 // ? string.Join(",", modes.Select(x => Format(x))) // : "None")); bool noModes = true; Console.Write(" Mode(data): "); Mode(data, Step.Gaps <double>( x => { Console.Write(Format(x)); noModes = false; }, x => Console.Write(", "))); if (noModes) { Console.Write("None"); } Console.WriteLine(); double geometricMean = GeometricMean(data); Console.WriteLine(" Geometric Mean(data): " + Format(geometricMean)); Range(out double min, out double max, data); Console.WriteLine(" Range(data): " + Format(min) + "-" + Format(max)); double variance = Variance(dataArray.ToStepper()); Console.WriteLine(" Variance(data): " + Format(variance)); double standardDeviation = StandardDeviation(data); Console.WriteLine(" Standard Deviation(data): " + Format(standardDeviation)); double meanDeviation = MeanDeviation(data); Console.WriteLine(" Mean Deviation(data): " + Format(meanDeviation)); double[] quatiles = Quantiles(4, data); Console.Write(" Quartiles(data):"); foreach (double i in quatiles) { Console.Write(Format(i)); } Console.WriteLine(); Console.WriteLine(); #endregion #region Linear Algebra Console.WriteLine(" Linear Algebra------------------------------------"); Console.WriteLine(); // Vector Construction Vector <double> V = new Vector <double>(4, i => 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: " + V.DotProduct(V)); Console.WriteLine(); // Vector Cross Product Vector <double> V3 = new Vector <double>(3, i => 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 = new Matrix <double>(4, 4, (row, column) => 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); // 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(" ref(M): "); ConsoleWrite(Matrix <double> .Echelon(M)); // 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 = null, u = null; //Matrix<double>.DecomposeLowerUpper(M, ref l, ref u); // this method is probably bugged... working on it //Console.WriteLine(" Lower-Upper Decomposition:"); //Console.WriteLine(); //Console.WriteLine(" lower(M):"); //ConsoleWrite(l); //Console.WriteLine(" upper(M):"); //ConsoleWrite(u); // Matrix Inverse Matrix <double> inverse = M.Inverse(); Console.WriteLine(" Inverse(M):"); ConsoleWrite(inverse); // 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) => 2d * (x / 7d); var symbolicExpression1 = Symbolics.Parse(expression1); Console.WriteLine(" Expression 1: " + symbolicExpression1); Console.WriteLine(" Simplified: " + symbolicExpression1.Simplify()); Console.WriteLine(" Plugin(5): " + symbolicExpression1.Substitute("x", 5d).Simplify()); Expression <Func <double, double> > expression2 = (x) => 2d * x / 7d; var symbolicExpression2 = Symbolics.Parse(expression2); Console.WriteLine(" Expression 2: " + symbolicExpression2); Console.WriteLine(" Simplified: " + symbolicExpression2.Simplify()); Console.WriteLine(" Plugin(5): " + symbolicExpression2.Substitute("x", 5d).Simplify()); Expression <Func <double, double> > expression3 = (x) => 2d - x + 7d; var symbolicExpression3 = Symbolics.Parse(expression3); Console.WriteLine(" Expression 3: " + symbolicExpression3); Console.WriteLine(" Simplified: " + symbolicExpression3.Simplify()); Console.WriteLine(" Plugin(5): " + symbolicExpression3.Substitute("x", 5d).Simplify()); Expression <Func <double, double> > expression4 = (x) => 2d + (x - 7d); var symbolicExpression4 = Symbolics.Parse(expression4); Console.WriteLine(" Expression 4: " + symbolicExpression4); Console.WriteLine(" Simplified: " + symbolicExpression4.Simplify()); Console.WriteLine(" Plugin(5): " + symbolicExpression4.Substitute("x", 5d).Simplify()); var symbolicExpression6 = Symbolics.Parse <double>("2 * (7 / [x])"); Console.WriteLine(" Expression 6: " + symbolicExpression6); Console.WriteLine(" Simplified: " + symbolicExpression6.Simplify()); Symbolics.Expression symbolicExpression6Simplified = symbolicExpression6.Substitute("x", 9d).Simplify(); Console.WriteLine(" Plugin(x = 9): " + symbolicExpression6Simplified); var symbolicExpression7 = Symbolics.Parse <double>("10 + 8 * (7 / [x]) + 7 ^ 2"); Console.WriteLine(" Expression 7: " + symbolicExpression7); Console.WriteLine(" Simplified: " + symbolicExpression7.Simplify()); Console.WriteLine(" Plugin(x = 11): " + symbolicExpression7.Substitute("x", 11d).Simplify()); Console.WriteLine(); #endregion Console.WriteLine(); Console.WriteLine("================================================="); Console.WriteLine("Example Complete..."); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("You are runnning the Mathematics example."); Console.WriteLine("=========================================="); Console.WriteLine(); Console.WriteLine(" The Towel.Mathemeatics.Compute class allows generic mathematics (you can use any type). :)"); Console.WriteLine(); #region Basic Operations Console.WriteLine(" Basics----------------------------------------------"); Console.WriteLine(); // Negation Console.WriteLine(" Negate(7): " + Negate(7)); // Addition Console.WriteLine(" Add(7, 7): " + Add(7m, 7m)); // Subtraction Console.WriteLine(" Subtract(14, 7): " + Subtract(14f, 7f)); // Multiplication Console.WriteLine(" Multiply(7, 7): " + Multiply((long)7, (long)7)); // Division Console.WriteLine(" Divide(14, 7): " + Divide((short)14, (short)7)); // Absolute Value Console.WriteLine(" AbsoluteValue(-7): " + AbsoluteValue((double)-7)); // Clamp Fraction32 clamp_a = (Fraction32)(-123d / 9d); Fraction32 clamp_b = (Fraction32)(7d / 12d); Fraction32 clamp_c = (Fraction32)(14d / 15d); Console.WriteLine(" Clamp(" + clamp_a + ", " + clamp_b + ", " + clamp_c + "): " + Clamp(clamp_a, clamp_b, clamp_c)); // Maximum Console.WriteLine(" Maximum(1, 2, 3): " + Maximum(1, 2, 3)); // Minimum Console.WriteLine(" Minimum(1, 2, 3): " + Minimum(1, 2, 3)); // Less Than Console.WriteLine(" LessThan(1, 2): " + LessThan((Fraction128)1, (Fraction128)2)); // Greater Than Console.WriteLine(" GreaterThan(1, 2): " + GreaterThan((Fraction64)1, (Fraction64)2)); // Compare Console.WriteLine(" Compare(7, 7): " + Compare((Fraction32)7, (Fraction32)7)); // Equate Console.WriteLine(" Equate(7, 6): " + Equal(7, 6)); // EqualsLeniency Console.WriteLine(" EqualsLeniency(2, 1, 1): " + EqualLeniency(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 + "): " + 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] + "): " + 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] + "): " + LeastCommonMultiple(lcm.Stepper())); Console.WriteLine(); #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) = " + 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.Next(5, 7)]; for (int i = 0; i < statistics_data.Length; i++) { if (i == 1 || i == statistics_data.Length - 1) { statistics_data[i] = mode_temp; } else { statistics_data[i] = random.NextDouble() * 100; } } // Print the data to the console... string stat_data_string = " data: [" + string.Format("{0:0.00}", statistics_data[0]); for (int i = 1; i < statistics_data.Length; i++) { stat_data_string += ", " + string.Format("{0:0.00}", statistics_data[i]); } stat_data_string += "]"; Console.WriteLine(stat_data_string); Console.WriteLine(); // Mean Console.WriteLine(" Mean(data): " + string.Format("{0:0.00}", Mean(statistics_data.Stepper()))); // Median //Console.WriteLine(" Median(data): " + string.Format("{0:0.00}", Median(statistics_data.Stepper()))); // Mode //Console.WriteLine(" Mode(data): "); //Heap<Link<double, int>> modes = 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}", GeometricMean(statistics_data.Stepper()))); // Range double min; double max; Range(out min, out max, statistics_data.Stepper()); Console.WriteLine(" Range(data): " + string.Format("{0:0.00}", min) + "-" + string.Format("{0:0.00}", max)); // Variance Console.WriteLine(" Variance(data): " + string.Format("{0:0.00}", Variance(statistics_data.Stepper()))); // Standard Deviation Console.WriteLine(" Standard Deviation(data): " + string.Format("{0:0.00}", StandardDeviation(statistics_data.Stepper()))); // Mean Deviation Console.WriteLine(" Mean Deviation(data): " + string.Format("{0:0.00}", 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 + "): "); 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}", 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 = 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!: " + Factorial(7)); Console.WriteLine(); // Combinations Console.WriteLine(" 7! / (3! * 4!): " + Combinations(7, new int[] { 3, 4 })); Console.WriteLine(); // Choose Console.WriteLine(" 7 choose 2: " + 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)); double[,] doubleData = 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() }, }; // Matrix Construction Matrix <double> M = new Matrix <double>(4, 4, (row, column) => doubleData[row, column]); 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 symbolicExpression1 = Symbolics.Parse(expression1); Console.WriteLine(" Expression 1: " + symbolicExpression1); Console.WriteLine(" Simplified: " + symbolicExpression1.Simplify()); Console.WriteLine(" Plugin(5): " + symbolicExpression1.Substitute("x", 5).Simplify()); Expression <Func <double, double> > expression2 = (x) => 2 * x / 7; var symbolicExpression2 = Symbolics.Parse(expression2); Console.WriteLine(" Expression 2: " + symbolicExpression2); Console.WriteLine(" Simplified: " + symbolicExpression2.Simplify()); Console.WriteLine(" Plugin(5): " + symbolicExpression2.Substitute("x", 5).Simplify()); Expression <Func <double, double> > expression3 = (x) => 2 - x + 7; var symbolicExpression3 = Symbolics.Parse(expression3); Console.WriteLine(" Expression 3: " + symbolicExpression3); Console.WriteLine(" Simplified: " + symbolicExpression3.Simplify()); Console.WriteLine(" Plugin(5): " + symbolicExpression3.Substitute("x", 5).Simplify()); Expression <Func <double, double> > expression4 = (x) => 2 + (x - 7); var symbolicExpression4 = Symbolics.Parse(expression4); Console.WriteLine(" Expression 4: " + symbolicExpression4); Console.WriteLine(" Simplified: " + symbolicExpression4.Simplify()); Console.WriteLine(" Plugin(5): " + symbolicExpression4.Substitute("x", 5).Simplify()); Expression <Func <double, double, double, double> > expression5 = (x, y, z) => Power(x, 3d) + 2d * x * y * Power(z, 2d) - y * z + 1d; var symbolicExpression5 = Symbolics.Parse(expression5); Console.WriteLine(" Expression 5: " + symbolicExpression5); Console.WriteLine(" Simplified: " + symbolicExpression5.Simplify()); Console.WriteLine(" Plugin(x = 5): " + symbolicExpression5.Substitute("x", 5).Simplify()); var symbolicExpression6 = Symbolics.Parse <double>("2 * (7 / [x])", double.TryParse); Console.WriteLine(" Expression 6: " + symbolicExpression6); Console.WriteLine(" Simplified: " + symbolicExpression6.Simplify()); Console.WriteLine(" Plugin(x = 5): " + symbolicExpression6.Substitute("x", 9).Simplify()); var symbolicExpression7 = Symbolics.Parse <double>("10 + 8 * (7 / [x]) + 7 ^ 2", double.TryParse); Console.WriteLine(" Expression 7: " + symbolicExpression7); Console.WriteLine(" Simplified: " + symbolicExpression7.Simplify()); Console.WriteLine(" Plugin(x = 5): " + symbolicExpression7.Substitute("x", 11).Simplify()); #endregion Console.WriteLine(); Console.WriteLine("================================================="); Console.WriteLine("Example Complete..."); Console.ReadLine(); }