Пример #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);
        }
Пример #2
0
        public MatrixR GetTranspose()
        {
            MatrixR m = this;

            m.Transpose();
            return(m);
        }
Пример #3
0
        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);
        }
Пример #4
0
        public MatrixR Clone()
        {
            // returns a deep copy of the matrix
            MatrixR m = new MatrixR(matrix);

            m.matrix = (double[, ])matrix.Clone();
            return(m);
        }
Пример #5
0
 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));
 }
Пример #6
0
 public static bool CompareDimension(MatrixR m1, MatrixR m2)
 {
     if (m1.GetRows() == m2.GetRows() && m1.GetCols() == m2.GetCols())
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Пример #7
0
        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);
        }
Пример #8
0
        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;
        }
Пример #9
0
        public static VectorR LinearRegression(double[] xarray, double[] yarray, ModelFunction[] f, out double sigma)
        {
            int     m = f.Length;
            MatrixR A = new MatrixR(m, m);
            VectorR b = new VectorR(m);
            int     n = xarray.Length;

            for (int k = 0; k < m; k++)
            {
                b[k] = 0.0;
                for (int i = 0; i < n; i++)
                {
                    b[k] += f[k](xarray[i]) * yarray[i];
                }
            }

            for (int j = 0; j < m; j++)
            {
                for (int k = 0; k < m; k++)
                {
                    A[j, k] = 0.0;
                    for (int i = 0; i < n; i++)
                    {
                        A[j, k] += f[j](xarray[i]) * f[k](xarray[i]);
                    }
                }
            }

            //LinearSystem ls = new LinearSystem();
            //VectorR coef = ls.GaussJordan(A, b);
            VectorR coef = GaussJordan(A, b);

            // Calculate the standard deviation:
            double s = 0.0;

            for (int i = 0; i < n; i++)
            {
                double s1 = 0.0;
                for (int j = 0; j < m; j++)
                {
                    s1 += coef[j] * f[j](xarray[i]);
                }
                s += (yarray[i] - s1) * (yarray[i] - s1);
            }
            sigma = Math.Sqrt(s / (n - m));

            return(coef);
        }
Пример #10
0
        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);
        }
Пример #11
0
        public static VectorR PolynomialFit(double[] xarray, double[] yarray, int m, out double sigma)
        {
            m++;
            MatrixR A = new MatrixR(m, m);
            VectorR b = new VectorR(m);
            int     n = xarray.Length;

            for (int k = 0; k < m; k++)
            {
                b[k] = 0.0;
                for (int i = 0; i < n; i++)
                {
                    b[k] += Math.Pow(xarray[i], k) * yarray[i];
                }
            }

            for (int j = 0; j < m; j++)
            {
                for (int k = 0; k < m; k++)
                {
                    A[j, k] = 0.0;
                    for (int i = 0; i < n; i++)
                    {
                        A[j, k] += Math.Pow(xarray[i], j + k);
                    }
                }
            }

            VectorR coef = GaussJordan(A, b);

            // Calculate the standard deviation:
            double s = 0.0;

            for (int i = 0; i < n; i++)
            {
                double s1 = 0.0;
                for (int j = 0; j < m; j++)
                {
                    s1 += coef[j] * Math.Pow(xarray[i], j);
                }
                s += (yarray[i] - s1) * (yarray[i] - s1);
            }
            sigma = Math.Sqrt(s / (n - m));

            return(coef);
        }
Пример #12
0
        public static 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) < 1.0e-500)
                {
                    throw new ArgumentException("Diagonal element is too small!");
                }
                x[i] = (b[i] - VectorR.DotProduct(A.GetRowVector(i), x)) / d;
            }
            return(x);
        }
Пример #13
0
        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());
        }
Пример #14
0
        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);
        }
Пример #15
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(), v1.GetSize());

            for (int i = 0; i < v1.GetSize(); i++)
            {
                for (int j = 0; j < v1.GetSize(); j++)
                {
                    result[j, i] = v1[i] * v2[j];
                }
            }
            return(result);
        }
Пример #16
0
        private static 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]);
        }
Пример #17
0
        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);
        }
Пример #18
0
        private static void Triangulate(MatrixR A, VectorR b)
        {
            int     n = A.GetRows();
            VectorR v = new VectorR(n);

            for (int i = 0; i < n - 1; i++)
            {
                double d = Pivot(A, b, i);
                if (Math.Abs(d) < 1.0e-500)
                {
                    throw new ArgumentException("Diagonal element is too small!");
                }
                for (int j = i + 1; j < n; j++)
                {
                    double dd = A[j, i] / d;
                    for (int k = i + 1; k < n; k++)
                    {
                        A[j, k] -= dd * A[i, k];
                    }
                    b[j] -= dd * b[i];
                }
            }
        }
Пример #19
0
        public static MatrixR operator *(MatrixR m1, MatrixR m2)
        {
            if (m1.GetCols() != m2.GetRows())
            {
                throw new ArgumentOutOfRangeException(
                          "Columns", m1, "The numbers of columns of the first matrix must be equal to" +
                          " the number of rows of the second matrix!");
            }
            MatrixR result = new MatrixR(m1.GetRows(), m2.GetCols());
            VectorR v1     = new VectorR(m1.GetCols());
            VectorR v2     = new VectorR(m2.GetRows());

            for (int i = 0; i < m1.GetRows(); i++)
            {
                v1 = m1.GetRowVector(i);
                for (int j = 0; j < m2.GetCols(); j++)
                {
                    v2           = m2.GetColVector(j);
                    result[i, j] = VectorR.DotProduct(v1, v2);
                }
            }
            return(result);
        }
Пример #20
0
 public MatrixR(MatrixR m)
 {
     Rows   = m.GetRows();
     Cols   = m.GetCols();
     matrix = m.matrix;
 }
Пример #21
0
 public bool Equals(MatrixR m)
 {
     return(matrix == m.matrix);
 }