Ejemplo n.º 1
0
        /// <summary>
        /// 矩阵相乘
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static CustomMatrix operator *(CustomMatrix a, CustomMatrix b)
        {
            if (a == null || b == null)
            {
                return(null);
            }
            if (a.Column != b.Row)
            {
                MessageBox.Show("相乘矩阵维数不匹配。");
                return(null);
            }
            CustomMatrix result = new CustomMatrix(a.Row, b.Column);

            for (int i = 0; i < a.Row; i++)
            {
                for (int j = 0; j < b.Column; j++)
                {
                    for (int k = 0; k < a.Column; k++)
                    {
                        result[i, j] += a[i, k] * b[k, j];
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 矩阵乘以实数
        /// </summary>
        /// <param name="matrix"></param>
        /// <param name="factor"></param>
        /// <returns></returns>
        public static CustomMatrix operator *(CustomMatrix matrix, double factor)
        {
            if (matrix == null)
            {
                return(null);
            }
            CustomMatrix result = new CustomMatrix(matrix.Row, matrix.Column);

            for (int i = 0; i < matrix.Row; i++)
            {
                for (int j = 0; j < matrix.Column; j++)
                {
                    matrix[i, j] = matrix[i, j] * factor;
                }
            }
            return(matrix);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 矩阵相加
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static CustomMatrix operator +(CustomMatrix a, CustomMatrix b)
        {
            if (a == null || b == null)
            {
                return(null);
            }
            if (a.Row != b.Row || a.Column != b.Column)
            {
                MessageBox.Show("注意!行数和列数不相等的矩阵不能相加...");
                return(null);
            }
            CustomMatrix result = new CustomMatrix(a.Row, a.Column);

            for (int i = 0; i < a.Row; i++)
            {
                for (int j = 0; j < a.Column; j++)
                {
                    result[i, j] = a[i, j] + b[i, j];
                }
            }
            return(result);
        }