public void Solve() { // Example taken from https://tutorial.math.lamar.edu/classes/de/BoundaryValueProblem.aspx // y'' + 4y = 0, 0 <= t <= pi/4, y(0) = -2, y(pi/4) = 10 DoubleRange t = new DoubleRange(0, Math.PI / 4, increment: 0.1); double y0 = -2; double ypi_4 = 10; //DoubleFunction3 yPP = new DoubleFunction3((t,y,yP) => (-4 * y)); // y`` = 0*y` -4*y + 0 var de = new SecondOrderDE <double>(new DoubleFunction((x) => 0), new DoubleFunction((x) => - 4), new DoubleFunction((x) => 0)); var solver = new LinearShootingBvpSolver(); var(approxy, _) = solver.SolveBoundary(de, t, y0, ypi_4); //discard approximation to y` var exact = new DoubleFunction((t) => - 2 * Math.Cos(2 * t) + 10 * Math.Sin(2 * t)); SaveCsv("LinearShootingBVP", approxy, exact, t); }
public static double NewtoneRafson(DoubleFunction f, DoubleFunction df, double p0, double eps, out List <PointF[]> lines, bool silent) { double p_prev; int stepsMaden = 0; lines = new List <PointF[]>(); do { p_prev = p0; p0 = p0 - f(p0) / df(p0); //p0 = gNR(p0); stepsMaden++; lines.Add(new PointF[] { new PointF((float)p_prev, (float)f(p_prev)), new PointF((float)p0, 0) } ); if (stepsMaden % 100 == 0) { if (silent) { return(double.NaN); } if (MessageBox.Show("Performed " + stepsMaden.ToString() + " steps, continue?", "Разошлось наверно?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) != DialogResult.OK) { return(Double.NaN); } } }while ((Math.Abs(p0 - p_prev) >= eps) && (Math.Abs(f(p0)) > eps)); return(p0); }
/// <summary> /// Assigns the result of a function to each cell; <tt>x[i] = function(x[i])</tt>. /// (Iterates downwards from <tt>[size()-1]</tt> to <tt>[0]</tt>). /// </summary> /// <param name="function"> /// A function taking as argument the current cell's value. /// </param> /// <returns> /// <tt>this</tt> (for convenience only). /// </returns> public override DoubleMatrix1D Assign(DoubleFunction function) { int s = Stride; int i = Index(0); double[] elems = Elements; if (elems == null) { throw new ApplicationException(); } for (int k = Size; --k >= 0;) { elems[i] = function(elems[i]); i += s; } return(this); }
/// <summary> /// Applies a function to each cell and aggregates the results. /// </summary> /// <param name="aggr"> /// An aggregation function taking as first argument the current aggregation and as second argument the transformed current cell value. /// </param> /// <param name="f"> /// A function transforming the current cell value. /// </param> /// <returns> /// The aggregated measure. /// </returns> public override double Aggregate(DoubleDoubleFunction aggr, DoubleFunction f) { double result = double.NaN; bool first = true; foreach (var e in Elements.Values) { if (first) { first = false; result = f(e); } else { result = aggr(result, f(e)); } } return(result); }
/// <summary> /// Applies a function to each cell and aggregates the results. /// </summary> /// <param name="aggr"> /// An aggregation function taking as first argument the current aggregation and as second argument the transformed current cell value. /// </param> /// <param name="f"> /// A function transforming the current cell value. /// </param> /// <returns> /// The aggregated measure. /// </returns> public double Aggregate(DoubleDoubleFunction aggr, DoubleFunction f) { if (Size == 0) { return(double.NaN); } double a = f(this[Rows - 1, Columns - 1]); int d = 1; // last cell already done for (int row = Rows; --row >= 0;) { for (int column = Columns - d; --column >= 0;) { a = aggr(a, f(this[row, column])); } d = 0; } return(a); }
/// <summary> /// Applies a function to each cell and aggregates the results. /// </summary> /// <param name="aggr"> /// An aggregation function taking as first argument the current aggregation and as second argument the transformed current cell value. /// </param> /// <param name="f"> /// A function transforming the current cell value. /// </param> /// <returns> /// The aggregated measure. /// </returns> public override double Aggregate(DoubleDoubleFunction aggr, DoubleFunction f) { double result = double.NaN; bool first = true; AutoParallel.AutoParallelForEach(Elements.Values, (e) => { if (first) { first = false; result = f(e); } else { result = aggr(result, f(e)); } }); return(result); }
private static double NextRoot(DoubleFunction f, double xn_2, double xn_1, double xn) { double ВтораяРазделённаяРазность = (f(xn) - 2 * f(xn_1) + f(xn_2)) / (xn - xn_1) / (xn_1 - xn_2); double W = w(f, xn_2, xn_1, xn); double res1 = xn + 2 * f(xn) / (-W - Math.Sqrt( Math.Abs(W * W - 4 * f(xn) * ВтораяРазделённаяРазность)) ); double res2 = xn + 2 * f(xn) / (-W + Math.Sqrt( Math.Abs(W * W - 4 * f(xn) * ВтораяРазделённаяРазность)) ); return(Math.Abs(res1 - xn) > Math.Abs(res2 - xn) ? res2 : res1); }
/// <summary> /// Returns ID of added grapgic /// </summary> /// <param name="f">Real function of DoubleFunction</param> /// <param name="x1">Left tabulation bound</param> /// <param name="x2">Right tabulation bound</param> /// <param name="drawMode">Weather to join nodes with lines</param> /// <returns></returns> public int AddGraphic(DoubleFunction f, float x1, float x2, DrawModes drawMode, Color penColor) { if (grs == null) { grs = new List <MathGraphic>(100); } MathGraphic newGraphic = new MathGraphic(f); newGraphic.LeftBound = x1; newGraphic.RightBound = x2; newGraphic.Step = 0.01f; newGraphic.DrawMode = drawMode; newGraphic.PenColor = penColor; newGraphic.Tabulate(); grs.Add(newGraphic); return(grs.IndexOf(newGraphic)); }
double[] Iteration(DoubleFunction f, double a, double b, double h) { List <double> roots = new List <double>(); int lastSign = Math.Sign(f(a)); for (double x = a; x <= b; x += h) { int sign = Math.Sign(f(x)); if (sign == 0) { sign = lastSign; } if (sign != lastSign) { roots.Add(x); //OnRootFound(x); } lastSign = sign; } return(roots.ToArray()); }
public void TestDelegateFunctionExpressions() { //for purposes of an example in documentation Dictionary<string, object> vars = new Dictionary<string, object>(); vars["sqrt"] = new DoubleFunction(Sqrt); double result = (double)ExpressionEvaluator.GetValue(null, "#sqrt(64)", vars); Assert.AreEqual(8, result); vars = new Dictionary<string, object>(); vars["max"] = new DoubleFunctionTwoArgs(Max); result = (double) ExpressionEvaluator.GetValue(null, "#max(5,25)", vars); Assert.AreEqual(25, result); }
protected Mult(double multiplicator) { this._multiplicator = multiplicator; Apply = new DoubleFunction((a) => { return(a * _multiplicator); }); }
public MathGraphic(DoubleFunction fn) { n = 100; f = fn; tabulated = false; }
public static PointF[][] Tabulate(DoubleFunction fx, DoubleFunction fy, float T1, float T2, float StepT) { if (StepT < float.Epsilon) { throw new ArgumentOutOfRangeException("StepT", "Step is too small"); } if (fx == null) { throw new ArgumentNullException("fx"); } if (fy == null) { throw new ArgumentNullException("fy"); } float dt = StepT; float x, y; List <PointF> pts = new List <PointF>(); List <PointF[]> grAsList = new List <PointF[]>(); int i = 0; for (float t = T1; t <= T2; t += dt, i++) { try { x = (float)fx(t); y = (float)fy(t); if ((x > int.MaxValue) || (x < int.MinValue)) { throw new ArithmeticException(); } if (float.IsInfinity(x) || float.IsNaN(x)) { throw new ArithmeticException(); } if ((y > int.MaxValue) || (y < int.MinValue)) { throw new ArithmeticException(); } if (float.IsInfinity(y) || float.IsNaN(y)) { throw new ArithmeticException(); } } catch (ArithmeticException) { if (pts.Count > 0) { grAsList.Add(pts.ToArray()); pts.Clear(); } continue; } pts.Add(new PointF(x, y)); } if (pts.Count > 0) { grAsList.Add(pts.ToArray()); } return(grAsList.ToArray()); }
/// <summary> /// Constructs the function <i>f( g(a), h(b) )</i>. /// </summary> /// <param name="f">a binary function.</param> /// <param name="g">a binary function.</param> /// <param name="h">a binary function.</param> /// <returns>the binary function <i>f( g(a), h(b) )</i>.</returns> public static DoubleDoubleFunction Chain(DoubleDoubleFunction f, DoubleFunction g, DoubleFunction h) { return(new DoubleDoubleFunction((a, b) => { return f(g(a), h(b)); })); }
/// <summary> /// Constructs the function <i>f( g(a), h(b) )</i>. /// </summary> /// <param name="g">a binary function.</param> /// <param name="h">a binary function.</param> /// <returns>the binary function <i>f( g(a), h(b) )</i>.</returns> public static DoubleFunction Chain(DoubleFunction g, DoubleFunction h) { return(new DoubleFunction((a) => { return g(h(a)); })); }
/// <summary> /// Constructs the function <tt>g( h(a) )</tt>. /// </summary> /// <param name="g"> /// The unary function g. /// </param> /// <param name="h"> /// The unary function h. /// </param> /// <returns> /// The unary function <tt>g( h(a) )</tt>. /// </returns> public static DoubleFunction Chain(DoubleFunction g, DoubleFunction h) { return(a => g(h(a))); }