public void TestSolve(double a0, double a1, double a2, double[] want) { IList <double> got = QuadraticFunction.Solve(a0, a1, a2).ToList(); Assert.Equal(want.Length, got.Count); // There are no guarantees as to the order of values returned, so sort both sequences and compare items // at equal locations in the sorted sequence. Expect them all to match. We don't need to try any other // permutations of these lists to find out if there's some way they do match: // // If the smallest unmatched elements of each set are not adequately near each other, there is no way to // match the smaller of the two to any element in its other set. We have just compared it against the // smallest element in the other set, and found it to be too large. It cannot be too small, because we // are specifically considering the smaller element of the mismatched pair. Since the too-large element // was a smallest element of its set, all other elements are at least that large. Therefore, there is no // element small enough to match the smallest unmatched element between both sets, so the sets are unequal. // Backtracking does not help, because to find a smaller element of the opposite set, we must take it away // from its match to an element that is no larger than the one we are now matching (because all larger // elements are still in the unmatched portion of the set), and now _that_ element has the same problem // but worse. // // Therefore, there is no reason to attempt any other order than an element-by-element pairwise comparison // of the sorted sequences. ∎ Array.Sort(want); int i = 0; var gotSorted = from c in got orderby c select c; foreach (double f in gotSorted) { Assert.Equal(want[i++], f, 6); } }
public void TestValueCalculation(double a0, double a1, double a2, double x, double expected) { QuadraticFunction q = new QuadraticFunction(a0, a1, a2); Assert.NotNull(q); Assert.Equal(expected, q.GetValueAt(x), 6); Assert.Equal(expected, q.GetNthDerivativeAt(x, 0), 6); }
public void TestValueCalculation(float a0, float a1, float a2, float x, float expected) { QuadraticFunction q = new QuadraticFunction(a0, a1, a2); Assert.NotNull(q); Assert.True(Near(expected, q.GetValueAt(x))); Assert.True(Near(expected, q.GetNthDerivativeAt(x, 0))); }
public void TestValueNaN(float a0, float a1, float a2, float x) { QuadraticFunction q = new QuadraticFunction(a0, a1, a2); Assert.NotNull(q); Assert.True(float.IsNaN(q.GetValueAt(x))); Assert.True(float.IsNaN(q.GetNthDerivativeAt(x, 0))); }
public void TestQuadraticFunction() { var minimizer = new QNMinimizer(); var f = new QuadraticFunction(); var x = minimizer.Minimize(f); var minValue = f.ValueAt(x); Assert.AreEqual(x[0], 1.0, 0.000001); Assert.AreEqual(x[1], 5.0, 0.000001); Assert.AreEqual(minValue, 10.0, 0.000001); }
public void TestDerivativesAt(double a0, double a1, double a2, double x, double atD0, double atD1, double atD2) { QuadraticFunction q = new QuadraticFunction(a0, a1, a2); Assert.NotNull(q); Assert.Equal(q.GetNthDerivativeAt(x, 0), atD0, 6); Assert.Equal(q.GetNthDerivativeAt(x, 1), atD1, 6); Assert.Equal(q.GetDerivativeAt(x), atD1, 6); Assert.Equal(q.GetNthDerivativeAt(x, 2), atD2, 6); Assert.Equal(q.GetNthDerivativeAt(x, 3), 0); Assert.Equal(q.GetNthDerivativeAt(x, 4), 0); }
public void TestDerivativesAt(float a0, float a1, float a2, float x, float atD0, float atD1, float atD2) { QuadraticFunction q = new QuadraticFunction(a0, a1, a2); Assert.NotNull(q); Assert.True(Near(q.GetNthDerivativeAt(x, 0), atD0)); Assert.True(Near(q.GetNthDerivativeAt(x, 1), atD1)); Assert.True(Near(q.GetDerivativeAt(x), atD1)); Assert.True(Near(q.GetNthDerivativeAt(x, 2), atD2)); Assert.Equal(q.GetNthDerivativeAt(x, 3), 0); Assert.Equal(q.GetNthDerivativeAt(x, 4), 0); }
public CQuadraticFunction(QuadraticFunction qd) { A = qd.A; B = qd.B; C = qd.C; }