예제 #1
0
        /// <summary>Check if size(A) == size(B) *</summary>

        private void CheckMatrixDimensions(NNMatrix B)
        {
            if (B.m != m || B.n != n)
            {
                throw new System.ArgumentException("GeneralMatrix dimensions must agree.");
            }
        }
예제 #2
0
        /// <summary>Linear algebraic matrix multiplication, A * B</summary>
        /// <param name="B">   another matrix
        /// </param>
        /// <returns>     Matrix product, A * B
        /// </returns>
        /// <exception cref="System.ArgumentException">  Matrix inner dimensions must agree.
        /// </exception>
        public virtual NNMatrix Multiply(NNMatrix B)
        {
            if (B.n != m)
            {
                throw new System.ArgumentException("GeneralMatrix inner dimensions must agree.");
            }
            NNMatrix X = new NNMatrix(this.n, B.m);

            double[][] C     = X.Array;
            double[]   Bcolj = new double[m];
            for (int j = 0; j < B.m; j++)
            {
                for (int k = 0; k < m; k++)
                {
                    Bcolj[k] = B.A[k][j];
                }
                for (int i = 0; i < n; i++)
                {
                    double[] Arowi = A[i];
                    double   s     = 0;
                    for (int k = 0; k < m; k++)
                    {
                        s += Arowi[k] * Bcolj[k];
                    }
                    C[i][j] = s;
                }
            }
            return(X);
        }
예제 #3
0
        /// <summary>Element-by-element left division in place, A = A.\B</summary>
        /// <param name="B">   another matrix
        /// </param>
        /// <returns>     A.\B
        /// </returns>

        public virtual NNMatrix ArrayLeftDivideEquals(NNMatrix B)
        {
            CheckMatrixDimensions(B);
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    A[i][j] = B.A[i][j] / A[i][j];
                }
            }
            return(this);
        }
예제 #4
0
        /// <summary>Element-by-element multiplication in place, A = A.*B</summary>
        /// <param name="B">   another matrix
        /// </param>
        /// <returns>     A.*B
        /// </returns>

        public virtual NNMatrix ArrayMultiplyEquals(NNMatrix B)
        {
            CheckMatrixDimensions(B);
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    A[i][j] = A[i][j] * B.A[i][j];
                }
            }
            return(this);
        }
예제 #5
0
        /// <summary>A = A - B</summary>
        /// <param name="B">   another matrix
        /// </param>
        /// <returns>     A - B
        /// </returns>

        public virtual NNMatrix SubtractEquals(NNMatrix B)
        {
            CheckMatrixDimensions(B);
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    A[i][j] = A[i][j] - B.A[i][j];
                }
            }
            return(this);
        }
예제 #6
0
        public virtual NNMatrix Transpose()
        {
            NNMatrix X = new NNMatrix(this.m, this.n);

            for (int j = 0; j < m; j++)
            {
                for (int i = 0; i < n; i++)
                {
                    X[j, i] = this[i, j];
                }
            }
            return(X);
        }
예제 #7
0
        /// <summary>Generate identity matrix</summary>
        /// <param name="m">   Number of rows.
        /// </param>
        /// <param name="n">   Number of colums.
        /// </param>
        /// <returns>     An m-by-n matrix with ones on the diagonal and zeros elsewhere.
        /// </returns>
        public static NNMatrix Identity(int m, int n)
        {
            NNMatrix A = new NNMatrix(m, n);

            double[][] X = A.Array;
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    X[i][j] = (i == j ? 1.0 : 0.0);
                }
            }
            return(A);
        }
예제 #8
0
        /// <summary>Multiply a matrix by a scalar, C = s*A</summary>
        /// <param name="s">   scalar
        /// </param>
        /// <returns>     s*A
        /// </returns>

        public virtual NNMatrix Multiply(double s)
        {
            NNMatrix X = new NNMatrix(m, n);

            double[][] C = X.Array;
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    C[i][j] = s * A[i][j];
                }
            }
            return(X);
        }
예제 #9
0
        /// <summary>Unary minus</summary>
        /// <returns>    -A
        /// </returns>
        public virtual NNMatrix UnaryMinus()
        {
            NNMatrix X = new NNMatrix(m, n);

            double[][] C = X.Array;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    C[i][j] = -A[i][j];
                }
            }
            return(X);
        }
예제 #10
0
        /// <summary>Make a deep copy of a matrix</summary>

        public virtual NNMatrix Copy()
        {
            NNMatrix X = new NNMatrix(n, m);

            double[][] C = X.Array;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    C[i][j] = A[i][j];
                }
            }
            return(X);
        }
예제 #11
0
        /// <summary>Element-by-element left division, C = A.\B</summary>
        /// <param name="B">   another matrix
        /// </param>
        /// <returns>     A.\B
        /// </returns>

        public virtual NNMatrix ArrayLeftDivide(NNMatrix B)
        {
            CheckMatrixDimensions(B);
            NNMatrix X = new NNMatrix(m, n);

            double[][] C = X.Array;
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    C[i][j] = B.A[i][j] / A[i][j];
                }
            }
            return(X);
        }
예제 #12
0
        /// <summary>C = A - B</summary>
        /// <param name="B">   another matrix
        /// </param>
        /// <returns>     A - B
        /// </returns>

        public virtual NNMatrix Subtract(NNMatrix B)
        {
            CheckMatrixDimensions(B);
            NNMatrix X = new NNMatrix(m, n);

            double[][] C = X.Array;
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    C[i][j] = A[i][j] - B.A[i][j];
                }
            }
            return(X);
        }
예제 #13
0
        /// <summary>Generate matrix with random elements</summary>
        /// <param name="m">   Number of rows.
        /// </param>
        /// <param name="n">   Number of colums.
        /// </param>
        /// <returns>     An m-by-n matrix with uniformly distributed random elements.
        /// </returns>

        public static NNMatrix Random(int m, int n)
        {
            System.Random random = new System.Random();

            NNMatrix A = new NNMatrix(m, n);

            double[][] X = A.Array;
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    X[i][j] = random.NextDouble();
                }
            }
            return(A);
        }
예제 #14
0
 public bool Equals(NNMatrix B)
 {
     if ((this.m != B.m) || (this.n != B.n))
     {
         return(false);
     }
     for (int i = 0; i < this.n; i++)
     {
         for (int j = 0; j < this.m; j++)
         {
             if (this[i, j] != B[i, j])
             {
                 return(false);
             }
         }
     }
     return(true);
 }
예제 #15
0
        /// <summary>Construct a matrix from a copy of a 2-D array.</summary>
        /// <param name="A">   Two-dimensional array of doubles.
        /// </param>
        /// <exception cref="System.ArgumentException">   All rows must have the same length
        /// </exception>
        public static NNMatrix Create(double[][] A)
        {
            int      n = A.Length;
            int      m = A[0].Length;
            NNMatrix X = new NNMatrix(n, m);

            double[][] C = X.Array;
            for (int i = 0; i < n; i++)
            {
                if (A[i].Length != m)
                {
                    throw new System.ArgumentException("All rows must have the same length.");
                }
                for (int j = 0; j < m; j++)
                {
                    C[i][j] = A[i][j];
                }
            }
            return(X);
        }