internal T Value <T>() { Symbolics.Expression expression = Symbolics.Parse <T>(Expression); if (expression.Simplify() is not Symbolics.Constant <T> constant) { throw new TowelBugException($"encountered a measurement conversion expression that could not be simplified to a constant: {expression}"); } return(constant.Value); }
internal T Value <T>() { try { Symbolics.Expression expression = Symbolics.Parse <T>(Expression); Symbolics.Constant <T> constant = expression.Simplify() as Symbolics.Constant <T>; return(constant.Value); } catch (Exception exception) { throw new Exception("There is a BUG in " + nameof(Towel) + ". A " + nameof(ConversionFactorAttribute) + " expression could not simplify to a constant.", exception); } }
internal static Func <T, T>[][] BuildConversionTable <T>() { T A = Symbolics.ParseAndSimplifyToConstant <T>("273.15"); T B = Symbolics.ParseAndSimplifyToConstant <T>("9 / 5"); T C = Symbolics.ParseAndSimplifyToConstant <T>("459.67"); T D = Symbolics.ParseAndSimplifyToConstant <T>("32"); Func <T, T>[][] table = ConstructSquareJaggedArray <Func <T, T> >(3); table[(int)Units.Kelvin][(int)Units.Kelvin] = x => x; table[(int)Units.Kelvin][(int)Units.Celsius] = x => Subtraction(x, A); table[(int)Units.Kelvin][(int)Units.Fahrenheit] = x => Subtraction(Multiplication(x, B), C); table[(int)Units.Celsius][(int)Units.Celsius] = x => x; table[(int)Units.Celsius][(int)Units.Kelvin] = x => Addition(x, A); table[(int)Units.Celsius][(int)Units.Fahrenheit] = x => Addition(Multiplication(x, B), D); table[(int)Units.Fahrenheit][(int)Units.Fahrenheit] = x => x; table[(int)Units.Fahrenheit][(int)Units.Celsius] = x => Division(Subtraction(x, D), B); table[(int)Units.Fahrenheit][(int)Units.Kelvin] = x => Division(Addition(x, C), B); return(table); }
public void Parse_String_Testing() { Symbolics.Constant <int> ONE = new Symbolics.Constant <int>(1); #region Basic Negate Tests { var A = Symbolics.Parse <int>("-1", int.TryParse); var B = -ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("- 1", int.TryParse); var B = -ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } #endregion #region Basic Add Tests { var A = Symbolics.Parse <int>("1+1", int.TryParse); var B = ONE + ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 + 1", int.TryParse); var B = ONE + ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 + (1 + 1)", int.TryParse); var B = ONE + (ONE + ONE); Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 + (1 + (1 + 1))", int.TryParse); var B = ONE + (ONE + (ONE + ONE)); Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } #endregion #region Basic Subtract Tests { var A = Symbolics.Parse <int>("1-1", int.TryParse); var B = ONE - ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 - 1", int.TryParse); var B = ONE - ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 - (1 - 1)", int.TryParse); var B = ONE - (ONE - ONE); Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 - (1 - (1 - 1))", int.TryParse); var B = ONE - (ONE - (ONE - ONE)); Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } #endregion #region Basic Multiply Tests { var A = Symbolics.Parse <int>("1*1", int.TryParse); var B = ONE * ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 * 1", int.TryParse); var B = ONE * ONE; Assert.IsTrue(A.Equals(B)); } { var A = Symbolics.Parse <int>("1 * (1 * 1)", int.TryParse); var B = ONE * (ONE * ONE); Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 * (1 * (1 * 1))", int.TryParse); var B = ONE * (ONE * (ONE * ONE)); Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } #endregion #region Basic Divide Tests { var A = Symbolics.Parse <int>("1/1", int.TryParse); var B = ONE / ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 / 1", int.TryParse); var B = ONE / ONE; Assert.IsTrue(A.Equals(B)); } { var A = Symbolics.Parse <int>("1 / (1 / 1)", int.TryParse); var B = ONE / (ONE / ONE); Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 / (1 / (1 / 1))", int.TryParse); var B = ONE / (ONE / (ONE / ONE)); Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } #endregion #region Basic Factorial Tests { var A = Symbolics.Parse <int>("1!", int.TryParse); var B = new Symbolics.Factorial(ONE); Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 !", int.TryParse); var B = new Symbolics.Factorial(ONE); Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } #endregion #region Order Of Operations (2 operands) { var A = Symbolics.Parse <int>("1 + -1", int.TryParse); var B = ONE + -ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("-1 + 1", int.TryParse); var B = -ONE + ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 + 1 - 1", int.TryParse); var B = ONE + ONE - ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 - 1 + 1", int.TryParse); var B = ONE - ONE + ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 + 1 * 1", int.TryParse); var B = ONE + ONE * ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 * 1 + 1", int.TryParse); var B = ONE * ONE + ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 - 1 * 1", int.TryParse); var B = ONE - ONE * ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 * 1 - 1", int.TryParse); var B = ONE * ONE - ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 * 1 / 1", int.TryParse); var B = ONE * ONE / ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 / 1 * 1", int.TryParse); var B = ONE / ONE * ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 + 1!", int.TryParse); var B = ONE + new Symbolics.Factorial(ONE); Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1! + 1", int.TryParse); var B = new Symbolics.Factorial(ONE) + ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 - 1!", int.TryParse); var B = ONE - new Symbolics.Factorial(ONE); Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1! - 1", int.TryParse); var B = new Symbolics.Factorial(ONE) - ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 * 1!", int.TryParse); var B = ONE * new Symbolics.Factorial(ONE); Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1! * 1", int.TryParse); var B = new Symbolics.Factorial(ONE) * ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1 / 1!", int.TryParse); var B = ONE / new Symbolics.Factorial(ONE); Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } { var A = Symbolics.Parse <int>("1! / 1", int.TryParse); var B = new Symbolics.Factorial(ONE) / ONE; Assert.IsTrue(A.Equals(B)); Assert.IsTrue(A.ToString().Equals(B.ToString())); } #endregion //Assert.Inconclusive("Test Method Not Fully Implemented"); }
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(); }
/// <summary>Parses a measurement from a string.</summary> /// <typeparam name="T">The numeric type to parse the quantity as.</typeparam> /// <param name="string">The string to parse.</param> /// <param name="measurement">The parsed measurement if successful or default if unsuccessful.</param> /// <param name="tryParseNumeric">Explicit try parse function for the numeric type.</param> /// <returns>True if successful or false if not.</returns> public static bool TryParse <T>(string @string, out object measurement, Symbolics.TryParseNumeric <T> tryParseNumeric = null) { if (!ParsingLibraryBuilt) { BuildParsingLibrary(); } if (!TypeSpecificParsingLibrary <T> .TypeSpecificParsingLibraryBuilt) { TypeSpecificParsingLibrary <T> .BuildTypeSpecificParsingLibrary(); } List <object> parameters = new List <object>(); bool AtLeastOneUnit = false; bool? numerator = null; MatchCollection matchCollection = Regex.Matches(@string, AllUnitsRegexPattern); if (matchCollection.Count <= 0 || matchCollection[0].Index <= 0) { measurement = default; return(false); } string numericString = @string.Substring(0, matchCollection[0].Index); T value; try { value = Symbolics.ParseAndSimplifyToConstant <T>(numericString, tryParseNumeric); } catch { measurement = default; return(false); } StringBuilder stringBuilder = new StringBuilder(); foreach (Match match in matchCollection) { string matchValue = match.Value; if (matchValue.Equals("*") || matchValue.Equals("/")) { if (!(numerator is null)) { measurement = default; return(false); } if (!AtLeastOneUnit) { measurement = default; return(false); } numerator = matchValue.Equals("*"); continue; } if (!UnitStringToEnumMap.TryGetValue(match.Value, out Enum @enum)) { measurement = default; return(false); } if (!AtLeastOneUnit) { if (!(numerator is null)) { measurement = default; return(false); } AtLeastOneUnit = true; stringBuilder.Append(@enum.GetType().DeclaringType.Name); parameters.Add(@enum); } else { if (numerator is null) { measurement = default; return(false); } if (numerator.Value) { stringBuilder.Append("*" + @enum.GetType().DeclaringType.Name); parameters.Add(@enum); } else { stringBuilder.Append("/" + @enum.GetType().DeclaringType.Name); parameters.Add(@enum); } } numerator = null; } if (!(numerator is null)) { measurement = default; return(false); } string key = stringBuilder.ToString(); Func <T, object[], object> factory = TypeSpecificParsingLibrary <T> .UnitsStringsToFactoryFunctions[key]; measurement = factory(value, parameters.ToArray()); return(true); }
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(); }
internal T Value <T>() { Symbolics.Expression expression = Symbolics.Parse <T>(Expression); Symbolics.Constant <T> constant = expression.Simplify() as Symbolics.Constant <T>; return(constant.Value); }
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(); }