public static PiecewiseFunction Calculate(PiecewiseFunction f, PiecewiseFunction g) { var resultPiecewiseFunction = new PiecewiseFunction(); var fSplitted = GetSplittedPiecewiseFunction(f); var gSplitted = GetSplittedPiecewiseFunction(g); var breaks = GetResultBreakPoints(fSplitted, gSplitted); if (double.IsInfinity(breaks[0])) { var appropriateSegments = FindSegments(fSplitted, gSplitted, breaks[1] - 1); var convRunner = new ConvolutionRunner(appropriateSegments); Func <double, double> probabilityFunction = (x) => convRunner.GetConvolutionValueAtPointQuotient(x); var minusInfSegment = new MinusInfinitySegment(breaks[1], probabilityFunction); resultPiecewiseFunction.AddSegment(minusInfSegment); breaks.RemoveAt(0); } if (double.IsInfinity(breaks[breaks.Count - 1])) { var appropriateSegments = FindSegments(fSplitted, gSplitted, breaks[breaks.Count - 2] + 1); var convRunner = new ConvolutionRunner(appropriateSegments); Func <double, double> probabilityFunction = (x) => convRunner.GetConvolutionValueAtPointQuotient(x); var plusInfSegment = new PlusInfinitySegment(breaks[breaks.Count - 2], probabilityFunction); resultPiecewiseFunction.AddSegment(plusInfSegment); breaks.RemoveAt(breaks.Count - 1); } for (var i = 0; i < breaks.Count - 1; i++) { var segments = FindSegments(fSplitted, gSplitted, (breaks[i] + breaks[i + 1]) / 2); var runner = new ConvolutionRunner(segments); Func <double, double> func = (x) => runner.GetConvolutionValueAtPointQuotient(x); //Segment newSegment = null; var newSegment = new Segment(breaks[i], breaks[i + 1], func); //if (breaks[i] == 0) //{ // if (poleAtZero) // { // newSegment = new SegmentWithPole(breaks[i], breaks[i + 1], func, true); // //var segmentWithPole = new SegmentWithPole(breaks[i], breaks[i + 1], func, true); // //newSegment = segmentWithPole.ToInterpolatedSegment(); // } // else // { // newSegment = new Segment(breaks[i], breaks[i + 1], func); // } //} //else if (breaks[i + 1] == 0) //{ // if (poleAtZero) // { // newSegment = new SegmentWithPole(breaks[i], breaks[i + 1], func, false); // } // else // { // newSegment = new Segment(breaks[i], breaks[i + 1], func); // } //} //else //{ // newSegment = new Segment(breaks[i], breaks[i + 1], func); //} //var newSegment = new Segment(breaks[i], breaks[i + 1], func); resultPiecewiseFunction.AddSegment(newSegment); } return(resultPiecewiseFunction); }
public static PiecewiseFunction Calculate(PiecewiseFunction f, PiecewiseFunction g) { var breaks = GetResultBreaks(f, g); var resultPiecewiseFunction = new PiecewiseFunction(); if (breaks.Count > 1 && double.IsNegativeInfinity(breaks[0].X)) { //var appropriateSegments = FindSegments(f, g, breaks[1].X - 1); var appropriateSegments = double.IsPositiveInfinity(breaks[1].X) ? FindSegments(f, g, Math.Pow(10, 20)) : FindSegments(f, g, breaks[1].X - 1); var convRunner = new ConvolutionRunner(appropriateSegments); Func <double, double> probabilityFunction = (x) => convRunner.GetConvolutionValueAtPoint(x); if (double.IsPositiveInfinity(breaks[1].X)) { var fromInfToInfSegment = new FromInfinityToInfinitySegment(probabilityFunction); resultPiecewiseFunction.AddSegment(fromInfToInfSegment); } else { var minusInfSegment = new MinusInfinitySegment(breaks[1].X, probabilityFunction); resultPiecewiseFunction.AddSegment(minusInfSegment); } breaks.RemoveAt(0); } if (breaks.Count > 1 && double.IsPositiveInfinity(breaks[breaks.Count - 1].X)) { var appropriateSegments = FindSegments(f, g, breaks[breaks.Count - 2].X + 1); var convRunner = new ConvolutionRunner(appropriateSegments); Func <double, double> probabilityFunction = (x) => convRunner.GetConvolutionValueAtPoint(x); var plusInfSegment = new PlusInfinitySegment(breaks[breaks.Count - 2].X, probabilityFunction); resultPiecewiseFunction.AddSegment(plusInfSegment); breaks.RemoveAt(breaks.Count - 1); } for (var i = 0; i < breaks.Count - 1; i++) { #region Comment //if (i == 0 && double.IsNegativeInfinity(breaks[0].X)) //{ // var appropriateSegments = FindSegments(f, g, breaks[1].X - 1); // var convRunner = new ConvolutionRunner(appropriateSegments); // Func<double, double> probabilityFunction = (x) => convRunner.GetConvolutionValueAtPoint(x); // var minusInfSegment = new MinusInfinitySegment(breaks[1].X, probabilityFunction); // resultPiecewiseFunction.AddSegment(minusInfSegment); // continue; //} //if (i == breaksCount - 1 && double.IsPositiveInfinity(breaks[breaksCount - 1].X)) //{ // var appropriateSegments = FindSegments(f, g, breaks[breaksCount - 2].X + 1); // var convRunner = new ConvolutionRunner(appropriateSegments); // Func<double, double> probabilityFunction = (x) => convRunner.GetConvolutionValueAtPoint(x); // var plusInfSegment = new PlusInfinitySegment(breaks[breaksCount - 2].X, probabilityFunction); // resultPiecewiseFunction.AddSegment(plusInfSegment); // continue; //} #endregion var segments = FindSegments(f, g, (breaks[i].X + breaks[i + 1].X) / 2); var runner = new ConvolutionRunner(segments); Func <double, double> func = (x) => runner.GetConvolutionValueAtPoint(x); var newSegment = new Segment(breaks[i].X, breaks[i + 1].X, func); resultPiecewiseFunction.AddSegment(newSegment); } return(resultPiecewiseFunction); }
public static PiecewiseFunction Calculate(PiecewiseFunction f, PiecewiseFunction g) { //var breaks = GetResultBreaks(f, g); var fSplitted = f.SplitByPoints(new List <double> { -1, 0, 1 }); var gSplitted = g.SplitByPoints(new List <double> { -1, 0, 1 }); var f_0 = fSplitted[0]; var g_0 = gSplitted[0]; var poleAtZero = f_0 > 0 && g_0 > 0; var breaks = GetResultBreakPoints(fSplitted, gSplitted); Func <double, double, double> operation = (a, b) => a * b; var resultPiecewiseFunction = new PiecewiseFunction(); //if (breaks.Count > 1 && double.IsNegativeInfinity(breaks[0])) if (double.IsInfinity(breaks[0])) { var appropriateSegments = FindSegments(fSplitted, gSplitted, breaks[1] - 1); var convRunner = new ConvolutionRunner(appropriateSegments); Func <double, double> probabilityFunction = (x) => convRunner.GetConvolutionValueAtPointProduct(x); var minusInfSegment = new MinusInfinitySegment(breaks[1], probabilityFunction); resultPiecewiseFunction.AddSegment(minusInfSegment); breaks.RemoveAt(0); } //if (breaks.Count > 1 && double.IsPositiveInfinity(breaks[breaks.Count - 1])) if (double.IsInfinity(breaks[breaks.Count - 1])) { var appropriateSegments = FindSegments(fSplitted, gSplitted, breaks[breaks.Count - 2] + 1); var convRunner = new ConvolutionRunner(appropriateSegments); Func <double, double> probabilityFunction = (x) => convRunner.GetConvolutionValueAtPointProduct(x); var plusInfSegment = new PlusInfinitySegment(breaks[breaks.Count - 2], probabilityFunction); resultPiecewiseFunction.AddSegment(plusInfSegment); breaks.RemoveAt(breaks.Count - 1); } for (var i = 0; i < breaks.Count - 1; i++) { var segments = FindSegments(fSplitted, gSplitted, (breaks[i] + breaks[i + 1]) / 2); var runner = new ConvolutionRunner(segments); Func <double, double> func = (x) => runner.GetConvolutionValueAtPointProduct(x); Segment newSegment = null; if (breaks[i] == 0) { if (poleAtZero) { newSegment = new SegmentWithPole(breaks[i], breaks[i + 1], func, true); //var segmentWithPole = new SegmentWithPole(breaks[i], breaks[i + 1], func, true); //newSegment = segmentWithPole.ToInterpolatedSegment(); } else { newSegment = new Segment(breaks[i], breaks[i + 1], func); } } else if (breaks[i + 1] == 0) { if (poleAtZero) { newSegment = new SegmentWithPole(breaks[i], breaks[i + 1], func, false); } else { newSegment = new Segment(breaks[i], breaks[i + 1], func); } } else { newSegment = new Segment(breaks[i], breaks[i + 1], func); } //var newSegment = new Segment(breaks[i], breaks[i + 1], func); resultPiecewiseFunction.AddSegment(newSegment); } return(resultPiecewiseFunction); }