/// <summary> /// Approximates a value for a differential equation using Euler's formula /// </summary> /// <param name="formula">Differential formula to approximate</param> /// <param name="step">How far forwards to go each step</param> /// <param name="x">Initial x value</param> /// <param name="y">Initial y value</param> /// <param name="iterations">How many steps to take</param> /// <returns>A double indicating y value at final value of x</returns> public static double Eulers(Queue <Element> formula, double step, double x, double y, int iterations) { for (int i = iterations; i > 0; i--) { y += step * RPN.Compute(formula, ("x", x), ("y", y)); x += step; } return(y); }
public void ComplexReset() { RPN test = new RPN("x^2").Compute(); PostFix math = new PostFix(test); Assert.AreEqual(4, math.SetVariable("x", 2).Compute()); test.SetEquation("2x"); test.Compute(); math = new PostFix(test); Assert.AreEqual(6, math.SetVariable("x", 3).Compute()); }
public void MixedDivisionMultiplication() { RPN test = new RPN("1/2x"); test.Data.ImplicitMultiplicationPriority = true; test.Compute(); Assert.AreEqual("1 2 x * /", test.Polish.Print()); test.SetEquation("8/2(2 + 2)").Compute(); Assert.AreEqual("8 2 4 * /", test.Polish.Print()); test.Data.ImplicitMultiplicationPriority = false; test.SetEquation("1/2x").Compute(); Assert.AreEqual("x 2 /", test.Polish.Print()); test.SetEquation("8/2(2 + 2)").Compute(); Assert.AreEqual("16", test.Polish.Print()); }
/// <summary> /// Integrates a function using the mid-ordinate approximation /// </summary> /// <param name="function">Function to integrate</param> /// <param name="lowerBound">Lower bound of integration</param> /// <param name="upperBound">Upper bound of integration</param> /// <param name="numOrdinates">Number of ordinates to integrate with</param> /// <returns>A double? where null means it was an invalid operation</returns> public static double?MidOrdinate(Queue <Element> function, double lowerBound, double upperBound, double numOrdinates) { //if lower bound is higher than upper bound, then it's invalid if (lowerBound >= upperBound) { return(null); } //find the distance between ordinates double h = (upperBound - lowerBound) / numOrdinates; double sum = 0; //compute the function at midpoints between the ordinates, and add up for (double i = lowerBound; i < upperBound; i += h) { sum += RPN.Compute(function, ("x", i + h / 2)); } sum *= h; return(sum); }
/// <summary> /// Integrates a function using simpson's approximation /// </summary> /// <param name="function">Function to integrate</param> /// <param name="lowerBound">Lower bound of integration</param> /// <param name="upperBound">Upper bound of integration</param> /// <param name="numOrdinates">Number of ordinates to integrate with</param> /// <returns>A double? where null means it was an invalid operation</returns> public static double?Simpsons(Queue <Element> function, double lowerBound, double upperBound, double numOrdinates) { //if lower bound is higher than upper bound, then it's invalid if (lowerBound >= upperBound) { return(null); } //find the distance between ordinates double h = (upperBound - lowerBound) / numOrdinates; //calculate the sum of the first and last ordinate double firstSum = RPN.Compute(function, ("x", lowerBound)) + RPN.Compute(function, ("x", upperBound)); //calculate the sum of the odd ordinates double secondSum = 0; for (double i = lowerBound + h; i < upperBound; i += 2 * h) { secondSum += RPN.Compute(function, ("x", i)); } //calculate the sum of the even ordinates double thirdSum = 0; for (double i = lowerBound + 2 * h; i < upperBound; i += 2 * h) { thirdSum += RPN.Compute(function, ("x", i)); } //find total sum from the calculated values double sum = firstSum + 4 * secondSum + 2 * thirdSum; sum *= h / 3; return(sum); }
public void ComputeTests(double expected, string input, double xValue = 0) { Queue <Element> elements = new(); foreach (string token in input.Split(' ')) { if (double.TryParse(token, out double number)) { elements.Enqueue(new Value(number)); } else if (OperatorsHelper.TryParse(token, out Operators op)) { elements.Enqueue(new Operator(op)); } else { elements.Enqueue(new Variable(token)); } } double value = RPN.Compute(elements, ("x", xValue)); Assert.AreEqual(expected, value, 1e-6); }
static void Main(string[] args) { Console.Title = "AbMath v1.0.4"; Console.WindowWidth = Console.BufferWidth; Console.WriteLine("(C) 2018. Abhishek Sathiabalan"); Console.WriteLine("Recent Changes:"); Console.WriteLine("Unary negative is now implemented."); Console.WriteLine("Composite Function bug should now be fixed."); Console.WriteLine("Implicit multiplication."); Console.WriteLine("Variadic Function Support"); Console.WriteLine(); while (true) { Console.ForegroundColor = ConsoleColor.Gray; string equation = string.Empty; while (string.IsNullOrWhiteSpace(equation)) { Console.Write("Equation>"); equation = Console.ReadLine(); if (equation.Length == 0) { Console.Clear(); } } RPN = new RPN(equation); if (RPN.Data.MarkdownTables) { Console.Clear(); Console.WriteLine($"Equation>``{equation}``"); } RPN.Logger += Write; RPN.Output += Write; RPN.Compute(); PostFix postFix = new PostFix(RPN); postFix.Logger += Write; if (RPN.ContainsVariables) { Console.WriteLine("Set the variables"); for (int i = 0; i < RPN.Data.Variables.Count; i++) { Console.Write(RPN.Data.Variables[i] + "="); postFix.SetVariable(RPN.Data.Variables[i], Console.ReadLine()); } } Console.ForegroundColor = ConsoleColor.White; double answer = postFix.Compute(); if (RPN.Data.MarkdownTables) { Console.Write($"Answer: ``{answer}``"); } else { Console.Write($"Answer: {answer}"); } Console.WriteLine(); Console.WriteLine(RPN.Data.TimeRecords().ToString()); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Gray; Console.Write("Press any key to continue..."); Console.ReadKey(true); Console.Clear(); } void Write(object sender, string Event) { Console.WriteLine(Event); } }
public void RPNIntegrationTests(double expected, string input, double xValue = 0) { Queue <Element> calculated = RPN.SYA(input); Assert.AreEqual(expected, RPN.Compute(calculated, ("x", xValue)), 1e-6); }