Ejemplo n.º 1
0
    public LUTridiagonalSolver(LUTridiagonalSolver source)
    {
        a = new Vector <double>(source.a);
        b = new Vector <double>(source.b);
        c = new Vector <double>(source.c);
        r = new Vector <double>(source.r);

        Size = source.Size;
    }
Ejemplo n.º 2
0
    }                                         // AG

    public CubicSplineInterpolator(Vector <double> xarr,
                                   Vector <double> yarr,
                                   // CubicSplineBC BCType,
                                   int BCType,
                                   // double alpha = 0.0,	double beta = 0.0)
                                   double alpha, double beta) : base(xarr, yarr)
    {
        // Arrays must have the same size
        x    = xarr;
        y    = yarr;
        N    = xarr.Size - 1;
        type = BCType;


        a = alpha;               // LHS
        b = beta;                // RHS

        // Redundant internal arrays
        int si = x.MinIndex + 1;                         // !! start index
        // cout << "si" << si;
        // int t; cin >> t;
        double defVal = 0.0;

        // Calculate array of offset
        h = new Vector <double>(N, si, defVal);
        for (int j = 1; j <= N; j++)
        {
            h[j] = x[j] - x[j - 1];
        }
        // print(h); cin >> t;

        // All arrays have start index 1
        // Compared to the equations in the book, M(j) --> M(j+1)
        M = new Vector <double>(N + 1, si, defVal);       // Solution
        A = new Vector <double>(N + 1, si, defVal);
        B = new Vector <double>(N + 1, si, defVal + 2.0);
        C = new Vector <double>(N + 1, si, defVal);
        r = new Vector <double>(N + 1, si, defVal);


        // Calculate the elements
        calculateVectors();

        // // /	print(A); print(B); print(C); int y; cin >> y;

        LUTridiagonalSolver mySolver = new LUTridiagonalSolver(A, B, C, r);

        // The matrix must be diagonally dominant; we call the
        // assert macro and the programs stops

        // assert (mySolver.diagonallyDominant() == true); C++!

        M = mySolver.solve();
    }
Ejemplo n.º 3
0
    public override void Ini(IEnumerable <double> xarr, IEnumerable <double> yarr)
    {
        x    = new Vector <double>(xarr.ToArray(), 0);
        y    = new Vector <double>(yarr.ToArray(), 0);
        N    = xarr.Count() - 1;
        type = 1;
        a    = 0; // LHS
        b    = 0; // RHS

        // Redundant internal arrays
        int si = x.MinIndex + 1;                 // !! start index
        // cout << "si" << si;
        // int t; cin >> t;
        double defVal = 0.0;

        // Calculate array of offset
        h = new Vector <double>(N, si, defVal);
        for (int j = 1; j <= N; j++)
        {
            h[j] = x[j] - x[j - 1];
        }
        // print(h); cin >> t;

        // All arrays have start index 1
        // Compared to the equations in the book, M(j) --> M(j+1)
        M = new Vector <double>(N + 1, si, defVal);  // Solution
        A = new Vector <double>(N + 1, si, defVal);
        B = new Vector <double>(N + 1, si, defVal + 2.0);
        C = new Vector <double>(N + 1, si, defVal);
        r = new Vector <double>(N + 1, si, defVal);


        // Calculate the elements
        calculateVectors();

        // // /	print(A); print(B); print(C); int y; cin >> y;

        LUTridiagonalSolver mySolver = new LUTridiagonalSolver(A, B, C, r);

        // The matrix must be diagonally dominant; we call the
        // assert macro and the programs stops

        // assert (mySolver.diagonallyDominant() == true); C++!

        M = mySolver.solve();
    }
    override public void calculate()
    { // Tells how to calculate sol. at n+1
        // In general we need to solve a tridiagonal system

        double tmp1, tmp2;

        for (int i = F.MinIndex; i <= F.MaxIndex; i++)
        {
            tmp1 = (k * fitting_factor(xarr[i + 1], tnow));
            tmp2 = (0.5 * k * h * (pde.convection(xarr[i + 1], tnow)));

            // Coefficients of the U terms
            A[i] = tmp1 - tmp2;
            B[i] = -h2 - (2.0 * tmp1) + (k * h2 * (pde.zeroterm(xarr[i + 1], tnow)));
            C[i] = tmp1 + tmp2;

            F[i] = h2 * (k * (pde.RHS(xarr[i + 1], tnow)) - vecOld[i + 1]);       //?&
        }

        // Correction term for RHS
        F[1]     -= A[1] * vecNew[vecNew.MinIndex];
        F[J - 1] -= C[J - 1] * vecNew[vecNew.MaxIndex];


        // Now solve the system of equations
        LUTridiagonalSolver mySolver = new LUTridiagonalSolver(A, B, C, F);

        // The matrix must be diagonally dominant; we call the
        // assert macro and the programs stops


        Vector <double> solution = mySolver.solve();

        for (int ii = vecNew.MinIndex + 1; ii <= vecNew.MaxIndex - 1; ii++)
        {
            vecNew[ii] = solution[ii - 1];
        }
    }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            // Jagged arrays

            // Lower triangular matrix
            int NR = 10;
            int NC = 10;

            double[][] lowerTriangular = new double[NR][];
            for (int j = 0; j < NC; j++)
            {
                lowerTriangular[j] = new double[j + 1];
            }



            // Generic arrays for starters
            NR = 4;
            NC = 4;
            //MatrixTwoArrayImpl<double> myMatrixStructure = new MatrixTwoArrayImpl<double>(NR, NC);
            UpperTriangularImpl <double> myMatrixStructure = new UpperTriangularImpl <double>(NR, NC);
            GenericMatrix <double, MatrixTwoArrayImpl <double> > myMatrix = new GenericMatrix <double, MatrixTwoArrayImpl <double> >(NR, NC);

            myMatrix.extendedPrint();

            for (int i = myMatrix.MinRowIndex; i <= myMatrix.MaxRowIndex; i++)
            {
                for (int j = myMatrix.MinColumnIndex; j <= myMatrix.MaxColumnIndex; j++)
                {
                    myMatrix[i, j] = -1;
                }
            }
            myMatrix.extendedPrint();

            MatrixOneArrayImpl <double> myMatrixStructure2 = new MatrixOneArrayImpl <double>(NR, NC);
            GenericMatrix <double, MatrixOneArrayImpl <double> > myMatrix2 = new GenericMatrix <double, MatrixOneArrayImpl <double> >(NR, NC);

            myMatrix2.extendedPrint();

            for (int i = myMatrix2.MinRowIndex; i <= myMatrix2.MaxRowIndex; i++)
            {
                for (int j = myMatrix2.MinColumnIndex; j <= myMatrix2.MaxColumnIndex; j++)
                {
                    myMatrix2[i, j] = 2;
                }
            }
            myMatrix2[myMatrix2.MinRowIndex, myMatrix2.MinColumnIndex] = 99;
            myMatrix2[myMatrix2.MaxRowIndex, myMatrix2.MaxColumnIndex] = 98;

            myMatrix2.extendedPrint();

            // Algebra and matrix manipulation
            int J = 3;


            Vector <double> a = new Vector <double>(J, 1, 1.0);
            Vector <double> b = new Vector <double>(J, 1, 2.0);
            Vector <double> c = new Vector <double>(J, 1, 1.0);
            Vector <double> r = new Vector <double>(J, 1, 0.0);                                 // Right-hand side


            for (int i = r.MinIndex; i <= r.MaxIndex; i++)
            {
                r[i] = 1.0;
            }

            r[2] = -1.0;

            /*   r[1] = 4.0;
             *  r[2] = 10.0;
             *  r[3] = 8.0;*/

            Console.WriteLine("LU stuff");
            LUTridiagonalSolver mySolver = new LUTridiagonalSolver(a, b, c, r);
            Vector <double>     result   = mySolver.solve();

            Console.WriteLine("Solution");
            result.extendedPrint();

            // Array
            int            startIndex = -1;
            Array <double> arr        = new Array <double>(10, startIndex);

            for (int j = arr.MinIndex; j <= arr.MaxIndex; j++)
            {
                arr[j] = (double)(j);
            }

            arr[arr.MinIndex] = 99.98;

            //  arr.extendedPrint();


            // Matrix
            Matrix <int> mat = new Matrix <int>(4, 4, 2, 1);

            mat[mat.MinRowIndex, mat.MinColumnIndex] = 99;
            mat.initCells(3);
            mat[mat.MinRowIndex, mat.MinColumnIndex] = 98;
            mat.extendedPrint();

            for (int i = mat.MinRowIndex; i <= mat.MaxRowIndex; i++)
            {
                for (int j = mat.MinColumnIndex; j <= mat.MaxColumnIndex; j++)
                {
                    mat[i, j] = i * j;
                }
            }
            mat[mat.MinRowIndex, mat.MinColumnIndex] = 98;
            mat.extendedPrint();

            // Vectors
            Vector <double> vec = new Vector <double>(10);

            for (int j = vec.MinIndex; j <= vec.MaxIndex; j++)
            {
                vec[j] = (double)(vec.MaxIndex - j);
            }
            vec.print();

            Vector <double> vec2 = vec + vec;

            vec2.print();

            vec2 = vec - vec;
            vec2.print();

            vec2 = vec - 3.0;
            vec2.print();

            vec2 = 2.0 + vec;
            vec2.print();


            // Numeric Matrices, initial tests
            NumericMatrix <double> mat2 = new NumericMatrix <double>(4, 4);

            mat2.initCells(1.0);
            mat2.extendedPrint();
            for (int i = mat2.MinRowIndex; i <= mat2.MaxRowIndex; i++)
            {
                for (int j = mat2.MinColumnIndex; j <= mat2.MaxColumnIndex; j++)
                {
                    mat2[i, j] = i * j;
                }
            }
            Console.WriteLine("Matrix multiplication...");
            Vector <double> vec3 = new Vector <double>(4, 1, 2.0);

            Vector <double> vec4 = mat2 * vec3;

            vec4.print();

            NumericMatrix <double> mat4 = mat2 * mat2;

            mat4.extendedPrint();

            // Numeric Matrices, testing accuracy

            /*       int rows = 10;
             *     int columns = 10;
             *     int startRow = 1;
             *     int startColumn = 1;
             *     NumericMatrix<double>  A = new NumericMatrix<double>(rows, columns, startRow, startColumn);
             *     A[A.MinRowIndex, A.MinColumnIndex] = 1.0; A[A.MinRowIndex, A.MinColumnIndex + 1] = 2.0;
             *     A[A.MinRowIndex + 1, A.MinColumnIndex] = 3.0; A[A.MinRowIndex + 1, A.MinColumnIndex + 1] = 4.0;
             * //       A.extendedPrint();
             *
             *     int si = 1; // Start index
             *     Vector<double> x = new Vector<double>(10, si);
             *     x[si] = 1.0;
             *     x[si + 1] = 2.0;
             *
             *     Vector<double> y = A * x;
             *  //   y.extendedPrint();
             *
             *     NumericMatrix<double> B = A * A;
             *  //   B.extendedPrint();
             *
             *     B = B * B;
             *     B = B * B;
             * //     B.extendedPrint();
             *
             *     // Modify rows and columns of matrices
             *     Vector<double> r1 = new Vector<double>(10, 1, 3.3);
             *     //r1.extendedPrint();
             *  //   A.Row(A.MinRowIndex, r1);
             *  //   A.Column(A.MaxColumnIndex, r1);
             *     A[A.MinRowIndex, A.MinColumnIndex] = 00.0001;
             *     A[A.MaxRowIndex, A.MaxColumnIndex] = -99.99;
             *     A[A.MaxRowIndex, A.MinColumnIndex] = 88.88;
             *     A.extendedPrint();
             *
             *     // Slices
             *     Vector<double> vecSlice = A.getRow(A.MaxRowIndex);
             *     vecSlice.extendedPrint();
             *     vecSlice = A.getColumn(A.MinColumnIndex);
             *     vecSlice.extendedPrint();
             *
             *     int[, ,] tensor = new int[3, 3, 3];
             *         for (int i = 0; i < tensor.GetLength(0); i++)
             *             for (int j = 0; j < tensor.GetLength(1); j++)
             *                 for (int k = 0; k < tensor.GetLength(2); k++)
             *                         tensor[i, j, k] = i * j * k;
             *
             *  /*   for (int i = 0; i < td.GetLength(0); i++)
             *         for (int j = 0; j < td.GetLength(0); j++)
             *             for (int k = 0; k < td.GetLength(0); k++)
             *                 Console.Write(td[i, j, k]); Console.Write(", ");*/
            //A.Column(2, r1);
            // Tensors
            int             nrows    = 2;
            int             ncols    = 2;
            int             ndepth   = 10;
            Tensor <double> myTensor = new Tensor <double>(nrows, ncols, ndepth);

            // myTensor[myTensor.MinThirdIndex] = B;

            for (int j = myTensor.MinThirdIndex + 1; j <= myTensor.MaxThirdIndex; j++)
            {
                //       myTensor[j] = A*A;
            }

            for (int j = myTensor.MinThirdIndex + 1; j <= myTensor.MaxThirdIndex; j++)
            {
                //  myTensor[j].extendedPrint();
            }
        }