Ejemplo n.º 1
0
        public override T Cast <T>()
        {
            if (typeof(T) == typeof(ProfileMatrix))
            {
                int           i, j, k;
                double[]      profile_di = new double[size];
                int[]         profile_ia = new int[size + 1];
                List <double> profile_au = new List <double>();
                List <double> profile_al = new List <double>();

                ProfileMatrix newMatrix = new ProfileMatrix(size, profile_ia, profile_di, null, null);

                for (i = 0; i < size; i++)
                {
                    profile_di[i] = di[i];
                }

                for (i = 0; i < size; i++)
                {
                    profile_ia[i + 1] = profile_ia[i];

                    if (ig[i + 1] - ig[i] > 0)
                    {
                        for (j = ig[i]; j < ig[i + 1] - 1; j++)
                        {
                            profile_al.Add(ggl[j]);
                            profile_au.Add(ggu[j]);
                            profile_ia[i + 1]++;

                            k = jg[j];
                            while (k < jg[j + 1] - 1)
                            {
                                profile_al.Add(0.0);
                                profile_au.Add(0.0);
                                profile_ia[i + 1]++;
                                k++;
                            }
                        }

                        profile_al.Add(ggl[j]);
                        profile_au.Add(ggu[j]);
                        profile_ia[i + 1]++;

                        k = jg[j];
                        while (k < i - 1)
                        {
                            profile_al.Add(0.0);
                            profile_au.Add(0.0);
                            profile_ia[i + 1]++;
                            k++;
                        }
                    }
                }

                newMatrix.al = profile_al.ToArray();
                newMatrix.au = profile_au.ToArray();
                return(newMatrix as T);
            }
            return(null);
        }
Ejemplo n.º 2
0
        public static void SolveLU(ProfileMatrix matrix, Vector vec)
        {
            int size = matrix.size;

            int[]    ia = matrix.ia;
            double[] di = matrix.di;
            double[] al = matrix.al;
            double[] au = matrix.au;

            double[] b = vec.values;

            int i, j, index_i, index_j, k;

            // factorization
            for (i = 1; i < size; i++)
            {
                for (j = ia[i]; j < ia[i + 1]; j++)
                {
                    index_i = j - 1;
                    k       = i - (ia[i + 1] - j);
                    index_j = ia[k + 1] - 1;

                    for (; index_i >= ia[i] && index_j >= ia[k]; index_i--, index_j--)
                    {
                        al[j] -= al[index_i] * au[index_j];
                        au[j] -= al[index_j] * au[index_i];
                    }
                    au[j] /= di[k];

                    di[i] -= al[j] * au[j];
                }
            }

            // straight
            for (i = 0; i < size; i++)
            {
                for (j = ia[i]; j < ia[i + 1]; j++)
                {
                    b[i] -= b[i - (ia[i + 1] - j)] * al[j];
                }
                b[i] /= di[i];
            }

            // backward
            for (i = size - 1; i > 0; i--)
            {
                for (j = ia[i]; j < ia[i + 1]; j++)
                {
                    b[i - (ia[i + 1] - j)] -= b[i] * au[j];
                }
            }
        }