示例#1
0
        public static Matrix Transpose(Matrix a)
        {
            Matrix result = new Matrix(a.Width(), a.Height());

            for (int i = 0; i < a.Height(); i++)
            {
                for (int j = 0; j < a.Width(); j++)
                {
                    result.set(j, i, a.get(i, j));
                }
            }

            return(result);
        }
示例#2
0
        public static Matrix Map(Matrix m, Func <double, double> func)
        {
            Matrix result = new Matrix(m.Height(), m.Width());

            for (int i = 0; i < m.Height(); i++)
            {
                for (int j = 0; j < m.Width(); j++)
                {
                    result.set(i, j, func(m.get(i, j)));
                }
            }

            return(result);
        }
示例#3
0
        public static double Dot(Matrix a, Matrix b)
        {
            if (!(a.Height() == 1 && b.Width() == 1 && a.Width() == b.Height()))
            {
                throw new MatrixException("Dot product incompatible");
            }

            double result = 0;

            for (int i = 0; i < a.Width(); i++)
            {
                result += a.get(0, i) * b.get(i, 0);
            }

            return(result);
        }
示例#4
0
        public static Matrix Sub(Matrix a, Matrix b)
        {
            if (a.Width() != b.Width() || a.Height() != b.Height())
            {
                throw new MatrixException("Dimension not egual");
            }

            Matrix m = new Matrix(a.Height(), a.Width());

            for (int l = 0; l < a.Height(); l++)
            {
                for (int c = 0; c < a.Width(); c++)
                {
                    m.set(l, c, a.get(l, c) - b.get(l, c));
                }
            }

            return(m);
        }
示例#5
0
        /*
         * Display
         */

        public static void Print(Matrix m)
        {
            for (int l = 0; l < m.Height(); l++)
            {
                Console.Write("[ " + centeredString(m.get(l, 0).ToString(), 6));
                for (int c = 1; c < m.Width(); c++)
                {
                    Console.Write(", " + centeredString(m.get(l, c).ToString(), 6));
                }
                Console.Write("]\n");
            }
            Console.Write("\n");
        }
示例#6
0
        public static Matrix Mult(Matrix a, Matrix b)
        {
            if (a.Width() != b.Height())
            {
                throw new MatrixException("Width is not egual to the Height (" + a.Width() + ", " + b.Height() + ")");
            }

            Matrix tmp = new Matrix(a.Height(), b.Width());

            for (int l = 0; l < tmp.Height(); l++)
            {
                for (int c = 0; c < tmp.Width(); c++)
                {
                    for (int k = 0; k < b.Height(); k++)
                    {
                        tmp.set(l, c, tmp.get(l, c) + a.get(l, k) * b.get(k, c));
                    }
                }
            }

            return(tmp);
        }
示例#7
0
        /*
         * Static function
         */

        public static Matrix Randomized(int l, int c, int rStart, int rEnd)
        {
            Matrix m = new Matrix(l, c);

            for (int i = 0; i < m.Height(); i++)
            {
                for (int j = 0; j < m.Width(); j++)
                {
                    m.set(i, j, random.Next(rStart, rEnd));
                }
            }

            return(m);
        }
示例#8
0
        /// <summary>
        /// Add another matrix to the instance
        /// </summary>
        /// <param name="m">Matrix to add</param>
        /// <exception cref="MatrixException">Dimension not egual</exception>
        public void Add(Matrix m)
        {
            if (Width() != m.Width() || Height() != m.Height())
            {
                throw new MatrixException("Dimension not egual");
            }

            for (int c = 0; c < Width(); c++)
            {
                for (int l = 0; l < Height(); l++)
                {
                    _matrix[l, c] += m.get(l, c);
                }
            }
        }