public void RandomRectangularSVD()
        {
            for (int c = 1; c < 64; c += 11)
            {
                Console.WriteLine(c);

                RectangularMatrix R = GenerateRandomMatrix(64, c);

                SingularValueDecomposition SVD = R.SingularValueDecomposition();

                Assert.IsTrue(SVD.RowCount == R.RowCount);
                Assert.IsTrue(SVD.ColumnCount == SVD.ColumnCount);
                Assert.IsTrue(SVD.Dimension == SVD.ColumnCount);

                SquareMatrix U = SVD.LeftTransformMatrix();
                Assert.IsTrue(U.Dimension == R.RowCount);
                Assert.IsTrue(TestUtilities.IsNearlyEqual(U.Transpose() * U, TestUtilities.CreateSquareUnitMatrix(U.Dimension)));

                SquareMatrix V = SVD.RightTransformMatrix();
                Assert.IsTrue(V.Dimension == R.ColumnCount);
                Assert.IsTrue(TestUtilities.IsNearlyEqual(V.Transpose() * V, TestUtilities.CreateSquareUnitMatrix(V.Dimension)));

                RectangularMatrix S = U.Transpose() * R * V;
                for (int i = 0; i < SVD.Dimension; i++)
                {
                    double w = SVD.SingularValue(i);
                    Console.WriteLine("  {0} {1}", w, S[i, i]);
                    Assert.IsTrue(w >= 0.0);
                    Assert.IsTrue(TestUtilities.IsNearlyEqual(S[i, i], w));
                    Assert.IsTrue(TestUtilities.IsNearlyEqual(R * SVD.RightSingularVector(i), w * SVD.LeftSingularVector(i)));
                }
            }
        }
        public void SquareMatrixArithmetic()
        {
            SquareMatrix M = CreateSquareRandomMatrix(5);

            // addition is same a multiplication by two
            SquareMatrix MA = M + M;
            SquareMatrix M2 = 2.0 * M;

            Assert.IsTrue(MA == M2);

            // subraction of self same as multiplication by zero
            SquareMatrix MS = M - M;
            SquareMatrix M0 = 0.0 * M;

            Assert.IsTrue(MS == M0);

            // check transpose
            SquareMatrix MT = M.Transpose();

            Assert.IsTrue(MT != M);

            // matrix multiplication is not ableian
            SquareMatrix MMT = M * MT;
            SquareMatrix MTM = MT * M;

            Assert.IsFalse(MMT == MTM);

            // check that transpose of transpose is original
            SquareMatrix MTT = MT.Transpose();

            Assert.IsTrue(MTT == M);
        }
Beispiel #3
0
        public void SquareMatrixArithmetic()
        {
            SquareMatrix M = CreateSquareRandomMatrix(5);

            // Addition is same as multiplication by two
            SquareMatrix MA = M + M;
            SquareMatrix M2 = 2.0 * M;

            Assert.IsTrue(MA == M2);

            // Division by two returns us to original
            SquareMatrix MB = MA / 2.0;

            Assert.IsTrue(MB == M);

            // Subraction of self same as multiplication by zero
            SquareMatrix MS = M - M;
            SquareMatrix M0 = 0.0 * M;

            Assert.IsTrue(MS == M0);

            // Negation is same as multiplication by negative one
            SquareMatrix MN = -M;
            SquareMatrix MM = -1.0 * M;

            Assert.IsTrue(MN == MM);

            // check transpose
            SquareMatrix MT = M.Transpose();

            Assert.IsTrue(MT != M);

            // matrix multiplication is not ableian
            SquareMatrix MMT = M * MT;
            SquareMatrix MTM = MT * M;

            Assert.IsFalse(MMT == MTM);

            // check that transpose of transpose is original
            SquareMatrix MTT = MT.Transpose();

            Assert.IsTrue(MTT == M);
        }
        public void RectangularQRDecomposition()
        {
            RectangularMatrix M = GenerateRandomMatrix(30, 10);

            QRDecomposition QRD = M.QRDecomposition();

            Assert.IsTrue(QRD.RowCount == M.RowCount);
            Assert.IsTrue(QRD.ColumnCount == M.ColumnCount);

            SquareMatrix Q = QRD.QMatrix;

            Assert.IsTrue(Q.Dimension == M.RowCount);
            Assert.IsTrue(TestUtilities.IsNearlyEqual(Q * Q.Transpose(), TestUtilities.CreateSquareUnitMatrix(Q.Dimension)));

            RectangularMatrix R = QRD.RMatrix;

            Assert.IsTrue(R.RowCount == M.RowCount);
            Assert.IsTrue(R.ColumnCount == M.ColumnCount);

            RectangularMatrix QR = Q * R;

            Assert.IsTrue(TestUtilities.IsNearlyEqual(QR, M));
        }