Exemplo n.º 1
0
    private static void test04()

    //****************************************************************************80
    //
    //  Purpose:
    //
    //    TEST04 works with a CCS sparse matrix with many repeated index pairs.
    //
    //  Discussion:
    //
    //    To complete this test, I want to compare AST * X and ACC * X.
    //
    //  Licensing:
    //
    //    This code is distributed under the GNU LGPL license.
    //
    //  Modified:
    //
    //    23 July 2014
    //
    //  Author:
    //
    //    John Burkardt
    //
    {
        Console.WriteLine("");
        Console.WriteLine("TEST04");
        Console.WriteLine("  Convert a sparse matrix from ST to CCS format.");
        Console.WriteLine("  ST: sparse triplet,    I, J,  A.");
        Console.WriteLine("  CC: compressed column, I, CC, A.");
        Console.WriteLine("  The ST matrix is the Wathen finite element matrix.");
        Console.WriteLine("  It has many repeated index pairs.");
        Console.WriteLine("  To check, compare ACC*X - AST*X for a random X.");
        //
        //  Get the size of the ST matrix.
        //
        const int nx  = 3;
        const int ny  = 3;
        int       nst = SparseTriplet.wathen_st_size(nx, ny);

        Console.WriteLine("");
        Console.WriteLine("  Number of ST values = " + nst + "");
        //
        //  Set the formal matrix size
        //
        const int m = 3 * nx * ny + 2 * nx + 2 * ny + 1;
        //
        //  Set a random vector.
        //
        int seed = 123456789;

        double[] x = UniformRNG.r8vec_uniform_01_new(m, ref seed);
        //
        //  Allocate space.
        //
        int[] ist = new int[nst];
        int[] jst = new int[nst];
        //
        //  Create the ST matrix.
        //
        seed = 123456789;
        double[] ast = SparseTriplet.wathen_st(nx, ny, nst, ref seed, ref ist, ref jst);

        int i_min = typeMethods.i4vec_min(nst, ist);
        int i_max = typeMethods.i4vec_max(nst, ist);
        int j_min = typeMethods.i4vec_min(nst, jst);
        int j_max = typeMethods.i4vec_max(nst, jst);

        SparseTriplet.st_header_print(i_min, i_max, j_min, j_max, m, m, nst);
        //
        //  Compute B1 = AST * X
        //
        double[] b1 = SparseTriplet.st_mv(m, m, nst, ist, jst, ast, x);
        //
        //  Get the CCS size.
        //
        int ncc = SparseTriplet.st_to_ccs_size(nst, ist, jst);

        Console.WriteLine("  Number of CCS values = " + ncc + "");
        //
        //  Create the CCS indices.
        //
        int[] icc = new int[ncc];
        int[] ccc = new int[m + 1];
        SparseTriplet.st_to_ccs_index(nst, ist, jst, ncc, m, ref icc, ref ccc);
        //
        //  Create the CCS values.
        //
        double[] acc = SparseTriplet.st_to_ccs_values(nst, ist, jst, ast, ncc, m, icc, ccc);
        //
        //  Compute B2 = ACC * X.
        //
        double[] b2 = CompressedColumnStorage.ccs_mv(m, m, ncc, icc, ccc, acc, x);
        //
        //  Compare B1 and B2.
        //
        double r = typeMethods.r8vec_diff_norm(m, b1, b2);

        Console.WriteLine("  || ACC*X - AST*X|| = " + r + "");
    }