Ejemplo n.º 1
0
        public void testSpareMatrixReference()
        {
            int rows      = 10;
            int columns   = 10;
            int nMatrices = 5;
            int nElements = 50;

            MersenneTwisterUniformRng rng = new MersenneTwisterUniformRng(1234);

            SparseMatrix        expected = new SparseMatrix(rows, columns);
            List <SparseMatrix> refs     = new List <SparseMatrix>();

            for (int i = 0; i < nMatrices; ++i)
            {
                SparseMatrix m = new SparseMatrix(rows, columns);
                for (int j = 0; j < nElements; ++j)
                {
                    int row    = Convert.ToInt32(rng.next().value *rows);
                    int column = Convert.ToInt32(rng.next().value *columns);

                    double value = rng.next().value;
                    m[row, column]        += value;
                    expected[row, column] += value;
                }

                refs.Add(m);
            }

            SparseMatrix calculated = refs.accumulate(1, refs.Count, refs[0], (a, b) => a + b);

            for (int i = 0; i < rows; ++i)
            {
                for (int j = 0; j < columns; ++j)
                {
                    if (Math.Abs(calculated[i, j] - expected[i, j]) > 100 * Const.QL_EPSILON)
                    {
                        QAssert.Fail("Error using sparse matrix references in " +
                                     "Element (" + i + ", " + j + ")" +
                                     "\n expected  : " + expected[i, j] +
                                     "\n calculated: " + calculated[i, j]);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void testQRSolve()
        {
            // Testing QR solve...
            setup();

            double tol = 1.0e-12;
            MersenneTwisterUniformRng rng = new MersenneTwisterUniformRng(1234);
            Matrix bigM = new Matrix(50, 100, 0.0);

            for (int i = 0; i < Math.Min(bigM.rows(), bigM.columns()); ++i)
            {
                bigM[i, i] = i + 1.0;
            }
            Matrix[] testMatrices = { M1, M2,                   M3, Matrix.transpose(M3),
                                      M4, Matrix.transpose(M4), M5, I,                   M7,bigM, Matrix.transpose(bigM) };

            for (int j = 0; j < testMatrices.Length; j++)
            {
                Matrix A = testMatrices[j];
                Vector b = new Vector(A.rows());

                for (int k = 0; k < 10; ++k)
                {
                    for (int i = 0; i < b.Count; ++i)
                    {
                        b[i] = rng.next().value;
                    }
                    Vector x = MatrixUtilities.qrSolve(A, b, true);

                    if (A.columns() >= A.rows())
                    {
                        if (norm(A * x - b) > tol)
                        {
                            QAssert.Fail("A*x does not match vector b (norm = "
                                         + norm(A * x - b) + ")");
                        }
                    }
                    else
                    {
                        // use the SVD to calculate the reference values
                        int    n  = A.columns();
                        Vector xr = new Vector(n, 0.0);

                        SVD    svd       = new SVD(A);
                        Matrix V         = svd.V();
                        Matrix U         = svd.U();
                        Vector w         = svd.singularValues();
                        double threshold = n * Const.QL_EPSILON;

                        for (int i = 0; i < n; ++i)
                        {
                            if (w[i] > threshold)
                            {
                                double u    = 0;
                                int    zero = 0;
                                for (int kk = 0; kk < U.rows(); kk++)
                                {
                                    u += (U[kk, i] * b[zero++]) / w[i];
                                }

                                for (int jj = 0; jj < n; ++jj)
                                {
                                    xr[jj] += u * V[jj, i];
                                }
                            }
                        }

                        if (norm(xr - x) > tol)
                        {
                            QAssert.Fail("least square solution does not match (norm = "
                                         + norm(x - xr) + ")");
                        }
                    }
                }
            }
        }