Esempio n. 1
0
        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);
        }
Esempio n. 2
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);
        }
Esempio n. 3
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);
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
 public MatrixR ReplaceCol(VectorR v, int n)
 {
     if (n < 0 || n > Cols)
     {
         throw new ArgumentOutOfRangeException(
                   "n", n, "n is out of range!");
     }
     if (v.GetSize() != Rows)
     {
         throw new ArgumentOutOfRangeException(
                   "Vector size", v.GetSize(), "vector size is out of range!");
     }
     for (int i = 0; i < Rows; i++)
     {
         matrix[i, n] = v[i];
     }
     return(new MatrixR(matrix));
 }
Esempio n. 7
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);
        }
Esempio n. 8
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);
        }
Esempio n. 9
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]);
        }