public void SubstractEquals() { A = R.Copy(); Assert.IsFalse(A.Norm1() == 0.0); A.SubtractEquals(R); Assert.IsTrue(A.Norm1() == 0.0); }
/// <summary>Check norm of difference of Matrices. /// </summary> public static bool Check(GeneralMatrix X, GeneralMatrix Y) { bool result = false; double eps = System.Math.Pow(2.0, -52.0); if (X.Norm1() == 0.0 & Y.Norm1() < 10 * eps) { result = true; } else if (Y.Norm1() == 0.0 & X.Norm1() < 10 * eps) { result = true; } else if (X.Subtract(Y).Norm1() > 1000 * eps * System.Math.Max(X.Norm1(), Y.Norm1())) { throw new System.SystemException("The norm of (X-Y) is too large: " + X.Subtract(Y).Norm1().ToString()); } else { result = true; } return(result); }
public void Identity() { double[][] ivals = { new double[] { 1.0, 0.0, 0.0, 0.0 }, new double[] { 0.0, 1.0, 0.0, 0.0 }, new double[] { 0.0, 0.0, 1.0, 0.0 } }; GeneralMatrix I = new GeneralMatrix(ivals); GeneralMatrix K = GeneralMatrix.Identity(3, 4); Assert.IsTrue(I.Norm1() == K.Norm1() && I.Norm1() == 1); }
public void TestNorm1() { GeneralMatrix _gm = new GeneralMatrix(2, 2); _gm.SetElement(0, 0, 1); _gm.SetElement(0, 1, 2); _gm.SetElement(1, 0, 3); _gm.SetElement(1, 1, 4); Assert.AreEqual(6, _gm.Norm1()); }
public static void Main(System.String[] argv) { /* | Tests LU, QR, SVD and symmetric Eig decompositions. | | n = order of magic square. | trace = diagonal sum, should be the magic sum, (n^3 + n)/2. | max_eig = maximum eigenvalue of (A + A')/2, should equal trace. | rank = linear algebraic rank, | should equal n if n is odd, be less than n if n is even. | cond = L_2 condition number, ratio of singular values. | lu_res = test of LU factorization, norm1(L*U-A(p,:))/(n*eps). | qr_res = test of QR factorization, norm1(Q*R-A)/(n*eps). */ print("\n Test of GeneralMatrix Class, using magic squares.\n"); print(" See MagicSquareExample.main() for an explanation.\n"); print("\n n trace max_eig rank cond lu_res qr_res\n\n"); System.DateTime start_time = System.DateTime.Now; double eps = System.Math.Pow(2.0, -52.0); for (int n = 3; n <= 32; n++) { print(fixedWidthIntegertoString(n, 7)); GeneralMatrix M = magic(n); //UPGRADE_WARNING: Narrowing conversions may produce unexpected results in C#. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1042"' int t = (int)M.Trace(); print(fixedWidthIntegertoString(t, 10)); EigenvalueDecomposition E = new EigenvalueDecomposition(M.Add(M.Transpose()).Multiply(0.5)); double[] d = E.RealEigenvalues; print(fixedWidthDoubletoString(d[n - 1], 14, 3)); int r = M.Rank(); print(fixedWidthIntegertoString(r, 7)); double c = M.Condition(); print(c < 1 / eps ? fixedWidthDoubletoString(c, 12, 3):" Inf"); LUDecomposition LU = new LUDecomposition(M); GeneralMatrix L = LU.L; GeneralMatrix U = LU.U; int[] p = LU.Pivot; GeneralMatrix R = L.Multiply(U).Subtract(M.GetMatrix(p, 0, n - 1)); double res = R.Norm1() / (n * eps); print(fixedWidthDoubletoString(res, 12, 3)); QRDecomposition QR = new QRDecomposition(M); GeneralMatrix Q = QR.Q; R = QR.R; R = Q.Multiply(R).Subtract(M); res = R.Norm1() / (n * eps); print(fixedWidthDoubletoString(res, 12, 3)); print("\n"); } System.DateTime stop_time = System.DateTime.Now; double etime = (stop_time.Ticks - start_time.Ticks) / 1000.0; print("\nElapsed Time = " + fixedWidthDoubletoString(etime, 12, 3) + " seconds\n"); print("Adios\n"); }
public void Transpose2() { A.Transpose(); Assert.IsTrue(GeneralTests.Check(A.Norm1(), columnsummax)); }