From ed66bdf620fb04eaa5ac203476fb3b33c7551029 Mon Sep 17 00:00:00 2001 From: "MI\\dtarjeft" Date: Mon, 17 Oct 2016 14:18:30 -0400 Subject: [PATCH 1/4] Fixed broken tests and C#6'ified --- Rock.Math.UnitTests/DoubleExtensionTests.cs | 154 +++------ Rock.Math.UnitTests/PiecewiseFunctionTests.cs | 201 ++++-------- Rock.Math/DoubleExtensions.cs | 4 +- Rock.Math/LinearFunction.cs | 55 +--- Rock.Math/PiecewiseFunction.cs | 301 ++++++------------ 5 files changed, 209 insertions(+), 506 deletions(-) diff --git a/Rock.Math.UnitTests/DoubleExtensionTests.cs b/Rock.Math.UnitTests/DoubleExtensionTests.cs index 3c59434..7ec4046 100644 --- a/Rock.Math.UnitTests/DoubleExtensionTests.cs +++ b/Rock.Math.UnitTests/DoubleExtensionTests.cs @@ -1,4 +1,8 @@ -namespace DoubleExtensionTests +using System.Collections; +using System.Collections.Generic; +using NUnit.Core; + +namespace DoubleExtensionTests { using Rock.Math; @@ -7,119 +11,54 @@ [TestFixture] public class ApproximatelyEquals { - [Test] - [TestCase(double.NegativeInfinity)] - [TestCase(double.MinValue)] - [TestCase(-1000000000000.0)] - [TestCase(-100.0)] - [TestCase(-10.0)] - [TestCase(-1.0)] - [TestCase(-0.01)] - [TestCase(-0.0000000000001)] - [TestCase(0.0)] - [TestCase(0.0000000000001)] - [TestCase(0.01)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(100.0)] - [TestCase(1000000000000.0)] - [TestCase(double.MaxValue)] - [TestCase(double.PositiveInfinity)] - public void ReturnsTrueForSameNumber(double number) - { - Assert.IsTrue(number.ApproximatelyEquals(number)); - } + public IEnumerable Doubles + => + new[] + { + double.NegativeInfinity, + double.MinValue, + -1000000000000.0, + -100.0, + -10.0, + -1.0, + -0.01, + -0.0000000000001, + 0.0, + 0.0000000000001, + 0.01, + 1.0, + 10.0, + 100.0, + 1000000000000.0, + double.MaxValue, + double.PositiveInfinity + }; - [Test] - [TestCase(double.NegativeInfinity)] - [TestCase(double.MinValue)] - [TestCase(-1000000000000.0)] - [TestCase(-100.0)] - [TestCase(-10.0)] - [TestCase(-1.0)] - [TestCase(-0.01)] - [TestCase(-0.0000000000001)] - [TestCase(0.0)] - [TestCase(0.0000000000001)] - [TestCase(0.01)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(100.0)] - [TestCase(1000000000000.0)] - [TestCase(double.MaxValue)] - [TestCase(double.PositiveInfinity)] + [TestCaseSource(nameof(Doubles))] + public void ReturnsTrueForSameNumber(double number) => Assert.IsTrue(number.ApproximatelyEquals(number)); + + [TestCaseSource(nameof(Doubles))] public void ReturnsTrueForInfinitesimallyLargerNumber(double number) { var otherNumber = number + double.Epsilon; Assert.IsTrue(number.ApproximatelyEquals(otherNumber)); } - [Test] - [TestCase(double.NegativeInfinity)] - [TestCase(double.MinValue)] - [TestCase(-1000000000000.0)] - [TestCase(-100.0)] - [TestCase(-10.0)] - [TestCase(-1.0)] - [TestCase(-0.01)] - [TestCase(-0.0000000000001)] - [TestCase(0.0)] - [TestCase(0.0000000000001)] - [TestCase(0.01)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(100.0)] - [TestCase(1000000000000.0)] - [TestCase(double.MaxValue)] - [TestCase(double.PositiveInfinity)] + [TestCaseSource(nameof(Doubles))] public void ReturnsTrueForInfinitesimallySmallerNumber(double number) { var otherNumber = number - double.Epsilon; Assert.IsTrue(number.ApproximatelyEquals(otherNumber)); } - [Test] - [TestCase(double.NegativeInfinity)] - [TestCase(double.MinValue)] - [TestCase(-1000000000000.0)] - [TestCase(-100.0)] - [TestCase(-10.0)] - [TestCase(-1.0)] - [TestCase(-0.01)] - [TestCase(-0.0000000000001)] - [TestCase(0.0)] - [TestCase(0.0000000000001)] - [TestCase(0.01)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(100.0)] - [TestCase(1000000000000.0)] - [TestCase(double.MaxValue)] - [TestCase(double.PositiveInfinity)] + [TestCaseSource(nameof(Doubles))] public void ReturnsTrueForSlightlyLargerNumber(double number) { var otherNumber = number + 0.000000000000001; Assert.IsTrue(number.ApproximatelyEquals(otherNumber)); } - [Test] - [TestCase(double.NegativeInfinity)] - [TestCase(double.MinValue)] - [TestCase(-1000000000000.0)] - [TestCase(-100.0)] - [TestCase(-10.0)] - [TestCase(-1.0)] - [TestCase(-0.01)] - [TestCase(-0.0000000000001)] - [TestCase(0.0)] - [TestCase(0.0000000000001)] - [TestCase(0.01)] - [TestCase(1.0)] - [TestCase(10.0)] - [TestCase(100.0)] - [TestCase(1000000000000.0)] - [TestCase(double.MaxValue)] - [TestCase(double.PositiveInfinity)] + [TestCaseSource(nameof(Doubles))] public void ReturnsTrueForSlightlySmallerNumber(double number) { var otherNumber = number - 0.000000000000001; @@ -143,10 +82,7 @@ public void ReturnsTrueForSlightlySmallerNumber(double number) [TestCase(99999, 100000.00)] [TestCase(50000.0, 100000.0)] [TestCase(double.MaxValue, double.PositiveInfinity)] - public void ReturnsFalseForLargerNumber(double number, double largerNumber) - { - Assert.IsFalse(number.ApproximatelyEquals(largerNumber)); - } + public void ReturnsFalseForLargerNumber(double number, double largerNumber) => Assert.IsFalse(number.ApproximatelyEquals(largerNumber)); [Test] [TestCase(double.MinValue, double.NegativeInfinity)] @@ -165,10 +101,7 @@ public void ReturnsFalseForLargerNumber(double number, double largerNumber) [TestCase(100000.00, 99999)] [TestCase(100000.0, 50000.0)] [TestCase(double.PositiveInfinity, double.MaxValue)] - public void ReturnsFalseForSmallerNumber(double number, double smallerNumber) - { - Assert.IsFalse(number.ApproximatelyEquals(smallerNumber)); - } + public void ReturnsFalseForSmallerNumber(double number, double smallerNumber) => Assert.IsFalse(number.ApproximatelyEquals(smallerNumber)); } [TestFixture] @@ -185,19 +118,13 @@ public void ReturnsTrueForZero() [TestCase(0.000000000001)] [TestCase(0.0000000000001)] [TestCase(0.00000000000001)] - public void ReturnsTrueForVerySmallPositiveNumbers(double number) - { - Assert.IsTrue(number.IsApproximatelyZero()); - } + public void ReturnsTrueForVerySmallPositiveNumbers(double number) => Assert.IsTrue(number.IsApproximatelyZero()); [Test] [TestCase(-0.000000000001)] [TestCase(-0.0000000000001)] [TestCase(-0.00000000000001)] - public void ReturnsTrueForVerySmallNegativeNumbers(double number) - { - Assert.IsTrue(number.IsApproximatelyZero()); - } + public void ReturnsTrueForVerySmallNegativeNumbers(double number) => Assert.IsTrue(number.IsApproximatelyZero()); [Test] [TestCase(double.NegativeInfinity)] @@ -218,9 +145,6 @@ public void ReturnsTrueForVerySmallNegativeNumbers(double number) [TestCase(1000000000000.0)] [TestCase(double.MaxValue)] [TestCase(double.PositiveInfinity)] - public void ReturnsFalseForNumbersThatAreNotVerySmall(double number) - { - Assert.IsFalse(number.IsApproximatelyZero()); - } + public void ReturnsFalseForNumbersThatAreNotVerySmall(double number) => Assert.IsFalse(number.IsApproximatelyZero()); } } diff --git a/Rock.Math.UnitTests/PiecewiseFunctionTests.cs b/Rock.Math.UnitTests/PiecewiseFunctionTests.cs index aefe5de..c18656f 100644 --- a/Rock.Math.UnitTests/PiecewiseFunctionTests.cs +++ b/Rock.Math.UnitTests/PiecewiseFunctionTests.cs @@ -199,7 +199,7 @@ public void VerifyUpperBound(PiecewiseFunction function, double upperBound, bool { var found = false; - foreach (var piece in function.Pieces) + foreach (var piece in function) { if (upperBound.ApproximatelyEquals(piece.UpperBound)) { @@ -221,26 +221,26 @@ public void VerifyUpperBound(PiecewiseFunction function, double upperBound, bool public void VerifyMaxValuePiece(PiecewiseFunction function, double expectedValue, double lowerBound, bool includeLowerBound) { - Assert.Greater(function.Pieces.Count, 1); - Assert.AreEqual(lowerBound, function.Pieces[function.Pieces.Count - 2].UpperBound); - Assert.AreNotEqual(includeLowerBound, function.Pieces[function.Pieces.Count - 2].IncludeUpperBound); + Assert.Greater(function.Count, 1); + Assert.AreEqual(lowerBound, function[function.Count - 2].UpperBound); + Assert.AreNotEqual(includeLowerBound, function[function.Count - 2].IncludeUpperBound); this.VerifyMaxValuePiece(function, expectedValue); } public void VerifyMaxValuePiece(PiecewiseFunction function, double expectedValue) { - Assert.AreEqual(double.MaxValue, function.Pieces.Last().UpperBound); - Assert.AreEqual(true, function.Pieces.Last().IncludeUpperBound); - Assert.AreEqual(expectedValue, function.Pieces.Last().Value); + Assert.AreEqual(double.MaxValue, function.Last().UpperBound); + Assert.AreEqual(true, function.Last().IncludeUpperBound); + Assert.AreEqual(expectedValue, function.Last().Value); } public void VerifyConstantFunction(PiecewiseFunction function, double expectedConstantValue) { - Assert.AreEqual(1, function.Pieces.Count); - Assert.AreEqual(double.MaxValue, function.Pieces[0].UpperBound); - Assert.AreEqual(true, function.Pieces[0].IncludeUpperBound); - Assert.AreEqual(expectedConstantValue, function.Pieces[0].Value); + Assert.AreEqual(1, function.Count); + Assert.AreEqual(double.MaxValue, function[0].UpperBound); + Assert.AreEqual(true, function[0].IncludeUpperBound); + Assert.AreEqual(expectedConstantValue, function[0].Value); } } @@ -249,35 +249,32 @@ public class IsEquivalent : PiecewiseFunctionTest { public object[] Functions { get; private set; } - public IEnumerable GetFunctions() - { - return new List - { - this.Zero, - this.StepUpIncludeRight, - this.StepUpIncludeLeft, - this.StepDownIncludeRight, - this.StepDownIncludeLeft, - this.Floor, - this.Ceiling, - this.Round, - this.RoundMidpointUp, - this.RoundMidpointToEven, - this.LessThanFive, - this.LessThanOrEqualToFive, - this.GreaterThanFive, - this.GreaterThanOrEqualToFive, - this.LessThanTwo, - this.LessThanOrEqualToTwo, - this.GreaterThanTwo, - this.GreaterThanOrEqualToTwo, - this.TwoIfLessThanTwo, - this.TwoIfLessThanOrEqualToTwo, - this.TwoIfGreaterThanTwo, - this.TwoIfGreaterThanOrEqualToTwo, - this.Even - }; - } + public IEnumerable GetFunctions() => new List + { + this.Zero, + this.StepUpIncludeRight, + this.StepUpIncludeLeft, + this.StepDownIncludeRight, + this.StepDownIncludeLeft, + this.Floor, + this.Ceiling, + this.Round, + this.RoundMidpointUp, + this.RoundMidpointToEven, + this.LessThanFive, + this.LessThanOrEqualToFive, + this.GreaterThanFive, + this.GreaterThanOrEqualToFive, + this.LessThanTwo, + this.LessThanOrEqualToTwo, + this.GreaterThanTwo, + this.GreaterThanOrEqualToTwo, + this.TwoIfLessThanTwo, + this.TwoIfLessThanOrEqualToTwo, + this.TwoIfGreaterThanTwo, + this.TwoIfGreaterThanOrEqualToTwo, + this.Even + }; [Test] public void ReturnsTrueForEmptyFunctionAndZeroValuedFunction() @@ -288,7 +285,7 @@ public void ReturnsTrueForEmptyFunctionAndZeroValuedFunction() Assert.IsTrue(emptyFunction.IsEquivalent(this.Zero)); } - [Test, TestCaseSource("GetFunctions")] + [Test, TestCaseSource(nameof(GetFunctions))] public void ReturnsTrueForCopiedFunction(PiecewiseFunction function) { var copiedFunction = new PiecewiseFunction(function); @@ -571,7 +568,7 @@ public void HandlesEmptyInput() { var result = PiecewiseFunction.AddMany(new PiecewiseFunction[0]); - Assert.AreEqual(0, result.Pieces.Count); + Assert.AreEqual(0, result.Count); } [Test] @@ -993,145 +990,73 @@ public void ExtendsBothFunctionsToDoubleMaxValue() [TestFixture] public class GreaterThanPiecewiseFunctionOperator : InequalityTest { - public override PiecewiseFunction RunOperation(PiecewiseFunction first, PiecewiseFunction second) - { - return first > second; - } + public override PiecewiseFunction RunOperation(PiecewiseFunction first, PiecewiseFunction second) => first > second; - public override bool GreaterThan - { - get { return true; } - } + public override bool GreaterThan => true; - public override bool LessThan - { - get { return false; } - } + public override bool LessThan => false; - public override bool EqualTo - { - get { return false; } - } + public override bool EqualTo => false; } [TestFixture] public class GreaterThanOrEqualsPiecewiseFunctionOperator : InequalityTest { - public override PiecewiseFunction RunOperation(PiecewiseFunction first, PiecewiseFunction second) - { - return first >= second; - } + public override PiecewiseFunction RunOperation(PiecewiseFunction first, PiecewiseFunction second) => first >= second; - public override bool GreaterThan - { - get { return true; } - } + public override bool GreaterThan => true; - public override bool LessThan - { - get { return false; } - } + public override bool LessThan => false; - public override bool EqualTo - { - get { return true; } - } + public override bool EqualTo => true; } [TestFixture] public class LessThanPiecewiseFunctionOperator : InequalityTest { - public override PiecewiseFunction RunOperation(PiecewiseFunction first, PiecewiseFunction second) - { - return first < second; - } + public override PiecewiseFunction RunOperation(PiecewiseFunction first, PiecewiseFunction second) => first < second; - public override bool GreaterThan - { - get { return false; } - } + public override bool GreaterThan => false; - public override bool LessThan - { - get { return true; } - } + public override bool LessThan => true; - public override bool EqualTo - { - get { return false; } - } + public override bool EqualTo => false; } [TestFixture] public class LessThanOrEqualsPiecewiseFunctionOperator : InequalityTest { - public override PiecewiseFunction RunOperation(PiecewiseFunction first, PiecewiseFunction second) - { - return first <= second; - } + public override PiecewiseFunction RunOperation(PiecewiseFunction first, PiecewiseFunction second) => first <= second; - public override bool GreaterThan - { - get { return false; } - } + public override bool GreaterThan => false; - public override bool LessThan - { - get { return true; } - } + public override bool LessThan => true; - public override bool EqualTo - { - get { return true; } - } + public override bool EqualTo => true; } [TestFixture] public class EqualsPiecewiseFunctionOperator : InequalityTest { - public override PiecewiseFunction RunOperation(PiecewiseFunction first, PiecewiseFunction second) - { - return PiecewiseFunction.EqualTo(first, second); - } + public override PiecewiseFunction RunOperation(PiecewiseFunction first, PiecewiseFunction second) => PiecewiseFunction.EqualTo(first, second); - public override bool GreaterThan - { - get { return false; } - } + public override bool GreaterThan => false; - public override bool LessThan - { - get { return false; } - } + public override bool LessThan => false; - public override bool EqualTo - { - get { return true; } - } + public override bool EqualTo => true; } [TestFixture] public class NotEqualsPiecewiseFunctionOperator : InequalityTest { - public override PiecewiseFunction RunOperation(PiecewiseFunction first, PiecewiseFunction second) - { - return PiecewiseFunction.NotEqualTo(first, second); - } + public override PiecewiseFunction RunOperation(PiecewiseFunction first, PiecewiseFunction second) => PiecewiseFunction.NotEqualTo(first, second); - public override bool GreaterThan - { - get { return true; } - } + public override bool GreaterThan => true; - public override bool LessThan - { - get { return true; } - } + public override bool LessThan => true; - public override bool EqualTo - { - get { return false; } - } + public override bool EqualTo => false; } [TestFixture] diff --git a/Rock.Math/DoubleExtensions.cs b/Rock.Math/DoubleExtensions.cs index 4cfd32d..9ced3ea 100644 --- a/Rock.Math/DoubleExtensions.cs +++ b/Rock.Math/DoubleExtensions.cs @@ -51,8 +51,8 @@ public static bool IsApproximatelyZero(this double a, double approximatelyZero = if (approximatelyZero < 0.0) { throw new ArgumentException( - string.Format("Optional parameter must be greater than or equal to zero (was {0}).", approximatelyZero), - "approximatelyZero"); + $"Optional parameter must be greater than or equal to zero (was {approximatelyZero}).", + nameof(approximatelyZero)); } return Math.Abs(a) <= approximatelyZero; diff --git a/Rock.Math/LinearFunction.cs b/Rock.Math/LinearFunction.cs index 74b9fc8..861ff49 100644 --- a/Rock.Math/LinearFunction.cs +++ b/Rock.Math/LinearFunction.cs @@ -62,10 +62,7 @@ public LinearFunction(LinearFunction other) public double Slope { get; set; } - public double GetXValue(double yValue) - { - return (yValue - this.YIntersect) / this.Slope; - } + public double GetXValue(double yValue) => (yValue - this.YIntersect) / this.Slope; /// /// Finds the x-coordinate of the point where two linear functions meet (or null if they have approximately the same slope). @@ -103,10 +100,7 @@ public double GetXValue(double yValue) /// A linear function. /// A value by which to raise the linear function. /// A new linear function. - public static LinearFunction operator +(LinearFunction first, double second) - { - return new LinearFunction(first.Slope, first.YIntersect + second); - } + public static LinearFunction operator +(LinearFunction first, double second) => new LinearFunction(first.Slope, first.YIntersect + second); /// /// Creates a new linear function whose slope and y-intersect are equal to the sums of those of the argument functions. @@ -114,10 +108,7 @@ public double GetXValue(double yValue) /// A linear function. /// Another linear function. /// A new linear function. - public static LinearFunction operator +(LinearFunction first, LinearFunction second) - { - return new LinearFunction(first.Slope + second.Slope, first.YIntersect + second.YIntersect); - } + public static LinearFunction operator +(LinearFunction first, LinearFunction second) => new LinearFunction(first.Slope + second.Slope, first.YIntersect + second.YIntersect); /// /// Creates a new linear function by subtracting the given value from the function's y-intersect, i.e., lowering the line by this amount. @@ -125,10 +116,7 @@ public double GetXValue(double yValue) /// A linear function. /// A value by which to lower the linear function. /// A new linear function. - public static LinearFunction operator -(LinearFunction first, double second) - { - return new LinearFunction(first.Slope, first.YIntersect - second); - } + public static LinearFunction operator -(LinearFunction first, double second) => new LinearFunction(first.Slope, first.YIntersect - second); /// /// Creates a new linear function whose slope and y-intersect are equal to the values of the first argument function minus the values of the second argument function. @@ -136,10 +124,7 @@ public double GetXValue(double yValue) /// A linear function. /// A linear function by which to reduce the other function. /// A new linear function. - public static LinearFunction operator -(LinearFunction first, LinearFunction second) - { - return new LinearFunction(first.Slope - second.Slope, first.YIntersect - second.YIntersect); - } + public static LinearFunction operator -(LinearFunction first, LinearFunction second) => new LinearFunction(first.Slope - second.Slope, first.YIntersect - second.YIntersect); /// /// Creates a new linear function by multiplying the given value by the function's slope and y-intersect, i.e., scaling the function up by the provided ratio. @@ -147,10 +132,7 @@ public double GetXValue(double yValue) /// A linear function. /// A value by which to scale the linear function. /// A new linear function. - public static LinearFunction operator *(LinearFunction first, double second) - { - return new LinearFunction(first.Slope * second, first.YIntersect * second); - } + public static LinearFunction operator *(LinearFunction first, double second) => new LinearFunction(first.Slope * second, first.YIntersect * second); /// /// Creates a new linear function by dividing the given value by the function's slope and y-intersect, i.e., scaling the function down by the provided ratio. @@ -158,10 +140,7 @@ public double GetXValue(double yValue) /// A linear function. /// A value by which to scale down the linear function. /// A new linear function. - public static LinearFunction operator /(LinearFunction first, double second) - { - return new LinearFunction(first.Slope / second, first.YIntersect / second); - } + public static LinearFunction operator /(LinearFunction first, double second) => new LinearFunction(first.Slope / second, first.YIntersect / second); /// /// Creates a piecewise function that has a value of 1.0 wherever the linear function is greater than the piecewise function and 0.0 otherwise. @@ -169,10 +148,7 @@ public double GetXValue(double yValue) /// A linear function. /// A piecewise function. /// A piecewise function representing the ranges for which the inequality is true. - public static PiecewiseFunction operator >(LinearFunction linearFunction, PiecewiseFunction piecewiseFunction) - { - return piecewiseFunction < linearFunction; - } + public static PiecewiseFunction operator >(LinearFunction linearFunction, PiecewiseFunction piecewiseFunction) => piecewiseFunction < linearFunction; /// /// Creates a piecewise function that has a value of 1.0 wherever the linear function is less than the piecewise function and 0.0 otherwise. @@ -180,10 +156,7 @@ public double GetXValue(double yValue) /// A linear function. /// A piecewise function. /// A piecewise function representing the ranges for which the inequality is true. - public static PiecewiseFunction operator <(LinearFunction linearFunction, PiecewiseFunction piecewiseFunction) - { - return piecewiseFunction > linearFunction; - } + public static PiecewiseFunction operator <(LinearFunction linearFunction, PiecewiseFunction piecewiseFunction) => piecewiseFunction > linearFunction; /// /// Creates a piecewise function that has a value of 1.0 wherever the linear function is greater than or equal to the piecewise function and 0.0 otherwise. @@ -191,10 +164,7 @@ public double GetXValue(double yValue) /// A linear function. /// A piecewise function. /// A piecewise function representing the ranges for which the inequality is true. - public static PiecewiseFunction operator >=(LinearFunction linearFunction, PiecewiseFunction piecewiseFunction) - { - return piecewiseFunction <= linearFunction; - } + public static PiecewiseFunction operator >=(LinearFunction linearFunction, PiecewiseFunction piecewiseFunction) => piecewiseFunction <= linearFunction; /// /// Creates a piecewise function that has a value of 1.0 wherever the linear function is less than or equal to the piecewise function and 0.0 otherwise. @@ -202,9 +172,6 @@ public double GetXValue(double yValue) /// A linear function. /// A piecewise function. /// A piecewise function representing the ranges for which the inequality is true. - public static PiecewiseFunction operator <=(LinearFunction linearFunction, PiecewiseFunction piecewiseFunction) - { - return piecewiseFunction >= linearFunction; - } + public static PiecewiseFunction operator <=(LinearFunction linearFunction, PiecewiseFunction piecewiseFunction) => piecewiseFunction >= linearFunction; } } diff --git a/Rock.Math/PiecewiseFunction.cs b/Rock.Math/PiecewiseFunction.cs index 37bf04a..590c983 100644 --- a/Rock.Math/PiecewiseFunction.cs +++ b/Rock.Math/PiecewiseFunction.cs @@ -116,21 +116,9 @@ public PiecewiseFunction(PiecewiseFunction other) this.pieces = new List(other); } - public int Count - { - get - { - return this.pieces.Count; - } - } + public int Count => this.pieces.Count; - public Piece this[int index] - { - get - { - return this.pieces[index]; - } - } + public Piece this[int index] => this.pieces[index]; /// /// Given a piecewise function, returns an essentially-identical function that merges any adjacent pieces with the same value. @@ -146,20 +134,21 @@ public static PiecewiseFunction Consolidate(PiecewiseFunction function) var highPiece = newFunction[i]; var lowPiece = newFunction[i - 1]; - if (highPiece.Value.ApproximatelyEquals(lowPiece.Value)) + if (!highPiece.Value.ApproximatelyEquals(lowPiece.Value)) { - // Combine the two pieces - var newPiece = new Piece - { - IncludeUpperBound = highPiece.IncludeUpperBound, - UpperBound = highPiece.UpperBound, - Value = highPiece.Value - }; - - newFunction.pieces.RemoveAt(i); - newFunction.pieces.RemoveAt(i - 1); - newFunction.pieces.Insert(i - 1, newPiece); + continue; } + // Combine the two pieces + var newPiece = new Piece + { + IncludeUpperBound = highPiece.IncludeUpperBound, + UpperBound = highPiece.UpperBound, + Value = highPiece.Value + }; + + newFunction.pieces.RemoveAt(i); + newFunction.pieces.RemoveAt(i - 1); + newFunction.pieces.Insert(i - 1, newPiece); } return newFunction; @@ -244,10 +233,7 @@ public static PiecewiseFunction Combine( PiecewiseFunction second, Func combination, Func evaluation1, - Func evaluation2) - { - return Combine(first, second, null, combination, evaluation1, evaluation2); - } + Func evaluation2) => Combine(first, second, null, combination, evaluation1, evaluation2); /// /// Creates a new function representing the combination of the inputted functions, using the given mechanism to combine their values. @@ -452,10 +438,7 @@ private static void AddPieceNonRedundantly(List pieces, double upperBound /// A piecewise function. /// Some value by which to multiply the function. /// A new piecewise function where, for a given X-value, the Y-value should be equal to the Y-value of the inputted function times the inputted double value. - public static PiecewiseFunction operator *(PiecewiseFunction function, double constantValue) - { - return Modify(function, MultiplyByConstant, constantValue); - } + public static PiecewiseFunction operator *(PiecewiseFunction function, double constantValue) => Modify(function, MultiplyByConstant, constantValue); /// /// Creates a new function representing the piecewise product of the inputted functions. @@ -463,10 +446,7 @@ private static void AddPieceNonRedundantly(List pieces, double upperBound /// A piecewise function. /// Another piecewise function. /// A piecewise function where, for a given X-value, the Y-value should be equal to the product of the Y-values of the two functions at the same X-value. - public static PiecewiseFunction operator *(PiecewiseFunction first, PiecewiseFunction second) - { - return Combine(first, second, MultiplyPieces, ZeroFunction, ZeroFunction); - } + public static PiecewiseFunction operator *(PiecewiseFunction first, PiecewiseFunction second) => Combine(first, second, MultiplyPieces, ZeroFunction, ZeroFunction); /// /// Creates a new function representing the piecewise quotient of the inputted functions. @@ -474,10 +454,7 @@ private static void AddPieceNonRedundantly(List pieces, double upperBound /// A piecewise function. /// Another piecewise function. /// A piecewise function where, for a given X-value, the Y-value should be equal to the Y-value of the first divided by the Y-value of the second at the same X-value. - public static PiecewiseFunction operator /(PiecewiseFunction first, PiecewiseFunction second) - { - return Combine(first, second, DividePieces, ZeroFunction, ZeroFunction); - } + public static PiecewiseFunction operator /(PiecewiseFunction first, PiecewiseFunction second) => Combine(first, second, DividePieces, ZeroFunction, ZeroFunction); /// /// Creates a new function equal to the inputted function, but increased throughout by the given amount. @@ -485,10 +462,7 @@ private static void AddPieceNonRedundantly(List pieces, double upperBound /// A piecewise function. /// Some value by which to increase the function. /// A new piecewise function where, for a given X-value, the Y-value should be equal to the Y-value of the inputted function plus the inputted double value. - public static PiecewiseFunction operator +(PiecewiseFunction function, double constantValue) - { - return Modify(function, AddConstant, constantValue); - } + public static PiecewiseFunction operator +(PiecewiseFunction function, double constantValue) => Modify(function, AddConstant, constantValue); /// /// Creates a new function equal to the inputted function, but decreased throughout by the given amount. @@ -496,10 +470,7 @@ private static void AddPieceNonRedundantly(List pieces, double upperBound /// A piecewise function. /// Some value by which to decrease the function. /// A new piecewise function where, for a given X-value, the Y-value should be equal to the Y-value of the inputted function minus the inputted double value. - public static PiecewiseFunction operator -(PiecewiseFunction function, double constantValue) - { - return Modify(function, SubtractConstant, constantValue); - } + public static PiecewiseFunction operator -(PiecewiseFunction function, double constantValue) => Modify(function, SubtractConstant, constantValue); /// /// Creates a new function representing the sum of the inputted functions. @@ -507,10 +478,7 @@ private static void AddPieceNonRedundantly(List pieces, double upperBound /// A piecewise function. /// Another piecewise function. /// A piecewise function where, for a given X-value, the Y-value should be equal to the sum of the Y-values of the two functions at the same X-value. - public static PiecewiseFunction operator +(PiecewiseFunction first, PiecewiseFunction second) - { - return Combine(first, second, AddPieces, IdentityFunction, IdentityFunction); - } + public static PiecewiseFunction operator +(PiecewiseFunction first, PiecewiseFunction second) => Combine(first, second, AddPieces, IdentityFunction, IdentityFunction); /// /// Creates a new function representing the difference of the inputted functions. @@ -518,10 +486,7 @@ private static void AddPieceNonRedundantly(List pieces, double upperBound /// A piecewise function. /// Another piecewise function. /// A piecewise function where, for a given X-value, the Y-value should be equal to the Y-value of the first function less the Y-value of the second at the same X-value. - public static PiecewiseFunction operator -(PiecewiseFunction first, PiecewiseFunction second) - { - return Combine(first, second, SubtractPieces, IdentityFunction, NegationFunction); - } + public static PiecewiseFunction operator -(PiecewiseFunction first, PiecewiseFunction second) => Combine(first, second, SubtractPieces, IdentityFunction, NegationFunction); /// /// Creates a new function representing the greater value of the two functions at all possible X-values. @@ -529,10 +494,7 @@ private static void AddPieceNonRedundantly(List pieces, double upperBound /// A piecewise function. /// Another piecewise function. /// A piecewise function where, for a given X-value, the Y-value should be equal to the greater of the Y-values of the two functions at the same X-value. - public static PiecewiseFunction Greater(PiecewiseFunction first, PiecewiseFunction second) - { - return Combine(first, second, GreaterOf, ZeroOrGreater, ZeroOrGreater); - } + public static PiecewiseFunction Greater(PiecewiseFunction first, PiecewiseFunction second) => Combine(first, second, GreaterOf, ZeroOrGreater, ZeroOrGreater); /// /// Creates a new function representing the lesser value of the two functions at all possible X-values. @@ -540,10 +502,7 @@ public static PiecewiseFunction Greater(PiecewiseFunction first, PiecewiseFuncti /// A piecewise function. /// Another piecewise function. /// A piecewise function where, for a given X-value, the Y-value should be equal to the lesser of the Y-values of the two functions at the same X-value. - public static PiecewiseFunction Lesser(PiecewiseFunction first, PiecewiseFunction second) - { - return Combine(first, second, LesserOf, ZeroOrLess, ZeroOrLess); - } + public static PiecewiseFunction Lesser(PiecewiseFunction first, PiecewiseFunction second) => Combine(first, second, LesserOf, ZeroOrLess, ZeroOrLess); /// /// Creates a new function representing the sum of the inputted functions. @@ -561,7 +520,7 @@ public static PiecewiseFunction AddMany(PiecewiseFunction[] functions) var upperBound = double.MaxValue; var ending = new bool[count]; - bool anyExist = false; + var anyExist = false; int current; for (current = 0; current < functions.Length; current++) @@ -595,26 +554,28 @@ public static PiecewiseFunction AddMany(PiecewiseFunction[] functions) while (anyExist) { - bool notIncludeUpperBound = false; + var notIncludeUpperBound = false; for (var i = 0; i < functions.Length; i++) { - if (ending[i]) + if (!ending[i]) { - if (!pieces[i].Value.IncludeUpperBound) - { - // We can go ahead and move the enumerator forward on these pieces as we know they are ending - notIncludeUpperBound = true; - if (functions[i].Count > indexes[i] + 1) - { - indexes[i] = indexes[i] + 1; - pieces[i] = functions[i][indexes[i]]; - } - else - { - pieces[i] = null; - } - } + continue; + } + if (pieces[i] != null && pieces[i].Value.IncludeUpperBound) + { + continue; + } + // We can go ahead and move the enumerator forward on these pieces as we know they are ending + notIncludeUpperBound = true; + if (functions[i].Count > indexes[i] + 1) + { + indexes[i] = indexes[i] + 1; + pieces[i] = functions[i][indexes[i]]; + } + else + { + pieces[i] = null; } } @@ -629,17 +590,18 @@ public static PiecewiseFunction AddMany(PiecewiseFunction[] functions) newFunctionPieces.Add(new Piece { UpperBound = upperBound, IncludeUpperBound = true, Value = currentValue }); for (var i = 0; i < functions.Length; i++) { - if (ending[i]) + if (!ending[i]) + { + continue; + } + if (functions[i].Count > indexes[i] + 1) + { + indexes[i] = indexes[i] + 1; + pieces[i] = functions[i][indexes[i]]; + } + else { - if (functions[i].Count > indexes[i] + 1) - { - indexes[i] = indexes[i] + 1; - pieces[i] = functions[i][indexes[i]]; - } - else - { - pieces[i] = null; - } + pieces[i] = null; } } } @@ -652,21 +614,22 @@ public static PiecewiseFunction AddMany(PiecewiseFunction[] functions) for (var i = 0; i < count; i++) { - if (pieces[i].HasValue) + if (!pieces[i].HasValue) { - anyExist = true; - currentValue += pieces[i].Value.Value; + continue; + } + anyExist = true; + currentValue += pieces[i].Value.Value; - if (pieces[i].Value.UpperBound.ApproximatelyEquals(upperBound)) - { - ending[i] = true; - } - else if (pieces[i].Value.UpperBound < upperBound) - { - Clear(ending); - ending[i] = true; - upperBound = pieces[i].Value.UpperBound; - } + if (pieces[i].Value.UpperBound.ApproximatelyEquals(upperBound)) + { + ending[i] = true; + } + else if (pieces[i].Value.UpperBound < upperBound) + { + Clear(ending); + ending[i] = true; + upperBound = pieces[i].Value.UpperBound; } } } @@ -688,10 +651,7 @@ private static void Clear(bool[] flags) /// A piecewise function. /// Another piecewise function. /// A piecewise function where, for a given X-value, the Y-value should be equal 1 if both functions have a nonzero Y-value at the same X-value. - public static PiecewiseFunction operator &(PiecewiseFunction first, PiecewiseFunction second) - { - return Combine(first, second, PiecewiseAnd, ZeroFunction, ZeroFunction); - } + public static PiecewiseFunction operator &(PiecewiseFunction first, PiecewiseFunction second) => Combine(first, second, PiecewiseAnd, ZeroFunction, ZeroFunction); /// /// Creates a new function representing the logical-OR of the inputted functions. @@ -699,20 +659,14 @@ private static void Clear(bool[] flags) /// A piecewise function. /// Another piecewise function. /// A piecewise function where, for a given X-value, the Y-value should be equal 1 if either function has a nonzero Y-value at the same X-value. - public static PiecewiseFunction operator |(PiecewiseFunction first, PiecewiseFunction second) - { - return Combine(first, second, PiecewiseOr, IsTrueFunction, IsTrueFunction); - } + public static PiecewiseFunction operator |(PiecewiseFunction first, PiecewiseFunction second) => Combine(first, second, PiecewiseOr, IsTrueFunction, IsTrueFunction); /// /// Creates a new function representing the logical-NOT of the inputted function (i.e., with value 1 where the function has value 0 and value 0 where the function has any non-zero value). /// /// A piecewise function. /// A piecewise function where, for a given X-value, the Y-value is 1 if the original function's Y-value was 0, and otherwise the Y-value is 0. - public static PiecewiseFunction LogicalNot(PiecewiseFunction function) - { - return Modify(function, NotFunction); - } + public static PiecewiseFunction LogicalNot(PiecewiseFunction function) => Modify(function, NotFunction); /// /// Creates a new function representing the logical-XOR of the inputted functions. @@ -720,10 +674,7 @@ public static PiecewiseFunction LogicalNot(PiecewiseFunction function) /// A piecewise function. /// Another piecewise function. /// A piecewise function where, for a given X-value, the Y-value should be equal 1 if either the first function or the second function has a nonzero Y-value at the same X-value, but not both. - public static PiecewiseFunction operator ^(PiecewiseFunction first, PiecewiseFunction second) - { - return Combine(first, second, PiecewiseXOr, IsTrueFunction, IsTrueFunction); - } + public static PiecewiseFunction operator ^(PiecewiseFunction first, PiecewiseFunction second) => Combine(first, second, PiecewiseXOr, IsTrueFunction, IsTrueFunction); /// /// Creates a new function representing the inequality "Greater Than" for the inputted functions. @@ -731,10 +682,7 @@ public static PiecewiseFunction LogicalNot(PiecewiseFunction function) /// A piecewise function. /// Another piecewise function. /// A piecewise function where, for a given X-value, the Y-value of the first function should be greater than the Y-value of the second function at the same X-value. - public static PiecewiseFunction operator >(PiecewiseFunction first, PiecewiseFunction second) - { - return Combine(first, second, GreaterThan, IsGreaterThanZero, IsLessThanZero); - } + public static PiecewiseFunction operator >(PiecewiseFunction first, PiecewiseFunction second) => Combine(first, second, GreaterThan, IsGreaterThanZero, IsLessThanZero); /// /// Creates a new piecewise function representing the inequality "Greater Than" for the inputted piecewise function and linear function. @@ -742,10 +690,7 @@ public static PiecewiseFunction LogicalNot(PiecewiseFunction function) /// A piecewise function. /// A linear function. /// A piecewise function where, for a given X-value, the Y-value of the piecewise function should be 1.0 if the piecewise function is greater than the linear function at the same X-value, and 0.0 otherwise. - public static PiecewiseFunction operator >(PiecewiseFunction piecewiseFunction, LinearFunction linearFunction) - { - return PiecewiseLinearInequality(piecewiseFunction, linearFunction, false, false, true); - } + public static PiecewiseFunction operator >(PiecewiseFunction piecewiseFunction, LinearFunction linearFunction) => PiecewiseLinearInequality(piecewiseFunction, linearFunction, false, false, true); /// /// Creates a new function representing the inequality "Greater Than or Equal To" for the inputted functions. @@ -753,10 +698,7 @@ public static PiecewiseFunction LogicalNot(PiecewiseFunction function) /// A piecewise function. /// Another piecewise function. /// A piecewise function where, for a given X-value, the Y-value of the first function should be greater than or equal to the Y-value of the second function at the same X-value. - public static PiecewiseFunction operator >=(PiecewiseFunction first, PiecewiseFunction second) - { - return Combine(first, second, GreaterThanOrEquals, IsGreaterThanOrEqualToZero, IsLessThanOrEqualToZero); - } + public static PiecewiseFunction operator >=(PiecewiseFunction first, PiecewiseFunction second) => Combine(first, second, GreaterThanOrEquals, IsGreaterThanOrEqualToZero, IsLessThanOrEqualToZero); /// /// Creates a new piecewise function representing the inequality "Greater Than Or Equal To" for the inputted piecewise function and linear function. @@ -764,10 +706,7 @@ public static PiecewiseFunction LogicalNot(PiecewiseFunction function) /// A piecewise function. /// A linear function. /// A piecewise function where, for a given X-value, the Y-value of the piecewise function should be 1.0 if the piecewise function is greater than or equal to the linear function at the same X-value, and 0.0 otherwise. - public static PiecewiseFunction operator >=(PiecewiseFunction piecewiseFunction, LinearFunction linearFunction) - { - return PiecewiseLinearInequality(piecewiseFunction, linearFunction, false, true, true); - } + public static PiecewiseFunction operator >=(PiecewiseFunction piecewiseFunction, LinearFunction linearFunction) => PiecewiseLinearInequality(piecewiseFunction, linearFunction, false, true, true); /// /// Creates a new function representing the inequality "Less Than" for the inputted functions. @@ -775,10 +714,7 @@ public static PiecewiseFunction LogicalNot(PiecewiseFunction function) /// A piecewise function. /// Another piecewise function. /// A piecewise function where, for a given X-value, the Y-value of the first function should be less than the Y-value of the second function at the same X-value. - public static PiecewiseFunction operator <(PiecewiseFunction first, PiecewiseFunction second) - { - return Combine(first, second, LessThan, IsLessThanZero, IsGreaterThanZero); - } + public static PiecewiseFunction operator <(PiecewiseFunction first, PiecewiseFunction second) => Combine(first, second, LessThan, IsLessThanZero, IsGreaterThanZero); /// /// Creates a new piecewise function representing the inequality "Less Than" for the inputted piecewise function and linear function. @@ -786,10 +722,7 @@ public static PiecewiseFunction LogicalNot(PiecewiseFunction function) /// A piecewise function. /// A linear function. /// A piecewise function where, for a given X-value, the Y-value of the piecewise function should be 1.0 if the piecewise function is less than the linear function at the same X-value, and 0.0 otherwise. - public static PiecewiseFunction operator <(PiecewiseFunction piecewiseFunction, LinearFunction linearFunction) - { - return PiecewiseLinearInequality(piecewiseFunction, linearFunction, true, false, false); - } + public static PiecewiseFunction operator <(PiecewiseFunction piecewiseFunction, LinearFunction linearFunction) => PiecewiseLinearInequality(piecewiseFunction, linearFunction, true, false, false); /// /// Creates a new function representing the inequality "Less Than or Equal To" for the inputted functions. @@ -797,10 +730,7 @@ public static PiecewiseFunction LogicalNot(PiecewiseFunction function) /// A piecewise function. /// Another piecewise function. /// A piecewise function where, for a given X-value, the Y-value of the first function should be less than or equal to the Y-value of the second function at the same X-value. - public static PiecewiseFunction operator <=(PiecewiseFunction first, PiecewiseFunction second) - { - return Combine(first, second, LessThanOrEquals, IsLessThanOrEqualToZero, IsGreaterThanOrEqualToZero); - } + public static PiecewiseFunction operator <=(PiecewiseFunction first, PiecewiseFunction second) => Combine(first, second, LessThanOrEquals, IsLessThanOrEqualToZero, IsGreaterThanOrEqualToZero); /// /// Creates a new piecewise function representing the inequality "Less Than Or Equal To" for the inputted piecewise function and linear function. @@ -808,10 +738,7 @@ public static PiecewiseFunction LogicalNot(PiecewiseFunction function) /// A piecewise function. /// A linear function. /// A piecewise function where, for a given X-value, the Y-value of the piecewise function should be 1.0 if the piecewise function is less than or equal to the linear function at the same X-value, and 0.0 otherwise. - public static PiecewiseFunction operator <=(PiecewiseFunction piecewiseFunction, LinearFunction linearFunction) - { - return PiecewiseLinearInequality(piecewiseFunction, linearFunction, true, true, false); - } + public static PiecewiseFunction operator <=(PiecewiseFunction piecewiseFunction, LinearFunction linearFunction) => PiecewiseLinearInequality(piecewiseFunction, linearFunction, true, true, false); /// /// Creates a new piecewise function representing the inequality "Equal To" for the inputted piecewise function and linear function. @@ -819,10 +746,7 @@ public static PiecewiseFunction LogicalNot(PiecewiseFunction function) /// A piecewise function. /// A linear function. /// A piecewise function where, for a given X-value, the Y-value of the piecewise function should be 1.0 if the piecewise function is equal to the linear function at the same X-value, and 0.0 otherwise. - public static PiecewiseFunction EqualTo(PiecewiseFunction piecewiseFunction, LinearFunction linearFunction) - { - return PiecewiseLinearInequality(piecewiseFunction, linearFunction, false, true, false); - } + public static PiecewiseFunction EqualTo(PiecewiseFunction piecewiseFunction, LinearFunction linearFunction) => PiecewiseLinearInequality(piecewiseFunction, linearFunction, false, true, false); /// /// Creates a new piecewise function representing the inequality "Not Equal To" for the inputted piecewise function and linear function. @@ -830,10 +754,7 @@ public static PiecewiseFunction EqualTo(PiecewiseFunction piecewiseFunction, Lin /// A piecewise function. /// A linear function. /// A piecewise function where, for a given X-value, the Y-value of the piecewise function should be 1.0 if the piecewise function is not equal to the linear function at the same X-value, and 0.0 otherwise. - public static PiecewiseFunction NotEqualTo(PiecewiseFunction piecewiseFunction, LinearFunction linearFunction) - { - return PiecewiseLinearInequality(piecewiseFunction, linearFunction, true, false, true); - } + public static PiecewiseFunction NotEqualTo(PiecewiseFunction piecewiseFunction, LinearFunction linearFunction) => PiecewiseLinearInequality(piecewiseFunction, linearFunction, true, false, true); /// /// Creates a new function representing the equality "Equal To" for the inputted functions. @@ -841,10 +762,7 @@ public static PiecewiseFunction NotEqualTo(PiecewiseFunction piecewiseFunction, /// A piecewise function. /// Another piecewise function. /// A piecewise function where, for a given X-value, the Y-value of the first function should be equal to the Y-value of the second function at the same X-value. - public static PiecewiseFunction EqualTo(PiecewiseFunction first, PiecewiseFunction second) - { - return Combine(first, second, IsEqualTo, IsFalseFunction, IsFalseFunction); - } + public static PiecewiseFunction EqualTo(PiecewiseFunction first, PiecewiseFunction second) => Combine(first, second, IsEqualTo, IsFalseFunction, IsFalseFunction); /// /// Creates a new function representing the inequality "Not Equal To" for the inputted functions. @@ -852,15 +770,9 @@ public static PiecewiseFunction EqualTo(PiecewiseFunction first, PiecewiseFuncti /// A piecewise function. /// Another piecewise function. /// A piecewise function where, for a given X-value, the Y-value of the first function should be not equal to the Y-value of the second function at the same X-value. - public static PiecewiseFunction NotEqualTo(PiecewiseFunction first, PiecewiseFunction second) - { - return Combine(first, second, IsNotEqualTo, IsTrueFunction, IsTrueFunction); - } + public static PiecewiseFunction NotEqualTo(PiecewiseFunction first, PiecewiseFunction second) => Combine(first, second, IsNotEqualTo, IsTrueFunction, IsTrueFunction); - public IEnumerator GetEnumerator() - { - return this.pieces.GetEnumerator(); - } + public IEnumerator GetEnumerator() => this.pieces.GetEnumerator(); /// public override string ToString() @@ -876,33 +788,19 @@ public override string ToString() var previousPiece = this[i - 1]; subStrings.Add( - string.Format( - "{0}{1}{2}{3}: {4}", - previousPiece.UpperBound, - previousPiece.IncludeUpperBound ? " < x" : " <= x", - piece.IncludeUpperBound ? " <= " : " < ", - piece.UpperBound, - piece.Value)); + $"{previousPiece.UpperBound}{(previousPiece.IncludeUpperBound ? " < x" : " <= x")}{(piece.IncludeUpperBound ? " <= " : " < ")}{piece.UpperBound}: {piece.Value}"); } else { subStrings.Add( - string.Format( - "{0} <= x {1}{2}: {3}", - double.MinValue, - piece.IncludeUpperBound ? "<= " : "< ", - piece.UpperBound, - piece.Value)); + $"{double.MinValue} <= x {(piece.IncludeUpperBound ? "<= " : "< ")}{piece.UpperBound}: {piece.Value}"); } } return string.Join(", \r\n", subStrings); } - IEnumerator IEnumerable.GetEnumerator() - { - return this.GetEnumerator(); - } + IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); /// /// Add a piece to the end of the function. Only works if the LowerBound and IncludeLeft are appropriate to the function built thus far. @@ -946,10 +844,7 @@ public void Add(Piece piece) /// /// Upper bound of the piece. /// Value of the function for this piece. - public void Add(double upperBound, double value) - { - this.Add(upperBound, true, value); - } + public void Add(double upperBound, double value) => this.Add(upperBound, true, value); /// /// Add a piece to the end of the function. Lower bound, and whether it is included, will be determined automatically. @@ -1197,10 +1092,7 @@ public double LowestZeroPoint() /// First number. /// Second number. /// Whichever of the two numbers is lower. - private static double Lower(double a, double b) - { - return a < b ? a : b; - } + private static double Lower(double a, double b) => a < b ? a : b; /// /// Gets a new piecewise function that represents the specified inequality between a piecewise function and a linear function. @@ -1229,7 +1121,7 @@ private static PiecewiseFunction PiecewiseLinearInequality( // Possibly we just have a constant function instead of a linear function if (linearFunction.Slope == 0.0) { - foreach (Piece piece in piecewiseFunction) + foreach (var piece in piecewiseFunction) { if (piece.Value.ApproximatelyEquals(linearFunction.YIntersect)) { @@ -1252,7 +1144,7 @@ private static PiecewiseFunction PiecewiseLinearInequality( var currentLowerBound = double.MinValue; var includeLowerBound = true; - foreach (Piece piece in piecewiseFunction) + foreach (var piece in piecewiseFunction) { var intersection = linearFunction.GetXValue(piece.Value); @@ -1410,12 +1302,7 @@ public bool Contains(double value, double lowerBound, bool includeLowerBound) return false; } - if (value > this.UpperBound) - { - return false; - } - - return true; + return !(value > this.UpperBound); } } } From 69e0623748895ef69daf8b2514b399219431f986 Mon Sep 17 00:00:00 2001 From: "MI\\dtarjeft" Date: Mon, 17 Oct 2016 15:56:44 -0400 Subject: [PATCH 2/4] Housekeeping stuff --- Rock.Math/Properties/AssemblyInfo.cs | 8 ++++---- Rock.Math/Rock.Math.csproj | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Rock.Math/Properties/AssemblyInfo.cs b/Rock.Math/Properties/AssemblyInfo.cs index 0b32ed0..45dab2a 100644 --- a/Rock.Math/Properties/AssemblyInfo.cs +++ b/Rock.Math/Properties/AssemblyInfo.cs @@ -6,9 +6,9 @@ // set of attributes. Change these attribute values to modify the information // associated with an assembly. [assembly: AssemblyTitle("Rock.Math")] -[assembly: AssemblyDescription("")] +[assembly: AssemblyDescription("Some common math constructs.")] [assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("Quicken Loans")] +[assembly: AssemblyCompany("IT Team Einstein")] [assembly: AssemblyProduct("Rock.Math")] [assembly: AssemblyCopyright("Copyright © Quicken Loans 2014")] [assembly: AssemblyTrademark("")] @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("0.0.1.0")] +[assembly: AssemblyFileVersion("0.0.1.0")] diff --git a/Rock.Math/Rock.Math.csproj b/Rock.Math/Rock.Math.csproj index e81cd20..05c9677 100644 --- a/Rock.Math/Rock.Math.csproj +++ b/Rock.Math/Rock.Math.csproj @@ -7,8 +7,8 @@ {40B7A008-2B7D-4B53-AEA0-85CA1367695F} Library Properties - Rock.Framework.Math - Rock.Framework.Math + Rock.Math + Rock.Math v4.5 512 SAK From cac7bc20694068b9d18ca40aa9daa62375c95152 Mon Sep 17 00:00:00 2001 From: Tarjeft Date: Tue, 21 Feb 2017 16:00:26 -0500 Subject: [PATCH 3/4] Linear functions intersecting a piecewise function at a point (and only that point) were attempting to add an invalid piece. --- Rock.Math.UnitTests/PiecewiseFunctionTests.cs | 17 +++++++++++++++++ Rock.Math/PiecewiseFunction.cs | 5 ++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Rock.Math.UnitTests/PiecewiseFunctionTests.cs b/Rock.Math.UnitTests/PiecewiseFunctionTests.cs index c18656f..e8ded1d 100644 --- a/Rock.Math.UnitTests/PiecewiseFunctionTests.cs +++ b/Rock.Math.UnitTests/PiecewiseFunctionTests.cs @@ -1904,6 +1904,23 @@ public void HandlesNegativeLinearFunctionAtIncludedLowerBound() Assert.AreEqual(0.0, result.GetValue(1.0001)); Assert.AreEqual(0.0, result.GetValue(1.5)); } + + [Test] + public void HandlesIntersectionAtPieceThatSpansASinglePoint() + { + var linear = new LinearFunction(25, 0); + var pwf = new PiecewiseFunction + { + { 0.8, false, 2 }, // min < x < .8 : 2 + { 0.8, 20 }, // .8 <= x <= .8 : 20 + }; + + PiecewiseFunction result = null; + Assert.DoesNotThrow(() => result = PiecewiseFunction.EqualTo(pwf, linear)); + Assert.AreEqual(0.0, result.GetValue(0.8 - .000005)); + Assert.AreEqual(1.0, result.GetValue(0.8)); + Assert.AreEqual(0.0, result.GetValue(0.8 + .000005)); + } } [TestFixture] diff --git a/Rock.Math/PiecewiseFunction.cs b/Rock.Math/PiecewiseFunction.cs index 590c983..1654b70 100644 --- a/Rock.Math/PiecewiseFunction.cs +++ b/Rock.Math/PiecewiseFunction.cs @@ -1160,7 +1160,10 @@ private static PiecewiseFunction PiecewiseLinearInequality( result.Add(currentLowerBound, true, equalTo ? 1.0 : 0.0); } - result.Add(piece.UpperBound, piece.IncludeUpperBound, trueAfterIntersection ? 1.0 : 0.0); + if (!piece.UpperBound.ApproximatelyEquals(currentLowerBound)) + { + result.Add(piece.UpperBound, piece.IncludeUpperBound, trueAfterIntersection ? 1.0 : 0.0); + } } else if (intersection.ApproximatelyEquals(piece.UpperBound)) { From e89d6e144d7b3ca5b2ed984af333702741ef9864 Mon Sep 17 00:00:00 2001 From: Tarjeft Date: Tue, 21 Feb 2017 16:02:59 -0500 Subject: [PATCH 4/4] no message --- Rock.Math/Properties/AssemblyInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Rock.Math/Properties/AssemblyInfo.cs b/Rock.Math/Properties/AssemblyInfo.cs index 45dab2a..048460d 100644 --- a/Rock.Math/Properties/AssemblyInfo.cs +++ b/Rock.Math/Properties/AssemblyInfo.cs @@ -8,7 +8,7 @@ [assembly: AssemblyTitle("Rock.Math")] [assembly: AssemblyDescription("Some common math constructs.")] [assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("IT Team Einstein")] +[assembly: AssemblyCompany("Quicken Loans")] [assembly: AssemblyProduct("Rock.Math")] [assembly: AssemblyCopyright("Copyright © Quicken Loans 2014")] [assembly: AssemblyTrademark("")]