示例#1
0
        public static NcvMatrix DotProduct(NcvMatrix m1, NcvMatrix m2)
        {
            NcvMatrix m3 = new NcvMatrix(m1.cols(), m1.rows());

            for (int y = 0; y < m1.rows(); y++)
            {
                for (int x = 0; x < m1.cols(); x++)
                {
                    m3[x, y] = m1[x, y] * m2[x, y];
                }
            }
            return(m3);
        }
示例#2
0
        // copy each cell from M to this, throwing an error
        // if M and this don't have the same dimensions.
        public void copy(NcvMatrix M)
        {
            if (M.rows() != rows() || M.cols() != cols())
            {
                throw new ArgumentException("M");
            }

            subcopy(M);
        }
示例#3
0
 // copy each cell from M to this (or a sub-matrix of
 // this with the dimensions of M), assuming M has
 // less then or equal number of rows and columns as this.
 public void subcopy(NcvMatrix M)
 {
     for (int i = 0; i < M.rows(); i++)
     {
         for (int j = 0; j < M.cols(); j++)
         {
             this[i, j] = M[i, j];
         }
     }
 }
示例#4
0
        public static NcvMatrix del2(NcvMatrix m)
        {
            int rows = m.rows();
            int cols = m.cols();

            /* IV: Compute Laplace operator using Neuman condition */
            /* IV.1: Deal with corners */
            NcvMatrix Lu = new NcvMatrix(cols, rows);
            int       rows_1 = rows - 1; int rows_2 = rows - 2;
            int       cols_1 = cols - 1; int cols_2 = cols - 2;

            Lu[0, 0]           = (m[0, 1] + m[1, 0]) * 0.5 - m[0, 0];
            Lu[cols_1, 0]      = (m[cols_2, 0] + m[cols_1, 1]) * 0.5 - m[cols_1, 0];
            Lu[0, rows_1]      = (m[1, rows_1] + m[0, rows_2]) * 0.5 - m[0, rows_1];
            Lu[cols_1, rows_1] = (m[cols_2, rows_1] + m[cols_1, rows_2]) * 0.5 - m[cols_1, rows_1];

            /* IV.2: Deal with left and right columns */
            for (int y = 1; y <= rows_2; y++)
            {
                Lu[0, y]      = (2 * m[1, y] + m[0, y - 1] + m[0, y + 1]) * 0.25 - m[0, y];
                Lu[cols_1, y] = (2 * m[cols_2, y] + m[cols_1, y - 1]
                                 + m[cols_1, y + 1]) * 0.25 - m[cols_1, y];
            }

            /* IV.3: Deal with top and bottom rows */
            for (int x = 1; x <= cols_2; x++)
            {
                Lu[x, 0]      = (2 * m[x, 1] + m[x - 1, 0] + m[x + 1, 0]) * 0.25 - m[x, 0];
                Lu[x, rows_1] = (2 * m[x, rows_2] + m[x - 1, rows_1]
                                 + m[x + 1, rows_1]) * 0.25 - m[x, rows_1];
            }

            /* IV.4: Compute interior */
            for (int y = 1; y <= rows_2; y++)
            {
                for (int x = 1; x <= cols_2; x++)
                {
                    Lu[x, y] = (m[x - 1, y] + m[x + 1, y]
                                + m[x, y - 1] + m[x, y + 1]) * 0.25 - m[x, y];
                }
            }



            return(Lu);
        }
示例#5
0
 public NcvMatrix(NcvMatrix M)
 {
     data = new double[M.rows(), M.cols()];
     copy(M);
 }
示例#6
0
        public static NcvMatrix inv(NcvMatrix original)
        {
            NcvMatrix inv = new NcvMatrix(original.cols(), original.rows());

            return(inv.inverse(original));
        }
示例#7
0
        // sets this Matrix to the inverse of the original Matrix
        // and returns this.
        public NcvMatrix inverse(NcvMatrix original)
        {
            if (original.rows() < 1 || original.rows() != original.cols() ||
                rows() != original.rows() || rows() != cols())
            {
                return(this);
            }
            int n = rows();

            copy(new NcvMatrix(n)); // make identity matrix

            if (rows() == 1)
            {
                this[0, 0] = 1 / original[0, 0];
                return(this);
            }

            NcvMatrix b = new NcvMatrix(original);

            for (int i = 0; i < n; i++)
            {
                // find pivot
                double mag   = 0;
                int    pivot = -1;

                for (int j = i; j < n; j++)
                {
                    double mag2 = Math.Abs(b[j, i]);
                    if (mag2 > mag)
                    {
                        mag   = mag2;
                        pivot = j;
                    }
                }

                // no pivot (error)
                if (pivot == -1 || mag == 0)
                {
                    return(this);
                }

                // move pivot row into position
                if (pivot != i)
                {
                    double temp;
                    for (int j = i; j < n; j++)
                    {
                        temp        = b[i, j];
                        this[i, j]  = b[pivot, j];
                        b[pivot, j] = temp;
                    }

                    for (int j = 0; j < n; j++)
                    {
                        temp           = this[i, j];
                        this[i, j]     = this[pivot, j];
                        this[pivot, j] = temp;
                    }
                }

                // normalize pivot row
                mag = b[i, i];
                for (int j = i; j < n; j++)
                {
                    b[i, j] = b[i, j] / mag;
                }
                for (int j = 0; j < n; j++)
                {
                    this[i, j] = this[i, j] / mag;
                }

                // eliminate pivot row component from other rows
                for (int k = 0; k < n; k++)
                {
                    if (k == i)
                    {
                        continue;
                    }
                    double mag2 = b[k, i];

                    for (int j = i; j < n; j++)
                    {
                        b[k, j] = b[k, j] - mag2 * b[i, j];
                    }
                    for (int j = 0; j < n; j++)
                    {
                        this[k, j] = this[k, j] - mag2 * this[i, j];
                    }
                }
            }
            return(this);
        }