コード例 #1
0
        public VectorR GaussSeidel(MatrixR A, VectorR b, int MaxIterations, double tolerance)
        {
            int     n = b.GetSize();
            VectorR x = new VectorR(n);

            for (int nIteration = 0; nIteration < MaxIterations; nIteration++)
            {
                VectorR xOld = x.Clone();
                for (int i = 0; i < n; i++)
                {
                    double db = b[i];
                    double da = A[i, i];
                    if (Math.Abs(da) < epsilon)
                    {
                        throw new ArgumentException("Diagonal element is too small!");
                    }
                    for (int j = 0; j < i; j++)
                    {
                        db -= A[i, j] * x[j];
                    }
                    for (int j = i + 1; j < n; j++)
                    {
                        db -= A[i, j] * xOld[j];
                    }
                    x[i] = db / da;
                }
                VectorR dx = x - xOld;
                if (dx.GetNorm() < tolerance)
                {
                    //MessageBox.Show(nIteration.ToString());
                    return(x);
                }
            }
            return(x);
        }
コード例 #2
0
        private double LUSubstitute(MatrixR m, VectorR v) // m = A, v = b
        {
            int    n   = v.GetSize();
            double det = 1.0;

            for (int i = 0; i < n; i++)               // Ly = b
            {
                double d = v[i];
                for (int j = 0; j < i; j++)
                {
                    d -= m[i, j] * v[j];
                }
                double dd = m[i, i];
                if (Math.Abs(d) < epsilon)
                {
                    throw new ArgumentException("Diagonal element is too small!");
                }
                d   /= dd;
                v[i] = d;       //v = y
                det *= m[i, i];
            }
            for (int i = n - 1; i >= 0; i--)
            {
                double d = v[i];
                for (int j = i + 1; j < n; j++)
                {
                    d -= m[i, j] * v[j];
                }
                v[i] = d;       // v=x
            }
            return(det);
        }
コード例 #3
0
ファイル: MatrixR.cs プロジェクト: maddadder/algorithmscSharp
        public static VectorR Transform(VectorR v, MatrixR m)
        {
            VectorR result = new VectorR(v.GetSize());

            if (!m.IsSquared())
            {
                throw new ArgumentOutOfRangeException(
                          "Dimension", m.GetRows(), "The matrix must be squared!");
            }
            if (m.GetRows() != v.GetSize())
            {
                throw new ArgumentOutOfRangeException(
                          "Size", v.GetSize(), "The size of the vector must be equal"
                          + "to the number of rows of the matrix!");
            }
            for (int i = 0; i < m.GetRows(); i++)
            {
                result[i] = 0.0;
                for (int j = 0; j < m.GetCols(); j++)
                {
                    result[i] += v[j] * m[j, i];
                }
            }
            return(result);
        }
コード例 #4
0
        public static void Power(MatrixR A, double tolerance, out VectorR x, out double lambda)
        {
            int n = A.GetCols();

            x      = new VectorR(n);
            lambda = 0.0;
            double delta = 0.0;

            Random random = new Random();

            for (int i = 0; i < n; i++)
            {
                x[i] = random.NextDouble();
            }

            do
            {
                VectorR temp = x;
                x = MatrixR.Transform(A, x);
                x.Normalize();
                if (VectorR.DotProduct(temp, x) < 0)
                {
                    x = -x;
                }
                VectorR dx = temp - x;
                delta = dx.GetNorm();
            }while (delta > tolerance);
            lambda = VectorR.DotProduct(x, MatrixR.Transform(A, x));
        }
コード例 #5
0
        public static void Inverse(MatrixR A, double s, double tolerance, out VectorR x, out double lambda)
        {
            int n = A.GetCols();

            x      = new VectorR(n);
            lambda = 0.0;
            double  delta    = 0.0;
            MatrixR identity = new MatrixR(n, n);

            A = A - s * (identity.Identity());
            LinearSystem ls = new LinearSystem();

            A = ls.LUInverse(A);

            Random random = new Random();

            for (int i = 0; i < n; i++)
            {
                x[i] = random.NextDouble();
            }
            do
            {
                VectorR temp = x;
                x = MatrixR.Transform(A, x);
                x.Normalize();
                if (VectorR.DotProduct(temp, x) < 0)
                {
                    x = -x;
                }
                VectorR dx = temp - x;
                delta = dx.GetNorm();
            }while (delta > tolerance);
            lambda = s + 1.0 / (VectorR.DotProduct(x, MatrixR.Transform(A, x)));
        }
コード例 #6
0
        public static void Rayleigh(MatrixR A, double tolerance, out VectorR x, out double lambda)
        {
            int    n      = A.GetCols();
            double delta  = 0.0;
            Random random = new Random();

            x = new VectorR(n);
            for (int i = 0; i < n; i++)
            {
                x[i] = random.NextDouble();
            }
            x.Normalize();
            VectorR x0 = MatrixR.Transform(A, x);

            x0.Normalize();
            lambda = VectorR.DotProduct(x, x0);
            double temp = lambda;

            do
            {
                temp = lambda;
                x0   = x;
                x0.Normalize();
                x      = MatrixR.Transform(A, x0);
                lambda = VectorR.DotProduct(x, x0);
                delta  = Math.Abs((temp - lambda) / lambda);
            }while (delta > tolerance);
            x.Normalize();
        }
コード例 #7
0
ファイル: MatrixR.cs プロジェクト: maddadder/algorithmscSharp
        public MatrixR GetTranspose()
        {
            MatrixR m = this;

            m.Transpose();
            return(m);
        }
コード例 #8
0
        public static void RayleighQuotient(MatrixR A, double tolerance, int flag, out VectorR x, out double lambda)
        {
            int    n      = A.GetCols();
            double delta  = 0.0;
            Random random = new Random();

            x = new VectorR(n);
            if (flag != 2)
            {
                for (int i = 0; i < n; i++)
                {
                    x[i] = random.NextDouble();
                }
                x.Normalize();
                lambda = VectorR.DotProduct(x, MatrixR.Transform(A, x));
            }
            else
            {
                lambda = 0.0;
                Rayleigh(A, 1e-2, out x, out lambda);
            }

            double       temp     = lambda;
            MatrixR      identity = new MatrixR(n, n);
            LinearSystem ls       = new LinearSystem();

            do
            {
                temp = lambda;
                double d = ls.LUCrout(A - lambda * identity.Identity(), x);
                x.Normalize();
                lambda = VectorR.DotProduct(x, MatrixR.Transform(A, x));
                delta  = Math.Abs((temp - lambda) / lambda);
            }while (delta > tolerance);
        }
コード例 #9
0
ファイル: MatrixR.cs プロジェクト: maddadder/algorithmscSharp
        public static MatrixR Minor(MatrixR m, int row, int col)
        {
            MatrixR mm = new MatrixR(m.GetRows() - 1, m.GetCols() - 1);
            int     ii = 0, jj = 0;

            for (int i = 0; i < m.GetRows(); i++)
            {
                if (i == row)
                {
                    continue;
                }
                jj = 0;
                for (int j = 0; j < m.GetCols(); j++)
                {
                    if (j == col)
                    {
                        continue;
                    }
                    mm[ii, jj] = m[i, j];
                    jj++;
                }
                ii++;
            }
            return(mm);
        }
コード例 #10
0
        private void LUDecompose(MatrixR m)
        {
            int n = m.GetRows();

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    double d = m[i, j];
                    for (int k = 0; k < Math.Min(i, j); k++)
                    {
                        d -= m[i, k] * m[k, j];
                    }
                    if (j > i)
                    {
                        double dd = m[i, i];
                        if (Math.Abs(d) < epsilon)
                        {
                            throw new ArgumentException("Diagonal element is too small!");
                        }
                        d /= dd;
                    }
                    m[i, j] = d;
                }
            }
        }
コード例 #11
0
ファイル: MatrixR.cs プロジェクト: maddadder/algorithmscSharp
        public MatrixR Clone()
        {
            // returns a deep copy of the matrix
            MatrixR m = new MatrixR(matrix);

            m.matrix = (double[, ])matrix.Clone();
            return(m);
        }
コード例 #12
0
        private static void Transformation(MatrixR A, MatrixR R, int I, int J, out MatrixR A1, out MatrixR R1)
        {
            int    n  = A.GetCols();
            double t  = 0.0;
            double da = A[J, J] - A[I, I];

            if (Math.Abs(A[I, J]) < Math.Abs(da) * 1e-30)
            {
                t = A[I, J] / da;
            }
            else
            {
                double phi = da / (2.0 * A[I, J]);
                t = 1.0 / (Math.Abs(phi) + Math.Sqrt(1.0 + phi * phi));
                if (phi < 0.0)
                {
                    t = -t;
                }
            }

            double c    = 1.0 / Math.Sqrt(Math.Abs(t * t + 1.0));
            double s    = t * c;
            double tau  = s / (1.0 + c);
            double temp = A[I, J];

            A[I, J]  = 0.0;
            A[I, I] -= t * temp;
            A[J, J] += t * temp;

            for (int i = 0; i < I; i++)
            {
                temp     = A[i, I];
                A[i, I]  = temp - s * (A[i, J] + tau * temp);
                A[i, J] += s * (temp - tau * A[i, J]);
            }
            for (int i = I + 1; i < J; i++)
            {
                temp     = A[I, i];
                A[I, i]  = temp - s * (A[i, J] + tau * A[I, i]);
                A[i, J] += s * (temp - tau * A[i, J]);
            }
            for (int i = J + 1; i < n; i++)
            {
                temp     = A[I, i];
                A[I, i]  = temp - s * (A[J, i] + tau * temp);
                A[J, i] += s * (temp - tau * A[J, i]);
            }

            for (int i = 0; i < n; i++)
            {
                temp     = R[i, I];
                R[i, I]  = temp - s * (R[i, J] + tau * R[i, I]);
                R[i, J] += s * (temp - tau * R[i, J]);
            }

            A1 = A;
            R1 = R;
        }
コード例 #13
0
ファイル: MatrixR.cs プロジェクト: maddadder/algorithmscSharp
 public static MatrixR Inverse(MatrixR m)
 {
     if (Determinant(m) == 0)
     {
         throw new DivideByZeroException(
                   "Cannot inverse a matrix with a zero determinant!");
     }
     return(Adjoint(m) / Determinant(m));
 }
コード例 #14
0
ファイル: MatrixR.cs プロジェクト: maddadder/algorithmscSharp
 public static bool CompareDimension(MatrixR m1, MatrixR m2)
 {
     if (m1.GetRows() == m2.GetRows() && m1.GetCols() == m2.GetCols())
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
コード例 #15
0
        // Eigenvalues and eigenvectors of a tridiagonal matrix:

        public static void SetAlphaBeta(MatrixR T)
        {
            int n = T.GetCols();

            Alpha    = new double[n];
            Beta     = new double[n - 1];
            Alpha[0] = T[0, 0];
            for (int i = 1; i < n; i++)
            {
                Alpha[i]    = T[i, i];
                Beta[i - 1] = T[i - 1, i];
            }
        }
コード例 #16
0
ファイル: MatrixR.cs プロジェクト: maddadder/algorithmscSharp
        public static MatrixR operator /(double d, MatrixR m)
        {
            MatrixR result = new MatrixR(m.GetRows(), m.GetCols());

            for (int i = 0; i < m.GetRows(); i++)
            {
                for (int j = 0; j < m.GetCols(); j++)
                {
                    result[i, j] = d / m[i, j];
                }
            }
            return(result);
        }
コード例 #17
0
ファイル: MatrixR.cs プロジェクト: maddadder/algorithmscSharp
        public void Transpose()
        {
            MatrixR m = new MatrixR(Cols, Rows);

            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Cols; j++)
                {
                    m[j, i] = matrix[i, j];
                }
            }
            this = m;
        }
コード例 #18
0
        public static MatrixR SetTridiagonalMatrix()
        {
            int     n = Alpha.GetLength(0);
            MatrixR t = new MatrixR(n, n);

            t[0, 0] = Alpha[0];
            for (int i = 1; i < n; i++)
            {
                t[i, i]     = Alpha[i];
                t[i - 1, i] = Beta[i - 1];
                t[i, i - 1] = Beta[i - 1];
            }
            return(t);
        }
コード例 #19
0
        public static void Jacobi(MatrixR A, double tolerance, out MatrixR x, out VectorR lambda)
        {
            MatrixR AA           = A.Clone();
            int     n            = A.GetCols();
            int     maxTransform = 5 * n * n;
            MatrixR matrix       = new MatrixR(n, n);
            MatrixR R            = matrix.Identity();
            MatrixR R1           = R;
            MatrixR A1           = A;

            lambda = new VectorR(n);
            x      = R;

            double maxTerm = 0.0;
            int    I, J;

            do
            {
                maxTerm = MaxTerm(A, out I, out J);
                Transformation(A, R, I, J, out A1, out R1);
                A = A1;
                R = R1;
            }while (maxTerm > tolerance);

            x = R;
            for (int i = 0; i < n; i++)
            {
                lambda[i] = A[i, i];
            }

            for (int i = 0; i < n - 1; i++)
            {
                int    index = i;
                double d     = lambda[i];
                for (int j = i + 1; j < n; j++)
                {
                    if (lambda[j] > d)
                    {
                        index = j;
                        d     = lambda[j];
                    }
                }
                if (index != i)
                {
                    lambda = lambda.GetSwap(i, index);
                    x      = x.GetColSwap(i, index);
                }
            }
        }
コード例 #20
0
ファイル: MatrixR.cs プロジェクト: maddadder/algorithmscSharp
        public MatrixR Identity()
        {
            MatrixR m = new MatrixR(Rows, Cols);

            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Cols; j++)
                {
                    if (i == j)
                    {
                        m[i, j] = 1;
                    }
                }
            }
            return(m);
        }
コード例 #21
0
        public VectorR GaussJordan(MatrixR A, VectorR b)
        {
            Triangulate(A, b);
            int     n = b.GetSize();
            VectorR x = new VectorR(n);

            for (int i = n - 1; i >= 0; i--)
            {
                double d = A[i, i];
                if (Math.Abs(d) < epsilon)
                {
                    throw new ArgumentException("Diagonal element is too small!");
                }
                x[i] = (b[i] - VectorR.DotProduct(A.GetRowVector(i), x)) / d;
            }
            return(x);
        }
コード例 #22
0
        private static MatrixR Jacobian(MFunction f, VectorR x)
        {
            double  h        = 0.0001;
            int     n        = x.GetSize();
            MatrixR jacobian = new MatrixR(n, n);
            VectorR x1       = x.Clone();

            for (int j = 0; j < n; j++)
            {
                x1[j] = x[j] + h;
                for (int i = 0; i < n; i++)
                {
                    jacobian[i, j] = (f(x1)[i] - f(x)[i]) / h;
                }
            }
            return(jacobian);
        }
コード例 #23
0
        public static VectorR NewtonMultiEquations(MFunction f, VectorR x0, double tolerance)
        {
            LinearSystem ls = new LinearSystem();
            VectorR      dx = new VectorR(x0.GetSize());

            do
            {
                MatrixR A = Jacobian(f, x0);
                if (Math.Sqrt(VectorR.DotProduct(f(x0), f(x0)) / x0.GetSize()) < tolerance)
                {
                    return(x0);
                }
                dx = ls.GaussJordan(A, -f(x0));
                x0 = x0 + dx;
            }while (Math.Sqrt(VectorR.DotProduct(dx, dx)) > tolerance);
            return(x0);
        }
コード例 #24
0
        public MatrixR LUInverse(MatrixR m)
        {
            int     n = m.GetRows();
            MatrixR u = m.Identity();

            LUDecompose(m);
            VectorR uv = new VectorR(n);

            for (int i = 0; i < n; i++)
            {
                uv = u.GetRowVector(i);
                LUSubstitute(m, uv);
                u.ReplaceRow(uv, i);
            }
            MatrixR inv = u.GetTranspose();

            return(inv);
        }
コード例 #25
0
ファイル: MatrixR.cs プロジェクト: maddadder/algorithmscSharp
        public static MatrixR operator -(MatrixR m1, MatrixR m2)
        {
            if (!MatrixR.CompareDimension(m1, m2))
            {
                throw new ArgumentOutOfRangeException(
                          "Dimension", m1, "The dimensions of two matrices must be the same!");
            }
            MatrixR result = new MatrixR(m1.GetRows(), m1.GetCols());

            for (int i = 0; i < m1.GetRows(); i++)
            {
                for (int j = 0; j < m1.GetCols(); j++)
                {
                    result[i, j] = m1[i, j] - m2[i, j];
                }
            }
            return(result);
        }
コード例 #26
0
ファイル: MatrixR.cs プロジェクト: maddadder/algorithmscSharp
        public static MatrixR Adjoint(MatrixR m)
        {
            if (!m.IsSquared())
            {
                throw new ArgumentOutOfRangeException(
                          "Dimension", m.GetRows(), "The matrix must be squared!");
            }
            MatrixR ma = new MatrixR(m.GetRows(), m.GetCols());

            for (int i = 0; i < m.GetRows(); i++)
            {
                for (int j = 0; j < m.GetCols(); j++)
                {
                    ma[i, j] = Math.Pow(-1, i + j) * (Determinant(Minor(m, i, j)));
                }
            }
            return(ma.GetTranspose());
        }
コード例 #27
0
ファイル: MatrixR.cs プロジェクト: maddadder/algorithmscSharp
        public static MatrixR Transform(VectorR v1, VectorR v2)
        {
            /*if (v1.GetSize() != v2.GetSize())
             * {
             *  throw new ArgumentOutOfRangeException(
             *   "v1", v1.GetSize(), "The vectors must have the same size!");
             * }*/
            MatrixR result = new MatrixR(v1.GetSize(), v2.GetSize());

            for (int i = 0; i < v1.GetSize(); i++)
            {
                for (int j = 0; j < v2.GetSize(); j++)
                {
                    result[i, j] = v1[i] * v2[j];
                }
            }
            return(result);
        }
コード例 #28
0
ファイル: MatrixR.cs プロジェクト: maddadder/algorithmscSharp
        public static double Determinant(MatrixR m)
        {
            double result = 0.0;

            if (!m.IsSquared())
            {
                throw new ArgumentOutOfRangeException(
                          "Dimension", m.GetRows(), "The matrix must be squared!");
            }
            if (m.GetRows() == 1)
            {
                result = m[0, 0];
            }
            else
            {
                for (int i = 0; i < m.GetRows(); i++)
                {
                    result += Math.Pow(-1, i) * m[0, i] * Determinant(MatrixR.Minor(m, 0, i));
                }
            }
            return(result);
        }
コード例 #29
0
        private static double MaxTerm(MatrixR A, out int I, out int J)
        {
            int    n      = A.GetCols();
            double result = 0.0;

            I = 0;
            J = 1;

            for (int i = 0; i < n - 1; i++)
            {
                for (int j = i + 1; j < n; j++)
                {
                    if (Math.Abs(A[i, j]) > result)
                    {
                        result = Math.Abs(A[i, j]);
                        I      = i;
                        J      = j;
                    }
                }
            }
            return(result);
        }
コード例 #30
0
        private double pivot(MatrixR A, VectorR b, int q)
        {
            int    n = b.GetSize();
            int    i = q;
            double d = 0.0;

            for (int j = q; j < n; j++)
            {
                double dd = Math.Abs(A[j, q]);
                if (dd > d)
                {
                    d = dd;
                    i = j;
                }
            }
            if (i > q)
            {
                A.GetRowSwap(q, i);
                b.GetSwap(q, i);
            }
            return(A[q, q]);
        }