private static void showError() { #region definitions Polynomial[,] Pa = new Polynomial[3, 3]; Pa[0, 0] = new Polynomial(1, new double[] { 1, 2 }); Pa[0, 1] = new Polynomial(1, new double[] { 3, 4 }); Pa[0, 2] = new Polynomial(1, new double[] { 5, 6 }); Pa[1, 0] = new Polynomial(1, new double[] { 7, 8 }); Pa[1, 1] = new Polynomial(1, new double[] { 9, 0 }); Pa[1, 2] = new Polynomial(1, new double[] { 1, 2 }); Pa[2, 0] = new Polynomial(1, new double[] { 3, 4 }); Pa[2, 1] = new Polynomial(1, new double[] { 5, 6 }); Pa[2, 2] = new Polynomial(1, new double[] { 7, 8 }); Polynomial[,] Pb = new Polynomial[2, 2]; Pb[0, 0] = new Polynomial(1, new double[] { 3, 4 }); Pb[0, 1] = new Polynomial(1, new double[] { 5, 6 }); Pb[1, 0] = new Polynomial(1, new double[] { 1, 2 }); Pb[1, 1] = new Polynomial(1, new double[] { 3, 4 }); #endregion Matrix M1 = new Matrix(3, 3, Pa); Matrix M2 = new Matrix(2, 2, Pb); Console.WriteLine("M1: \n" + M1.ToString()); Console.WriteLine("M2: \n" + M2.ToString()); Console.WriteLine("M1 + M2: "); try { Matrix M3 = M1 + M2; Console.WriteLine("M3: \n" + M3.ToString()); } catch (MathException e) { Console.WriteLine("Es ist ein Fehler aufgetreten: \n" + e.what()); } }
public void MatrixConstructorTest2() { Matrix forCopy = new Matrix(2, 2); Matrix_Accessor target = new Matrix_Accessor(2, 2); Matrix_Accessor expected = new Matrix_Accessor(forCopy); Assert.AreEqual(target._rows, expected._rows); Assert.AreEqual(target._cols, expected._cols); Assert.AreEqual(target._dimT, expected._dimT); CollectionAssert.AreEqual(target._data, expected._data); Polynomial[,] p = new Polynomial[2, 2]; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) p[i, j] = new Polynomial(); Matrix_Accessor target2 = new Matrix_Accessor(2, 2, p); Assert.AreEqual(target2._rows, expected._rows); Assert.AreEqual(target2._cols, expected._cols); Assert.AreEqual(target2._dimT, expected._dimT); }
/// <summary> /// Calculates the square root of a Taylor polynomial. Implements the square root function <para/> /// for Taylor arithmetic.<para/> /// <para/> /// The following coefficient propagation rule is applied:<para/> /// <para/> /// v_k = 1 / 2*v_0 * [u_k - sum_{j=1}{k-1} v_j * v_{k-j}]<para/> /// <para/> /// for k = 1...d and v(t) = sqrt(u(t)), u and v being a Taylor polynomials, and d being <para/> /// the derivative degree. In particular, v_0 = sqrt(u_0).<para/> /// <para/> /// (See Griewank's book, p.222 from "Evaluating Derivatives: Principles and Techniques<para/> /// of Algorithmic Differentiation". In Frontiers in Applied Mathematics Nr. 19, SIAM, <para/> /// Philadelphia, PA, 2000)<para/> /// </summary> /// <returns></returns> public Polynomial sqrt() { Polynomial retval = new Polynomial(_order); retval[0] = Math.Sqrt(this[0]); for (int i = 1; i <= _order; i++) { double sum = 0.0; for (int j = 1; j < i; j++) sum += retval[j] * retval[i - j]; retval[i] = ( this[i] - sum ) / (2 * retval[0]); } return retval; }
/// <summary> /// Divides a Taylor polynomial (dividend) by another Taylor polynomial (divisor).<para/> /// Implements the / operator for Taylor arithmetic.<para/> /// <para/> /// v_k = 1 / w_0 * [u_k - sum_{j=0}{k-1} v_j * w_{k-j}]<para/> /// <para/> /// for k = 1...d and v(t) = u(t) / w(t), u, v, w being Taylor polynomials, and d being <para/> /// the derivative degree.<para/> /// <para/> /// It is assumed that all three Taylor polynomials have the same derivative degree d.<para/> /// <para/> /// (See Griewank's book, p.222 from "Evaluating Derivatives: Principles and Techniques<para/> /// of Algorithmic Differentiation". In Frontiers in Applied Mathematics Nr. 19, SIAM, <para/> /// Philadelphia, PA, 2000)<para/> /// </summary> /// <param name="a">Polynomial as divisor</param> /// <param name="b">Polynomial as divident</param> /// <returns>the resulting Taylor polynomial.</returns> public static Polynomial operator /(Polynomial a, Polynomial b) { if (a._order != b._order) { throw new MathException("The order of both Taylor Polynoms should match."); } Polynomial retval = new Polynomial(a._order); for (int i = 0; i <= a._order; i++) { double sum = 0.0; for (int j = 0; j < i; j++) { sum += retval[j] * b[i - j]; } retval[i] = (a[i] - sum) / b[0]; } return retval; }
/// <summary> /// Implements the unary - Operator /// </summary> /// <param name="a"></param> /// <returns></returns> public static Polynomial operator -(Polynomial a) { Polynomial p = new Polynomial(a._order); for (int i = 0; i <= a._order; i++) { p[i] = - a[i]; } return p; }
/// <summary> /// Multiplies two Taylor polynomials. Implements the * operator for Taylor arithmetic.<para/> /// The following coefficient propagation rule is applied:<para/> /// <para/> /// v_k = sum_{j=0}{k} u_j * w_{k-j}<para/> /// <para/> /// for k = 1...d and v(t) = u(t) * w(t), u, v, w being Taylor polynomials, and d being <para/> /// the derivative degree.<para/> /// <para/> /// It is assumed that all three Taylor polynomials have the same derivative degree d.<para/> /// Three different cases are distinguished here: when at least one of the polynomials<para/> /// is a constant polynomial and when both polynomials are not.<para/> /// <para/> /// (See Griewank's book, p.222 from "Evaluating Derivatives: Principles and Techniques<para/> /// of Algorithmic Differentiation". In Frontiers in Applied Mathematics Nr. 19, SIAM,<para/> /// Philadelphia, PA, 2000)<para/> /// </summary> /// <param name="a">the Taylor polynomial to be multiplied with.</param> /// <param name="b">the Taylor polynomial to be multiplied by.</param> /// <returns>The resulting Polynomail (without changing the order)</returns> public static Polynomial operator *(Polynomial a, Polynomial b) { if (a._order != b._order) { throw new MathException("The order of both Taylor Polynoms should match."); } Polynomial retVal = new Polynomial(a._order); if (a.isConst()) { retVal = new Polynomial(b); for (int i = 0; i <= a._order; i++) { retVal[i] *= a[0]; } } else if (b.isConst()) { retVal = new Polynomial(a); for (int i = 0; i <= b._order; i++) { retVal[i] *= b[0]; } } else { // 2. for (int i = 0; i < _order * 2; i++) for (int i = 0; i <= a._order; i++) { retVal[i] = 0.0; for (int j = 0; j < i + 1; j++) { retVal[i] += a[j] * b[i - j]; } } // 3. v._coeffs[_order*2] = _coeffs[_order] * p._coeffs[_order]; } return retVal; }
public void PolynomialSqrtTest() { Polynomial P1 = new Polynomial(3, new double[] { 1, 4, 10, 20 }); Polynomial PExpect = new Polynomial(3, new double[] { 1, 2, 3, 4 }); Polynomial P2 = P1.sqrt(); Assert.AreEqual(P2, PExpect); Polynomial PNotExpect = new Polynomial(3, new double[] { 1, 2, 3, 5 }); Assert.AreNotEqual(P2, PNotExpect); }
public void PolynomialSetSqrtTest() { Polynomial P1 = new Polynomial(3, new double[] { 1, 4, 10, 20 }); Polynomial P2 = P1.sqrt(); P1.setSqrt(); Assert.AreEqual(P2, P1); }
public void PolynomialOperatorMultiplyTest() { Polynomial a1 = new Polynomial(2, new double[] { 1, 2, 3 }); Polynomial a2 = new Polynomial(2, new double[] { 2, 3, 4 }); Polynomial expected = new Polynomial(2, new double[] { 2, 7, 16 }); Assert.IsTrue((a1 * a2) == expected); a1 *= a2; Assert.AreEqual(a1, expected); Polynomial a3 = new Polynomial(2, new double[] { 2, 0, 0 }); Polynomial expectedA3 = new Polynomial(2, new double[] { 4, 14, 32 }); Assert.IsTrue(a1 * a3 == expectedA3); Assert.IsTrue(a3 * a1 == expectedA3); a1 *= a3; Assert.AreEqual(a1, expectedA3); Polynomial a4 = new Polynomial(2, new double[] { 4, 5, 6 }); double d = 2; Polynomial expectedA4 = new Polynomial(2, new double[] { 8, 10, 12 }); Assert.IsTrue((a4 * d ) == expectedA4); a4 *= d; Assert.AreEqual(a4, expectedA4); Polynomial a5 = new Polynomial(3, new double[] { 1, 2, 3, 4 }); try { a5 *= a4; Assert.Fail(); } catch (MathException) { } catch (Exception) { Assert.Fail(); } }
public void PolynomialOperatorMinusTest() { Polynomial a1 = new Polynomial(3, new double[] { 6, 4, 2, 0 }); Polynomial a2 = new Polynomial(3, new double[] { 3, 2, 1, 0 }); Polynomial expected = new Polynomial(3, new double[] { 3, 2, 1, 0 }); Assert.IsTrue((a1 - a2) == expected); a1 -= a2; Assert.AreEqual(a1, expected); Polynomial a3 = new Polynomial(3, new double[] { 6, 0, 0, 0 }); Polynomial expectedA3 = new Polynomial(3, new double[] { -3, 2, 1, 0 }); Polynomial expectedA3_A1 = new Polynomial(3, new double[] { 3, -2, -1, 0 }); Assert.IsTrue(a1 - a3 == expectedA3); Assert.IsTrue(a3 - a1 == expectedA3_A1); a1 -= a3; Assert.AreEqual(a1, expectedA3); Polynomial a4 = new Polynomial(2, new double[] { 6, 0, 0 }); try { a3 -= a4; Assert.Fail(); } catch (MathException) { } catch (Exception) { Assert.Fail(); } }
public void PolynomialOperatorLtGtTest() { Polynomial target = new Polynomial(3, new double[] { 3, 2, 1, 0 }); Polynomial expected = new Polynomial(3, new double[] { 3, 2, 1, 0 }); Polynomial greater = new Polynomial(3, new double[] { 4, 2, 1, 0 }); Polynomial muchGreater = new Polynomial(4, new double[] { 4, 3, 2, 1, 0 }); Assert.IsTrue(target <= expected); Assert.IsFalse(target < expected); Assert.IsTrue(target >= expected); Assert.IsFalse(target > expected); Assert.IsTrue(greater >= expected); Assert.IsTrue(greater > expected); Assert.IsTrue(expected < greater); Assert.IsTrue(expected <= greater); Assert.IsTrue(muchGreater > greater); Assert.IsTrue(muchGreater >= greater); Assert.IsFalse(muchGreater < greater); Assert.IsFalse(muchGreater <= greater); Assert.IsFalse(greater > muchGreater); Assert.IsFalse(greater >= muchGreater); Assert.IsFalse(expected >= greater); Assert.IsTrue(greater < muchGreater); Assert.IsTrue(greater <= muchGreater); Assert.IsFalse(greater <= expected); }
public void PolynomialOperatorEqualsTest() { Polynomial target = new Polynomial(3, new double[] { 3, 2, 1, 0 }); Polynomial expected = new Polynomial(3, new double[] { 3, 2, 1, 0 }); Polynomial notExpected = new Polynomial(4, new double[] { 4, 3, 2, 1, 0 }); Assert.AreEqual(expected.ToString(), target.ToString()); Assert.IsTrue(target == expected); Assert.IsFalse(target != expected); Assert.AreNotEqual(notExpected.ToString(), target.ToString()); Assert.IsFalse(target == notExpected); Assert.IsTrue(target != notExpected); Assert.IsTrue((Polynomial)(null) == (Polynomial)(null)); Assert.IsFalse(target == null); }
public void PolynomialOperatorDivTest() { Polynomial a1 = new Polynomial(2, new double[] { 2, 4, 6 }); Polynomial a2 = new Polynomial(2, new double[] { 1, 2, 3 }); Polynomial a3 = a1 / a2; Polynomial a4 = a2 * a3; Assert.AreEqual(a1, a4); a1 /= a2; Polynomial expected = new Polynomial(2, new double[] { 2, 0, 0 }); Assert.AreEqual(a1, expected); Polynomial a5 = new Polynomial(3, new double[] {4, 3, 2, 1 }); try { a5 /= a4; Assert.Fail(); } catch (MathException) { } catch (Exception) { Assert.Fail(); } }
public void PolynomialIsZeroTest() { Polynomial P1 = new Polynomial(3, new double[] { 0, 0, 0, 0 }); Polynomial P2 = new Polynomial(3, new double[] { 0, 0, 4, 0 }); Polynomial P3 = new Polynomial(3, new double[] { 6, 0, 0, 0 }); Assert.IsTrue(P1.isZero()); Assert.IsFalse(P2.isZero()); Assert.IsFalse(P3.isZero()); }
public void PolynomialIsZeroEpsTest() { Polynomial P1 = new Polynomial(3, new double[] { 1, 2, 3, 4 }); Assert.IsTrue(P1.isZero(4)); Assert.IsFalse(P1.isZero(3)); }
public void PolynomialSetCoeffsTest() { Polynomial P1 = new Polynomial(3, new double[] { 1, 1, 2, 1 }); double[] d; try { d = new double[] { 1, 2, 3, 4, 5 }; P1.setCoeffs(d); Assert.Fail(); // If it gets to this line, no exception was thrown } catch (MathException) { } catch (Exception) { Assert.Fail(); } d = new double[] { 4, 2, 4, 2 }; P1.setCoeffs(d); Polynomial P2 = new Polynomial(3, new double[] { 4, 2, 4, 2 }); Assert.AreEqual(P1, P2); P1 = new Polynomial(3, new double[] { 1, 1, 2, 1 }); d = new double[] { 4, 2, 4 }; P2 = new Polynomial(3, new double[] { 4, 2, 4, 0 }); P1.setCoeffs(d); Assert.AreEqual(P1, P2); }
public void PolynomialSetSqrTest() { Polynomial P1 = new Polynomial(3, new double[] { 1, 2, 3, 4 }); Polynomial P2 = P1.sqr(); P1.setSqr(); Assert.AreEqual(P2, P1); }
public void PolynomialOperatorPlusTest() { Polynomial a1 = new Polynomial(3, new double[] { 3, 2, 1, 0 }); Polynomial a2 = new Polynomial(3, new double[] { 3, 2, 1, 0 }); Polynomial expected = new Polynomial(3, new double[] { 6, 4, 2, 0 }); Assert.IsTrue((a1 + a2) == expected); a1 += a2; Assert.AreEqual(a1, expected); Polynomial a3 = new Polynomial(3, new double[] { 6, 0, 0, 0 }); Polynomial expectedA3 = new Polynomial(3, new double[] { 12, 4, 2, 0 }); Assert.IsTrue(a1 + a3 == expectedA3); a1 += a3; Assert.AreEqual(a1, expectedA3); Polynomial a4 = new Polynomial(2, new double[] { 3, 2, 1 }); try { a1 += a4; Assert.Fail(); } catch (MathException) { } catch (Exception) { Assert.Fail(); } Polynomial a5 = new Polynomial(2, new double[] { 1, 0, 0 }); a5 += a4; }
public void PolynomialShiftTest() { Polynomial P1 = new Polynomial(3, new double[] { 1, 2, 3, 4 }); Polynomial P2 = new Polynomial(3, new double[] { 2, 6, 12, 0 }); P1.shift(); Assert.AreEqual(P1, P2); Polynomial P3 = new Polynomial(3, new double[] { 6, 24, 0, 0 }); P1.shift(); Assert.AreEqual(P1, P3); }
public void PolynomialOperatorSquareBracketsTest() { Polynomial target = new Polynomial(3, new double[] {3,2,1,0}); double expected = target[2]; double actual = target.getValueAt(2); Assert.AreEqual(expected, actual); Assert.AreEqual(expected, (double)1); Assert.AreEqual(actual, (double)1); Assert.AreNotEqual(expected, (double)2); }
public void PolynomialToStringTest() { Polynomial target = new Polynomial(3); string expected = "0x^3\t + 0x^2\t + 0x^1\t + 0"; string notExpected = "0x^3 + 0x^2 + 0x^1 + 0"; string actual = ""; actual = target.ToString(); Assert.AreEqual(expected, actual); Assert.AreNotEqual(notExpected, actual); Polynomial target2 = new Polynomial(); expected = "1"; notExpected = "\t1"; actual = target2.ToString(); Assert.AreEqual(expected, actual); Assert.AreNotEqual(notExpected, actual); }
public void PolynomialOperatorUnaryMinusTest() { Polynomial a1 = new Polynomial(3, new double[] { 3, 2, 1, 0 }); Polynomial a2 = new Polynomial(3, new double[] { -3, -2, -1, 0 }); Assert.IsTrue(-a1 == a2); Assert.AreEqual(-a1, a2); }
/// <summary> /// Implements the * operator.<para/> /// Multiplies a Taylor polynomial by a scalar.<para/> /// </summary> /// <param name="a">The Polynomial value to multiply with</param> /// <param name="d">The scalar value to multiply by</param> /// <returns></returns> public static Polynomial operator *(Polynomial a, double d) { Polynomial retVal = new Polynomial(a); for (int i = 0; i <= a._order; i++) { retVal[i] *= d; } return retVal; }
public void PolynomialPrintTest() { Polynomial P1 = new Polynomial(3, new double[] { 1, 1, 2, 1 }); string file = Path.GetTempFileName(); FileStream fs = new FileStream(file, FileMode.Create); TextWriter tmp = Console.Out; StreamWriter sw = new StreamWriter(fs); Console.SetOut(sw); #pragma warning disable 618 P1.print(); #pragma warning restore 618 Console.SetOut(tmp); sw.Close(); string actual = File.ReadAllText(file); string expected = "1\t1\t2\t1\t" + System.Environment.NewLine; Assert.AreEqual(actual, expected); File.Delete(file); P1.print(file); actual = File.ReadAllText(file); Assert.AreEqual(actual, expected); }
/// <summary> /// Implements the -= operator.<para/> /// Substracts a Taylor polynomial from the current one using pointers to arrays that <para/> /// store the coefficients.<para/> /// </summary> /// <param name="a">the Taylor polynomial to be substracted from.</param> /// <param name="b">Polynomial on the right side</param> /// <returns>a - b (if order mathches)</returns> public static Polynomial operator -(Polynomial a, Polynomial b) { if (a._order != b._order) { throw new MathException("The order of both Taylor Polynoms should match. "); } Polynomial retval = new Polynomial(a._order); if (a.isConst()) { retval = new Polynomial(-b); retval[0] = a[0] - b[0]; } else if (b.isConst()) { retval = new Polynomial(a); retval[0] -= b[0]; } // general case else { for (int i = 0; i <= a._order; i++) retval[i] = a[i] - b[i]; } return retval; }
public void PolynomialSet2ConstTest() { Polynomial P1 = new Polynomial(3, new double[] { 1, 1, 2, 1 }); Assert.IsFalse(P1.isConst()); P1.set2const(6); Assert.IsTrue(P1.isConst()); Polynomial P2 = new Polynomial(3, new double[] { 6, 0, 0, 0 }); Assert.AreEqual(P1, P2); }
/// <summary> /// Polynomial PExpect = new Polynomial(3, new double[] { 1, 4, 10, 20 });<para/> /// Taylor arithmetic.<para/> /// <para/> /// The following coefficient propagation rule is applied:<para/> /// <para/> /// v_k = sum_{j=0}{k} u_j * u_{k-j}<para/> /// <para/> /// for k = 1...d and v(t) = u(t)^2, u and v being a Taylor polynomials, and d being<para/> /// the derivative degree.<para/> /// <para/> /// (See Griewank's book, p.222 from "Evaluating Derivatives: Principles and Techniques<para/> /// of Algorithmic Differentiation". In Frontiers in Applied Mathematics Nr. 19, SIAM, <para/> /// Philadelphia, PA, 2000)<para/> /// </summary> /// <returns>The resulting Taylor polynomial.</returns> public Polynomial sqr() { Polynomial retval = new Polynomial(_order); for (int i = 0; i <= _order; i++) { retval[i] = 0.0; for (int j = 0; j < i + 1; j++) retval[i] += this[j] * this[i - j]; } return retval; }
public void PolynomialSet2IdTest() { Polynomial p = new Polynomial(2, new double[] { 3, 4, 5 }); Assert.IsFalse(p.isId()); p.set2Id(); Assert.IsTrue(p.isId()); }
/// <summary> /// Copy constructor.<para/> /// Polynomial newtp = new Polynomial(oldtp);<para/> /// Equivalent to Polynomial newtp = oldtp; /// </summary> /// <param name="p">The Taylor polynomial object to copy from.</param> public Polynomial(Polynomial p) { initializePolynomial(p.order, p._coeffs); }
public void PolynomialSet2zeroTest() { Polynomial P1 = new Polynomial(3, new double[] { 1, 1, 2, 1 }); Assert.IsFalse(P1.isZero()); Assert.IsFalse(P1.isZero(1)); Assert.IsTrue(P1.isZero(2)); P1.set2Zero(); Assert.IsTrue(P1.isZero()); P1 = new Polynomial(3, new double[] { 1, 1, 2, 1 }); P1.set2Zero(2); Assert.IsTrue(P1.isZero(1)); }